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

fix: validate placeholder is within modifier #132

Merged
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
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