From d84c4cd7188f484083091bee436dc07eb90b47e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 8 Oct 2019 08:26:42 -0700 Subject: [PATCH] Ignore `ExprKind::DropTemps` for some ref suggestions --- src/librustc/hir/mod.rs | 17 +++++++++++ src/librustc_typeck/check/demand.rs | 3 ++ src/test/ui/if/if-no-match-bindings.stderr | 28 ++++++------------- .../disallowed-positions.stderr | 4 +-- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index 30b05036688..ead7a08c397 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -1548,6 +1548,23 @@ impl Expr { } } } + + /// If `Self.kind` is `ExprKind::DropTemps(expr)`, drill down until we get a non-`DropTemps` + /// `Expr`. This is used in suggestions to ignore this `ExprKind` as it is semantically + /// silent, only signaling the ownership system. By doing this, suggestions that check the + /// `ExprKind` of any given `Expr` for presentation don't have to care about `DropTemps` + /// beyond remembering to call this function before doing analysis on it. + pub fn peel_drop_temps(&self) -> &Self { + let mut base_expr = self; + loop { + match &base_expr.kind { + ExprKind::DropTemps(expr) => { + base_expr = &expr; + } + _ => return base_expr, + } + } + } } impl fmt::Debug for Expr { diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs index 78bd4508e21..9f7f8b7680f 100644 --- a/src/librustc_typeck/check/demand.rs +++ b/src/librustc_typeck/check/demand.rs @@ -355,6 +355,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { }; let is_macro = sp.from_expansion() && !is_desugaring; + // `ExprKind::DropTemps` is semantically irrelevant for these suggestions. + let expr = expr.peel_drop_temps(); + match (&expr.kind, &expected.kind, &checked_ty.kind) { (_, &ty::Ref(_, exp, _), &ty::Ref(_, check, _)) => match (&exp.kind, &check.kind) { (&ty::Str, &ty::Array(arr, _)) | diff --git a/src/test/ui/if/if-no-match-bindings.stderr b/src/test/ui/if/if-no-match-bindings.stderr index 53b7aafc430..fa2455db2b2 100644 --- a/src/test/ui/if/if-no-match-bindings.stderr +++ b/src/test/ui/if/if-no-match-bindings.stderr @@ -2,10 +2,7 @@ error[E0308]: mismatched types --> $DIR/if-no-match-bindings.rs:18:8 | LL | if b_ref() {} - | ^^^^^^^ - | | - | expected bool, found &bool - | help: consider dereferencing the borrow: `*b_ref()` + | ^^^^^^^ expected bool, found &bool | = note: expected type `bool` found type `&bool` @@ -14,10 +11,7 @@ error[E0308]: mismatched types --> $DIR/if-no-match-bindings.rs:19:8 | LL | if b_mut_ref() {} - | ^^^^^^^^^^^ - | | - | expected bool, found &mut bool - | help: consider dereferencing the borrow: `*b_mut_ref()` + | ^^^^^^^^^^^ expected bool, found &mut bool | = note: expected type `bool` found type `&mut bool` @@ -29,7 +23,7 @@ LL | if &true {} | ^^^^^ | | | expected bool, found &bool - | help: consider dereferencing the borrow: `*&true` + | help: consider removing the borrow: `true` | = note: expected type `bool` found type `&bool` @@ -41,7 +35,7 @@ LL | if &mut true {} | ^^^^^^^^^ | | | expected bool, found &mut bool - | help: consider dereferencing the borrow: `*&mut true` + | help: consider removing the borrow: `true` | = note: expected type `bool` found type `&mut bool` @@ -50,10 +44,7 @@ error[E0308]: mismatched types --> $DIR/if-no-match-bindings.rs:24:11 | LL | while b_ref() {} - | ^^^^^^^ - | | - | expected bool, found &bool - | help: consider dereferencing the borrow: `*b_ref()` + | ^^^^^^^ expected bool, found &bool | = note: expected type `bool` found type `&bool` @@ -62,10 +53,7 @@ error[E0308]: mismatched types --> $DIR/if-no-match-bindings.rs:25:11 | LL | while b_mut_ref() {} - | ^^^^^^^^^^^ - | | - | expected bool, found &mut bool - | help: consider dereferencing the borrow: `*b_mut_ref()` + | ^^^^^^^^^^^ expected bool, found &mut bool | = note: expected type `bool` found type `&mut bool` @@ -77,7 +65,7 @@ LL | while &true {} | ^^^^^ | | | expected bool, found &bool - | help: consider dereferencing the borrow: `*&true` + | help: consider removing the borrow: `true` | = note: expected type `bool` found type `&bool` @@ -89,7 +77,7 @@ LL | while &mut true {} | ^^^^^^^^^ | | | expected bool, found &mut bool - | help: consider dereferencing the borrow: `*&mut true` + | help: consider removing the borrow: `true` | = note: expected type `bool` found type `&mut bool` diff --git a/src/test/ui/rfc-2497-if-let-chains/disallowed-positions.stderr b/src/test/ui/rfc-2497-if-let-chains/disallowed-positions.stderr index 619f9c85b24..ad4686c1915 100644 --- a/src/test/ui/rfc-2497-if-let-chains/disallowed-positions.stderr +++ b/src/test/ui/rfc-2497-if-let-chains/disallowed-positions.stderr @@ -520,7 +520,7 @@ LL | if &let 0 = 0 {} | ^^^^^^^^^^ | | | expected bool, found &bool - | help: consider dereferencing the borrow: `*&let 0 = 0` + | help: consider removing the borrow: `let 0 = 0` | = note: expected type `bool` found type `&bool` @@ -708,7 +708,7 @@ LL | while &let 0 = 0 {} | ^^^^^^^^^^ | | | expected bool, found &bool - | help: consider dereferencing the borrow: `*&let 0 = 0` + | help: consider removing the borrow: `let 0 = 0` | = note: expected type `bool` found type `&bool`