Skip to content
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

Suggest dereferencing boolean reference when used in 'if' or 'while' #65150

Merged
merged 3 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tells me that suggest_ref_or_into needs to check for literal borrows before suggesting, but that can be done in a subsequent PR.

|
= 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