Skip to content

Commit

Permalink
feat: library requirements syntax checker (paradigmxyz#168)
Browse files Browse the repository at this point in the history
* feat: library requirements syntax checker

* fix
  • Loading branch information
TilakMaddy authored Dec 13, 2024
1 parent 76fc3a2 commit e321f62
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
17 changes: 17 additions & 0 deletions crates/sema/src/ast_passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,23 @@ impl<'ast> Visit<'ast> for AstValidator<'_, 'ast> {
contract: &'ast ast::ItemContract<'ast>,
) -> ControlFlow<Self::BreakValue> {
self.contract = Some(contract);

if contract.kind.is_library() {
if !contract.bases.is_empty() {
self.dcx().err("library is not allowed to inherit").span(contract.name.span).emit();
}
for item in contract.body.iter() {
if let ast::ItemKind::Variable(var) = &item.kind {
if !var.mutability.is_some_and(|m| m.is_constant()) {
self.dcx()
.err("library cannot have non-constant state variable")
.span(var.span)
.emit();
}
}
}
}

let r = self.walk_item_contract(contract);
self.contract = None;
r
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/resolve/library_requirements.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
library A{}

library B is A {} //~ERROR: library is not allowed to inherit

library C {
uint256 constant x = 1;
uint256 y; //~ERROR: library cannot have non-constant state variable
}
16 changes: 16 additions & 0 deletions tests/ui/resolve/library_requirements.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error: library is not allowed to inherit
--> ROOT/tests/ui/resolve/library_requirements.sol:LL:CC
|
LL | library B is A {}
| ^
|

error: library cannot have non-constant state variable
--> ROOT/tests/ui/resolve/library_requirements.sol:LL:CC
|
LL | uint256 y;
| ^^^^^^^^^^
|

error: aborting due to 2 previous errors

0 comments on commit e321f62

Please sign in to comment.