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: syntax checker for functions with modifiers #164

Merged
merged 1 commit into from
Dec 11, 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
7 changes: 7 additions & 0 deletions crates/ast/src/ast/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,13 @@ pub struct ItemFunction<'ast> {
pub body: Option<Block<'ast>>,
}

impl ItemFunction<'_> {
/// Returns `true` if the function is implemented
pub fn is_implemented(&self) -> bool {
self.body.is_some()
}
}

/// A function header: `function helloWorld() external pure returns(string memory)`.
#[derive(Debug, Default)]
pub struct FunctionHeader<'ast> {
Expand Down
11 changes: 11 additions & 0 deletions crates/sema/src/ast_passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,17 @@ impl<'ast> Visit<'ast> for AstValidator<'_, 'ast> {
}
}
}
if contract.kind.is_interface() && !func.header.modifiers.is_empty() {
self.dcx()
.err("functions in interfaces cannot have modifiers")
.span(self.span)
.emit();
} else if !func.is_implemented() && !func.header.modifiers.is_empty() {
self.dcx()
.err("functions without implementation cannot have modifiers")
.span(self.span)
.emit();
}
}

if func.header.visibility.is_none() {
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/resolve/func_modifiers.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
abstract contract A {
modifier x() {
_;
}

function updateState() external virtual x; //~ERROR: functions without implementation cannot have modifiers
}

interface B {
modifier x() {
_;
}

function j() external x; //~ERROR: functions in interfaces cannot have modifiers
}
16 changes: 16 additions & 0 deletions tests/ui/resolve/func_modifiers.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error: functions without implementation cannot have modifiers
--> ROOT/tests/ui/resolve/func_modifiers.sol:LL:CC
|
LL | function updateState() external virtual x;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|

error: functions in interfaces cannot have modifiers
--> ROOT/tests/ui/resolve/func_modifiers.sol:LL:CC
|
LL | function j() external x;
| ^^^^^^^^^^^^^^^^^^^^^^^^
|

error: aborting due to 2 previous errors

Loading