Skip to content

Improve shadowed private field diagnostics#154675

Merged
rust-bors[bot] merged 5 commits intorust-lang:mainfrom
chenyukang:yukang-fix-149546-private-field-deref-fresh
Apr 4, 2026
Merged

Improve shadowed private field diagnostics#154675
rust-bors[bot] merged 5 commits intorust-lang:mainfrom
chenyukang:yukang-fix-149546-private-field-deref-fresh

Conversation

@chenyukang
Copy link
Copy Markdown
Member

@chenyukang chenyukang commented Apr 1, 2026

Fixes #149546

I tried to extend the diagnostic to more scenarios, like method call, type mismatch errors.

r? @estebank

@rustbot rustbot added 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. labels Apr 1, 2026
@chenyukang chenyukang force-pushed the yukang-fix-149546-private-field-deref-fresh branch from 6b61a4e to 86f99d2 Compare April 1, 2026 12:16
Copy link
Copy Markdown
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

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

I would like to add a bit more information. I think that with these small changes then we'll be giving the absolute best information we can. I don't think I could explain it any better in person :)

View changes since this review

| |
| arguments to this function are incorrect
|
= note: there is a field `field` on `A` with type `usize`, but it is private
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think the only things missing are I'd like to add is modify the message slightly to make it clearer that there's a field on a that is usize which is private, but that there's another field in a deref of a that was ultimately chosen instead, to point at the field we are talking about (maybe even both the bool and the usize with a label) and ideally the impl Deref as well. Something like:

note: there is a field `field` on `A` with type `usize` but it is private; `field` from `B` was accessed through auto-deref instead
   |
LL |     pub struct A {
   |                - in this struct
LL |         field: usize,
   |         ^^^^^ if this field wasn't private, it would be accessible
...
LL |     pub struct B {
   |                - this struct is accessible through auto-deref
LL |         field: bool,
   |         ----- this is the field that was accessed
...
LL |     impl std::ops::Deref for A {
   |     -------------------------- the field was accessed through this `Deref`

The above can be done by taking the span for A::field, calling into on it to turn it into a MultiSpan, then do push_span_label on it with the labels and the secondary spans, and then use that span on a span_note.

All of this adds a bunch of verbosity, but without it I fear that it'll be hard to figure things out. We could consider gating this only on local crates, where the information is most useful, but I feel that one could easily miss the subtle privacy issue when looking at a dependency expecting to be able to access the field, so I'd consider showing this always.

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.

yes, seems much clear, the code is updated.

Comment on lines +253 to +257
let mut hir_ids = Vec::new();
let mut push_hir_id = |hir_id| {
if !hir_ids.contains(&hir_id) {
hir_ids.push(hir_id);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's make this an FxHashSet first until we're done pushing HirIds, and then turn it into a Vec that you can sort. Otherwise, as is contains does a linear scan of all the collected ids on every push, which is O(n2) if I'm not mistaken.

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.

fixed.

@chenyukang chenyukang force-pushed the yukang-fix-149546-private-field-deref-fresh branch from 13cdaae to ae899cc Compare April 3, 2026 05:50
@estebank
Copy link
Copy Markdown
Contributor

estebank commented Apr 3, 2026

@bors r+

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 3, 2026

📌 Commit ae899cc has been approved by estebank

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 Apr 3, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 3, 2026
…te-field-deref-fresh, r=estebank

Improve shadowed private field diagnostics

Fixes rust-lang#149546

I tried to extend the diagnostic to more scenarios, like method call, type mismatch errors.

r? @estebank
rust-bors bot pushed a commit that referenced this pull request Apr 3, 2026
Rollup of 7 pull requests

Successful merges:

 - #153286 (various fixes for scalable vectors)
 - #153592 (Add  `min_adt_const_params` gate)
 - #154675 (Improve shadowed private field diagnostics)
 - #154653 (Remove rustc_on_unimplemented's append_const_msg)
 - #154743 (Remove an unused `StableHash` impl.)
 - #154752 (Add comment to borrow-checker)
 - #154764 (Add tests for three ICEs that have already been fixed)
rust-bors bot pushed a commit that referenced this pull request Apr 3, 2026
Rollup of 7 pull requests

Successful merges:

 - #153286 (various fixes for scalable vectors)
 - #153592 (Add  `min_adt_const_params` gate)
 - #154675 (Improve shadowed private field diagnostics)
 - #154653 (Remove rustc_on_unimplemented's append_const_msg)
 - #154743 (Remove an unused `StableHash` impl.)
 - #154752 (Add comment to borrow-checker)
 - #154764 (Add tests for three ICEs that have already been fixed)
jhpratt added a commit to jhpratt/rust that referenced this pull request Apr 4, 2026
…te-field-deref-fresh, r=estebank

Improve shadowed private field diagnostics

Fixes rust-lang#149546

I tried to extend the diagnostic to more scenarios, like method call, type mismatch errors.

r? @estebank
rust-bors bot pushed a commit that referenced this pull request Apr 4, 2026
Rollup of 10 pull requests

Successful merges:

 - #154376 (Remove more BuiltinLintDiag variants - part 4)
 - #127534 (feat(core): impl Step for NonZero<u*>)
 - #153286 (various fixes for scalable vectors)
 - #153592 (Add  `min_adt_const_params` gate)
 - #154675 (Improve shadowed private field diagnostics)
 - #154703 (Fix trailing comma in lifetime suggestion for empty angle brackets)
 - #154653 (Remove rustc_on_unimplemented's append_const_msg)
 - #154743 (Remove an unused `StableHash` impl.)
 - #154752 (Add comment to borrow-checker)
 - #154764 (Add tests for three ICEs that have already been fixed)
rust-bors bot pushed a commit that referenced this pull request Apr 4, 2026
Rollup of 7 pull requests

Successful merges:

 - #153286 (various fixes for scalable vectors)
 - #153592 (Add  `min_adt_const_params` gate)
 - #154675 (Improve shadowed private field diagnostics)
 - #154653 (Remove rustc_on_unimplemented's append_const_msg)
 - #154743 (Remove an unused `StableHash` impl.)
 - #154752 (Add comment to borrow-checker)
 - #154764 (Add tests for three ICEs that have already been fixed)
@rust-bors rust-bors bot merged commit ce74af2 into rust-lang:main Apr 4, 2026
11 checks passed
@rustbot rustbot added this to the 1.96.0 milestone Apr 4, 2026
rust-timer added a commit that referenced this pull request Apr 4, 2026
Rollup merge of #154675 - chenyukang:yukang-fix-149546-private-field-deref-fresh, r=estebank

Improve shadowed private field diagnostics

Fixes #149546

I tried to extend the diagnostic to more scenarios, like method call, type mismatch errors.

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

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

Private field can cause confusion with identically named field on a deref target

3 participants