mirror of
https://github.com/vlang/v.git
synced 2025-09-16 02:49:31 -04:00
This commit is contained in:
parent
80e09e2f91
commit
10c7a75c2d
@ -10,6 +10,11 @@ import v.util
|
||||
import v.pref
|
||||
import sync.stdatomic
|
||||
|
||||
// V type names that cannot be used as global var name
|
||||
pub const global_reserved_type_names = ['byte', 'bool', 'char', 'i8', 'i16', 'int', 'i64', 'u8',
|
||||
'u16', 'u32', 'u64', 'f32', 'f64', 'map', 'string', 'rune', 'usize', 'isize', 'voidptr', 'thread',
|
||||
'array']
|
||||
|
||||
pub type TypeDecl = AliasTypeDecl | FnTypeDecl | SumTypeDecl
|
||||
|
||||
// pub const int_type_name = $if amd64 || arm64 {
|
||||
|
@ -2302,6 +2302,11 @@ fn (mut c Checker) global_decl(mut node ast.GlobalDecl) {
|
||||
}
|
||||
for mut field in node.fields {
|
||||
c.check_valid_snake_case(field.name, 'global name', field.pos)
|
||||
|
||||
if field.name in ast.global_reserved_type_names {
|
||||
c.error('invalid use of reserved type `${field.name}` as a global name', field.pos)
|
||||
}
|
||||
|
||||
if field.name in c.global_names {
|
||||
c.error('duplicate global `${field.name}`', field.pos)
|
||||
}
|
||||
|
@ -4106,7 +4106,9 @@ fn (mut p Parser) global_decl() ast.GlobalDecl {
|
||||
is_exported: is_exported
|
||||
}
|
||||
fields << field
|
||||
p.table.global_scope.register(field)
|
||||
if name !in ast.global_reserved_type_names {
|
||||
p.table.global_scope.register(field)
|
||||
}
|
||||
comments = []
|
||||
if !is_block {
|
||||
break
|
||||
|
7
vlib/v/parser/tests/global_reserved_name_err.out
Normal file
7
vlib/v/parser/tests/global_reserved_name_err.out
Normal file
@ -0,0 +1,7 @@
|
||||
vlib/v/parser/tests/global_reserved_name_err.vv:5:2: error: invalid use of reserved type `array` as a global name
|
||||
3 |
|
||||
4 | __global (
|
||||
5 | array [16]int
|
||||
| ~~~~~
|
||||
6 | )
|
||||
7 |
|
9
vlib/v/parser/tests/global_reserved_name_err.vv
Normal file
9
vlib/v/parser/tests/global_reserved_name_err.vv
Normal file
@ -0,0 +1,9 @@
|
||||
@[has_globals]
|
||||
module main
|
||||
|
||||
__global (
|
||||
array [16]int
|
||||
)
|
||||
|
||||
fn main() {
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user