Skip to content

Fix suggestion for mutability mismatch between trait def and trait impl in 2051 edition - #161827

Open
GuillaumeGomez wants to merge 1 commit into
rust-lang:mainfrom
GuillaumeGomez:trait-mut-mismatch
Open

GuillaumeGomez wants to merge 1 commit into
rust-lang:mainfrom
GuillaumeGomez:trait-mut-mismatch

Conversation

@GuillaumeGomez

@GuillaumeGomez GuillaumeGomez commented Aug 26, 2026

Copy link
Copy Markdown
Member

This code:

mod bar {
    pub struct X;
}

pub trait A {
    fn f(x: &mut bar::X);
}

mod b {
    pub struct X;

    impl crate::A for X {
        fn f(_x: &crate::bar::X) {}
    }
}

emits:

error[E0053]: method `f` has an incompatible type for trait
  --> foo.rs:13:18
   |
13 |         fn f(_x: &crate::bar::X) {}
   |                  ^^^^^^^^^^^^^^ types differ in mutability
   |
note: type in trait
  --> foo.rs:6:13
   |
 6 |     fn f(x: &mut bar::X);
   |             ^^^^^^^^^^^
   = note: expected signature `fn(&mut X)`
              found signature `fn(&X)`
help: change the parameter type to match the trait
   |
13 -         fn f(_x: &crate::bar::X) {}
13 +         fn f(_x: &mut bar::X) {}
   |

Which is wrong. This PR fixes it. However the Ty doesn't hold a context (or at least not a correct one) allowing to have a correct path to the X type. So instead, in case of a mutability mismatch, we (try to) retrieve the code snippet, and remove/add the mut keyword. If we fail to, we revert to the current behaviour.

r? @mejrs

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 26, 2026
@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Aug 26, 2026
Comment thread tests/ui/impl-trait/trait_type.stderr Outdated
@GuillaumeGomez
GuillaumeGomez force-pushed the trait-mut-mismatch branch 2 times, most recently from b9e27f2 to 2f32fc5 Compare August 26, 2026 17:43

@mejrs mejrs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(for context: #t-compiler/help > Wrong Ty "scope"(?) in suggestion @ 💬)

It looks like the real problem here is there's no functionality (or we're unaware of it) for "what is the proper path to item X to use it in scope Y". Can you find or create something like that rather than have all of this just to support one case of a type error?

View changes since this review

Comment thread compiler/rustc_middle/src/ty/mod.rs Outdated
@GuillaumeGomez

Copy link
Copy Markdown
Member Author

It looks like the real problem here is there's no functionality (or we're unaware of it) for "what is the proper path to item X to use it in scope Y". Can you find or create something like that rather than have all of this just to support one case of a type error?

Agreed, the solution I came up with is not satisfying. So I'd love to create this new API, but I will likely need some help.

@mejrs

mejrs commented Aug 27, 2026

Copy link
Copy Markdown
Member

I think with_crate_prefix should work for this? Or with_resolve_crate_name? I haven't looked deeply in what they do but they look like what we want. But I'm unsure it's correct on the 2015 edition.

@mejrs mejrs added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 27, 2026
@GuillaumeGomez

Copy link
Copy Markdown
Member Author

With rustc_middle::ty::print::with_resolve_crate_name!(trait_ty):

&mut bar::X

With rustc_middle::ty::print::with_crate_prefix!(trait_ty):

&mut bar::X

And with rustc_middle::ty::print::with_crate_prefix!(rustc_middle::ty::print::with_resolve_crate_name!(trait_ty)):

&mut bar::X

So unless I wrote invalid code, I'll instead tweak how we display Ty directly, going through Adt if there is one, otherwise leaving the current output.

@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Ok I'm a moron. And I'll rewrite these macros because it made me loss way too much time.

@rustbot

rustbot commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@GuillaumeGomez GuillaumeGomez changed the title Fix suggestion for mutability mismatch between trait def and trait impl Fix suggestion for mutability mismatch between trait def and trait impl in 2051 edition Sep 2, 2026
@GuillaumeGomez

Copy link
Copy Markdown
Member Author

Updated. The fix was actually to allow the 2015 edition to have paths prepended with crate::.

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

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants