Skip to content

Commit ac9025c

Browse files
committed
Call Expr::peel_drop_temps() from more places for more accurate suggestions
1 parent d84c4cd commit ac9025c

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

src/librustc_typeck/check/demand.rs

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
109109
allow_two_phase: AllowTwoPhase)
110110
-> (Ty<'tcx>, Option<DiagnosticBuilder<'tcx>>) {
111111
let expected = self.resolve_type_vars_with_obligations(expected);
112+
let expr = expr.peel_drop_temps();
112113

113114
let e = match self.try_coerce(expr, checked_ty, expected, allow_two_phase) {
114115
Ok(ty) => return (ty, None),

src/librustc_typeck/check/expr.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -87,12 +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+
let expr = expr.peel_drop_temps();
9091
self.suggest_ref_or_into(&mut err, expr, expected_ty, ty);
91-
92-
let expr = match &expr.kind {
93-
ExprKind::DropTemps(expr) => expr,
94-
_ => expr,
95-
};
9692
extend_err(&mut err);
9793
// Error possibly reported in `check_assign` so avoid emitting error again.
9894
err.emit_unless(self.is_assign_to_bool(expr, expected_ty));

src/librustc_typeck/check/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -4216,20 +4216,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
42164216
pub fn suggest_mismatched_types_on_tail(
42174217
&self,
42184218
err: &mut DiagnosticBuilder<'tcx>,
4219-
expression: &'tcx hir::Expr,
4219+
expr: &'tcx hir::Expr,
42204220
expected: Ty<'tcx>,
42214221
found: Ty<'tcx>,
42224222
cause_span: Span,
42234223
blk_id: hir::HirId,
42244224
) -> bool {
4225-
self.suggest_missing_semicolon(err, expression, expected, cause_span);
4225+
let expr = expr.peel_drop_temps();
4226+
self.suggest_missing_semicolon(err, expr, expected, cause_span);
42264227
let mut pointing_at_return_type = false;
42274228
if let Some((fn_decl, can_suggest)) = self.get_fn_decl(blk_id) {
42284229
pointing_at_return_type = self.suggest_missing_return_type(
42294230
err, &fn_decl, expected, found, can_suggest);
42304231
}
4231-
self.suggest_ref_or_into(err, expression, expected, found);
4232-
self.suggest_boxing_when_appropriate(err, expression, expected, found);
4232+
self.suggest_ref_or_into(err, expr, expected, found);
4233+
self.suggest_boxing_when_appropriate(err, expr, expected, found);
42334234
pointing_at_return_type
42344235
}
42354236

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

+16-4
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`
@@ -44,7 +50,10 @@ error[E0308]: mismatched types
4450
--> $DIR/if-no-match-bindings.rs:24:11
4551
|
4652
LL | while b_ref() {}
47-
| ^^^^^^^ expected bool, found &bool
53+
| ^^^^^^^
54+
| |
55+
| expected bool, found &bool
56+
| help: consider dereferencing the borrow: `*b_ref()`
4857
|
4958
= note: expected type `bool`
5059
found type `&bool`
@@ -53,7 +62,10 @@ error[E0308]: mismatched types
5362
--> $DIR/if-no-match-bindings.rs:25:11
5463
|
5564
LL | while b_mut_ref() {}
56-
| ^^^^^^^^^^^ expected bool, found &mut bool
65+
| ^^^^^^^^^^^
66+
| |
67+
| expected bool, found &mut bool
68+
| help: consider dereferencing the borrow: `*b_mut_ref()`
5769
|
5870
= note: expected type `bool`
5971
found type `&mut bool`

0 commit comments

Comments
 (0)