Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AST validate check statement only in while/for loops #111

Merged
merged 4 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions crates/sema/src/ast_passes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
//! AST-related passes.

use solar_ast::{ast, visit::Visit};
use solar_ast::{
ast,
ast::{Stmt, StmtKind},
visit::Visit,
};
use solar_interface::{diagnostics::DiagCtxt, sym, Session, Span};

#[instrument(name = "ast_passes", level = "debug", skip_all)]
Expand All @@ -19,18 +23,23 @@ pub fn validate(sess: &Session, ast: &ast::SourceUnit<'_>) {
struct AstValidator<'sess> {
span: Span,
dcx: &'sess DiagCtxt,
in_loop_depth: u64,
}

impl<'sess> AstValidator<'sess> {
fn new(sess: &'sess Session) -> Self {
Self { span: Span::DUMMY, dcx: &sess.dcx }
Self { span: Span::DUMMY, dcx: &sess.dcx, in_loop_depth: 0 }
}

/// Returns the diagnostics context.
#[inline]
fn dcx(&self) -> &'sess DiagCtxt {
self.dcx
}

fn in_loop(&self) -> bool {
self.in_loop_depth != 0
}
}

impl<'ast> Visit<'ast> for AstValidator<'_> {
Expand Down Expand Up @@ -69,11 +78,40 @@ impl<'ast> Visit<'ast> for AstValidator<'_> {
}
}

fn visit_stmt(&mut self, stmt: &'ast ast::Stmt<'ast>) {
let Stmt { kind, .. } = stmt;

match kind {
StmtKind::While(_, body, ..)
| StmtKind::DoWhile(body, ..)
| StmtKind::For { body, .. } => {
self.in_loop_depth += 1;
self.walk_stmt(body);
self.in_loop_depth -= 1;
}
StmtKind::Break => {
ferranbt marked this conversation as resolved.
Show resolved Hide resolved
if !self.in_loop() {
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();
}
}
_ => {}
}
}

// Intentionally override unused default implementations to reduce bloat.

fn visit_expr(&mut self, _expr: &'ast ast::Expr<'ast>) {}

fn visit_stmt(&mut self, _stmt: &'ast ast::Stmt<'ast>) {}

fn visit_ty(&mut self, _ty: &'ast ast::Type<'ast>) {}
}
21 changes: 21 additions & 0 deletions tests/ui/parser/break_continue_outside_loop.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
contract C {
function f() {
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;
continue;
}

while (true) {
break;
continue;
}

do {
break;
continue;
} while (true);
}
}
16 changes: 16 additions & 0 deletions tests/ui/parser/break_continue_outside_loop.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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: "continue" has to be in a "for" or "while" loop.
--> ROOT/tests/ui/parser/break_continue_outside_loop.sol:LL:CC
|
LL | continue;
| ^^^^^^^^^
|

error: aborting due to 2 previous errors