Skip to content

fix: allow calling trait impl method from struct if multiple impls exist#7074

Closed
asterite wants to merge 4 commits intomasterfrom
ab/multiple-trait-impls
Closed

fix: allow calling trait impl method from struct if multiple impls exist#7074
asterite wants to merge 4 commits intomasterfrom
ab/multiple-trait-impls

Conversation

@asterite
Copy link
Collaborator

@asterite asterite commented Jan 15, 2025

Description

Problem

Related to #7090

Summary

Edit: this one is very had to fix. The main issue is that:

  1. We have to solve U60Repr::from2()
  2. That's a call, so we have to solve what the call is pointing to, that is, solve U60Repr::from2
  3. That's a path lookup. In our code a path lookup, for functions, end up returning a single FuncId. In this case it's a trait method so there are potentially multiple FuncId, so we'd need to represent the result in a different way here. And once we do that, change the code to support this new result variant.

It seems we had logic in place to remove a method definition if it gives an error while adding it. When multiple trait impl methods existed for the same struct (in different impls), the second definition would erase both, though only from the ModuleData (I think the definitions still exist somewhere else so that's why using Trait:: worked).

I'm not sure why that was done, but I changed it to not do that. All tests seem to pass, so either that was unneeded or there is a scenario that breaks with this change but it's untested.

Additional Context

I also introduced refactors/renames to make the code a bit clearer.

Documentation

Check one:

  • No documentation needed.
  • Documentation included in this PR.
  • [For Experimental Features] Documentation to be submitted in a separate PR.

PR Checklist

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

@github-actions
Copy link
Contributor

github-actions bot commented Jan 15, 2025

Compilation Memory Report

Program Peak Memory
keccak256 77.79M
workspace 123.97M
regression_4709 424.16M
ram_blowup_regression 1.58G
rollup-root 204.78M
rollup-merge 204.78M
rollup-block-root-single-tx 204.78M
rollup-block-root-empty 204.78M
rollup-block-root 204.78M
rollup-block-merge 204.78M
rollup-base-public 204.78M
rollup-base-private 204.78M
private-kernel-tail 168.88M
private-kernel-reset 168.91M
private-kernel-inner 168.88M

@github-actions
Copy link
Contributor

github-actions bot commented Jan 15, 2025

Execution Memory Report

Program Peak Memory
keccak256 74.75M
workspace 123.98M
regression_4709 316.07M
ram_blowup_regression 512.61M
rollup-root 204.78M
rollup-merge 204.78M
rollup-block-root 204.78M
rollup-block-merge 204.78M
rollup-base-public 204.78M
rollup-base-private 204.78M
private-kernel-tail 168.88M
private-kernel-reset 168.91M
private-kernel-inner 168.88M

@asterite asterite marked this pull request as draft January 15, 2025 14:29
@asterite
Copy link
Collaborator Author

Closing as this PR is only part of the fix but doing the full fix is very hard.

@asterite asterite closed this Jan 15, 2025
@asterite asterite deleted the ab/multiple-trait-impls branch January 15, 2025 16:23
@TomAFrench
Copy link
Member

@asterite can you make a new PR with tests from this one with #[should_panic] attributes? These should link off to an issue on this repo tracking that we need to fix these.

Comment on lines -1521 to -1525
// Only remove the existing function from scope if it is from a trait impl as
// well. If it is from a non-trait impl that should override trait impl methods
// anyway so that Foo::bar always resolves to the non-trait impl version.
if self.interner.function_meta(&existing).trait_impl.is_some() {
module.remove_function(method.name_ident());
Copy link
Contributor

Choose a reason for hiding this comment

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

IIRC without this check the compiler would still call one of the trait methods on error that wasn't removed instead of erroring that there were two matching candidates or something. Could be wrong, this has been here a while

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants