Skip to content

Commit a4daa4c

Browse files
committed
v3: skip disabled non-function declarations
1 parent a9f4757 commit a4daa4c

2 files changed

Lines changed: 32 additions & 8 deletions

File tree

vlib/v3/parser/parser.v

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -841,21 +841,42 @@ fn (mut p Parser) top_level_stmt() flat.NodeId {
841841
// here so the declaration itself is parsed next. When the attribute group is a
842842
// disabled `@[if flag ?]` (skip_next_decl set), functions are emitted as no-op
843843
// stubs (empty body): the signature must remain so call sites still type-check
844-
// and link, but the body is elided. Non-function declarations are emitted as-is.
844+
// and link, but the body is elided. Other disabled declarations are skipped.
845845
fn (mut p Parser) parse_decl_after_attrs() flat.NodeId {
846-
for p.tok == .semicolon {
847-
p.next()
848-
}
846+
p.consume_decl_prefix_after_attrs()
849847
if p.skip_next_decl {
850848
p.skip_next_decl = false
851-
p.disable_fn_body = true
852-
res := p.top_level_stmt()
853-
p.disable_fn_body = false
854-
return res
849+
if p.cur_decl_is_fn() {
850+
p.disable_fn_body = true
851+
res := p.top_level_stmt()
852+
p.disable_fn_body = false
853+
p.skip_next_decl = false
854+
return res
855+
}
856+
p.skip_top_level_stmt()
857+
p.skip_next_decl = false
858+
return flat.empty_node
855859
}
856860
return p.top_level_stmt()
857861
}
858862

863+
fn (mut p Parser) consume_decl_prefix_after_attrs() {
864+
for p.tok == .semicolon || p.tok == .attribute || p.tok == .lsbr {
865+
if p.tok == .semicolon {
866+
p.next()
867+
continue
868+
}
869+
p.skip_attrs()
870+
}
871+
}
872+
873+
fn (mut p Parser) cur_decl_is_fn() bool {
874+
if p.tok == .key_fn {
875+
return true
876+
}
877+
return p.tok == .key_pub && p.peek() == .key_fn
878+
}
879+
859880
fn (mut p Parser) fn_decl() flat.NodeId {
860881
p.check(.key_fn)
861882
mut name := ''

vlib/v3/tests/type_checker_errors_test.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ fn test_type_checker_reports_core_semantic_errors() {
222222
'moda/moda.v': 'module moda\n\n__global hit int\n\nstruct Tracer {}\n\n@[if trace ?]\nfn (t Tracer) trace(x int) {}\n\nfn side_effect() int {\n\thit = 99\n\treturn 1\n}\n\npub fn run() int {\n\tt := Tracer{}\n\tt.trace(side_effect())\n\treturn hit\n}\n'
223223
}, 'main.v')
224224
assert disabled_if_method_out == '0'
225+
disabled_if_non_fn_out := run_good(v3_bin, 'disabled_if_non_fn_decl_skipped',
226+
'@[if trace ?]\nstruct DisabledStruct {\n\tbad MissingDisabledType\n}\n\nstruct EnabledStruct {\n\tvalue int\n}\n\nfn main() {\n\tprintln(int_str(EnabledStruct{value: 7}.value))\n}\n')
227+
assert disabled_if_non_fn_out == '7'
225228
cross_module_array_append_c := gen_c_project(v3_bin, 'array_append_distinct_module_types', {
226229
'main.v': 'module main\n\nimport moda\nimport modb\n\nfn main() {\n\tmut xs := []moda.Foo{}\n\tys := []modb.Foo{}\n\txs << ys\n}\n'
227230
'moda/moda.v': 'module moda\n\nstruct Foo {\n\ta int\n}\n'

0 commit comments

Comments
 (0)