Skip to content

Commit c51cd31

Browse files
committed
Fix: create a separate scope for the for loop clauses and body
1 parent b4c5cd0 commit c51cd31

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

src/b.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,7 @@ pub unsafe fn compile_statement(l: *mut Lexer, c: *mut Compiler) -> Option<()> {
790790
Some(())
791791
}
792792
Token::For => {
793+
scope_push(&mut (*c).vars);
793794
get_and_expect_token(l, Token::OParen)?;
794795

795796
let cond_label = allocate_label_index(c);
@@ -843,6 +844,7 @@ pub unsafe fn compile_statement(l: *mut Lexer, c: *mut Compiler) -> Option<()> {
843844
push_opcode(Op::JmpLabel{label: iter_label}, (*l).loc, c);
844845
push_opcode(Op::Label {label: out_label}, (*l).loc, c);
845846

847+
scope_pop(&mut (*c).vars);
846848
Some(())
847849
}
848850
Token::Return => {

tests/for_loop.b

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
main() {
2+
auto i; i = 69;
23
for (auto i = 0; i < 3; i++) printf("i: %lld\n", i);
4+
printf("i outside: %lld\n", i);
35

46
auto j;
57
for (j = 3; j < 9; j += 2) printf("j: %lld\n", j);

0 commit comments

Comments
 (0)