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 user defined type duplicates to the checker to resolve parsing issues #21147

Merged
merged 3 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
11 changes: 11 additions & 0 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,12 @@ fn (mut c Checker) check_valid_pascal_case(name string, identifier string, pos t
}

fn (mut c Checker) type_decl(node ast.TypeDecl) {
if node.typ == ast.invalid_type_idx && (node is ast.AliasTypeDecl || node is ast.SumTypeDecl) {
typ_desc := if node is ast.AliasTypeDecl { 'alias' } else { 'sum type' }
c.error('cannot register ${typ_desc} `${node.name}`, another type with this name exists',
node.pos)
return
}
match node {
ast.AliasTypeDecl { c.alias_type_decl(node) }
ast.FnTypeDecl { c.fn_type_decl(node) }
Expand Down Expand Up @@ -1778,6 +1784,11 @@ fn (mut c Checker) const_decl(mut node ast.ConstDecl) {

fn (mut c Checker) enum_decl(mut node ast.EnumDecl) {
c.check_valid_pascal_case(node.name, 'enum name', node.pos)
if node.typ == ast.invalid_type_idx {
c.error('cannot register enum `${node.name}`, another type with this name exists',
node.pos)
return
}
mut useen := []u64{}
mut iseen := []i64{}
mut seen_enum_field_names := map[string]int{}
Expand Down
3 changes: 0 additions & 3 deletions vlib/v/checker/tests/alias_type_exists.out

This file was deleted.

3 changes: 3 additions & 0 deletions vlib/v/checker/tests/alias_type_unkown.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vlib/v/checker/tests/alias_type_unkown.vv:1:15: error: unknown type `Bird`
1 | type Pigeon = Bird
| ~~~~
28 changes: 28 additions & 0 deletions vlib/v/checker/tests/type_exists_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
vlib/v/checker/tests/type_exists_err.vv:3:1: error: cannot register alias `Foo`, another type with this name exists
1 | struct Foo {}
2 |
3 | type Foo = Foo
| ~~~~~~~~
4 |
5 | type Foo = string
vlib/v/checker/tests/type_exists_err.vv:5:1: error: cannot register alias `Foo`, another type with this name exists
3 | type Foo = Foo
4 |
5 | type Foo = string
| ~~~~~~~~
6 |
7 | type Foo = int | string
vlib/v/checker/tests/type_exists_err.vv:7:1: error: cannot register sum type `Foo`, another type with this name exists
5 | type Foo = string
6 |
7 | type Foo = int | string
| ~~~~~~~~
8 |
9 | enum Foo {
vlib/v/checker/tests/type_exists_err.vv:9:1: error: cannot register enum `Foo`, another type with this name exists
7 | type Foo = int | string
8 |
9 | enum Foo {
| ~~~~~~~~
10 | @none
11 | }
11 changes: 11 additions & 0 deletions vlib/v/checker/tests/type_exists_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
struct Foo {}

type Foo = Foo

type Foo = string

type Foo = int | string

enum Foo {
@none
}
2 changes: 1 addition & 1 deletion vlib/v/fmt/fmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ pub fn (mut f Fmt) enum_decl(node ast.EnumDecl) {
f.write('pub ')
}
mut name := node.name.after('.')
if node.typ != ast.int_type {
if node.typ != ast.int_type && node.typ != ast.invalid_type_idx {
senum_type := f.table.type_to_str_using_aliases(node.typ, f.mod2alias)
name += ' as ${senum_type}'
}
Expand Down
12 changes: 6 additions & 6 deletions vlib/v/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -4296,11 +4296,13 @@ fn (mut p Parser) enum_decl() ast.EnumDecl {
}
is_pub: is_pub
})
if idx in [ast.invalid_type_idx, ast.string_type_idx, ast.rune_type_idx, ast.array_type_idx,
ast.map_type_idx] {
if idx in [ast.string_type_idx, ast.rune_type_idx, ast.array_type_idx, ast.map_type_idx] {
p.error_with_pos('cannot register enum `${name}`, another type with this name exists',
end_pos)
}
if idx == ast.invalid_type_idx {
enum_type = idx
}

enum_decl := ast.EnumDecl{
name: name
Expand Down Expand Up @@ -4402,8 +4404,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
}
is_pub: is_pub
})
if typ in [ast.invalid_type_idx, ast.string_type_idx, ast.rune_type_idx, ast.array_type_idx,
ast.map_type_idx] {
if typ in [ast.string_type_idx, ast.rune_type_idx, ast.array_type_idx, ast.map_type_idx] {
p.error_with_pos('cannot register sum type `${name}`, another type with this name exists',
name_pos)
return ast.SumTypeDecl{}
Expand Down Expand Up @@ -4443,8 +4444,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
is_pub: is_pub
})
type_end_pos := p.prev_tok.pos()
if idx in [ast.invalid_type_idx, ast.string_type_idx, ast.rune_type_idx, ast.array_type_idx,
ast.map_type_idx] {
if idx in [ast.string_type_idx, ast.rune_type_idx, ast.array_type_idx, ast.map_type_idx] {
p.error_with_pos('cannot register alias `${name}`, another type with this name exists',
name_pos)
return ast.AliasTypeDecl{}
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/parser/tests/sum_type_exists_err.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
vlib/v/parser/tests/sum_type_exists_err.vv:1:6: error: cannot register sum type `Option`, another type with this name exists
vlib/v/parser/tests/sum_type_exists_err.vv:1:1: error: cannot register sum type `Option`, another type with this name exists
1 | type Option = string | int
| ~~~~~~
| ~~~~~~~~~~~
7 changes: 0 additions & 7 deletions vlib/v/parser/tests/type_alias_existing_type_err.out

This file was deleted.

7 changes: 0 additions & 7 deletions vlib/v/parser/tests/type_alias_existing_type_err.vv

This file was deleted.