Skip to content

Commit

Permalink
fix: respect namespaces when resolving overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr committed Aug 8, 2024
1 parent e856292 commit 18c2ed4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
77 changes: 40 additions & 37 deletions crates/sol-macro-expander/src/expand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,52 +257,55 @@ impl<'ast> ExpCtxt<'ast> {
fn mk_overloads_map(&mut self) -> std::result::Result<(), ()> {
let mut overloads_map = std::mem::take(&mut self.overloads);

for (namespace, overloaded_items) in &self.overloaded_items.0 {
let all_orig_names: Vec<_> =
overloaded_items.values().flatten().filter_map(|f| f.name()).collect();

for namespace in &self.overloaded_items.0.keys().cloned().collect::<Vec<_>>() {
let mut failed = false;

for functions in overloaded_items.values().filter(|fs| fs.len() >= 2) {
// check for same parameters
for (i, &a) in functions.iter().enumerate() {
for &b in functions.iter().skip(i + 1) {
if a.eq_by_types(b) {
self.with_namespace(namespace.clone(), |this| {
let overloaded_items = this.overloaded_items.0.get(namespace).unwrap();
let all_orig_names: Vec<_> =
overloaded_items.values().flatten().filter_map(|f| f.name()).collect();

for functions in overloaded_items.values().filter(|fs| fs.len() >= 2) {
// check for same parameters
for (i, &a) in functions.iter().enumerate() {
for &b in functions.iter().skip(i + 1) {
if a.eq_by_types(b) {
failed = true;
emit_error!(
a.span(),
"{} with same name and parameter types defined twice",
a.desc();

note = b.span() => "other declaration is here";
);
}
}
}

for (i, &item) in functions.iter().enumerate() {
let Some(old_name) = item.name() else {
continue;
};
let new_name = format!("{old_name}_{i}");
if let Some(other) = all_orig_names.iter().find(|x| x.0 == new_name) {
failed = true;
emit_error!(
a.span(),
"{} with same name and parameter types defined twice",
a.desc();
old_name.span(),
"{} `{old_name}` is overloaded, \
but the generated name `{new_name}` is already in use",
item.desc();

note = b.span() => "other declaration is here";
);
note = other.span() => "other declaration is here";
)
}
}
}

for (i, &item) in functions.iter().enumerate() {
let Some(old_name) = item.name() else {
continue;
};
let new_name = format!("{old_name}_{i}");
if let Some(other) = all_orig_names.iter().find(|x| x.0 == new_name) {
failed = true;
emit_error!(
old_name.span(),
"{} `{old_name}` is overloaded, \
but the generated name `{new_name}` is already in use",
item.desc();

note = other.span() => "other declaration is here";
)
overloads_map
.entry(namespace.clone())
.or_default()
.insert(item.signature(this), new_name);
}

overloads_map
.entry(namespace.clone())
.or_default()
.insert(item.signature(self), new_name);
}
}
});

if failed {
return Err(());
Expand Down
2 changes: 1 addition & 1 deletion crates/sol-types/tests/macros/sol/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,4 @@ fn balancer_v2_vault() {
BalancerV2Vault::PoolBalanceChanged::SIGNATURE,
"PoolBalanceChanged(bytes32,address,address[],int256[],uint256[])"
);
}
}

0 comments on commit 18c2ed4

Please sign in to comment.