Skip to content

Commit

Permalink
feat: modifier definitions must have a placeholder (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
TilakMaddy authored Dec 8, 2024
1 parent aedc2a9 commit 76309d7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
16 changes: 16 additions & 0 deletions crates/sema/src/ast_passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct AstValidator<'sess, 'ast> {
function_kind: Option<ast::FunctionKind>,
in_unchecked_block: bool,
in_loop_depth: u64,
placeholder_count: u64,
}

impl<'sess> AstValidator<'sess, '_> {
Expand All @@ -36,6 +37,7 @@ impl<'sess> AstValidator<'sess, '_> {
function_kind: None,
in_unchecked_block: false,
in_loop_depth: 0,
placeholder_count: 0,
}
}

Expand Down Expand Up @@ -150,6 +152,7 @@ impl<'ast> Visit<'ast> for AstValidator<'_, 'ast> {
return r;
}
ast::StmtKind::Placeholder => {
self.placeholder_count += 1;
if !self.function_kind.is_some_and(|k| k.is_modifier()) {
self.dcx()
.err("placeholder statements can only be used in modifiers")
Expand Down Expand Up @@ -193,8 +196,21 @@ impl<'ast> Visit<'ast> for AstValidator<'_, 'ast> {
}
}

let current_placeholder_count = self.placeholder_count;
let r = self.walk_item_function(func);
self.function_kind = None;

if func.kind.is_modifier() {
let num_placeholders_increased = self.placeholder_count - current_placeholder_count;
if num_placeholders_increased == 0 {
if let Some(func_name) = func.header.name {
self.dcx()
.err("modifier must have a `_;` placeholder statement")
.span(func_name.span)
.emit();
}
}
}
r
}

Expand Down
11 changes: 11 additions & 0 deletions tests/ui/resolve/modifier_without_placeholder.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
contract U {
modifier P() {} //~ERROR: modifier must have a `_;` placeholder statement
}

contract C {
modifier P() {
if (true) {
_;
}
}
}
9 changes: 9 additions & 0 deletions tests/ui/resolve/modifier_without_placeholder.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error: modifier must have a `_;` placeholder statement
--> ROOT/tests/ui/resolve/modifier_without_placeholder.sol:LL:CC
|
LL | modifier P() {}
| ^
|

error: aborting due to 1 previous error

0 comments on commit 76309d7

Please sign in to comment.