Skip to content

Commit 09efaaf

Browse files
committed
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.
1 parent 509ea8e commit 09efaaf

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

src/librustc_typeck/check/demand.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc::infer::InferOk;
1515
use rustc::traits::ObligationCause;
1616

1717
use syntax::ast;
18+
use syntax::util::parser::{expr_precedence, AssocOp};
1819
use syntax_pos::{self, Span};
1920
use rustc::hir;
2021
use rustc::hir::print;
@@ -335,10 +336,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
335336
// For now, don't suggest casting with `as`.
336337
let can_cast = false;
337338

338-
let needs_paren = match expr.node {
339-
hir::ExprBinary(..) => true,
340-
_ => false,
341-
};
339+
let needs_paren = expr_precedence(expr) < (AssocOp::As.precedence() as i8);
342340

343341
if let Ok(src) = self.tcx.sess.codemap().span_to_snippet(expr.span) {
344342
let msg = format!("you can cast an `{}` to `{}`", checked_ty, expected_ty);
@@ -508,11 +506,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
508506
true
509507
}
510508
(&ty::TyFloat(ref exp), &ty::TyUint(ref found)) => {
509+
// if `found` is `None` (meaning found is `usize`), don't suggest `.into()`
511510
if exp.bit_width() > found.bit_width().unwrap_or(256) {
512511
err.span_suggestion(expr.span,
513512
&format!("{}, producing the floating point \
514-
representation of the integer, rounded if \
515-
necessary",
513+
representation of the integer",
516514
msg),
517515
into_suggestion);
518516
} else if can_cast {
@@ -526,11 +524,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
526524
true
527525
}
528526
(&ty::TyFloat(ref exp), &ty::TyInt(ref found)) => {
527+
// if `found` is `None` (meaning found is `isize`), don't suggest `.into()`
529528
if exp.bit_width() > found.bit_width().unwrap_or(256) {
530529
err.span_suggestion(expr.span,
531530
&format!("{}, producing the floating point \
532-
representation of the integer, rounded if \
533-
necessary",
531+
representation of the integer",
534532
msg),
535533
into_suggestion);
536534
} else if can_cast {

src/test/ui/suggestions/numeric-cast.stderr

+10-10
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ error[E0308]: mismatched types
723723
|
724724
272 | foo::<f64>(x_u32);
725725
| ^^^^^ expected f64, found u32
726-
help: you can cast an `u32` to `f64`, producing the floating point representation of the integer, rounded if necessary
726+
help: you can cast an `u32` to `f64`, producing the floating point representation of the integer
727727
|
728728
272 | foo::<f64>(x_u32.into());
729729
| ^^^^^^^^^^^^
@@ -733,7 +733,7 @@ error[E0308]: mismatched types
733733
|
734734
274 | foo::<f64>(x_u16);
735735
| ^^^^^ expected f64, found u16
736-
help: you can cast an `u16` to `f64`, producing the floating point representation of the integer, rounded if necessary
736+
help: you can cast an `u16` to `f64`, producing the floating point representation of the integer
737737
|
738738
274 | foo::<f64>(x_u16.into());
739739
| ^^^^^^^^^^^^
@@ -743,7 +743,7 @@ error[E0308]: mismatched types
743743
|
744744
276 | foo::<f64>(x_u8);
745745
| ^^^^ expected f64, found u8
746-
help: you can cast an `u8` to `f64`, producing the floating point representation of the integer, rounded if necessary
746+
help: you can cast an `u8` to `f64`, producing the floating point representation of the integer
747747
|
748748
276 | foo::<f64>(x_u8.into());
749749
| ^^^^^^^^^^^
@@ -765,7 +765,7 @@ error[E0308]: mismatched types
765765
|
766766
282 | foo::<f64>(x_i32);
767767
| ^^^^^ expected f64, found i32
768-
help: you can cast an `i32` to `f64`, producing the floating point representation of the integer, rounded if necessary
768+
help: you can cast an `i32` to `f64`, producing the floating point representation of the integer
769769
|
770770
282 | foo::<f64>(x_i32.into());
771771
| ^^^^^^^^^^^^
@@ -775,7 +775,7 @@ error[E0308]: mismatched types
775775
|
776776
284 | foo::<f64>(x_i16);
777777
| ^^^^^ expected f64, found i16
778-
help: you can cast an `i16` to `f64`, producing the floating point representation of the integer, rounded if necessary
778+
help: you can cast an `i16` to `f64`, producing the floating point representation of the integer
779779
|
780780
284 | foo::<f64>(x_i16.into());
781781
| ^^^^^^^^^^^^
@@ -785,7 +785,7 @@ error[E0308]: mismatched types
785785
|
786786
286 | foo::<f64>(x_i8);
787787
| ^^^^ expected f64, found i8
788-
help: you can cast an `i8` to `f64`, producing the floating point representation of the integer, rounded if necessary
788+
help: you can cast an `i8` to `f64`, producing the floating point representation of the integer
789789
|
790790
286 | foo::<f64>(x_i8.into());
791791
| ^^^^^^^^^^^
@@ -823,7 +823,7 @@ error[E0308]: mismatched types
823823
|
824824
298 | foo::<f32>(x_u16);
825825
| ^^^^^ expected f32, found u16
826-
help: you can cast an `u16` to `f32`, producing the floating point representation of the integer, rounded if necessary
826+
help: you can cast an `u16` to `f32`, producing the floating point representation of the integer
827827
|
828828
298 | foo::<f32>(x_u16.into());
829829
| ^^^^^^^^^^^^
@@ -833,7 +833,7 @@ error[E0308]: mismatched types
833833
|
834834
300 | foo::<f32>(x_u8);
835835
| ^^^^ expected f32, found u8
836-
help: you can cast an `u8` to `f32`, producing the floating point representation of the integer, rounded if necessary
836+
help: you can cast an `u8` to `f32`, producing the floating point representation of the integer
837837
|
838838
300 | foo::<f32>(x_u8.into());
839839
| ^^^^^^^^^^^
@@ -861,7 +861,7 @@ error[E0308]: mismatched types
861861
|
862862
308 | foo::<f32>(x_i16);
863863
| ^^^^^ expected f32, found i16
864-
help: you can cast an `i16` to `f32`, producing the floating point representation of the integer, rounded if necessary
864+
help: you can cast an `i16` to `f32`, producing the floating point representation of the integer
865865
|
866866
308 | foo::<f32>(x_i16.into());
867867
| ^^^^^^^^^^^^
@@ -871,7 +871,7 @@ error[E0308]: mismatched types
871871
|
872872
310 | foo::<f32>(x_i8);
873873
| ^^^^ expected f32, found i8
874-
help: you can cast an `i8` to `f32`, producing the floating point representation of the integer, rounded if necessary
874+
help: you can cast an `i8` to `f32`, producing the floating point representation of the integer
875875
|
876876
310 | foo::<f32>(x_i8.into());
877877
| ^^^^^^^^^^^

0 commit comments

Comments
 (0)