mirror of
https://github.com/vlang/v.git
synced 2025-09-13 01:16:02 -04:00
parser, checker: move error handling for any
type to the checker to resolve parsing issues (#21146)
This commit is contained in:
parent
6f99796188
commit
b97fe6831f
@ -579,7 +579,9 @@ fn (mut c Checker) alias_type_decl(node ast.AliasTypeDecl) {
|
|||||||
}
|
}
|
||||||
// The rest of the parent symbol kinds are also allowed, since they are either primitive types,
|
// The rest of the parent symbol kinds are also allowed, since they are either primitive types,
|
||||||
// that in turn do not allow recursion, or are abstract enough so that they can not be checked at comptime:
|
// that in turn do not allow recursion, or are abstract enough so that they can not be checked at comptime:
|
||||||
else {}
|
else {
|
||||||
|
c.check_any_type(node.parent_type, parent_typ_sym, node.type_pos)
|
||||||
|
}
|
||||||
/*
|
/*
|
||||||
.voidptr, .byteptr, .charptr {}
|
.voidptr, .byteptr, .charptr {}
|
||||||
.char, .rune, .bool {}
|
.char, .rune, .bool {}
|
||||||
@ -602,6 +604,13 @@ fn (mut c Checker) check_alias_vs_element_type_of_parent(node ast.AliasTypeDecl,
|
|||||||
node.type_pos)
|
node.type_pos)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn (mut c Checker) check_any_type(typ ast.Type, sym &ast.TypeSymbol, pos token.Pos) {
|
||||||
|
if sym.kind == .any && !typ.has_flag(.generic) && sym.language != .js
|
||||||
|
&& c.file.mod.name != 'builtin' {
|
||||||
|
c.error('cannot use type `any` here', pos)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn (mut c Checker) fn_type_decl(node ast.FnTypeDecl) {
|
fn (mut c Checker) fn_type_decl(node ast.FnTypeDecl) {
|
||||||
c.check_valid_pascal_case(node.name, 'fn type', node.pos)
|
c.check_valid_pascal_case(node.name, 'fn type', node.pos)
|
||||||
typ_sym := c.table.sym(node.typ)
|
typ_sym := c.table.sym(node.typ)
|
||||||
@ -693,6 +702,7 @@ and use a reference to the sum type instead: `var := &${node.name}(${variant_nam
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
c.check_any_type(variant.typ, sym, variant.pos)
|
||||||
|
|
||||||
if sym.name.trim_string_left(sym.mod + '.') == node.name {
|
if sym.name.trim_string_left(sym.mod + '.') == node.name {
|
||||||
c.error('sum type cannot hold itself', variant.pos)
|
c.error('sum type cannot hold itself', variant.pos)
|
||||||
@ -3039,6 +3049,7 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
|
|||||||
if to_type.has_flag(.result) {
|
if to_type.has_flag(.result) {
|
||||||
c.error('casting to Result type is forbidden', node.pos)
|
c.error('casting to Result type is forbidden', node.pos)
|
||||||
}
|
}
|
||||||
|
c.check_any_type(to_type, to_sym, node.pos)
|
||||||
|
|
||||||
if (to_sym.is_number() && from_sym.name == 'JS.Number')
|
if (to_sym.is_number() && from_sym.name == 'JS.Number')
|
||||||
|| (to_sym.is_number() && from_sym.name == 'JS.BigInt')
|
|| (to_sym.is_number() && from_sym.name == 'JS.BigInt')
|
||||||
|
@ -182,7 +182,9 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
|
|||||||
c.warn('byte is deprecated, use u8 instead', field.type_pos)
|
c.warn('byte is deprecated, use u8 instead', field.type_pos)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {}
|
else {
|
||||||
|
c.check_any_type(field.typ, sym, field.type_pos)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if field.has_default_expr {
|
if field.has_default_expr {
|
||||||
|
34
vlib/v/checker/tests/any_type_err.out
Normal file
34
vlib/v/checker/tests/any_type_err.out
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
vlib/v/checker/tests/any_type_err.vv:3:16: error: cannot use type `any` here
|
||||||
|
1 | // Any types should error, while parametrically polymorphic should not.
|
||||||
|
2 |
|
||||||
|
3 | type AnyType = any
|
||||||
|
| ~~~
|
||||||
|
4 | type AnySumType = any | string
|
||||||
|
5 | type AnyPolySumType = T | any
|
||||||
|
vlib/v/checker/tests/any_type_err.vv:4:19: error: cannot use type `any` here
|
||||||
|
2 |
|
||||||
|
3 | type AnyType = any
|
||||||
|
4 | type AnySumType = any | string
|
||||||
|
| ~~~
|
||||||
|
5 | type AnyPolySumType = T | any
|
||||||
|
6 |
|
||||||
|
vlib/v/checker/tests/any_type_err.vv:5:27: error: cannot use type `any` here
|
||||||
|
3 | type AnyType = any
|
||||||
|
4 | type AnySumType = any | string
|
||||||
|
5 | type AnyPolySumType = T | any
|
||||||
|
| ~~~
|
||||||
|
6 |
|
||||||
|
7 | type PolyType = T
|
||||||
|
vlib/v/checker/tests/any_type_err.vv:11:6: error: cannot use type `any` here
|
||||||
|
9 |
|
||||||
|
10 | struct AnyStructField[T] {
|
||||||
|
11 | foo any
|
||||||
|
| ~~~
|
||||||
|
12 | bar T
|
||||||
|
13 | }
|
||||||
|
vlib/v/checker/tests/any_type_err.vv:16:7: error: cannot use type `any` here
|
||||||
|
14 |
|
||||||
|
15 | fn any_cast() {
|
||||||
|
16 | _ := any('foo')
|
||||||
|
| ~~~~~~~~~~
|
||||||
|
17 | }
|
17
vlib/v/checker/tests/any_type_err.vv
Normal file
17
vlib/v/checker/tests/any_type_err.vv
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
// Any types should error, while parametrically polymorphic should not.
|
||||||
|
|
||||||
|
type AnyType = any
|
||||||
|
type AnySumType = any | string
|
||||||
|
type AnyPolySumType = T | any
|
||||||
|
|
||||||
|
type PolyType = T
|
||||||
|
type PolySumType = T | string
|
||||||
|
|
||||||
|
struct AnyStructField[T] {
|
||||||
|
foo any
|
||||||
|
bar T
|
||||||
|
}
|
||||||
|
|
||||||
|
fn any_cast() {
|
||||||
|
_ := any('foo')
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
vlib/v/checker/tests/struct_field_with_any_type_err.vv:2:6: error: cannot use `any` type here
|
vlib/v/checker/tests/struct_field_with_any_type_err.vv:2:6: error: cannot use type `any` here
|
||||||
1 | struct My_type {
|
1 | struct My_type {
|
||||||
2 | fld any
|
2 | fld any
|
||||||
| ~~~
|
| ~~~
|
||||||
|
@ -723,9 +723,6 @@ fn (mut p Parser) parse_any_type(language ast.Language, is_ptr bool, check_dot b
|
|||||||
ret = ast.int_literal_type
|
ret = ast.int_literal_type
|
||||||
}
|
}
|
||||||
'any' {
|
'any' {
|
||||||
if p.file_backend_mode != .js && p.mod != 'builtin' {
|
|
||||||
p.error('cannot use `any` type here')
|
|
||||||
}
|
|
||||||
ret = ast.any_type
|
ret = ast.any_type
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -1,6 +0,0 @@
|
|||||||
vlib/v/parser/tests/cast_to_any_type_err.vv:2:7: error: cannot use `any` type here
|
|
||||||
1 | fn main() {
|
|
||||||
2 | a := any(22)
|
|
||||||
| ~~~
|
|
||||||
3 | println(a)
|
|
||||||
4 | }
|
|
@ -1,4 +0,0 @@
|
|||||||
fn main() {
|
|
||||||
a := any(22)
|
|
||||||
println(a)
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user