Skip to content

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

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
GuillaumeGomez:trait-mut-mismatch
Sep 22, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
GuillaumeGomez:trait-mut-mismatch

Conversation

@GuillaumeGomez

@GuillaumeGomez GuillaumeGomez commented Aug 26, 2026 •

Copy link
Copy Markdown
Member

View all comments

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

This comment has been minimized.

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

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

Copy link
Copy Markdown
Member Author

@rustbot ready

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

@mejrs mejrs Sep 21, 2026 •

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.

it looks like this test need not have edition 2015/2018 revisions anymore.

View changes since the review

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.

Indeed, removed revisions.

@mejrs mejrs Sep 21, 2026 •

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.

Can you change this test to run for both edition 2015 and 2018? The differences in path resolution can be quite subtle.

View changes since the review

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.

Added it.

@rustbot

rustbot commented Sep 21, 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

Copy link
Copy Markdown
Member Author

Applied suggestions.

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

r=me when ci green

View changes since this review

@mejrs

mejrs commented Sep 21, 2026

Copy link
Copy Markdown
Member

@bors r+ rollup

@rust-bors

rust-bors Bot commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 7542191 has been approved by mejrs

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 21, 2026
@rust-bors

This comment has been minimized.

@rust-bors rust-bors Bot added merged-by-bors This PR was explicitly merged by bors. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Sep 22, 2026
@rust-bors

rust-bors Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

☀️ Test successful - CI
Approved by: mejrs
Duration: 3h 6m 34s
Pushing 88638df to main...

@rust-bors
rust-bors Bot merged commit 88638df into rust-lang:main Sep 22, 2026
14 checks passed
@rustbot rustbot added this to the 1.100.0 milestone Sep 22, 2026
@github-actions

Copy link
Copy Markdown
Contributor
What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing 1303417 (parent) -> 88638df (this PR)

Test differences

Show 17 test diffs

Stage 1

  • [ui (polonius)] tests/ui/impl-restriction/impl-restriction-check.rs: [missing] -> pass (J0)
  • [ui (polonius)] tests/ui/impl-restriction/impl-restriction-check.rs#e2015: pass -> [missing] (J0)
  • [ui (polonius)] tests/ui/impl-restriction/impl-restriction-check.rs#e2018: pass -> [missing] (J0)
  • [ui (polonius)] tests/ui/mut/mismatch-trait-mut.rs#e2015: [missing] -> pass (J0)
  • [ui (polonius)] tests/ui/mut/mismatch-trait-mut.rs#e2018: [missing] -> pass (J0)
  • [ui] tests/ui/impl-restriction/impl-restriction-check.rs: [missing] -> pass (J2)
  • [ui] tests/ui/impl-restriction/impl-restriction-check.rs#e2015: pass -> [missing] (J2)
  • [ui] tests/ui/impl-restriction/impl-restriction-check.rs#e2018: pass -> [missing] (J2)
  • [ui] tests/ui/mut/mismatch-trait-mut.rs#e2015: [missing] -> pass (J2)
  • [ui] tests/ui/mut/mismatch-trait-mut.rs#e2018: [missing] -> pass (J2)

Stage 2

  • [ui] tests/ui/impl-restriction/impl-restriction-check.rs: [missing] -> pass (J1)
  • [ui] tests/ui/impl-restriction/impl-restriction-check.rs#e2015: pass -> [missing] (J1)
  • [ui] tests/ui/impl-restriction/impl-restriction-check.rs#e2018: pass -> [missing] (J1)
  • [ui] tests/ui/mut/mismatch-trait-mut.rs#e2015: [missing] -> pass (J1)
  • [ui] tests/ui/mut/mismatch-trait-mut.rs#e2018: [missing] -> pass (J1)

Additionally, 2 doctest diffs were found. These are ignored, as they are noisy.

Job group index

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard 88638df1e741778019e42fcdf307072865d19501 --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. test-x86_64-msvc-2: 1h 43m -> 2h 40m (+55.2%)
  2. test-i686-gnu-nopt-1: 1h 32m -> 2h 21m (+53.1%)
  3. dist-armv7-linux: 1h 9m -> 1h 38m (+40.9%)
  4. dist-x86_64-mingw: 2h 53m -> 1h 48m (-37.4%)
  5. dist-powerpc64-linux-gnu: 1h 38m -> 1h 3m (-35.6%)
  6. test-x86_64-gnu-llvm-21: 56m 26s -> 36m 24s (-35.5%)
  7. test-x86_64-gnu-llvm-21-3: 1h 29m -> 1h 59m (+34.1%)
  8. test-aarch64-apple-macos-26-1: 2h 28m -> 1h 38m (-33.8%)
  9. test-aarch64-apple-macos-26-2: 3h 9m -> 2h 5m (-33.5%)
  10. dist-x86_64-llvm-mingw: 1h 41m -> 2h 15m (+33.5%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (88638df): comparison URL.

Overall result: ❌ regressions - no action needed

@rustbot label: -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.4% [0.4%, 0.4%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary 2.9%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.9% [2.9%, 2.9%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.9% [2.9%, 2.9%] 1

Cycles

This perf run didn't have relevant results for this metric.

Binary size

Results (primary -0.0%, secondary -0.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.0% [-0.1%, -0.0%] 11
Improvements ✅
(secondary)
-0.0% [-0.1%, -0.0%] 6
All ❌✅ (primary) -0.0% [-0.1%, -0.0%] 11

Bootstrap: 485.912s -> 487.826s (0.39%)
Artifact size: 406.36 MiB -> 406.29 MiB (-0.02%)

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

Labels

merged-by-bors This PR was explicitly merged by bors. 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.

4 participants