Skip to content

Commit

Permalink
Rollup merge of rust-lang#65150 - XiangQingW:master, r=estebank
Browse files Browse the repository at this point in the history
Suggest dereferencing boolean reference when used in 'if' or 'while'

Implements rust-lang#64557
  • Loading branch information
Centril authored Oct 7, 2019
2 parents a3d0c90 + bbb69d1 commit 934f3c2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
6 changes: 5 additions & 1 deletion src/librustc_typeck/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

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

match (&expr.kind, &expected.kind, &checked_ty.kind) {
(_, &ty::Ref(_, exp, _), &ty::Ref(_, check, _)) => match (&exp.kind, &check.kind) {
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_typeck/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}

if let Some(mut err) = self.demand_suptype_diag(expr.span, expected_ty, ty) {
self.suggest_ref_or_into(&mut err, expr, expected_ty, ty);

let expr = match &expr.kind {
ExprKind::DropTemps(expr) => expr,
_ => expr,
Expand Down
40 changes: 32 additions & 8 deletions src/test/ui/if/if-no-match-bindings.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ error[E0308]: mismatched types
--> $DIR/if-no-match-bindings.rs:18:8
|
LL | if b_ref() {}
| ^^^^^^^ expected bool, found &bool
| ^^^^^^^
| |
| expected bool, found &bool
| help: consider dereferencing the borrow: `*b_ref()`
|
= note: expected type `bool`
found type `&bool`
Expand All @@ -11,7 +14,10 @@ error[E0308]: mismatched types
--> $DIR/if-no-match-bindings.rs:19:8
|
LL | if b_mut_ref() {}
| ^^^^^^^^^^^ expected bool, found &mut bool
| ^^^^^^^^^^^
| |
| expected bool, found &mut bool
| help: consider dereferencing the borrow: `*b_mut_ref()`
|
= note: expected type `bool`
found type `&mut bool`
Expand All @@ -20,7 +26,10 @@ error[E0308]: mismatched types
--> $DIR/if-no-match-bindings.rs:20:8
|
LL | if &true {}
| ^^^^^ expected bool, found &bool
| ^^^^^
| |
| expected bool, found &bool
| help: consider dereferencing the borrow: `*&true`
|
= note: expected type `bool`
found type `&bool`
Expand All @@ -29,7 +38,10 @@ error[E0308]: mismatched types
--> $DIR/if-no-match-bindings.rs:21:8
|
LL | if &mut true {}
| ^^^^^^^^^ expected bool, found &mut bool
| ^^^^^^^^^
| |
| expected bool, found &mut bool
| help: consider dereferencing the borrow: `*&mut true`
|
= note: expected type `bool`
found type `&mut bool`
Expand All @@ -38,7 +50,10 @@ error[E0308]: mismatched types
--> $DIR/if-no-match-bindings.rs:24:11
|
LL | while b_ref() {}
| ^^^^^^^ expected bool, found &bool
| ^^^^^^^
| |
| expected bool, found &bool
| help: consider dereferencing the borrow: `*b_ref()`
|
= note: expected type `bool`
found type `&bool`
Expand All @@ -47,7 +62,10 @@ error[E0308]: mismatched types
--> $DIR/if-no-match-bindings.rs:25:11
|
LL | while b_mut_ref() {}
| ^^^^^^^^^^^ expected bool, found &mut bool
| ^^^^^^^^^^^
| |
| expected bool, found &mut bool
| help: consider dereferencing the borrow: `*b_mut_ref()`
|
= note: expected type `bool`
found type `&mut bool`
Expand All @@ -56,7 +74,10 @@ error[E0308]: mismatched types
--> $DIR/if-no-match-bindings.rs:26:11
|
LL | while &true {}
| ^^^^^ expected bool, found &bool
| ^^^^^
| |
| expected bool, found &bool
| help: consider dereferencing the borrow: `*&true`
|
= note: expected type `bool`
found type `&bool`
Expand All @@ -65,7 +86,10 @@ error[E0308]: mismatched types
--> $DIR/if-no-match-bindings.rs:27:11
|
LL | while &mut true {}
| ^^^^^^^^^ expected bool, found &mut bool
| ^^^^^^^^^
| |
| expected bool, found &mut bool
| help: consider dereferencing the borrow: `*&mut true`
|
= note: expected type `bool`
found type `&mut bool`
Expand Down
10 changes: 8 additions & 2 deletions src/test/ui/rfc-2497-if-let-chains/disallowed-positions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,10 @@ error[E0308]: mismatched types
--> $DIR/disallowed-positions.rs:32:8
|
LL | if &let 0 = 0 {}
| ^^^^^^^^^^ expected bool, found &bool
| ^^^^^^^^^^
| |
| expected bool, found &bool
| help: consider dereferencing the borrow: `*&let 0 = 0`
|
= note: expected type `bool`
found type `&bool`
Expand Down Expand Up @@ -702,7 +705,10 @@ error[E0308]: mismatched types
--> $DIR/disallowed-positions.rs:96:11
|
LL | while &let 0 = 0 {}
| ^^^^^^^^^^ expected bool, found &bool
| ^^^^^^^^^^
| |
| expected bool, found &bool
| help: consider dereferencing the borrow: `*&let 0 = 0`
|
= note: expected type `bool`
found type `&bool`
Expand Down

0 comments on commit 934f3c2

Please sign in to comment.