Skip to content

Commit ff51611

Browse files
authored
Rollup merge of #65213 - estebank:peel-drop-temps, r=Centril
Ignore `ExprKind::DropTemps` for some ref suggestions Introduce `Expr::peel_drop_temps()` to ignore `ExprKind::DropTemps` for suggestions that depend on the `ExprKind` for accuracy.
2 parents 4a6304f + d0eea6f commit ff51611

File tree

6 files changed

+29
-15
lines changed

6 files changed

+29
-15
lines changed

src/librustc/hir/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,19 @@ impl Expr {
15481548
}
15491549
}
15501550
}
1551+
1552+
/// If `Self.kind` is `ExprKind::DropTemps(expr)`, drill down until we get a non-`DropTemps`
1553+
/// `Expr`. This is used in suggestions to ignore this `ExprKind` as it is semantically
1554+
/// silent, only signaling the ownership system. By doing this, suggestions that check the
1555+
/// `ExprKind` of any given `Expr` for presentation don't have to care about `DropTemps`
1556+
/// beyond remembering to call this function before doing analysis on it.
1557+
pub fn peel_drop_temps(&self) -> &Self {
1558+
let mut expr = self;
1559+
while let ExprKind::DropTemps(inner) = &expr.kind {
1560+
expr = inner;
1561+
}
1562+
expr
1563+
}
15511564
}
15521565

15531566
impl fmt::Debug for Expr {

src/librustc_typeck/check/demand.rs

+4
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
115115
Err(e) => e
116116
};
117117

118+
let expr = expr.peel_drop_temps();
118119
let cause = self.misc(expr.span);
119120
let expr_ty = self.resolve_type_vars_with_obligations(checked_ty);
120121
let mut err = self.report_mismatched_types(&cause, expected, expr_ty, e);
@@ -355,6 +356,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
355356
};
356357
let is_macro = sp.from_expansion() && !is_desugaring;
357358

359+
// `ExprKind::DropTemps` is semantically irrelevant for these suggestions.
360+
let expr = expr.peel_drop_temps();
361+
358362
match (&expr.kind, &expected.kind, &checked_ty.kind) {
359363
(_, &ty::Ref(_, exp, _), &ty::Ref(_, check, _)) => match (&exp.kind, &check.kind) {
360364
(&ty::Str, &ty::Array(arr, _)) |

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ LL | if &true {}
2929
| ^^^^^
3030
| |
3131
| expected bool, found &bool
32-
| help: consider dereferencing the borrow: `*&true`
32+
| help: consider removing the borrow: `true`
3333
|
3434
= note: expected type `bool`
3535
found type `&bool`
@@ -41,7 +41,7 @@ LL | if &mut true {}
4141
| ^^^^^^^^^
4242
| |
4343
| expected bool, found &mut bool
44-
| help: consider dereferencing the borrow: `*&mut true`
44+
| help: consider removing the borrow: `true`
4545
|
4646
= note: expected type `bool`
4747
found type `&mut bool`
@@ -77,7 +77,7 @@ LL | while &true {}
7777
| ^^^^^
7878
| |
7979
| expected bool, found &bool
80-
| help: consider dereferencing the borrow: `*&true`
80+
| help: consider removing the borrow: `true`
8181
|
8282
= note: expected type `bool`
8383
found type `&bool`
@@ -89,7 +89,7 @@ LL | while &mut true {}
8989
| ^^^^^^^^^
9090
| |
9191
| expected bool, found &mut bool
92-
| help: consider dereferencing the borrow: `*&mut true`
92+
| help: consider removing the borrow: `true`
9393
|
9494
= note: expected type `bool`
9595
found type `&mut bool`

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ LL | if &let 0 = 0 {}
520520
| ^^^^^^^^^^
521521
| |
522522
| expected bool, found &bool
523-
| help: consider dereferencing the borrow: `*&let 0 = 0`
523+
| help: consider removing the borrow: `let 0 = 0`
524524
|
525525
= note: expected type `bool`
526526
found type `&bool`
@@ -708,7 +708,7 @@ LL | while &let 0 = 0 {}
708708
| ^^^^^^^^^^
709709
| |
710710
| expected bool, found &bool
711-
| help: consider dereferencing the borrow: `*&let 0 = 0`
711+
| help: consider removing the borrow: `let 0 = 0`
712712
|
713713
= note: expected type `bool`
714714
found type `&bool`

0 commit comments

Comments
 (0)