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

parser: check for illegal use of any type (fix #15003) (#15020)

This commit is contained in:
yuyi 2022-07-11 20:29:25 +08:00 committed by GitHub
parent 5498a6c263
commit 9231697966
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 5 deletions

View File

@ -35,10 +35,6 @@ 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

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/struct_field_with_any_type_err.vv:2:6: error: struct field cannot be the `any` type, use generics instead
vlib/v/checker/tests/struct_field_with_any_type_err.vv:2:6: error: cannot use `any` type here, `any` will be implemented in V 0.4
1 | struct My_type {
2 | fld any
| ~~~

View File

@ -607,6 +607,12 @@ pub fn (mut p Parser) parse_any_type(language ast.Language, is_ptr bool, check_d
'int_literal' {
ret = ast.int_literal_type
}
'any' {
if p.file_backend_mode != .js && p.mod != 'builtin' {
p.error('cannot use `any` type here, `any` will be implemented in V 0.4')
}
ret = ast.any_type
}
else {
p.next()
if name.len == 1 && name[0].is_capital() {

View File

@ -0,0 +1,6 @@
vlib/v/parser/tests/cast_to_any_type_err.vv:2:7: error: cannot use `any` type here, `any` will be implemented in V 0.4
1 | fn main() {
2 | a := any(22)
| ~~~
3 | println(a)
4 | }

View File

@ -0,0 +1,4 @@
fn main() {
a := any(22)
println(a)
}