Skip to content

Commit

Permalink
shift check from CallExpr to FnDecl and add static_type_pos field to …
Browse files Browse the repository at this point in the history
…FnDecl
  • Loading branch information
Delta456 committed Aug 1, 2024
1 parent b13133e commit 7153275
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
1 change: 1 addition & 0 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ pub:
receiver_pos token.Pos // `(u User)` in `fn (u User) name()` position
is_method bool
is_static_type_method bool // true for `fn Foo.bar() {}`
static_type_pos token.Pos // `Foo` in `fn Foo.bar() {}`
method_type_pos token.Pos // `User` in ` fn (u User)` position
method_idx int
rec_mut bool // is receiver mutable
Expand Down
16 changes: 7 additions & 9 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,13 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
&& node.name.after_char(`.`) in reserved_type_names {
c.error('top level declaration cannot shadow builtin type', node.pos)
}
if _ := node.name.index('__static__') {
if sym := c.table.find_sym(node.name.all_before('__static__')) {
if sym.kind == .placeholder {
c.error('unknown type `${sym.name}`', node.static_type_pos)
}
}
}
}
}
if node.return_type != ast.Type(0) {
Expand Down Expand Up @@ -923,15 +930,6 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
}
}
}

if _ := fn_name.index('__static__') {
if sym := c.table.find_sym(node.name.all_before('__static__')) {
if sym.kind == .placeholder {
c.error('unknown type `${sym.name}`', node.name_pos)
}
}
}

// Enum.from_string, `mod.Enum.from_string('item')`, `Enum.from_string('item')`
if !found && fn_name.ends_with('__static__from_string') {
enum_name := fn_name.all_before('__static__')
Expand Down
11 changes: 6 additions & 5 deletions vlib/v/checker/tests/static_fn_call_no_struct_decl_err.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
vlib/v/checker/tests/static_fn_call_no_struct_decl_err.vv:2:2: error: unknown type `Mystery`
1 | fn main() {
2 | Mystery.hey()
| ~~~~~~~
vlib/v/checker/tests/static_fn_call_no_struct_decl_err.vv:5:4: error: unknown type `Mystery`
3 | }
4 |
4 |
5 | fn Mystery.hey() {
| ~~~~~~~
6 | println('hey')
7 | }
3 changes: 3 additions & 0 deletions vlib/v/parser/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -308,13 +308,15 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
mut name := ''
mut type_sym := p.table.sym(rec.typ)
mut name_pos := p.tok.pos()
mut static_type_pos := p.tok.pos()
if p.tok.kind == .name {
mut check_name := ''
// TODO: high order fn
is_static_type_method = p.tok.lit.len > 0 && p.tok.lit[0].is_capital()
&& p.peek_tok.kind == .dot && language == .v // `fn Foo.bar() {}`
if is_static_type_method {
type_name := p.tok.lit // "Foo"
static_type_pos = p.tok.pos()
rec.typ = p.parse_type()
p.check(.dot)
check_name = p.check_name()
Expand Down Expand Up @@ -647,6 +649,7 @@ run them via `v file.v` instead',
receiver_pos: rec.pos
is_method: is_method
is_static_type_method: is_static_type_method
static_type_pos: static_type_pos
method_type_pos: rec.type_pos
method_idx: type_sym_method_idx
rec_mut: rec.is_mut
Expand Down

0 comments on commit 7153275

Please sign in to comment.