Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser, checker: move error handling for any type to the checker to resolve parsing issues #21146

Merged
merged 4 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -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,
// 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 {}
.char, .rune, .bool {}
Expand All @@ -602,6 +604,13 @@ fn (mut c Checker) check_alias_vs_element_type_of_parent(node ast.AliasTypeDecl,
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) {
c.check_valid_pascal_case(node.name, 'fn type', node.pos)
typ_sym := c.table.sym(node.typ)
Expand Down Expand Up @@ -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 {
c.error('sum type cannot hold itself', variant.pos)
Expand Down Expand Up @@ -3039,6 +3049,7 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
if to_type.has_flag(.result) {
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')
|| (to_sym.is_number() && from_sym.name == 'JS.BigInt')
Expand Down
4 changes: 3 additions & 1 deletion vlib/v/checker/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
else {}
else {
c.check_any_type(field.typ, sym, field.type_pos)
}
}

if field.has_default_expr {
Expand Down
34 changes: 34 additions & 0 deletions vlib/v/checker/tests/any_type_err.out
Original file line number Diff line number Diff line change
@@ -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 changes: 17 additions & 0 deletions vlib/v/checker/tests/any_type_err.vv
Original file line number Diff line number Diff line change
@@ -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')
}
2 changes: 1 addition & 1 deletion vlib/v/checker/tests/struct_field_with_any_type_err.out
Original file line number Diff line number Diff line change
@@ -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 {
2 | fld any
| ~~~
Expand Down
3 changes: 0 additions & 3 deletions vlib/v/parser/parse_type.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
'any' {
if p.file_backend_mode != .js && p.mod != 'builtin' {
p.error('cannot use `any` type here')
}
ret = ast.any_type
}
else {
Expand Down
6 changes: 0 additions & 6 deletions vlib/v/parser/tests/cast_to_any_type_err.out

This file was deleted.

4 changes: 0 additions & 4 deletions vlib/v/parser/tests/cast_to_any_type_err.vv

This file was deleted.