Skip to content

Commit 66d6a0b

Browse files
authored
Rollup merge of #107203 - chenyukang:yukang/fix-106496-remove-deref, r=compiler-errors
Suggest remove deref for type mismatch Fixes #106496
2 parents e4ed082 + 2aa5555 commit 66d6a0b

File tree

7 files changed

+144
-5
lines changed

7 files changed

+144
-5
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12331233
sugg_sp = receiver.span;
12341234
}
12351235
}
1236+
1237+
if let hir::ExprKind::Unary(hir::UnOp::Deref, ref inner) = expr.kind
1238+
&& let Some(1) = self.deref_steps(expected, checked_ty) {
1239+
// We have `*&T`, check if what was expected was `&T`.
1240+
// If so, we may want to suggest removing a `*`.
1241+
sugg_sp = sugg_sp.with_hi(inner.span.lo());
1242+
return Some((
1243+
sugg_sp,
1244+
"consider removing deref here".to_string(),
1245+
"".to_string(),
1246+
Applicability::MachineApplicable,
1247+
true,
1248+
false,
1249+
));
1250+
}
1251+
12361252
if let Ok(src) = sm.span_to_snippet(sugg_sp) {
12371253
let needs_parens = match expr.kind {
12381254
// parenthesize if needed (Issue #46756)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// run-rustfix
2+
3+
trait GatTrait {
4+
type Gat<'a> where Self: 'a;
5+
6+
fn test(&self) -> Self::Gat<'_>;
7+
}
8+
9+
trait SuperTrait<T>
10+
where
11+
Self: 'static,
12+
for<'a> Self: GatTrait<Gat<'a> = &'a T>,
13+
{
14+
fn copy(&self) -> Self::Gat<'_> where T: Copy {
15+
self.test()
16+
//~^ mismatched types
17+
}
18+
}
19+
20+
fn main() {}

tests/ui/generic-associated-types/issue-88360.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// run-rustfix
2+
13
trait GatTrait {
24
type Gat<'a> where Self: 'a;
35

tests/ui/generic-associated-types/issue-88360.stderr

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
error[E0308]: mismatched types
2-
--> $DIR/issue-88360.rs:13:9
2+
--> $DIR/issue-88360.rs:15:9
33
|
44
LL | trait SuperTrait<T>
55
| - this type parameter
66
...
77
LL | fn copy(&self) -> Self::Gat<'_> where T: Copy {
88
| ------------- expected `&T` because of return type
99
LL | *self.test()
10-
| ^^^^^^^^^^^^
11-
| |
12-
| expected `&T`, found type parameter `T`
13-
| help: consider borrowing here: `&*self.test()`
10+
| ^^^^^^^^^^^^ expected `&T`, found type parameter `T`
1411
|
1512
= note: expected reference `&T`
1613
found type parameter `T`
14+
help: consider removing deref here
15+
|
16+
LL - *self.test()
17+
LL + self.test()
18+
|
1719

1820
error: aborting due to previous error
1921

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// run-rustfix
2+
3+
//issue #106496
4+
5+
struct S;
6+
7+
trait X {}
8+
impl X for S {}
9+
10+
fn foo<T: X>(_: &T) {}
11+
fn test_foo() {
12+
let hello = &S;
13+
foo(hello);
14+
//~^ ERROR mismatched types
15+
}
16+
17+
fn bar(_: &String) {}
18+
fn test_bar() {
19+
let v = String::from("hello");
20+
let s = &v;
21+
bar(s);
22+
//~^ ERROR mismatched types
23+
}
24+
25+
fn main() {
26+
test_foo();
27+
test_bar();
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// run-rustfix
2+
3+
//issue #106496
4+
5+
struct S;
6+
7+
trait X {}
8+
impl X for S {}
9+
10+
fn foo<T: X>(_: &T) {}
11+
fn test_foo() {
12+
let hello = &S;
13+
foo(*hello);
14+
//~^ ERROR mismatched types
15+
}
16+
17+
fn bar(_: &String) {}
18+
fn test_bar() {
19+
let v = String::from("hello");
20+
let s = &v;
21+
bar(*s);
22+
//~^ ERROR mismatched types
23+
}
24+
25+
fn main() {
26+
test_foo();
27+
test_bar();
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/suggest-remove-deref.rs:13:9
3+
|
4+
LL | foo(*hello);
5+
| --- ^^^^^^ expected reference, found struct `S`
6+
| |
7+
| arguments to this function are incorrect
8+
|
9+
= note: expected reference `&_`
10+
found struct `S`
11+
note: function defined here
12+
--> $DIR/suggest-remove-deref.rs:10:4
13+
|
14+
LL | fn foo<T: X>(_: &T) {}
15+
| ^^^ -----
16+
help: consider removing deref here
17+
|
18+
LL - foo(*hello);
19+
LL + foo(hello);
20+
|
21+
22+
error[E0308]: mismatched types
23+
--> $DIR/suggest-remove-deref.rs:21:9
24+
|
25+
LL | bar(*s);
26+
| --- ^^ expected `&String`, found struct `String`
27+
| |
28+
| arguments to this function are incorrect
29+
|
30+
note: function defined here
31+
--> $DIR/suggest-remove-deref.rs:17:4
32+
|
33+
LL | fn bar(_: &String) {}
34+
| ^^^ ----------
35+
help: consider removing deref here
36+
|
37+
LL - bar(*s);
38+
LL + bar(s);
39+
|
40+
41+
error: aborting due to 2 previous errors
42+
43+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)