noir icon indicating copy to clipboard operation
noir copied to clipboard

fix: correctly solve paths and generics in trait default methods

Open asterite opened this issue 10 months ago • 1 comments

Description

Problem

Resolves #8632

Summary

This PR implements a workaround for solving #8632. The correct solution would be the one described in https://github.com/noir-lang/noir/issues/9020 but, as noted there, it's not trivial to do so. But this workaround would unblock users that use trait default methods (which, I think, is a very useful feature).

There are two workarounds here:

  1. Because the trait default method is copied to the trait impl, if the default method refers to a type or function by path, that path was incorrectly looked up starting from the trait impl's module, not from the trait module. For this, default methods copied into trait impls will now have a path_resolution_module set, which will be set in the Elaborator so it uses that module for path resolution.
  2. The next issue was that if a trait default method refers to a trait generic, that generic no longer exists in the trait impl as it's been substituted by a concrete type. To solve this, we define these generics with solved typed when defining the copied trait method.

Once #9020 is implemented (could be after 1.0) we can undo these workarounds.

Additional Context

Documentation

Check one:

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

PR Checklist

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

asterite avatar Jun 25 '25 14:06 asterite

Not sure about implementing a workaround when I'm also unsure how difficult the full fix would be. I'd like to take another stab at https://github.com/noir-lang/noir/issues/8797 which could maybe open us up to a different implementation of this where we elaborate once on the trait and use that instead of copying beforehand.

jfecher avatar Jun 25 '25 16:06 jfecher