Skip to content

Commit 8280407

Browse files
authored
Rollup merge of #135601 - samueltardieu:push-xslotxrnooym, r=jieyouxu
Fix suggestion to convert dereference of raw pointer to ref Fix #135580
2 parents 231057c + 8d59545 commit 8280407

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2682,6 +2682,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
26822682

26832683
if let hir::ExprKind::Unary(hir::UnOp::Deref, inner) = expr.kind
26842684
&& let Some(1) = self.deref_steps_for_suggestion(expected, checked_ty)
2685+
&& self.typeck_results.borrow().expr_ty(inner).is_ref()
26852686
{
26862687
// We have `*&T`, check if what was expected was `&T`.
26872688
// If so, we may want to suggest removing a `*`.

tests/ui/suggestions/raw-to-ref.fixed

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ run-rustfix
2+
// Regression test for #135580: check that we do not suggest to simply drop
3+
// the `*` to make the types match when the source is a raw pointer while
4+
// the target type is a reference.
5+
6+
struct S;
7+
8+
fn main() {
9+
let mut s = S;
10+
let x = &raw const s;
11+
let _: &S = unsafe { &*x };
12+
//~^ ERROR mismatched types
13+
//~| HELP consider borrowing here
14+
15+
let x = &raw mut s;
16+
let _: &mut S = unsafe { &mut *x };
17+
//~^ ERROR mismatched types
18+
//~| HELP consider mutably borrowing here
19+
}

tests/ui/suggestions/raw-to-ref.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ run-rustfix
2+
// Regression test for #135580: check that we do not suggest to simply drop
3+
// the `*` to make the types match when the source is a raw pointer while
4+
// the target type is a reference.
5+
6+
struct S;
7+
8+
fn main() {
9+
let mut s = S;
10+
let x = &raw const s;
11+
let _: &S = unsafe { *x };
12+
//~^ ERROR mismatched types
13+
//~| HELP consider borrowing here
14+
15+
let x = &raw mut s;
16+
let _: &mut S = unsafe { *x };
17+
//~^ ERROR mismatched types
18+
//~| HELP consider mutably borrowing here
19+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/raw-to-ref.rs:11:26
3+
|
4+
LL | let _: &S = unsafe { *x };
5+
| ^^ expected `&S`, found `S`
6+
|
7+
help: consider borrowing here
8+
|
9+
LL | let _: &S = unsafe { &*x };
10+
| +
11+
12+
error[E0308]: mismatched types
13+
--> $DIR/raw-to-ref.rs:16:30
14+
|
15+
LL | let _: &mut S = unsafe { *x };
16+
| ^^ expected `&mut S`, found `S`
17+
|
18+
help: consider mutably borrowing here
19+
|
20+
LL | let _: &mut S = unsafe { &mut *x };
21+
| ++++
22+
23+
error: aborting due to 2 previous errors
24+
25+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)