-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Improve shadowed private field diagnostics #154675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rust-bors
merged 5 commits into
rust-lang:main
from
chenyukang:yukang-fix-149546-private-field-deref-fresh
Apr 4, 2026
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
62db1eb
Improve shadowed private field diagnostics
chenyukang 257d655
extend note for private field to more diagnostics
chenyukang 86f99d2
extend note for private field to method call
chenyukang ce46df2
Sort shadowed field notes by source order
chenyukang ae899cc
Clarify private field autoderef notes
chenyukang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
tests/ui/privacy/private-field-deref-confusion-issue-149546.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| // Field lookup still resolves to the public field on the Deref target, but | ||
| // follow-up diagnostics should explain that the original type has a same-named | ||
| // private field with a different type. | ||
|
|
||
| mod structs { | ||
| pub struct A { | ||
| field: usize, | ||
| b: B, | ||
| } | ||
|
|
||
| pub struct B { | ||
| pub field: bool, | ||
| } | ||
|
|
||
| impl std::ops::Deref for A { | ||
| type Target = B; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| &self.b | ||
| } | ||
| } | ||
| } | ||
|
|
||
| use structs::A; | ||
|
|
||
| fn takes_usize(_: usize) {} | ||
| //~^ NOTE function defined here | ||
|
|
||
| trait Marker {} | ||
|
|
||
| impl Marker for usize {} | ||
| //~^ HELP the trait `Marker` is implemented for `usize` | ||
|
|
||
| struct Wrapper(i32); | ||
|
|
||
| impl<T: Marker> std::ops::Add<T> for Wrapper { | ||
| //~^ NOTE required for `Wrapper` to implement `Add<bool>` | ||
| //~| NOTE unsatisfied trait bound introduced here | ||
| type Output = (); | ||
|
|
||
| fn add(self, _: T) {} | ||
| } | ||
|
|
||
| fn by_value(a: A) { | ||
| a.field + 5; | ||
| //~^ ERROR cannot add `{integer}` to `bool` | ||
| //~| NOTE bool | ||
| //~| NOTE {integer} | ||
| //~| NOTE there is a field `field` on `A` with type `usize`, but it is private | ||
| } | ||
|
|
||
| fn by_ref(a: &A) { | ||
| a.field + 5; | ||
| //~^ ERROR cannot add `{integer}` to `bool` | ||
| //~| NOTE bool | ||
| //~| NOTE {integer} | ||
| //~| NOTE there is a field `field` on `A` with type `usize`, but it is private | ||
| } | ||
|
|
||
| fn rhs_by_value(a: A) { | ||
| 5 + a.field; | ||
| //~^ ERROR cannot add `bool` to `{integer}` | ||
| //~| NOTE no implementation for `{integer} + bool` | ||
| //~| HELP the trait `Add<bool>` is not implemented for `{integer}` | ||
| //~| HELP the following other types implement trait `Add<Rhs>`: | ||
| //~| NOTE there is a field `field` on `A` with type `usize`, but it is private | ||
| } | ||
|
|
||
| fn rhs_by_ref(a: &A) { | ||
| 5 + a.field; | ||
| //~^ ERROR cannot add `bool` to `{integer}` | ||
| //~| NOTE no implementation for `{integer} + bool` | ||
| //~| HELP the trait `Add<bool>` is not implemented for `{integer}` | ||
| //~| HELP the following other types implement trait `Add<Rhs>`: | ||
| //~| NOTE there is a field `field` on `A` with type `usize`, but it is private | ||
| } | ||
|
|
||
| fn rhs_assign_op_by_value(a: A) { | ||
| let mut n = 5; | ||
| n += a.field; | ||
| //~^ ERROR cannot add-assign `bool` to `{integer}` | ||
| //~| NOTE no implementation for `{integer} += bool` | ||
| //~| HELP the trait `AddAssign<bool>` is not implemented for `{integer}` | ||
| //~| HELP the following other types implement trait `AddAssign<Rhs>`: | ||
| //~| NOTE there is a field `field` on `A` with type `usize`, but it is private | ||
| } | ||
|
|
||
| fn rhs_assign_op_by_ref(a: &A) { | ||
| let mut n = 5; | ||
| n += a.field; | ||
| //~^ ERROR cannot add-assign `bool` to `{integer}` | ||
| //~| NOTE no implementation for `{integer} += bool` | ||
| //~| HELP the trait `AddAssign<bool>` is not implemented for `{integer}` | ||
| //~| HELP the following other types implement trait `AddAssign<Rhs>`: | ||
| //~| NOTE there is a field `field` on `A` with type `usize`, but it is private | ||
| } | ||
|
|
||
| fn rhs_nested_obligation(a: A) { | ||
| Wrapper(5) + a.field; | ||
| //~^ ERROR the trait bound `bool: Marker` is not satisfied | ||
| //~| NOTE the trait `Marker` is not implemented for `bool` | ||
| //~| NOTE there is a field `field` on `A` with type `usize`, but it is private | ||
| } | ||
|
|
||
| fn method_call(a: A) { | ||
| a.field.count_ones(); | ||
| //~^ ERROR no method named `count_ones` found for type `bool` in the current scope | ||
| //~| NOTE method not found in `bool` | ||
| //~| NOTE there is a field `field` on `A` with type `usize`, but it is private | ||
| } | ||
|
|
||
| fn type_mismatch(a: A) { | ||
| let value: usize = a.field; | ||
| //~^ ERROR mismatched types | ||
| //~| NOTE expected `usize`, found `bool` | ||
| //~| NOTE expected due to this | ||
| //~| NOTE there is a field `field` on `A` with type `usize`, but it is private | ||
| eprintln!("value: {value}"); | ||
| } | ||
|
|
||
| fn function_arg(a: A) { | ||
| takes_usize(a.field); | ||
| //~^ ERROR mismatched types | ||
| //~| NOTE expected `usize`, found `bool` | ||
| //~| NOTE arguments to this function are incorrect | ||
| //~| NOTE there is a field `field` on `A` with type `usize`, but it is private | ||
| } | ||
|
|
||
| fn return_value(a: &A) -> usize { | ||
| //~^ NOTE expected `usize` because of return type | ||
| a.field | ||
| //~^ ERROR mismatched types | ||
| //~| NOTE expected `usize`, found `bool` | ||
| //~| NOTE there is a field `field` on `A` with type `usize`, but it is private | ||
| } | ||
|
|
||
| fn main() {} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.