From e321f62afb1e6451b73c2c9b89a6e21ee7f60d9e Mon Sep 17 00:00:00 2001 From: Tilak Madichetti Date: Sat, 14 Dec 2024 05:18:02 +0530 Subject: [PATCH] feat: library requirements syntax checker (#168) * feat: library requirements syntax checker * fix --- crates/sema/src/ast_passes.rs | 17 +++++++++++++++++ tests/ui/resolve/library_requirements.sol | 8 ++++++++ tests/ui/resolve/library_requirements.stderr | 16 ++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 tests/ui/resolve/library_requirements.sol create mode 100644 tests/ui/resolve/library_requirements.stderr diff --git a/crates/sema/src/ast_passes.rs b/crates/sema/src/ast_passes.rs index d684c052..9bd962fc 100644 --- a/crates/sema/src/ast_passes.rs +++ b/crates/sema/src/ast_passes.rs @@ -220,6 +220,23 @@ impl<'ast> Visit<'ast> for AstValidator<'_, 'ast> { contract: &'ast ast::ItemContract<'ast>, ) -> ControlFlow { 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 diff --git a/tests/ui/resolve/library_requirements.sol b/tests/ui/resolve/library_requirements.sol new file mode 100644 index 00000000..c532c28a --- /dev/null +++ b/tests/ui/resolve/library_requirements.sol @@ -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 +} diff --git a/tests/ui/resolve/library_requirements.stderr b/tests/ui/resolve/library_requirements.stderr new file mode 100644 index 00000000..4f100428 --- /dev/null +++ b/tests/ui/resolve/library_requirements.stderr @@ -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 +