Skip to content

Commit

Permalink
parser: disallow type FnType = fn(string) FnType and `type FnType =…
Browse files Browse the repository at this point in the history
… fn(FnType) string`
  • Loading branch information
Delta456 committed Jun 25, 2024
1 parent 9bba12e commit 31f65e0
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 0 deletions.
9 changes: 9 additions & 0 deletions vlib/v/parser/parse_type.v
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ fn (mut p Parser) parse_fn_type(name string, generic_types []ast.Type) ast.Type
has_generic = true
break
}
if p.table.sym(param.typ).name == name {
p.error_with_pos('`${name}` cannot be a parameter as it references the fntype',
param.type_pos)
}
}
mut return_type := ast.void_type
mut return_type_pos := token.Pos{}
Expand Down Expand Up @@ -342,6 +346,11 @@ fn (mut p Parser) parse_fn_type(name string, generic_types []ast.Type) ast.Type
p.error_with_pos('`${name}` type is generic fntype, must specify the generic type names, e.g. ${name}[T]',
fn_type_pos)
}

if p.table.sym(return_type).name == name {
p.error_with_pos('`${name}` cannot be a return type as it references the fntype',
return_type_pos)
}
// MapFooFn typedefs are manually added in cheaders.v
// because typedefs get generated after the map struct is generated
has_decl := p.builtin_mod && name.starts_with('Map') && name.ends_with('Fn')
Expand Down
3 changes: 3 additions & 0 deletions vlib/v/parser/tests/fn_type_decl_same_return_type.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vlib/v/parser/tests/fn_type_decl_same_return_type.vv:1:27: error: `FnType` cannot be a return type as it references the fntype
1 | type FnType = fn (string) FnType
| ~~~~~~
1 change: 1 addition & 0 deletions vlib/v/parser/tests/fn_type_decl_same_return_type.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type FnType = fn (string) FnType
3 changes: 3 additions & 0 deletions vlib/v/parser/tests/fn_type_del_same_param_type.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vlib/v/parser/tests/fn_type_del_same_param_type.vv:1:19: error: `FnType` cannot be a return type as it references the fntype
1 | type FnType = fn (FnType) string
| ~~~~~~
1 change: 1 addition & 0 deletions vlib/v/parser/tests/fn_type_del_same_param_type.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type FnType = fn (FnType) string

0 comments on commit 31f65e0

Please sign in to comment.