-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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 dereferencing non-lval mutable reference on assignment #94639
Conversation
(rust-highfive has picked a reviewer for you, use r? to override) |
r? rust-lang/compiler |
@@ -963,6 +964,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |||
.find_map(|(ty, steps)| self.probe(|_| coerce.unify(ty, target)).ok().map(|_| steps)) | |||
} | |||
|
|||
/// Given a type, this function will calculate and return the type given | |||
/// for `<Ty as Deref>::Target` only if `Ty` also implements `DerefMut`. | |||
/// This function is for diagnostics, since it does not register sub-obligations. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit concerned this statement is easy to miss. Is it possible we can move this method to somewhere in the diagnostics code? Or perhaps naming this method so that it includes some mention of this being for diagnostics only.
help: consider dereferencing here to assign to the mutable borrowed piece of memory | ||
| | ||
LL | *vec![].last_mut().unwrap() = 3_u8; | ||
| + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we know why we lost this message? (It seems like it is correct unlike the one in the previous commit so I'm surprised it's missing.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think it's bc i added must_apply_modulo_regions when checking if the type implements the trait, instead of may_apply, and the fact that we have an infer var on the lhs. i'll see if it causes too many false errors here.
@rustbot author |
58316f3
to
0af2014
Compare
@rustbot ready |
This comment has been minimized.
This comment has been minimized.
0af2014
to
b03a154
Compare
| | ||
help: `+=` can be used on `isize`, you can dereference `x` | ||
| | ||
LL | let x = |ref x: isize| { *x += 1; }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is kinda a regression, but not actually... &isize
doesn't have a +=
. Perhaps we could suggest changing the ref to ref mut
, but that seems out of scope for this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
r=me with or without nit. Nice work! 💯
"consider dereferencing here to assign to the mutable \ | ||
borrowed piece of memory", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: "piece of" just reads a bit awkwardly to me. Maybe "consider dereferencing here to assign to the mutably borrowed memory"?
b03a154
to
d50d3fc
Compare
@bors r=wesleywiser |
📌 Commit d50d3fc has been approved by |
Rollup of 6 pull requests Successful merges: - rust-lang#94639 (Suggest dereferencing non-lval mutable reference on assignment) - rust-lang#95979 (update coherence docs, fix generator + opaque type ICE) - rust-lang#96378 (Mention traits and types involved in unstable trait upcasting) - rust-lang#96917 (Make HashMap fall back to RtlGenRandom if BCryptGenRandom fails) - rust-lang#97101 (Add tracking issue for ExitCode::exit_process) - rust-lang#97123 (Clean fix for rust-lang#96223) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
// Suppressing this diagnostic, we'll properly print it in `check_expr_assign` | ||
return None; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@compiler-errors For future reference, adding a delay_span_bug
here might be a good idea to protect from miscompilations in the face of refactors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shoot, yeah. i can add one in a follow-up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, this just suppresses a suggestion, not the error itself. And the other case I actually did suppress a real error, I downgraded it to a delayed bug. See: https://github.com/rust-lang/rust/pull/94639/files#diff-c46757032f463a1170b40e5c41d569ce058668cfe259774fb8a7936e3fdd82f4R57
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@compiler-errors perfect!
DerefMut
Fixes #46276
Fixes #93980