From e355ae7b3ca28916dabfc546efc0c4140701a0a2 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 29 Aug 2022 21:55:26 +0800 Subject: [PATCH] checker: check generics struct field type error (#15593) --- vlib/v/checker/struct.v | 5 +++++ .../checker/tests/generics_struct_field_type_err.out | 7 +++++++ .../v/checker/tests/generics_struct_field_type_err.vv | 11 +++++++++++ 3 files changed, 23 insertions(+) create mode 100644 vlib/v/checker/tests/generics_struct_field_type_err.out create mode 100644 vlib/v/checker/tests/generics_struct_field_type_err.vv diff --git a/vlib/v/checker/struct.v b/vlib/v/checker/struct.v index e97571fde5..266fb40770 100644 --- a/vlib/v/checker/struct.v +++ b/vlib/v/checker/struct.v @@ -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, Foo', + field.type_pos) + } } if sym.kind == .multi_return { c.error('cannot use multi return as field type', field.type_pos) diff --git a/vlib/v/checker/tests/generics_struct_field_type_err.out b/vlib/v/checker/tests/generics_struct_field_type_err.out new file mode 100644 index 0000000000..4f844c4bd7 --- /dev/null +++ b/vlib/v/checker/tests/generics_struct_field_type_err.out @@ -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, Foo + 2 | mut: + 3 | value T + 4 | next &LL = unsafe { 0 } + | ~~ + 5 | } + 6 | diff --git a/vlib/v/checker/tests/generics_struct_field_type_err.vv b/vlib/v/checker/tests/generics_struct_field_type_err.vv new file mode 100644 index 0000000000..950faea0fb --- /dev/null +++ b/vlib/v/checker/tests/generics_struct_field_type_err.vv @@ -0,0 +1,11 @@ +struct LL { + mut: + value T + next &LL = unsafe { 0 } +} + +fn main() { + mut l := LL{} + l.value = 5 + println(l.value) +}