Skip to content

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

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

Fix suggestion for mutability mismatch between trait def and trait impl#161827
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
pub use crate::ty::diagnostics::*;
use crate::ty::fast_reject::SimplifiedType;
use crate::ty::layout::{FnAbiError, LayoutError};
pub use crate::ty::print::guess_def_namespace;

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.

this (and the function being made pub) looks vestigial

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Removed (so I can have a "clean" state for the next step).

@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.

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

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. 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