checker: fix missing reserved name check on global declaration (fix #22345) (#22347)

This commit is contained in:
Felipe Pena 2024-09-29 02:49:38 -03:00 committed by GitHub
parent 80e09e2f91
commit 10c7a75c2d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 29 additions and 1 deletions

View File

@ -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 {

View File

@ -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)
}

View File

@ -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

View 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 |

View File

@ -0,0 +1,9 @@
@[has_globals]
module main
__global (
array [16]int
)
fn main() {
}