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: library requirements syntax checker #168

Merged
merged 4 commits into from
Dec 13, 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
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

Loading