1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: check generics struct field type error (#15593)

This commit is contained in:
yuyi 2022-08-29 21:55:26 +08:00 committed by GitHub
parent 9703410391
commit e355ae7b3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 0 deletions

View File

@ -74,6 +74,11 @@ pub fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
if info.is_heap && !field.typ.is_ptr() {
struct_sym.info.is_heap = true
}
if info.generic_types.len > 0 && !field.typ.has_flag(.generic)
&& info.concrete_types.len == 0 {
c.error('field `$field.name` type is generic struct, must specify the generic type names, e.g. Foo<T>, Foo<int>',
field.type_pos)
}
}
if sym.kind == .multi_return {
c.error('cannot use multi return as field type', field.type_pos)

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/generics_struct_field_type_err.vv:4:9: error: field `next` type is generic struct, must specify the generic type names, e.g. Foo<T>, Foo<int>
2 | mut:
3 | value T
4 | next &LL = unsafe { 0 }
| ~~
5 | }
6 |

View File

@ -0,0 +1,11 @@
struct LL<T> {
mut:
value T
next &LL = unsafe { 0 }
}
fn main() {
mut l := LL<int>{}
l.value = 5
println(l.value)
}