Repository navigation
regression: rustc suggests .as_ref() at incorrect location and other spans have regressed #90286
Description
Activity
- addedA-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-betaPerformance or correctness regression from stable to beta.Performance or correctness regression from stable to beta.C-bugCategory: This is a bug.Category: This is a bug.A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.Diagnostics: A structured suggestion resulting in incorrect code.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.Diagnostics: A diagnostic that is giving misleading or incorrect information.
on Oct 25, 2021 - addedI-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}Issue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
on Oct 25, 2021 - addedE-needs-bisectionCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustcCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc
on Oct 25, 2021 My hunch is that this regression is caused by the change in error spans between stable and beta, which also feels like a regression to me since it makes the span less specific:
stable:
2 | opt.map(|x| x.to_string()).unwrap_or_else(String::new) | ^^^ move occurs because `*opt` has type `Option<Box<i32>>`, which does not implement the `Copy` traitbeta:
2 | opt.map(|x| x.to_string()).unwrap_or_else(String::new) | ^^^^^^^^^^^^^^^^^^^^^^^^^^ move occurs because `*opt` has type `Option<Box<i32>>`, which does not implement the `Copy` traitsearched nightlies: from nightly-2021-09-29 to nightly-2021-10-21
regressed nightly: nightly-2021-10-01
searched commit range: 1149193...aa7aca3
regressed commit: 4aa7879bisected with cargo-bisect-rustc v0.6.1
Host triple: x86_64-unknown-linux-gnu
Reproduce with:cargo bisect-rustc --preserve --test-dir=tstdr --end=2021-10-21 --script=./test.sh
test.sh:#!/bin/sh cargo check 2>&1 | grep " ^^^ move occurs because"
The new spans caused this suggestion regression, and I feel like they make the errors harder to read in some cases.
@camelid: Setting aside the incorrect suggestion for the moment, I think the new span is better. By itself, pointing to
optdoes not explain why a move is occuring, as not every usage of a variable name is a move. Pointing to the the method call helps emphasize that the method call is responsible for the move.However, I think adding a note could help emphasize that the method is causing the move (we already do this for other borrowcheck errors).
The incorrect suggestion is tricky - I'll see if I can do something about that.
Setting aside the incorrect suggestion for the moment, I think the new span is better. By itself, pointing to
optdoes not explain why a move is occuring, as not every usage of a variable name is a move. Pointing to the the method call helps emphasize that the method call is responsible for the move.I understand, and I can see how highlighting the whole call is more correct in some ways, but then a lot of code is highlighted, which can be visually distracting. I wonder if it could make sense to highlight just the method name?
opt.map(|x| x.to_string()) ^^^^^^^Although that looks a bit weird. Another option is something like this:
opt.map(|x| x.to_string()) ^^^ --- `opt` moved by `map` hereAdditionally, a large part of why I feel the new span is visually distracting is that
move occurs because `*opt` has type `Option<Box<i32>>`, which does not implement the `Copy` traittakes up a lot of space as aspan_labelwith the new span. Perhaps the new span could be made easier to read if the label were moved to anote?Reacted by waffle19 remaining items
- addedregression-from-stable-to-stablePerformance or correctness regression from one stable version to another.Performance or correctness regression from one stable version to another.and removedregression-from-stable-to-betaPerformance or correctness regression from stable to beta.Performance or correctness regression from stable to beta.
on Dec 2, 2021 Could, as a temporary somewhat-partial mitigation, the
as_refsuggestion be downgraded from {points directly at a (currently wrong) location in the code} to {it is suggested in a note that an as_ref should be inserted near the highlighted code, but without a direct pointer with +++}?. That would reduce the confusion a bit, I suppose.Triage: fixed in current beta:
error[E0507]: cannot move out of `*opt` which is behind a shared reference --> src/lib.rs:2:5 | 2 | opt.map(|x| x.to_string()).unwrap_or_else(String::new) | ^^^^---------------------- | | | | | `*opt` moved due to this method call | move occurs because `*opt` has type `Option<Box<i32>>`, which does not implement the `Copy` trait | note: this function takes ownership of the receiver `self`, which moves `*opt` help: consider calling `.as_ref()` to borrow the type's contents | 2 | opt.as_ref().map(|x| x.to_string()).unwrap_or_else(String::new) | +++++++++Reacted by Noah Lev- addedE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.Call for participation: An issue has been fixed and does not reproduce, but no test has been added.and removedP-highHigh priorityHigh priorityregression-from-stable-to-stablePerformance or correctness regression from one stable version to another.Performance or correctness regression from one stable version to another.C-bugCategory: This is a bug.Category: This is a bug.
on Aug 19, 2022 Hm, it seems like there are already tests for
as_refsuggestion, namelyoption-content-move.rs,as-ref.rsandas-ref-2.rs, should this be closed then?Reacted by Esteban Kubershould we wait for the commits which seem to fix this to hit stable, or can we close this already?
We can close now.
Code
Playground (regressed rustc version)
Output (stable 1.56.0, good)
Output (beta 1.57.0-beta.2, incorrect)