Skip to content

Commit e355ae7

Browse files
authored
checker: check generics struct field type error (#15593)
1 parent 9703410 commit e355ae7

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

vlib/v/checker/struct.v

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ pub fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
7474
if info.is_heap && !field.typ.is_ptr() {
7575
struct_sym.info.is_heap = true
7676
}
77+
if info.generic_types.len > 0 && !field.typ.has_flag(.generic)
78+
&& info.concrete_types.len == 0 {
79+
c.error('field `$field.name` type is generic struct, must specify the generic type names, e.g. Foo<T>, Foo<int>',
80+
field.type_pos)
81+
}
7782
}
7883
if sym.kind == .multi_return {
7984
c.error('cannot use multi return as field type', field.type_pos)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
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+
2 | mut:
3+
3 | value T
4+
4 | next &LL = unsafe { 0 }
5+
| ~~
6+
5 | }
7+
6 |
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
struct LL<T> {
2+
mut:
3+
value T
4+
next &LL = unsafe { 0 }
5+
}
6+
7+
fn main() {
8+
mut l := LL<int>{}
9+
l.value = 5
10+
println(l.value)
11+
}

0 commit comments

Comments
 (0)