diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index f3f81f25ea..e1acd34b01 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -286,10 +286,9 @@ pub mut: // break, continue pub struct BranchStmt { pub: - kind token.Kind - label string - pos token.Position - parent_pos int + kind token.Kind + label string + pos token.Position } pub struct CallExpr { diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index adc170edb8..3507f1eb82 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -126,6 +126,7 @@ mut: // sum type deref needs to know which index to deref because unions take care of the correct field aggregate_type_idx int returned_var_name string // to detect that a var doesn't need to be freed since it's being returned + branch_parent_pos int // used in BranchStmt (continue/break) for autofree stop position } const ( @@ -853,7 +854,7 @@ fn (mut g Gen) stmt(node ast.Stmt) { if g.pref.autofree && !g.is_builtin_mod { g.writeln('// free before continue/break') g.autofree_scope_vars_stop(node.pos.pos - 1, node.pos.line_nr, true, - node.parent_pos) + g.branch_parent_pos) } g.writeln('$node.kind;') } @@ -1008,6 +1009,8 @@ fn (mut g Gen) stmt(node ast.Stmt) { g.for_in(node) } ast.ForStmt { + prev_branch_parent_pos := g.branch_parent_pos + g.branch_parent_pos = node.pos.pos g.write_v_source_line_info(node.pos) g.is_vlines_enabled = false if node.label.len > 0 { @@ -1031,6 +1034,7 @@ fn (mut g Gen) stmt(node ast.Stmt) { if node.label.len > 0 { g.writeln('${node.label}__break: {}') } + g.branch_parent_pos = prev_branch_parent_pos } ast.GlobalDecl { g.global_decl(node) diff --git a/vlib/v/parser/for.v b/vlib/v/parser/for.v index 99d8133355..924faf5925 100644 --- a/vlib/v/parser/for.v +++ b/vlib/v/parser/for.v @@ -9,11 +9,6 @@ import v.table fn (mut p Parser) for_stmt() ast.Stmt { p.check(.key_for) pos := p.tok.position() - prev_branch_parent_pos := p.branch_parent_pos - p.branch_parent_pos = pos.pos - defer { - p.branch_parent_pos = prev_branch_parent_pos - } p.open_scope() p.inside_for = true if p.tok.kind == .key_match { diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 5be20a52c5..504f7150a8 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -60,7 +60,6 @@ mut: vet_errors []string cur_fn_name string in_generic_params bool // indicates if parsing between `<` and `>` of a method/function - branch_parent_pos int // used in BranchStmt (continue/break) autofree stop position } // for tests @@ -691,7 +690,6 @@ pub fn (mut p Parser) stmt(is_top_level bool) ast.Stmt { kind: tok.kind label: label pos: tok.position() - parent_pos: p.branch_parent_pos } } .key_unsafe {