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

parser: disallow having builtin type as type names for enum, sum type and alias (#19043)

This commit is contained in:
Swastik Baranwal 2023-08-03 14:12:31 +05:30 committed by GitHub
parent 9f5e9ba1cf
commit 6a4bfef2c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 24 additions and 3 deletions

View File

@ -4011,7 +4011,8 @@ fn (mut p Parser) enum_decl() ast.EnumDecl {
} }
is_pub: is_pub is_pub: is_pub
}) })
if idx == -1 { if idx in [ast.invalid_type_idx, 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', p.error_with_pos('cannot register enum `${name}`, another type with this name exists',
end_pos) end_pos)
} }
@ -4109,7 +4110,8 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
} }
is_pub: is_pub is_pub: is_pub
}) })
if typ == ast.invalid_type_idx { if typ in [ast.invalid_type_idx, 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', p.error_with_pos('cannot register sum type `${name}`, another type with this name exists',
name_pos) name_pos)
return ast.SumTypeDecl{} return ast.SumTypeDecl{}
@ -4149,7 +4151,8 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
is_pub: is_pub is_pub: is_pub
}) })
type_end_pos := p.prev_tok.pos() type_end_pos := p.prev_tok.pos()
if idx == ast.invalid_type_idx { if idx in [ast.invalid_type_idx, 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', p.error_with_pos('cannot register alias `${name}`, another type with this name exists',
name_pos) name_pos)
return ast.AliasTypeDecl{} return ast.AliasTypeDecl{}

View File

@ -0,0 +1,3 @@
vlib/v/parser/tests/builtin_alias_type_name_err.vv:1:6: error: cannot register alias `string`, another type with this name exists
1 | type string = int
| ~~~~~~

View File

@ -0,0 +1 @@
type string = int

View File

@ -0,0 +1,5 @@
vlib/v/parser/tests/builtin_enum_type_name_err.vv:1:6: error: cannot register enum `rune`, another type with this name exists
1 | enum rune {
| ~~~~
2 | a
3 | b

View File

@ -0,0 +1,5 @@
enum rune {
a
b
c
}

View File

@ -0,0 +1,3 @@
vlib/v/parser/tests/builtin_sum_type_type_name_err.vv:1:6: error: cannot register sum type `map`, another type with this name exists
1 | type map = int | u64
| ~~~

View File

@ -0,0 +1 @@
type map = int | u64