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: check duplicate calls to base constructor #171

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
15 changes: 15 additions & 0 deletions crates/ast/src/ast/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,21 @@ pub struct ItemContract<'ast> {
pub body: Box<'ast, [Item<'ast>]>,
}

impl<'a> ItemContract<'a> {
/// Returns the constructor, if present
/// NOTE: It doesn't check the kind of contract
pub fn constructor(&'a self) -> Option<&'a crate::ItemFunction<'a>> {
for item in self.body.iter() {
if let crate::ItemKind::Function(func) = &item.kind {
if func.kind.is_constructor() {
return Some(func);
}
}
}
None
}
}

/// The kind of contract.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, EnumIs)]
pub enum ContractKind {
Expand Down
3 changes: 3 additions & 0 deletions crates/sema/src/ast_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ pub(crate) fn lower<'sess, 'hir>(
// Clean up.
lcx.shrink_to_fit();

// Perform validation checks
lcx.check_base_ctor_args();

lcx.finish()
}

Expand Down
80 changes: 79 additions & 1 deletion crates/sema/src/ast_lowering/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{builtins::Builtin, hir, ParsedSources};
use solar_ast as ast;
use solar_data_structures::{
index::{Idx, IndexVec},
map::{FxIndexMap, IndexEntry},
map::{FxHashMap, FxIndexMap, IndexEntry},
smallvec::SmallVec,
BumpExt,
};
Expand Down Expand Up @@ -196,6 +196,84 @@ impl super::LoweringContext<'_, '_, '_> {
}
}

#[instrument(level = "debug", skip_all)]
pub(super) fn check_base_ctor_args(&self) {
for main_contract in self.hir.contracts() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this using the AST instead of being a HIR check like the other ones you did? this shouldnt happen during lowering

let mut ctor_args: FxHashMap<hir::ContractId, Span> = Default::default();

for &lbc_id in main_contract.linearized_bases {
let item = self.hir_to_ast[&hir::ItemId::Contract(lbc_id)];

// AST
let ast::ItemKind::Contract(ast_lbc_contract) = &item.kind else { unreachable!() };

// HIR
let hir_lbc_contract = self.hir.contract(lbc_id);
let scopes = SymbolResolverScopes::new_in(hir_lbc_contract.source, None);

struct BCError<'a> {
base_con_name: &'a str,
first_call_span: Span,
second_call_span: Span,
}

impl<'a> BCError<'a> {
fn from(
base_con_name: &'a str,
first_call_span: Span,
second_call_span: Span,
) -> Self {
Self { base_con_name, first_call_span, second_call_span }
}
}

let mut bc_errors = vec![];

// annotate calls from inheritance specifiers
for b in ast_lbc_contract.bases.iter().filter(|b| !b.arguments.is_empty()) {
let Ok(base_id) = self
.resolver
.resolve_path_as::<hir::ContractId>(b.name, &scopes, "contract")
else {
continue;
};
if let Some(old_value) = ctor_args.insert(base_id, b.name.span()) {
let base_con_name = self.hir.contract(base_id).name.as_str();
bc_errors.push(BCError::from(base_con_name, old_value, b.name.span()));
}
}

// annotate calls from constructor modifiers
if let Some(c) = ast_lbc_contract.constructor() {
for m in c.header.modifiers.iter().filter(|m| !m.arguments.is_empty()) {
let Ok(base_id) = self
.resolver
.resolve_path_as::<hir::ContractId>(m.name, &scopes, "contract")
else {
continue;
};
if let Some(old_value) = ctor_args.insert(base_id, m.name.span()) {
let base_con_name = self.hir.contract(base_id).name.as_str();
bc_errors.push(BCError::from(base_con_name, old_value, m.name.span()));
}
}
}

for bc_error in bc_errors {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can make this a closure that is called instead of pushing

self.dcx()
.err(format!(
"contract makes multiple calls to base constructor {}",
bc_error.base_con_name
))
.span(main_contract.name.span)
.emit();
self.dcx().note("first call here").span(bc_error.first_call_span).emit();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these should be notes on the error, this creates a separate diagnostic

self.dcx().note("second call here").span(bc_error.second_call_span).emit();
}
}
}
}

#[instrument(level = "debug", skip_all)]
pub(super) fn assign_constructors(&mut self) {
for contract_id in self.hir.contract_ids() {
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/resolve/base_ctor_args.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
contract C {
constructor(uint b) {}
}

contract D is C(3) {
constructor() {}
}

contract E is C {
constructor() C(5) {}
}

contract F is E { //~ERROR: contract makes multiple calls to base constructor C
constructor() C(2) {}
}

contract G is F {} //~ERROR: contract makes multiple calls to base constructor C
44 changes: 44 additions & 0 deletions tests/ui/resolve/base_ctor_args.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
error: contract makes multiple calls to base constructor C
--> ROOT/tests/ui/resolve/base_ctor_args.sol:LL:CC
|
LL | contract F is E {
| ^
|

note: first call here
--> ROOT/tests/ui/resolve/base_ctor_args.sol:LL:CC
|
LL | constructor() C(2) {}
| -
|

note: second call here
--> ROOT/tests/ui/resolve/base_ctor_args.sol:LL:CC
|
LL | constructor() C(5) {}
| -
|

error: contract makes multiple calls to base constructor C
--> ROOT/tests/ui/resolve/base_ctor_args.sol:LL:CC
|
LL | contract G is F {}
| ^
|

note: first call here
--> ROOT/tests/ui/resolve/base_ctor_args.sol:LL:CC
|
LL | constructor() C(2) {}
| -
|

note: second call here
--> ROOT/tests/ui/resolve/base_ctor_args.sol:LL:CC
|
LL | constructor() C(5) {}
| -
|

error: aborting due to 2 previous errors

Loading