Improve shadowed private field diagnostics#154675
Improve shadowed private field diagnostics#154675rust-bors[bot] merged 5 commits intorust-lang:mainfrom
Conversation
6b61a4e to
86f99d2
Compare
compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs
Outdated
Show resolved
Hide resolved
compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs
Outdated
Show resolved
Hide resolved
| | | | ||
| | arguments to this function are incorrect | ||
| | | ||
| = note: there is a field `field` on `A` with type `usize`, but it is private |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
yes, seems much clear, the code is updated.
| let mut hir_ids = Vec::new(); | ||
| let mut push_hir_id = |hir_id| { | ||
| if !hir_ids.contains(&hir_id) { | ||
| hir_ids.push(hir_id); | ||
| } |
There was a problem hiding this comment.
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.
13cdaae to
ae899cc
Compare
|
@bors r+ |
…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
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)
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)
…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
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)
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)
Fixes #149546
I tried to extend the diagnostic to more scenarios, like method call, type mismatch errors.
r? @estebank