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

feat: modifier definitions must have a placeholder #159

Merged
merged 1 commit into from
Dec 8, 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
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

Loading