Skip to content

Commit

Permalink
feat: syntax checker for functions with modifiers (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
TilakMaddy authored Dec 11, 2024
1 parent fd68e05 commit a5b5253
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
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

0 comments on commit a5b5253

Please sign in to comment.