checker: check struct field using 'any' type (#12489)

pull/12490/head
yuyi 2021-11-17 17:42:05 +08:00 committed by GitHub
parent 2eb02ff5a7
commit 1370516f53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 0 deletions

View File

@ -638,6 +638,9 @@ pub fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
} }
} }
for i, field in node.fields { 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 } c.ensure_type_exists(field.typ, field.type_pos) or { return }
if field.typ.has_flag(.generic) { if field.typ.has_flag(.generic) {
has_generic_types = true has_generic_types = true

View File

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

View File

@ -0,0 +1,8 @@
struct My_type {
fld any
}
fn main() {
a := My_type{}
println(a)
}