@@ -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
384394pub 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
27192729fn (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) {
0 commit comments