Skip to content

Commit

Permalink
Update error message
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranbt committed Nov 11, 2024
1 parent 0d9efa4 commit b667fb2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
15 changes: 13 additions & 2 deletions crates/sema/src/ast_passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,20 @@ impl<'ast> Visit<'ast> for AstValidator<'_> {
self.walk_stmt(body);
self.in_loop_depth -= 1;
}
StmtKind::Break | StmtKind::Continue => {
StmtKind::Break => {
if !self.in_loop() {
self.dcx().err("`break` outside of a loop").span(stmt.span).emit();
self.dcx()
.err("\"break\" has to be in a \"for\" or \"while\" loop.")
.span(stmt.span)
.emit();
}
}
StmtKind::Continue => {
if !self.in_loop() {
self.dcx()
.err("\"continue\" has to be in a \"for\" or \"while\" loop.")
.span(stmt.span)
.emit();
}
}
_ => {}
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/parser/break_continue_outside_loop.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
contract C {
function f() {
break; //~ ERROR: `break` outside of a loop
continue; //~ ERROR: `break` outside of a loop
break; //~ ERROR: "break" has to be in a "for" or "while" loop.
continue; //~ ERROR: "continue" has to be in a "for" or "while" loop.

for (uint256 i = 0; i < 10; i++) {
break;
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/parser/break_continue_outside_loop.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error: `break` outside of a loop
error: "break" has to be in a "for" or "while" loop.
--> ROOT/tests/ui/parser/break_continue_outside_loop.sol:LL:CC
|
LL | break;
| ^^^^^^
|

error: `break` outside of a loop
error: "continue" has to be in a "for" or "while" loop.
--> ROOT/tests/ui/parser/break_continue_outside_loop.sol:LL:CC
|
LL | continue;
Expand Down

0 comments on commit b667fb2

Please sign in to comment.