Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Sep 28, 2024
1 parent e0f83f2 commit c71918f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
8 changes: 8 additions & 0 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ pub const array_builtin_methods = ['filter', 'clone', 'repeat', 'reverse', 'map'
'pop', 'delete']
pub const array_builtin_methods_chk = token.new_keywords_matcher_from_array_trie(array_builtin_methods)
// TODO: remove `byte` from this list when it is no longer supported
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 const reserved_type_names = ['byte', 'bool', 'char', 'i8', 'i16', 'int', 'i64', 'u8', 'u16',
'u32', 'u64', 'f32', 'f64', 'map', 'string', 'rune', 'usize', 'isize', 'voidptr', 'thread']
pub const reserved_type_names_chk = token.new_keywords_matcher_from_array_trie(reserved_type_names)
Expand Down Expand Up @@ -2302,6 +2305,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 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
9 changes: 3 additions & 6 deletions vlib/v/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -4062,11 +4062,6 @@ fn (mut p Parser) global_decl() ast.GlobalDecl {
}
pos := p.tok.pos()
name := p.check_name()

if name in reserved_type_names {
p.error_with_pos('invalid use of reserved type `${name}` as a global name',
pos)
}
has_expr := p.tok.kind == .assign
mut expr := ast.empty_expr
mut typ := ast.void_type
Expand Down Expand Up @@ -4114,7 +4109,9 @@ fn (mut p Parser) global_decl() ast.GlobalDecl {
is_exported: is_exported
}
fields << field
p.table.global_scope.register(field)
if name !in reserved_type_names {
p.table.global_scope.register(field)
}
comments = []
if !is_block {
break
Expand Down
1 change: 0 additions & 1 deletion vlib/v/parser/tests/global_reserved_name_err.vv
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ __global (
)

fn main() {
assert array.len == 16
}

0 comments on commit c71918f

Please sign in to comment.