Skip to content

Commit

Permalink
fix: validate placeholder is within modifier (#132)
Browse files Browse the repository at this point in the history
* fix: validate placeholder is within modifier

* clippy
  • Loading branch information
0xalpharush authored Nov 19, 2024
1 parent c3b47d5 commit 5ade828
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
28 changes: 27 additions & 1 deletion crates/sema/src/ast_passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,18 @@ struct AstValidator<'sess> {
dcx: &'sess DiagCtxt,
in_loop_depth: u64,
in_unchecked_block: bool,
in_modifier: bool,
}

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

/// Returns the diagnostics context.
Expand Down Expand Up @@ -142,6 +149,14 @@ impl<'ast> Visit<'ast> for AstValidator<'_> {
self.in_unchecked_block = prev;
return r;
}
StmtKind::Placeholder => {
if !self.in_modifier {
self.dcx()
.err("placeholder statements can only be used in modifiers")
.span(stmt.span)
.emit();
}
}
_ => {}
}

Expand Down Expand Up @@ -182,4 +197,15 @@ impl<'ast> Visit<'ast> for AstValidator<'_> {
fn visit_ty(&mut self, _ty: &'ast ast::Type<'ast>) -> ControlFlow<Self::BreakValue> {
ControlFlow::Continue(())
}

fn visit_item_function(
&mut self,
func: &'ast ast::ItemFunction<'ast>,
) -> ControlFlow<Self::BreakValue> {
let ast::ItemFunction { kind, .. } = func;
self.in_modifier = *kind == ast::FunctionKind::Modifier;
let r = self.walk_item_function(func);
self.in_modifier = false;
r
}
}
8 changes: 8 additions & 0 deletions tests/ui/typeck/invalid_placeholder.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
contract test {
modifier e() {
_;
}
function f() external {
_; //~ ERROR: placeholder statements can only be used in modifiers
}
}
9 changes: 9 additions & 0 deletions tests/ui/typeck/invalid_placeholder.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error: placeholder statements can only be used in modifiers
--> ROOT/tests/ui/typeck/invalid_placeholder.sol:LL:CC
|
LL | _;
| ^^
|

error: aborting due to 1 previous error

0 comments on commit 5ade828

Please sign in to comment.