Skip to content

Compiler does not suggest to dereference a reference to reference #93983

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

Closed
ChayimFriedman2 opened this issue Feb 14, 2022 · 1 comment
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ChayimFriedman2
Copy link
Contributor

If I miss a * when assigning to a variable, rustc suggests dereferencing it:

let mut v: &mut ();
v = ();
error[E0308]: mismatched types
 --> src/main.rs:3:9
  |
2 |     let v: &mut ();
  |            ------- expected due to this type
3 |     v = ();
  |         ^^ expected `&mut ()`, found `()`
  |
help: consider dereferencing here to assign to the mutable borrowed piece of memory
  |
3 |     *v = ();
  |     +

Playground.

But not the referent is a reference:

let v: &mut &();
v = &();
error[E0308]: mismatched types
 --> src/main.rs:3:9
  |
3 |     v = &();
  |         ^^^ types differ in mutability
  |
  = note: expected mutable reference `&mut &()`
                     found reference `&()`

Playground.

This is because in compiler/rustc_typeck/src/check/demand.rs, we first check whether both the LHS and RHS are references and try to suggest adding/removing b"...". But even if we did not succeed in that check, we don't continue to the next check where we check whether the LHS is a reference and should be dereferenced:

(_, &ty::Ref(_, exp, _), &ty::Ref(_, check, _)) => match (exp.kind(), check.kind()) {
(&ty::Str, &ty::Array(arr, _) | &ty::Slice(arr)) if arr == self.tcx.types.u8 => {
if let hir::ExprKind::Lit(_) = expr.kind {
if let Ok(src) = sm.span_to_snippet(sp) {
if replace_prefix(&src, "b\"", "\"").is_some() {
let pos = sp.lo() + BytePos(1);
return Some((
sp.with_hi(pos),
"consider removing the leading `b`",
String::new(),
Applicability::MachineApplicable,
true,
));
}
}
}
}
(&ty::Array(arr, _) | &ty::Slice(arr), &ty::Str) if arr == self.tcx.types.u8 => {
if let hir::ExprKind::Lit(_) = expr.kind {
if let Ok(src) = sm.span_to_snippet(sp) {
if replace_prefix(&src, "\"", "b\"").is_some() {
return Some((
sp.shrink_to_lo(),
"consider adding a leading `b`",
"b".to_string(),
Applicability::MachineApplicable,
true,
));
}
}
}
}
_ => {}
},
(_, &ty::Ref(_, _, mutability), _) => {

@ChayimFriedman2 ChayimFriedman2 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 14, 2022
@compiler-errors
Copy link
Member

This is fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants