mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
parser: check generic fntype declaration without type name (#18033)
This commit is contained in:
parent
7ac7020192
commit
79819c4fcb
@ -1,9 +1,9 @@
|
||||
pub type FnArrayInit = fn (idx int) T
|
||||
pub type FnArrayInit[T] = fn (idx int) T
|
||||
|
||||
pub fn new_array[T](len int, initfn FnArrayInit[T]) []T {
|
||||
mut res := []T{len: len}
|
||||
for idx in 0 .. res.len {
|
||||
res[idx] = initfn(idx)
|
||||
res[idx] = initfn[T](idx)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
@ -230,6 +230,7 @@ pub fn (mut p Parser) parse_multi_return_type() ast.Type {
|
||||
|
||||
// given anon name based off signature when `name` is blank
|
||||
pub fn (mut p Parser) parse_fn_type(name string, generic_types []ast.Type) ast.Type {
|
||||
fn_type_pos := p.peek_token(-2).pos()
|
||||
p.check(.key_fn)
|
||||
|
||||
for attr in p.attrs {
|
||||
@ -277,6 +278,10 @@ pub fn (mut p Parser) parse_fn_type(name string, generic_types []ast.Type) ast.T
|
||||
is_method: false
|
||||
attrs: p.attrs
|
||||
}
|
||||
if has_generic && generic_types.len == 0 && name.len > 0 {
|
||||
p.error_with_pos('`${name}` type is generic fntype, must specify the generic type names, e.g. ${name}[T]',
|
||||
fn_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')
|
||||
|
5
vlib/v/parser/tests/generic_fn_type_decl_err.out
Normal file
5
vlib/v/parser/tests/generic_fn_type_decl_err.out
Normal file
@ -0,0 +1,5 @@
|
||||
vlib/v/parser/tests/generic_fn_type_decl_err.vv:1:6: error: `Fn` type is generic fntype, must specify the generic type names, e.g. Fn[T]
|
||||
1 | type Fn = fn (I) !(O, R)
|
||||
| ~~
|
||||
2 |
|
||||
3 | fn main() {
|
4
vlib/v/parser/tests/generic_fn_type_decl_err.vv
Normal file
4
vlib/v/parser/tests/generic_fn_type_decl_err.vv
Normal file
@ -0,0 +1,4 @@
|
||||
type Fn = fn (I) !(O, R)
|
||||
|
||||
fn main() {
|
||||
}
|
Loading…
Reference in New Issue
Block a user