mirror of https://github.com/vlang/v.git
parser: disallow having builtin type as type names for `enum`, `sum type` and `alias` (#19043)
This commit is contained in:
parent
9f5e9ba1cf
commit
6a4bfef2c5
|
@ -4011,7 +4011,8 @@ fn (mut p Parser) enum_decl() ast.EnumDecl {
|
|||
}
|
||||
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',
|
||||
end_pos)
|
||||
}
|
||||
|
@ -4109,7 +4110,8 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
|
|||
}
|
||||
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',
|
||||
name_pos)
|
||||
return ast.SumTypeDecl{}
|
||||
|
@ -4149,7 +4151,8 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
|
|||
is_pub: is_pub
|
||||
})
|
||||
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',
|
||||
name_pos)
|
||||
return ast.AliasTypeDecl{}
|
||||
|
|
|
@ -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
|
||||
| ~~~~~~
|
|
@ -0,0 +1 @@
|
|||
type string = int
|
|
@ -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
|
|
@ -0,0 +1,5 @@
|
|||
enum rune {
|
||||
a
|
||||
b
|
||||
c
|
||||
}
|
|
@ -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
|
||||
| ~~~
|
|
@ -0,0 +1 @@
|
|||
type map = int | u64
|
Loading…
Reference in New Issue