Skip to content

Commit 3702796

Browse files
authored
checker: fix global empty const type check (fix #26324) (#26332)
1 parent 661cc49 commit 3702796

5 files changed

Lines changed: 45 additions & 7 deletions

File tree

‎vlib/v/checker/checker.v‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ pub mut:
6666
expected_or_type ast.Type // fn() or { 'this type' } eg. string. expected or block type
6767
expected_expr_type ast.Type // if/match is_expr: expected_type
6868
mod string // current module name
69+
has_globals_in_module bool // true if the current module has @[has_globals] attribute
6970
const_var &ast.ConstField = unsafe { nil } // the current constant, when checking const declarations
7071
const_deps []string
7172
const_names []string
@@ -218,6 +219,7 @@ fn (mut c Checker) reset_checker_state_at_start_of_new_file() {
218219
c.inside_interface_deref = false
219220
c.inside_decl_rhs = false
220221
c.inside_if_guard = false
222+
c.has_globals_in_module = false
221223
c.error_details.clear()
222224
}
223225

@@ -379,6 +381,14 @@ pub fn (mut c Checker) change_current_file(file &ast.File) {
379381
import_sym.alias
380382
}
381383
}
384+
// Check if the current module has has_globals attribute
385+
c.has_globals_in_module = false
386+
for attr in file.mod.attrs {
387+
if attr.name == 'has_globals' {
388+
c.has_globals_in_module = true
389+
break
390+
}
391+
}
382392
}
383393

384394
pub fn (mut c Checker) check_files(ast_files []&ast.File) {
@@ -2717,6 +2727,13 @@ fn (mut c Checker) branch_stmt(node ast.BranchStmt) {
27172727
}
27182728

27192729
fn (mut c Checker) global_decl(mut node ast.GlobalDecl) {
2730+
if !c.pref.enable_globals && !c.pref.is_fmt && !c.pref.is_vet && !c.pref.translated
2731+
&& !c.pref.is_livemain && !c.pref.building_v && !c.is_builtin_mod
2732+
&& !c.has_globals_in_module && !c.file.is_translated {
2733+
c.error('use `v -enable-globals ...` to enable globals', node.pos)
2734+
return
2735+
}
2736+
27202737
required_args_attr := ['_linker_section']
27212738
for attr_name in required_args_attr {
27222739
if attr := node.attrs.find_first(attr_name) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
vlib/v/checker/tests/globals_error.vv:1:1: error: use `v -enable-globals ...` to enable globals
22
1 | __global (
3-
| ~~~~~~~~
3+
| ~~~~~~~~~~
44
2 | rfcnt int
55
3 | )
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vlib/v/checker/tests/with_check_option/empty_global_const.vv:5:1: error: use `v -enable-globals ...` to enable globals
2+
3 | const buf_len = 256
3+
4 |
4+
5 | __global not_used_in_code_but_helps_triggering = u64(-1)
5+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6+
6 |
7+
7 | struct Buffer {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module main
2+
3+
const buf_len = 256
4+
5+
__global not_used_in_code_but_helps_triggering = u64(-1)
6+
7+
struct Buffer {
8+
mut:
9+
data [256]u8
10+
pos int
11+
}
12+
13+
fn (mut buf Buffer) clear() {
14+
buf.pos = 0
15+
for i in 0 .. buf_len {
16+
buf.data[i] = 0
17+
}
18+
}

‎vlib/v/parser/parser.v‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2757,12 +2757,8 @@ fn (mut p Parser) global_decl() ast.GlobalDecl {
27572757
}
27582758
}
27592759

2760-
if !p.has_globals && !p.pref.enable_globals && !p.pref.is_fmt && !p.pref.is_vet
2761-
&& !p.pref.translated && !p.is_translated && !p.pref.is_livemain && !p.pref.building_v
2762-
&& !p.builtin_mod {
2763-
p.error('use `v -enable-globals ...` to enable globals')
2764-
return ast.GlobalDecl{}
2765-
}
2760+
// Parser always parses __global declarations correctly
2761+
// Checker will report an error if globals are not enabled
27662762
start_pos := p.tok.pos()
27672763
p.check(.key_global)
27682764
if p.disallow_declarations_in_script_mode() {

0 commit comments

Comments
 (0)