From c7a31e115d99a0d88d81f749f0488d4ca22640c9 Mon Sep 17 00:00:00 2001 From: mibac138 <5672750+mibac138@users.noreply.github.com> Date: Sun, 6 Dec 2020 01:14:21 +0100 Subject: [PATCH 1/2] Properly suggest deref in else block --- compiler/rustc_typeck/src/check/demand.rs | 22 ++++++++++++++++++++++ src/test/ui/deref-suggestion.rs | 10 ++++++++++ src/test/ui/deref-suggestion.stderr | 11 ++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_typeck/src/check/demand.rs b/compiler/rustc_typeck/src/check/demand.rs index 2728e03171a75..1346da0b9684c 100644 --- a/compiler/rustc_typeck/src/check/demand.rs +++ b/compiler/rustc_typeck/src/check/demand.rs @@ -360,6 +360,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { false } + crate fn hir_id_sole_block_element( + &self, + hir_id: hir::HirId, + ) -> Option<&'tcx rustc_hir::Expr<'tcx>> { + let node: Option> = self.tcx.hir().find(hir_id); + match node { + Some(Node::Expr(rustc_hir::Expr { + kind: rustc_hir::ExprKind::Block(block, ..), + .. + })) if block.stmts.len() == 0 => block.expr, + _ => None, + } + } + /// This function is used to determine potential "simple" improvements or users' errors and /// provide them useful help. For example: /// @@ -626,6 +640,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { }; let suggestion = if is_struct_pat_shorthand_field { format!("{}: *{}", code, code) + } else if let Some(expr) = + self.hir_id_sole_block_element(expr.hir_id) + { + if let Ok(inner_code) = sm.span_to_snippet(expr.span) { + format!("*{}", inner_code) + } else { + format!("*{}", code) + } } else { format!("*{}", code) }; diff --git a/src/test/ui/deref-suggestion.rs b/src/test/ui/deref-suggestion.rs index 580410aecf4f8..5cb680541dd9c 100644 --- a/src/test/ui/deref-suggestion.rs +++ b/src/test/ui/deref-suggestion.rs @@ -45,4 +45,14 @@ fn main() { //~^ ERROR mismatched types let r = R { i: i }; //~^ ERROR mismatched types + + + let a = &1; + let b = &2; + let val: i32 = if true { + a + 1 + } else { + b + //~^ ERROR mismatched types + }; } diff --git a/src/test/ui/deref-suggestion.stderr b/src/test/ui/deref-suggestion.stderr index f59f05db9c047..6eef06b68cb0e 100644 --- a/src/test/ui/deref-suggestion.stderr +++ b/src/test/ui/deref-suggestion.stderr @@ -89,6 +89,15 @@ LL | let r = R { i: i }; | expected `u32`, found `&{integer}` | help: consider dereferencing the borrow: `*i` -error: aborting due to 10 previous errors +error[E0308]: mismatched types + --> $DIR/deref-suggestion.rs:55:9 + | +LL | b + | ^ + | | + | expected `i32`, found `&{integer}` + | help: consider dereferencing the borrow: `*b` + +error: aborting due to 11 previous errors For more information about this error, try `rustc --explain E0308`. From 9ef83f308f9000469b94ed9c8d38773a9839c0aa Mon Sep 17 00:00:00 2001 From: mibac138 <5672750+mibac138@users.noreply.github.com> Date: Thu, 17 Dec 2020 13:24:51 +0100 Subject: [PATCH 2/2] Address review comments --- compiler/rustc_typeck/src/check/demand.rs | 13 +++---------- src/test/ui/deref-suggestion.rs | 8 ++++++++ src/test/ui/deref-suggestion.stderr | 11 ++++++++++- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_typeck/src/check/demand.rs b/compiler/rustc_typeck/src/check/demand.rs index 1346da0b9684c..08c05d7bd8979 100644 --- a/compiler/rustc_typeck/src/check/demand.rs +++ b/compiler/rustc_typeck/src/check/demand.rs @@ -360,16 +360,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { false } - crate fn hir_id_sole_block_element( - &self, - hir_id: hir::HirId, - ) -> Option<&'tcx rustc_hir::Expr<'tcx>> { - let node: Option> = self.tcx.hir().find(hir_id); - match node { - Some(Node::Expr(rustc_hir::Expr { - kind: rustc_hir::ExprKind::Block(block, ..), - .. - })) if block.stmts.len() == 0 => block.expr, + crate fn hir_id_sole_block_element(&self, hir_id: hir::HirId) -> Option<&'tcx hir::Expr<'tcx>> { + match self.tcx.hir().find(hir_id)? { + Node::Expr(hir::Expr { kind: hir::ExprKind::Block(block, ..), .. }) => block.expr, _ => None, } } diff --git a/src/test/ui/deref-suggestion.rs b/src/test/ui/deref-suggestion.rs index 5cb680541dd9c..e62e8467bd28a 100644 --- a/src/test/ui/deref-suggestion.rs +++ b/src/test/ui/deref-suggestion.rs @@ -55,4 +55,12 @@ fn main() { b //~^ ERROR mismatched types }; + let val: i32 = if true { + let _ = 2; + a + 1 + } else { + let _ = 2; + b + //~^ ERROR mismatched types + }; } diff --git a/src/test/ui/deref-suggestion.stderr b/src/test/ui/deref-suggestion.stderr index 6eef06b68cb0e..0de125695e60b 100644 --- a/src/test/ui/deref-suggestion.stderr +++ b/src/test/ui/deref-suggestion.stderr @@ -98,6 +98,15 @@ LL | b | expected `i32`, found `&{integer}` | help: consider dereferencing the borrow: `*b` -error: aborting due to 11 previous errors +error[E0308]: mismatched types + --> $DIR/deref-suggestion.rs:63:9 + | +LL | b + | ^ + | | + | expected `i32`, found `&{integer}` + | help: consider dereferencing the borrow: `*b` + +error: aborting due to 12 previous errors For more information about this error, try `rustc --explain E0308`.