From 16be6a6dc0f96cd1330e6f85f4e395fde1fbdb63 Mon Sep 17 00:00:00 2001 From: TilakMaddy Date: Fri, 13 Dec 2024 01:20:38 +0530 Subject: [PATCH 1/2] feat: library requirements syntax checker --- 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 58e77ac8..a1073462 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 == ast::VarMut::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 + From c18d178e520082706b347f85a5df9a02139b16b7 Mon Sep 17 00:00:00 2001 From: TilakMaddy Date: Sat, 14 Dec 2024 00:37:00 +0530 Subject: [PATCH 2/2] fix --- crates/sema/src/ast_passes.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/sema/src/ast_passes.rs b/crates/sema/src/ast_passes.rs index a1073462..f80e6d31 100644 --- a/crates/sema/src/ast_passes.rs +++ b/crates/sema/src/ast_passes.rs @@ -227,7 +227,7 @@ impl<'ast> Visit<'ast> for AstValidator<'_, 'ast> { } for item in contract.body.iter() { if let ast::ItemKind::Variable(var) = &item.kind { - if !var.mutability.is_some_and(|m| m == ast::VarMut::Constant) { + if !var.mutability.is_some_and(|m| m.is_constant()) { self.dcx() .err("library cannot have non-constant state variable") .span(var.span)