about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-12-17 01:56:12 +0000
committerbors <bors@rust-lang.org>2017-12-17 01:56:12 +0000
commit35376587c802f3f142c63a443422184fd0ad40bd (patch)
tree4f4448a55f26e62d4603978e350fcc8fb9fc730b
parent1b1c792c77ad16a466d7943eb887b0a8a76526e1 (diff)
parent73a90194f95a1a9604c54c482df07d8ed278b183 (diff)
Auto merge of #46761 - zackmdavis:concerning_incorrect_suggestions_for_referencing_a_cast, r=estebank
in which suggestions to borrow casts or binary expressions are rectified

 resolves #46756

r? @estebank
-rw-r--r--src/librustc_typeck/check/demand.rs8
-rw-r--r--src/test/ui/suggestions/issue-46756-consider-borrowing-cast-or-binexpr.rs24
-rw-r--r--src/test/ui/suggestions/issue-46756-consider-borrowing-cast-or-binexpr.stderr26
3 files changed, 56 insertions, 2 deletions
diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs
index 4724a0fa51b..45ddae5c644 100644
--- a/src/librustc_typeck/check/demand.rs
+++ b/src/librustc_typeck/check/demand.rs
@@ -260,12 +260,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
                     // Use the callsite's span if this is a macro call. #41858
                     let sp = self.sess().codemap().call_span_if_macro(expr.span);
                     if let Ok(src) = self.tcx.sess.codemap().span_to_snippet(sp) {
+                        let sugg_expr = match expr.node { // parenthesize if needed (Issue #46756)
+                            hir::ExprCast(_, _) | hir::ExprBinary(_, _, _) => format!("({})", src),
+                            _ => src,
+                        };
                         return Some(match mutability.mutbl {
                             hir::Mutability::MutMutable => {
-                                ("consider mutably borrowing here", format!("&mut {}", src))
+                                ("consider mutably borrowing here", format!("&mut {}", sugg_expr))
                             }
                             hir::Mutability::MutImmutable => {
-                                ("consider borrowing here", format!("&{}", src))
+                                ("consider borrowing here", format!("&{}", sugg_expr))
                             }
                         });
                     }
diff --git a/src/test/ui/suggestions/issue-46756-consider-borrowing-cast-or-binexpr.rs b/src/test/ui/suggestions/issue-46756-consider-borrowing-cast-or-binexpr.rs
new file mode 100644
index 00000000000..5617c46afa9
--- /dev/null
+++ b/src/test/ui/suggestions/issue-46756-consider-borrowing-cast-or-binexpr.rs
@@ -0,0 +1,24 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![allow(unused)]
+
+fn light_flows_our_war_of_mocking_words(and_yet: &usize) -> usize {
+    and_yet + 1
+}
+
+fn main() {
+    let behold: isize = 2;
+    let with_tears: usize = 3;
+    light_flows_our_war_of_mocking_words(behold as usize);
+    //~^ ERROR mismatched types [E0308]
+    light_flows_our_war_of_mocking_words(with_tears + 4);
+    //~^ ERROR mismatched types [E0308]
+}
diff --git a/src/test/ui/suggestions/issue-46756-consider-borrowing-cast-or-binexpr.stderr b/src/test/ui/suggestions/issue-46756-consider-borrowing-cast-or-binexpr.stderr
new file mode 100644
index 00000000000..4b3e8a32025
--- /dev/null
+++ b/src/test/ui/suggestions/issue-46756-consider-borrowing-cast-or-binexpr.stderr
@@ -0,0 +1,26 @@
+error[E0308]: mismatched types
+  --> $DIR/issue-46756-consider-borrowing-cast-or-binexpr.rs:20:42
+   |
+20 |     light_flows_our_war_of_mocking_words(behold as usize);
+   |                                          ^^^^^^^^^^^^^^^
+   |                                          |
+   |                                          expected &usize, found usize
+   |                                          help: consider borrowing here: `&(behold as usize)`
+   |
+   = note: expected type `&usize`
+              found type `usize`
+
+error[E0308]: mismatched types
+  --> $DIR/issue-46756-consider-borrowing-cast-or-binexpr.rs:22:42
+   |
+22 |     light_flows_our_war_of_mocking_words(with_tears + 4);
+   |                                          ^^^^^^^^^^^^^^
+   |                                          |
+   |                                          expected &usize, found usize
+   |                                          help: consider borrowing here: `&(with_tears + 4)`
+   |
+   = note: expected type `&usize`
+              found type `usize`
+
+error: aborting due to 2 previous errors
+