Skip to content

Commit

Permalink
checker: fix missing reserved name check on global declaration (fix v…
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Sep 29, 2024
1 parent 80e09e2 commit 10c7a75
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 3 additions & 1 deletion vlib/v/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/parser/tests/global_reserved_name_err.out
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions vlib/v/parser/tests/global_reserved_name_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@[has_globals]
module main

__global (
array [16]int
)

fn main() {
}

0 comments on commit 10c7a75

Please sign in to comment.