Skip to content
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

Suggest unwrapping ???<T> if a method cannot be found on it but is present on T. #102288

Merged
merged 4 commits into from
Sep 28, 2022
Merged

Conversation

mejrs
Copy link
Contributor

@mejrs mejrs commented Sep 25, 2022

This suggests various ways to get inside wrapper types if the method cannot be found on the wrapper type, but is present on the wrappee.

For this PR, those wrapper types include Localkey, MaybeUninit, RefCell, RwLock and Mutex.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @compiler-errors (or someone else) soon.

Please see the contribution instructions for more information.

@rustbot rustbot added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 25, 2022
@rustbot
Copy link
Collaborator

rustbot commented Sep 25, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 25, 2022
compiler/rustc_typeck/src/check/method/suggest.rs Outdated Show resolved Hide resolved
compiler/rustc_typeck/src/check/method/suggest.rs Outdated Show resolved Hide resolved
compiler/rustc_typeck/src/check/method/suggest.rs Outdated Show resolved Hide resolved
compiler/rustc_typeck/src/check/method/suggest.rs Outdated Show resolved Hide resolved
compiler/rustc_typeck/src/check/method/suggest.rs Outdated Show resolved Hide resolved
@compiler-errors compiler-errors 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 Sep 25, 2022
@rust-log-analyzer

This comment has been minimized.

@jackh726
Copy link
Member

I wonder if a cooler approach would be to make a diagnostic attribute that can be added to any struct that signals "hey, this is really just a wrapper for an inner type"

(Though, its probably just easily to take the current approach)

@mejrs
Copy link
Contributor Author

mejrs commented Sep 27, 2022

I wonder if a cooler approach would be to make a diagnostic attribute that can be added to any struct that signals "hey, this is really just a wrapper for an inner type"

That's a cool idea - worth considering I think. It could end up being rather wordy though, like how rustc_unimplemented is really wordy at times.

@bors
Copy link
Contributor

bors commented Sep 27, 2022

☔ The latest upstream changes (presumably #102306) made this pull request unmergeable. Please resolve the merge conflicts.

@rustbot rustbot added T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) A-rustdoc-json Area: Rustdoc JSON backend A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic labels Sep 27, 2022
@rustbot
Copy link
Collaborator

rustbot commented Sep 27, 2022

Some changes occurred in HTML/CSS themes.

cc @GuillaumeGomez

rustc_error_messages was changed

cc @davidtwco, @compiler-errors, @JohnTitor, @estebank, @TaKO8Ki

rustc_macros::diagnostics was changed

cc @davidtwco, @compiler-errors, @JohnTitor, @estebank, @TaKO8Ki

rustdoc-json-types is a public (although nightly-only) API. If possible, consider changing src/librustdoc/json/conversions.rs; otherwise, make sure you bump the FORMAT_VERSION constant.

cc @CraftSpider, @aDotInTheVoid, @Enselic, @obi1kenobi

Some changes occurred in HTML/CSS/JS.

cc @GuillaumeGomez, @Folyd, @jsha

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

A change occurred in the Ayu theme.

cc @Cldfire

This PR changes MIR

cc @oli-obk, @RalfJung, @JakobDegen, @davidtwco, @celinval, @vakaras

Some changes occurred in src/librustdoc/clean/types.rs

cc @camelid

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

@compiler-errors
Copy link
Member

Hm, looks like a rebase was messed up here..

@Noratrieb
Copy link
Member

@rustbot label -A-bootstrap -A-query-system -A-rustdoc-json -A-translation -T-libs

@rustbot rustbot removed T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) A-rustdoc-json Area: Rustdoc JSON backend A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 27, 2022
Comment on lines 1525 to 1546
Some(Mutability::Not) => {
err.span_suggestion_verbose(
expr.span.shrink_to_hi(),
format!(
"use `.borrow()` to borrow the `{ty}`, \
panicking if any outstanding mutable borrows exist."
),
".borrow()",
Applicability::MaybeIncorrect,
);
}
Some(Mutability::Mut) => {
err.span_suggestion_verbose(
expr.span.shrink_to_hi(),
format!(
"use `.borrow_mut()` to mutably borrow the `{ty}`, \
panicking if any outstanding borrows exist."
),
".borrow_mut()",
Applicability::MaybeIncorrect,
);
}
Copy link
Member

@compiler-errors compiler-errors Sep 27, 2022

Choose a reason for hiding this comment

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

Perhaps deduplicate a bit?

Suggested change
Some(Mutability::Not) => {
err.span_suggestion_verbose(
expr.span.shrink_to_hi(),
format!(
"use `.borrow()` to borrow the `{ty}`, \
panicking if any outstanding mutable borrows exist."
),
".borrow()",
Applicability::MaybeIncorrect,
);
}
Some(Mutability::Mut) => {
err.span_suggestion_verbose(
expr.span.shrink_to_hi(),
format!(
"use `.borrow_mut()` to mutably borrow the `{ty}`, \
panicking if any outstanding borrows exist."
),
".borrow_mut()",
Applicability::MaybeIncorrect,
);
}
Some(mutbl) => {
let (suggestion, mutbly, mutbl) = match mutbl {
Mutability::Not => (".borrow()", "", "mutable "),
Mutability::Mut => (".borrow()", "mutably ", ""),
};
err.span_suggestion_verbose(
expr.span.shrink_to_hi(),
format!(
"use `.borrow()` to {mutbly}borrow the `{ty}`, \
panicking if any outstanding {mutbl}borrows exist."
),
suggestion,
Applicability::MaybeIncorrect,
);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm I'm not sure. I prefer being too verbose rather than too clever, and this looks very clever 😄

Copy link
Member

Choose a reason for hiding this comment

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

This is a very common pattern to deduplicate essentially identical diagnostic messages. I'd actually prefer if you deduplicated it even more, but this one is the easiest to apply.

@rust-log-analyzer

This comment has been minimized.

@compiler-errors
Copy link
Member

Actually, since you're adding a bunch of new custom error messages here, do you mind just converting them to derived structs?

https://rustc-dev-guide.rust-lang.org/diagnostics/diagnostic-structs.html

@mejrs
Copy link
Contributor Author

mejrs commented Sep 28, 2022

@rustbot ready

I've deduplicated some of the logic but I don't think I can do anything more clever without making a big mess.

Actually, since you're adding a bunch of new custom error messages here, do you mind just converting them to derived structs?

I could be misunderstanding but I can't emit a subdiagnostic struct on a diagnosticbuilder. So it looks to me like that would entail refactoring basically all of suggest.rs and maybe more, because nothing in it uses error structs. I'm not going to do that.

@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 28, 2022
@compiler-errors
Copy link
Member

I can't emit a subdiagnostic struct on a diagnosticbuilder

https://doc.rust-lang.org/beta/nightly-rustc/rustc_errors/struct.DiagnosticBuilder.html#method.subdiagnostic

@compiler-errors
Copy link
Member

Whatever though, this is fine for now.

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Sep 28, 2022

📌 Commit 4ff83ce has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors 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 28, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 28, 2022
Rollup of 8 pull requests

Successful merges:

 - rust-lang#100747 (Add long description and test for E0311)
 - rust-lang#102232 (Stabilize bench_black_box)
 - rust-lang#102288 (Suggest unwrapping `???<T>` if a method cannot be found on it but is present on `T`.)
 - rust-lang#102338 (Deny associated type bindings within associated type bindings)
 - rust-lang#102347 (Unescaping cleanups)
 - rust-lang#102348 (Tweak `FulfillProcessor`.)
 - rust-lang#102378 (Use already resolved `self_ty` in `confirm_fn_pointer_candidate`)
 - rust-lang#102380 (rustdoc: remove redundant mobile `.source > .sidebar` CSS)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 9436ffc into rust-lang:master Sep 28, 2022
@rustbot rustbot added this to the 1.66.0 milestone Sep 28, 2022
Aaron1011 pushed a commit to Aaron1011/rust that referenced this pull request Jan 6, 2023
Suggest unwrapping `???<T>` if a method cannot be found on it but is present on `T`.

This suggests various ways to get inside wrapper types if the method cannot be found on the wrapper type, but is present on the wrappee.

For this PR, those wrapper types include `Localkey`, `MaybeUninit`, `RefCell`, `RwLock` and `Mutex`.
Aaron1011 pushed a commit to Aaron1011/rust that referenced this pull request Jan 6, 2023
Rollup of 8 pull requests

Successful merges:

 - rust-lang#100747 (Add long description and test for E0311)
 - rust-lang#102232 (Stabilize bench_black_box)
 - rust-lang#102288 (Suggest unwrapping `???<T>` if a method cannot be found on it but is present on `T`.)
 - rust-lang#102338 (Deny associated type bindings within associated type bindings)
 - rust-lang#102347 (Unescaping cleanups)
 - rust-lang#102348 (Tweak `FulfillProcessor`.)
 - rust-lang#102378 (Use already resolved `self_ty` in `confirm_fn_pointer_candidate`)
 - rust-lang#102380 (rustdoc: remove redundant mobile `.source > .sidebar` CSS)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
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.

8 participants