Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
TilakMaddy committed Dec 15, 2024
1 parent 45074a4 commit 499cc68
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
34 changes: 11 additions & 23 deletions crates/sema/src/ast_lowering/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,20 +212,18 @@ impl super::LoweringContext<'_, '_, '_> {
let scopes = SymbolResolverScopes::new_in(hir_lbc_contract.source, None);

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

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

Expand All @@ -241,13 +239,7 @@ impl super::LoweringContext<'_, '_, '_> {
};
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();
let main_con_name = main_contract.name.as_str();
bc_errors.push(BCError::from(
main_con_name,
base_con_name,
old_value,
b.name.span(),
));
bc_errors.push(BCError::from(base_con_name, old_value, b.name.span()));
}
}

Expand All @@ -262,24 +254,20 @@ impl super::LoweringContext<'_, '_, '_> {
};
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();
let main_con_name = main_contract.name.as_str();
bc_errors.push(BCError::from(
main_con_name,
base_con_name,
old_value,
m.name.span(),
));
bc_errors.push(BCError::from(base_con_name, old_value, m.name.span()));
}
}
}

for bc_error in bc_errors {
let msg = format!(
"in {}, the base contract {}'s constructor arguments are called multiple times",
bc_error.main_con_name,
bc_error.base_con_name
);
self.dcx().err(msg).span(bc_error.first_call_span).emit();
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();
self.dcx().note("second call here").span(bc_error.second_call_span).emit();
}
}
Expand Down
7 changes: 3 additions & 4 deletions tests/ui/resolve/base_ctor_args.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ contract E is C {
constructor() C(5) {}
}

contract F is E {
constructor() C(2) {} //~ERROR: in F, the base contract C's constructor arguments are called multiple times
//~^ERROR: in G, the base contract C's constructor arguments are called multiple times
contract F is E { //~ERROR: contract makes multiple calls to base constructor C
constructor() C(2) {}
}

contract G is F {}
contract G is F {} //~ERROR: contract makes multiple calls to base constructor C
26 changes: 20 additions & 6 deletions tests/ui/resolve/base_ctor_args.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
error: in F, the base contract C's constructor arguments are called multiple times
error: contract makes multiple calls to base constructor C
--> ROOT/tests/ui/resolve/base_ctor_args.sol:LL:CC
|
LL | constructor() C(2) {}
| ^
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
Expand All @@ -12,11 +19,18 @@ LL | constructor() C(5) {}
| -
|

error: in G, the base contract C's constructor arguments are called multiple times
error: contract makes multiple calls to base constructor C
--> ROOT/tests/ui/resolve/base_ctor_args.sol:LL:CC
|
LL | constructor() C(2) {}
| ^
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
Expand Down

0 comments on commit 499cc68

Please sign in to comment.