From 0d9efa4f94d53208f6c11b73750fe6219618ddc0 Mon Sep 17 00:00:00 2001 From: Ferran Borreguero Date: Tue, 12 Nov 2024 01:31:52 +0700 Subject: [PATCH] Fix comments --- crates/sema/src/ast_passes.rs | 15 ++++++------- .../ui/parser/break_continue_outside_loop.sol | 21 +++++++++++++++++++ .../parser/break_continue_outside_loop.stderr | 16 ++++++++++++++ tests/ui/parser/break_outside_loop.sol | 3 --- tests/ui/parser/break_outside_loop.stderr | 11 ---------- 5 files changed, 43 insertions(+), 23 deletions(-) create mode 100644 tests/ui/parser/break_continue_outside_loop.sol create mode 100644 tests/ui/parser/break_continue_outside_loop.stderr delete mode 100644 tests/ui/parser/break_outside_loop.sol delete mode 100644 tests/ui/parser/break_outside_loop.stderr diff --git a/crates/sema/src/ast_passes.rs b/crates/sema/src/ast_passes.rs index f066697..26fa4d7 100644 --- a/crates/sema/src/ast_passes.rs +++ b/crates/sema/src/ast_passes.rs @@ -82,19 +82,16 @@ impl<'ast> Visit<'ast> for AstValidator<'_> { let Stmt { kind, .. } = stmt; match kind { - StmtKind::DoWhile(stmt, ..) => { + StmtKind::While(_, body, ..) + | StmtKind::DoWhile(body, ..) + | StmtKind::For { body, .. } => { self.in_loop_depth += 1; - self.visit_stmt(stmt); + self.walk_stmt(body); self.in_loop_depth -= 1; } - StmtKind::For { body, .. } => { - self.in_loop_depth += 1; - self.visit_stmt(body); - self.in_loop_depth -= 1; - } - StmtKind::Break => { + StmtKind::Break | StmtKind::Continue => { if !self.in_loop() { - self.dcx().err("`break` outside of a loop").span(self.span).emit(); + self.dcx().err("`break` outside of a loop").span(stmt.span).emit(); } } _ => {} diff --git a/tests/ui/parser/break_continue_outside_loop.sol b/tests/ui/parser/break_continue_outside_loop.sol new file mode 100644 index 0000000..9369c73 --- /dev/null +++ b/tests/ui/parser/break_continue_outside_loop.sol @@ -0,0 +1,21 @@ +contract C { + function f() { + break; //~ ERROR: `break` outside of a loop + continue; //~ ERROR: `break` outside of a loop + + for (uint256 i = 0; i < 10; i++) { + break; + continue; + } + + while (true) { + break; + continue; + } + + do { + break; + continue; + } while (true); + } +} diff --git a/tests/ui/parser/break_continue_outside_loop.stderr b/tests/ui/parser/break_continue_outside_loop.stderr new file mode 100644 index 0000000..387a8ac --- /dev/null +++ b/tests/ui/parser/break_continue_outside_loop.stderr @@ -0,0 +1,16 @@ +error: `break` outside of a loop + --> ROOT/tests/ui/parser/break_continue_outside_loop.sol:LL:CC + | +LL | break; + | ^^^^^^ + | + +error: `break` outside of a loop + --> ROOT/tests/ui/parser/break_continue_outside_loop.sol:LL:CC + | +LL | continue; + | ^^^^^^^^^ + | + +error: aborting due to 2 previous errors + diff --git a/tests/ui/parser/break_outside_loop.sol b/tests/ui/parser/break_outside_loop.sol deleted file mode 100644 index 92508ca..0000000 --- a/tests/ui/parser/break_outside_loop.sol +++ /dev/null @@ -1,3 +0,0 @@ -function f() { - break; //~^ERROR: `break` outside of a loop -} diff --git a/tests/ui/parser/break_outside_loop.stderr b/tests/ui/parser/break_outside_loop.stderr deleted file mode 100644 index eddc275..0000000 --- a/tests/ui/parser/break_outside_loop.stderr +++ /dev/null @@ -1,11 +0,0 @@ -error: `break` outside of a loop - --> ROOT/tests/ui/parser/break_outside_loop.sol:LL:CC - | -LL | / function f() { -LL | | break; -LL | | } - | |_^ - | - -error: aborting due to 1 previous error -