Skip to content

Commit 5422ed7

Browse files
authored
Rollup merge of #65150 - XiangQingW:master, r=estebank
Suggest dereferencing boolean reference when used in 'if' or 'while' Implements #64557
2 parents 73685ec + bbb69d1 commit 5422ed7

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

src/librustc_typeck/check/demand.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
349349

350350
// If the span is from a macro, then it's hard to extract the text
351351
// and make a good suggestion, so don't bother.
352-
let is_macro = sp.from_expansion();
352+
let is_desugaring = match sp.desugaring_kind() {
353+
Some(k) => sp.is_desugaring(k),
354+
None => false
355+
};
356+
let is_macro = sp.from_expansion() && !is_desugaring;
353357

354358
match (&expr.kind, &expected.kind, &checked_ty.kind) {
355359
(_, &ty::Ref(_, exp, _), &ty::Ref(_, check, _)) => match (&exp.kind, &check.kind) {

src/librustc_typeck/check/expr.rs

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
8787
}
8888

8989
if let Some(mut err) = self.demand_suptype_diag(expr.span, expected_ty, ty) {
90+
self.suggest_ref_or_into(&mut err, expr, expected_ty, ty);
91+
9092
let expr = match &expr.kind {
9193
ExprKind::DropTemps(expr) => expr,
9294
_ => expr,

src/test/ui/if/if-no-match-bindings.stderr

+32-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0308]: mismatched types
22
--> $DIR/if-no-match-bindings.rs:18:8
33
|
44
LL | if b_ref() {}
5-
| ^^^^^^^ expected bool, found &bool
5+
| ^^^^^^^
6+
| |
7+
| expected bool, found &bool
8+
| help: consider dereferencing the borrow: `*b_ref()`
69
|
710
= note: expected type `bool`
811
found type `&bool`
@@ -11,7 +14,10 @@ error[E0308]: mismatched types
1114
--> $DIR/if-no-match-bindings.rs:19:8
1215
|
1316
LL | if b_mut_ref() {}
14-
| ^^^^^^^^^^^ expected bool, found &mut bool
17+
| ^^^^^^^^^^^
18+
| |
19+
| expected bool, found &mut bool
20+
| help: consider dereferencing the borrow: `*b_mut_ref()`
1521
|
1622
= note: expected type `bool`
1723
found type `&mut bool`
@@ -20,7 +26,10 @@ error[E0308]: mismatched types
2026
--> $DIR/if-no-match-bindings.rs:20:8
2127
|
2228
LL | if &true {}
23-
| ^^^^^ expected bool, found &bool
29+
| ^^^^^
30+
| |
31+
| expected bool, found &bool
32+
| help: consider dereferencing the borrow: `*&true`
2433
|
2534
= note: expected type `bool`
2635
found type `&bool`
@@ -29,7 +38,10 @@ error[E0308]: mismatched types
2938
--> $DIR/if-no-match-bindings.rs:21:8
3039
|
3140
LL | if &mut true {}
32-
| ^^^^^^^^^ expected bool, found &mut bool
41+
| ^^^^^^^^^
42+
| |
43+
| expected bool, found &mut bool
44+
| help: consider dereferencing the borrow: `*&mut true`
3345
|
3446
= note: expected type `bool`
3547
found type `&mut bool`
@@ -38,7 +50,10 @@ error[E0308]: mismatched types
3850
--> $DIR/if-no-match-bindings.rs:24:11
3951
|
4052
LL | while b_ref() {}
41-
| ^^^^^^^ expected bool, found &bool
53+
| ^^^^^^^
54+
| |
55+
| expected bool, found &bool
56+
| help: consider dereferencing the borrow: `*b_ref()`
4257
|
4358
= note: expected type `bool`
4459
found type `&bool`
@@ -47,7 +62,10 @@ error[E0308]: mismatched types
4762
--> $DIR/if-no-match-bindings.rs:25:11
4863
|
4964
LL | while b_mut_ref() {}
50-
| ^^^^^^^^^^^ expected bool, found &mut bool
65+
| ^^^^^^^^^^^
66+
| |
67+
| expected bool, found &mut bool
68+
| help: consider dereferencing the borrow: `*b_mut_ref()`
5169
|
5270
= note: expected type `bool`
5371
found type `&mut bool`
@@ -56,7 +74,10 @@ error[E0308]: mismatched types
5674
--> $DIR/if-no-match-bindings.rs:26:11
5775
|
5876
LL | while &true {}
59-
| ^^^^^ expected bool, found &bool
77+
| ^^^^^
78+
| |
79+
| expected bool, found &bool
80+
| help: consider dereferencing the borrow: `*&true`
6081
|
6182
= note: expected type `bool`
6283
found type `&bool`
@@ -65,7 +86,10 @@ error[E0308]: mismatched types
6586
--> $DIR/if-no-match-bindings.rs:27:11
6687
|
6788
LL | while &mut true {}
68-
| ^^^^^^^^^ expected bool, found &mut bool
89+
| ^^^^^^^^^
90+
| |
91+
| expected bool, found &mut bool
92+
| help: consider dereferencing the borrow: `*&mut true`
6993
|
7094
= note: expected type `bool`
7195
found type `&mut bool`

src/test/ui/rfc-2497-if-let-chains/disallowed-positions.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,10 @@ error[E0308]: mismatched types
517517
--> $DIR/disallowed-positions.rs:32:8
518518
|
519519
LL | if &let 0 = 0 {}
520-
| ^^^^^^^^^^ expected bool, found &bool
520+
| ^^^^^^^^^^
521+
| |
522+
| expected bool, found &bool
523+
| help: consider dereferencing the borrow: `*&let 0 = 0`
521524
|
522525
= note: expected type `bool`
523526
found type `&bool`
@@ -702,7 +705,10 @@ error[E0308]: mismatched types
702705
--> $DIR/disallowed-positions.rs:96:11
703706
|
704707
LL | while &let 0 = 0 {}
705-
| ^^^^^^^^^^ expected bool, found &bool
708+
| ^^^^^^^^^^
709+
| |
710+
| expected bool, found &bool
711+
| help: consider dereferencing the borrow: `*&let 0 = 0`
706712
|
707713
= note: expected type `bool`
708714
found type `&bool`

0 commit comments

Comments
 (0)