diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 240f75d369..f2bc94942d 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -638,6 +638,9 @@ pub fn (mut c Checker) struct_decl(mut node ast.StructDecl) { } } for i, field in node.fields { + if field.typ == ast.any_type { + c.error('struct field cannot be the `any` type, use generics instead', field.type_pos) + } c.ensure_type_exists(field.typ, field.type_pos) or { return } if field.typ.has_flag(.generic) { has_generic_types = true diff --git a/vlib/v/checker/tests/struct_field_with_any_type_err.out b/vlib/v/checker/tests/struct_field_with_any_type_err.out new file mode 100644 index 0000000000..ae9e972f13 --- /dev/null +++ b/vlib/v/checker/tests/struct_field_with_any_type_err.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/struct_field_with_any_type_err.vv:2:6: error: struct field cannot be the `any` type, use generics instead + 1 | struct My_type { + 2 | fld any + | ~~~ + 3 | } + 4 | diff --git a/vlib/v/checker/tests/struct_field_with_any_type_err.vv b/vlib/v/checker/tests/struct_field_with_any_type_err.vv new file mode 100644 index 0000000000..0bf4111267 --- /dev/null +++ b/vlib/v/checker/tests/struct_field_with_any_type_err.vv @@ -0,0 +1,8 @@ +struct My_type { + fld any +} + +fn main() { + a := My_type{} + println(a) +}