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

Correct Fixes for Options and Results in Certain Contexts #96537

Closed
wants to merge 4 commits into from

Conversation

George-lewis
Copy link
Contributor

Corrects the suggested fixes for Options and Results in certain contexts as mentioned in #96438.

The new code adjusts the span before applying the suggestion so that it's applied in the correct place, and also suggests both .as_ref and .as_mut. There may be a way to analyze the use of the moved variable to determine which one is needed, but I hope that this is sufficient for now.

Closes #96438

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 29, 2022
@rust-highfive
Copy link
Collaborator

r? @petrochenkov

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 29, 2022
// the suggestion format strangely, so we adjust the hi bound manually
span.with_hi(span.hi() - BytePos(1))
}
_ => span,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suspect that this case may never happen (as an Option or Result must always be unwraped, or expected, etc) but am not confident enough to make this a panic or some such. Can change at reviewer discretion

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure, let's keep it as is.
(And set applicability to MaybeIncorrect for this case.)

@@ -8,7 +8,7 @@ impl LipogramCorpora {
pub fn validate_all(&mut self) -> Result<(), char> {
for selection in &self.selections {
if selection.1.is_some() {
if selection.1.as_ref().unwrap().contains(selection.0) {
if selection.1.as_ref().as_mut().unwrap().contains(selection.0) {
Copy link
Contributor Author

@George-lewis George-lewis Apr 29, 2022

Choose a reason for hiding this comment

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

I'm not sure how to stop it from doing this (applying both suggestions)

Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if you use span_suggestion_verbose twice instead of one span_suggestions?

@@ -8,7 +8,7 @@ impl LipogramCorpora {
pub fn validate_all(&mut self) -> Result<(), char> {
for selection in &self.selections {
if selection.1.is_some() {
if selection.1.as_ref().unwrap().contains(selection.0) {
if selection.1.as_ref().as_mut().unwrap().contains(selection.0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if you use span_suggestion_verbose twice instead of one span_suggestions?


// Unfortunately the new span has a "." on the end which makes
// the suggestion format strangely, so we adjust the hi bound manually
span.with_hi(span.hi() - BytePos(1))
Copy link
Contributor

Choose a reason for hiding this comment

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

If it has . on the end, then maybe we should suggest as_ref(). instead of .as_ref().

Span arithmetic is dangerous because macros can set spans arbitrarily and span - BytePos(1) can potentially underflow or point into the middle of a unicode character producing an ICE.

Copy link
Contributor Author

@George-lewis George-lewis Apr 29, 2022

Choose a reason for hiding this comment

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

Sounds good to me, I'll remove the span arithmetic. This + the previous suggestion gives the following output for issue-96438.rs:

error[E0507]: cannot move out of dereference of `RefMut<'_, Option<Vec<()>>>`
 --> ./src/test/ui/borrowck/issue-96438.rs:4:5
  |
4 |     cell.borrow_mut().unwrap().pop().unwrap();
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^ move occurs because value has type `Option<Vec<()>>`, which does not implement the `Copy` trait
  |
help: consider borrowing the `Option`'s content
  |
4 |     cell.borrow_mut().as_ref().unwrap().pop().unwrap();
  |                       +++++++++
help: consider borrowing the `Option`'s content
  |
4 |     cell.borrow_mut().as_mut().unwrap().pop().unwrap();
  |                       +++++++++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0507`.

Which is alright, however it doesn't seem to fix the double application in option-content-move.stderr unfortunately.

// the suggestion format strangely, so we adjust the hi bound manually
span.with_hi(span.hi() - BytePos(1))
}
_ => span,
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure, let's keep it as is.
(And set applicability to MaybeIncorrect for this case.)

@petrochenkov petrochenkov 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 Apr 29, 2022
@JohnCSimon JohnCSimon 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 22, 2022
@bors
Copy link
Contributor

bors commented Jun 13, 2022

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

@JohnCSimon
Copy link
Member

@George-lewis
Ping from triage: I'm closing this due to inactivity, Please reopen when you are ready to continue with this.
Note: if you do please open the PR BEFORE you push to it, else you won't be able to reopen - this is a quirk of github.
Thanks for your contribution.

@rustbot label: +S-inactive

@JohnCSimon JohnCSimon closed this Jul 3, 2022
@rustbot rustbot added the S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. label Jul 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

Wrong fix suggested for missing as_mut in the presence of RefCell<Option<>>
6 participants