about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2018-01-09 18:41:24 -0800
committerEsteban Küber <esteban@kuber.com.ar>2018-01-14 22:46:24 -0800
commit09efaaf0764ddfd366bf6627533ecd1ea6ffb234 (patch)
tree4090b3600132cd94b26b60a56a7b8562df1281af /src
parent509ea8efc630d4e329dc2a440a2c0fc2a3fea236 (diff)
downloadrust-09efaaf0764ddfd366bf6627533ecd1ea6ffb234.tar.gz
rust-09efaaf0764ddfd366bf6627533ecd1ea6ffb234.zip
Use `s::u::p::expr_precedence` and fix message
 - Use `syntax::util::parser::expr_precedence` to determine wether
   parenthesis are needed around the casting target.
 - Update message to not incorrectly mention rounding on `.into()`
   suggestions, as those types that do have that implemented will never
   round.
Diffstat (limited to 'src')
-rw-r--r--src/librustc_typeck/check/demand.rs14
-rw-r--r--src/test/ui/suggestions/numeric-cast.stderr20
2 files changed, 16 insertions, 18 deletions
diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs
index dbda8a6e32b..d43d681c925 100644
--- a/src/librustc_typeck/check/demand.rs
+++ b/src/librustc_typeck/check/demand.rs
@@ -15,6 +15,7 @@ use rustc::infer::InferOk;
 use rustc::traits::ObligationCause;
 
 use syntax::ast;
+use syntax::util::parser::{expr_precedence, AssocOp};
 use syntax_pos::{self, Span};
 use rustc::hir;
 use rustc::hir::print;
@@ -335,10 +336,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
         // For now, don't suggest casting with `as`.
         let can_cast = false;
 
-        let needs_paren = match expr.node {
-            hir::ExprBinary(..) => true,
-            _ => false,
-        };
+        let needs_paren = expr_precedence(expr) < (AssocOp::As.precedence() as i8);
 
         if let Ok(src) = self.tcx.sess.codemap().span_to_snippet(expr.span) {
             let msg = format!("you can cast an `{}` to `{}`", checked_ty, expected_ty);
@@ -508,11 +506,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
                     true
                 }
                 (&ty::TyFloat(ref exp), &ty::TyUint(ref found)) => {
+                    // if `found` is `None` (meaning found is `usize`), don't suggest `.into()`
                     if exp.bit_width() > found.bit_width().unwrap_or(256) {
                         err.span_suggestion(expr.span,
                                             &format!("{}, producing the floating point \
-                                                      representation of the integer, rounded if \
-                                                      necessary",
+                                                      representation of the integer",
                                                       msg),
                                             into_suggestion);
                     } else if can_cast {
@@ -526,11 +524,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
                     true
                 }
                 (&ty::TyFloat(ref exp), &ty::TyInt(ref found)) => {
+                    // if `found` is `None` (meaning found is `isize`), don't suggest `.into()`
                     if exp.bit_width() > found.bit_width().unwrap_or(256) {
                         err.span_suggestion(expr.span,
                                             &format!("{}, producing the floating point \
-                                                      representation of the integer, rounded if \
-                                                      necessary",
+                                                      representation of the integer",
                                                       msg),
                                             into_suggestion);
                     } else if can_cast {
diff --git a/src/test/ui/suggestions/numeric-cast.stderr b/src/test/ui/suggestions/numeric-cast.stderr
index 9c05dacf4ec..0ce3d087f35 100644
--- a/src/test/ui/suggestions/numeric-cast.stderr
+++ b/src/test/ui/suggestions/numeric-cast.stderr
@@ -723,7 +723,7 @@ error[E0308]: mismatched types
     |
 272 |     foo::<f64>(x_u32);
     |                ^^^^^ expected f64, found u32
-help: you can cast an `u32` to `f64`, producing the floating point representation of the integer, rounded if necessary
+help: you can cast an `u32` to `f64`, producing the floating point representation of the integer
     |
 272 |     foo::<f64>(x_u32.into());
     |                ^^^^^^^^^^^^
@@ -733,7 +733,7 @@ error[E0308]: mismatched types
     |
 274 |     foo::<f64>(x_u16);
     |                ^^^^^ expected f64, found u16
-help: you can cast an `u16` to `f64`, producing the floating point representation of the integer, rounded if necessary
+help: you can cast an `u16` to `f64`, producing the floating point representation of the integer
     |
 274 |     foo::<f64>(x_u16.into());
     |                ^^^^^^^^^^^^
@@ -743,7 +743,7 @@ error[E0308]: mismatched types
     |
 276 |     foo::<f64>(x_u8);
     |                ^^^^ expected f64, found u8
-help: you can cast an `u8` to `f64`, producing the floating point representation of the integer, rounded if necessary
+help: you can cast an `u8` to `f64`, producing the floating point representation of the integer
     |
 276 |     foo::<f64>(x_u8.into());
     |                ^^^^^^^^^^^
@@ -765,7 +765,7 @@ error[E0308]: mismatched types
     |
 282 |     foo::<f64>(x_i32);
     |                ^^^^^ expected f64, found i32
-help: you can cast an `i32` to `f64`, producing the floating point representation of the integer, rounded if necessary
+help: you can cast an `i32` to `f64`, producing the floating point representation of the integer
     |
 282 |     foo::<f64>(x_i32.into());
     |                ^^^^^^^^^^^^
@@ -775,7 +775,7 @@ error[E0308]: mismatched types
     |
 284 |     foo::<f64>(x_i16);
     |                ^^^^^ expected f64, found i16
-help: you can cast an `i16` to `f64`, producing the floating point representation of the integer, rounded if necessary
+help: you can cast an `i16` to `f64`, producing the floating point representation of the integer
     |
 284 |     foo::<f64>(x_i16.into());
     |                ^^^^^^^^^^^^
@@ -785,7 +785,7 @@ error[E0308]: mismatched types
     |
 286 |     foo::<f64>(x_i8);
     |                ^^^^ expected f64, found i8
-help: you can cast an `i8` to `f64`, producing the floating point representation of the integer, rounded if necessary
+help: you can cast an `i8` to `f64`, producing the floating point representation of the integer
     |
 286 |     foo::<f64>(x_i8.into());
     |                ^^^^^^^^^^^
@@ -823,7 +823,7 @@ error[E0308]: mismatched types
     |
 298 |     foo::<f32>(x_u16);
     |                ^^^^^ expected f32, found u16
-help: you can cast an `u16` to `f32`, producing the floating point representation of the integer, rounded if necessary
+help: you can cast an `u16` to `f32`, producing the floating point representation of the integer
     |
 298 |     foo::<f32>(x_u16.into());
     |                ^^^^^^^^^^^^
@@ -833,7 +833,7 @@ error[E0308]: mismatched types
     |
 300 |     foo::<f32>(x_u8);
     |                ^^^^ expected f32, found u8
-help: you can cast an `u8` to `f32`, producing the floating point representation of the integer, rounded if necessary
+help: you can cast an `u8` to `f32`, producing the floating point representation of the integer
     |
 300 |     foo::<f32>(x_u8.into());
     |                ^^^^^^^^^^^
@@ -861,7 +861,7 @@ error[E0308]: mismatched types
     |
 308 |     foo::<f32>(x_i16);
     |                ^^^^^ expected f32, found i16
-help: you can cast an `i16` to `f32`, producing the floating point representation of the integer, rounded if necessary
+help: you can cast an `i16` to `f32`, producing the floating point representation of the integer
     |
 308 |     foo::<f32>(x_i16.into());
     |                ^^^^^^^^^^^^
@@ -871,7 +871,7 @@ error[E0308]: mismatched types
     |
 310 |     foo::<f32>(x_i8);
     |                ^^^^ expected f32, found i8
-help: you can cast an `i8` to `f32`, producing the floating point representation of the integer, rounded if necessary
+help: you can cast an `i8` to `f32`, producing the floating point representation of the integer
     |
 310 |     foo::<f32>(x_i8.into());
     |                ^^^^^^^^^^^