1
0
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:
yuyi 2023-04-24 17:03:29 +08:00 committed by GitHub
parent 7ac7020192
commit 79819c4fcb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 2 deletions

View File

@ -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
}

View File

@ -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')

View 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() {

View File

@ -0,0 +1,4 @@
type Fn = fn (I) !(O, R)
fn main() {
}