Skip to content

Commit

Permalink
parser: support a [has_globals] module attribute, to eliminate a pa…
Browse files Browse the repository at this point in the history
…rser whitelist (part 1)
  • Loading branch information
spytheman committed Nov 16, 2021
1 parent 3f37ab2 commit f0f5f97
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions vlib/v/parser/parser.v
Expand Up @@ -34,6 +34,7 @@ mut:
table &ast.Table
language ast.Language
fn_language ast.Language // .c for `fn C.abcd()` declarations
inside_vlib_file bool // true for all vlib/ files
inside_test_file bool // when inside _test.v or _test.vv file
inside_if bool
inside_if_expr bool
Expand All @@ -49,6 +50,7 @@ mut:
builtin_mod bool // are we in the `builtin` module?
mod string // current module name
is_manualfree bool // true when `[manualfree] module abc`, makes *all* fns in the current .v file, opt out of autofree
has_globals bool // `[has_globals] module abc` - allow globals declarations, even without -enable-globals, in that single .v file __only__
attrs []ast.Attr // attributes before next decl stmt
expr_mod string // for constructing full type names in parse_type()
scope &ast.Scope
Expand Down Expand Up @@ -154,6 +156,9 @@ pub fn (mut p Parser) set_path(path string) {
p.file_name = path
p.file_base = os.base(path)
p.file_name_dir = os.dir(path)
if p.file_name_dir.contains('vlib') {
p.inside_vlib_file = true
}
hash := fnv1a.sum64_string(path)
p.unique_prefix = hash.hex_full()
if p.file_base.ends_with('_test.v') || p.file_base.ends_with('_test.vv') {
Expand Down Expand Up @@ -2823,6 +2828,14 @@ fn (mut p Parser) module_decl() ast.Module {
'manualfree' {
p.is_manualfree = true
}
'has_globals' {
if p.inside_vlib_file {
p.has_globals = true
} else {
p.error_with_pos('[has_globals] is allowed only in .v files of `vlib` modules',
ma.pos)
}
}
else {
p.error_with_pos('unknown module attribute `[$ma.name]`', ma.pos)
return mod_node
Expand Down Expand Up @@ -3061,15 +3074,14 @@ fn (mut p Parser) return_stmt() ast.Return {
}
}

const (
// modules which allow globals by default
global_enabled_mods = ['rand', 'sokol.sapp']
)
// TODO: remove this whitelist of modules which allow globals by default
const global_enabled_mods = ['rand', 'sokol.sapp']

// left hand side of `=` or `:=` in `a,b,c := 1,2,3`
fn (mut p Parser) global_decl() ast.GlobalDecl {
if !p.pref.translated && !p.pref.is_livemain && !p.builtin_mod && !p.pref.building_v
&& !p.pref.enable_globals && !p.pref.is_fmt && p.mod !in parser.global_enabled_mods {
if !p.has_globals && !p.pref.enable_globals && !p.pref.is_fmt && !p.pref.translated
&& !p.pref.is_livemain && !p.pref.building_v && !p.builtin_mod
&& p.mod !in parser.global_enabled_mods {
p.error('use `v -enable-globals ...` to enable globals')
return ast.GlobalDecl{}
}
Expand Down

0 comments on commit f0f5f97

Please sign in to comment.