diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2018-01-07 12:27:03 -0800 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2018-01-14 22:46:24 -0800 |
| commit | af91d9955b036630657d69210ad4040e1725c14e (patch) | |
| tree | 0ff74ddf58bb1a25d48c4650913231217921e174 | |
| parent | aec16237e4cd8c2d4d650cacac72b9a33cac1f99 (diff) | |
| download | rust-af91d9955b036630657d69210ad4040e1725c14e.tar.gz rust-af91d9955b036630657d69210ad4040e1725c14e.zip | |
Use `into` for casting when possible
| -rw-r--r-- | src/librustc/hir/mod.rs | 31 | ||||
| -rw-r--r-- | src/librustc_typeck/check/demand.rs | 246 | ||||
| -rw-r--r-- | src/test/ui/mismatched_types/issue-26480.stderr | 2 | ||||
| -rw-r--r-- | src/test/ui/suggestions/numeric-cast-2.rs | 2 | ||||
| -rw-r--r-- | src/test/ui/suggestions/numeric-cast-2.stderr | 6 | ||||
| -rw-r--r-- | src/test/ui/suggestions/numeric-cast.rs | 2 | ||||
| -rw-r--r-- | src/test/ui/suggestions/numeric-cast.stderr | 88 |
7 files changed, 205 insertions, 172 deletions
diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index 38b6bffcadd..8d43b9b4aa7 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -1173,37 +1173,6 @@ impl fmt::Debug for Expr { } } -impl Expr { - - /// If casting this expression to a given numeric type would be appropriate in case of a type - /// mismatch. - /// - /// We want to minimize the amount of casting operations that are suggested, as it can be a - /// lossy operation with potentially bad side effects, so we only suggest when encountering an - /// expression that indicates that the original type couldn't be directly changed. - pub fn could_cast_in_type_mismatch(&self) -> bool { - match self.node { - ExprCall(..) | - ExprMethodCall(..) | - ExprBinary(..) | - ExprField(..) | - ExprTupField(..) | - ExprIndex(..) | - ExprPath(..) => true, - _ => false, - } - } - - pub fn needs_parens_around_cast(&self) -> bool { - match self.node { - ExprBinary(..) | - ExprCast(..) | - ExprType(..) => true, - _ => false, - } - } -} - #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub enum Expr_ { /// A `box x` expression. diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs index 7c55df7ff44..42cca2bbb5e 100644 --- a/src/librustc_typeck/check/demand.rs +++ b/src/librustc_typeck/check/demand.rs @@ -325,34 +325,58 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { let will_sign_extend = "will sign-extend the source value"; let will_zero_extend = "will zero-extend the source value"; - let needs_paren = expr.needs_parens_around_cast(); + // If casting this expression to a given numeric type would be appropriate in case of a type + // mismatch. + // + // We want to minimize the amount of casting operations that are suggested, as it can be a + // lossy operation with potentially bad side effects, so we only suggest when encountering + // an expression that indicates that the original type couldn't be directly changed. + let can_cast = match expr.node { + hir::ExprPath(..) | + hir::ExprCall(..) | + hir::ExprMethodCall(..) | + hir::ExprBinary(..) => true, + _ => false, + }; + + let needs_paren = match expr.node { + hir::ExprBinary(..) => true, + _ => false, + }; - if let (Ok(src), true) = (self.tcx.sess.codemap().span_to_snippet(expr.span), - expr.could_cast_in_type_mismatch()) { + 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); - let suggestion = format!("{}{} as {}{}", - if needs_paren { "(" } else { "" }, - src, - if needs_paren { ")" } else { "" }, - expected_ty); + let cast_suggestion = format!("{}{}{} as {}", + if needs_paren { "(" } else { "" }, + src, + if needs_paren { ")" } else { "" }, + expected_ty); + let into_suggestion = format!("{}{}{}.into()", + if needs_paren { "(" } else { "" }, + src, + if needs_paren { ")" } else { "" }); match (&expected_ty.sty, &checked_ty.sty) { (&ty::TyInt(ref exp), &ty::TyInt(ref found)) => { match (found.bit_width(), exp.bit_width()) { (Some(found), Some(exp)) if found > exp => { - err.span_suggestion(expr.span, - &format!("{}, which {}", msg, will_truncate), - suggestion); + if can_cast { + err.span_suggestion(expr.span, + &format!("{}, which {}", msg, will_truncate), + cast_suggestion); + } } (None, _) | (_, None) => { - err.span_suggestion(expr.span, - &format!("{}, which {}", msg, depending_on_isize), - suggestion); + if can_cast { + err.span_suggestion(expr.span, + &format!("{}, which {}", msg, depending_on_isize), + cast_suggestion); + } } _ => { err.span_suggestion(expr.span, &format!("{}, which {}", msg, will_sign_extend), - suggestion); + into_suggestion); } } true @@ -360,117 +384,155 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { (&ty::TyUint(ref exp), &ty::TyUint(ref found)) => { match (found.bit_width(), exp.bit_width()) { (Some(found), Some(exp)) if found > exp => { - err.span_suggestion(expr.span, - &format!("{}, which {}", msg, will_truncate), - suggestion); + if can_cast { + err.span_suggestion(expr.span, + &format!("{}, which {}", msg, will_truncate), + cast_suggestion); + } } (None, _) | (_, None) => { - err.span_suggestion(expr.span, - &format!("{}, which {}", msg, depending_on_usize), - suggestion); + if can_cast { + err.span_suggestion(expr.span, + &format!("{}, which {}", msg, depending_on_usize), + cast_suggestion); + } } _ => { err.span_suggestion(expr.span, &format!("{}, which {}", msg, will_zero_extend), - suggestion); + into_suggestion); } } true } (&ty::TyInt(ref exp), &ty::TyUint(ref found)) => { - match (found.bit_width(), exp.bit_width()) { - (Some(found), Some(exp)) if found > exp - 1 => { - err.span_suggestion(expr.span, - &format!("{}, which {}", msg, will_truncate), - suggestion); - } - (None, None) => { - err.span_suggestion(expr.span, - &format!("{}, which {}", msg, will_truncate), - suggestion); - } - (None, _) => { - err.span_suggestion(expr.span, - &format!("{}, which {}", msg, depending_on_isize), - suggestion); - } - (_, None) => { - err.span_suggestion(expr.span, - &format!("{}, which {}", msg, depending_on_usize), - suggestion); - } - _ => { - err.span_suggestion(expr.span, - &format!("{}, which {}", msg, will_zero_extend), - suggestion); + if can_cast { + match (found.bit_width(), exp.bit_width()) { + (Some(found), Some(exp)) if found > exp - 1 => { + err.span_suggestion(expr.span, + &format!("{}, which {}", msg, will_truncate), + cast_suggestion); + } + (None, None) => { + err.span_suggestion(expr.span, + &format!("{}, which {}", msg, will_truncate), + cast_suggestion); + } + (None, _) => { + err.span_suggestion(expr.span, + &format!("{}, which {}", msg, depending_on_isize), + cast_suggestion); + } + (_, None) => { + err.span_suggestion(expr.span, + &format!("{}, which {}", msg, depending_on_usize), + cast_suggestion); + } + _ => { + err.span_suggestion(expr.span, + &format!("{}, which {}", msg, will_zero_extend), + cast_suggestion); + } } } true } (&ty::TyUint(ref exp), &ty::TyInt(ref found)) => { - match (found.bit_width(), exp.bit_width()) { - (Some(found), Some(exp)) if found - 1 > exp => { - err.span_suggestion(expr.span, - &format!("{}, which {}", msg, will_truncate), - suggestion); - } - (None, None) => { - err.span_suggestion(expr.span, - &format!("{}, which {}", msg, will_sign_extend), - suggestion); - } - (None, _) => { - err.span_suggestion(expr.span, - &format!("{}, which {}", msg, depending_on_usize), - suggestion); - } - (_, None) => { - err.span_suggestion(expr.span, - &format!("{}, which {}", msg, depending_on_isize), - suggestion); - } - _ => { - err.span_suggestion(expr.span, - &format!("{}, which {}", msg, will_sign_extend), - suggestion); + if can_cast { + match (found.bit_width(), exp.bit_width()) { + (Some(found), Some(exp)) if found - 1 > exp => { + err.span_suggestion(expr.span, + &format!("{}, which {}", msg, will_truncate), + cast_suggestion); + } + (None, None) => { + err.span_suggestion(expr.span, + &format!("{}, which {}", msg, will_sign_extend), + cast_suggestion); + } + (None, _) => { + err.span_suggestion(expr.span, + &format!("{}, which {}", msg, depending_on_usize), + cast_suggestion); + } + (_, None) => { + err.span_suggestion(expr.span, + &format!("{}, which {}", msg, depending_on_isize), + cast_suggestion); + } + _ => { + err.span_suggestion(expr.span, + &format!("{}, which {}", msg, will_sign_extend), + cast_suggestion); + } } } true } (&ty::TyFloat(ref exp), &ty::TyFloat(ref found)) => { - if found.bit_width() > exp.bit_width() { + if found.bit_width() < exp.bit_width() { + err.span_suggestion(expr.span, + &format!("{} in a lossless way", + msg), + into_suggestion); + } else if can_cast { err.span_suggestion(expr.span, &format!("{}, producing the closest possible value", msg), - suggestion); - err.warn("casting here will cause Undefined Behavior if the value is \ + cast_suggestion); + err.warn("casting here will cause undefined behavior if the value is \ finite but larger or smaller than the largest or smallest \ finite value representable by `f32` (this is a bug and will be \ fixed)"); - } else { + } + true + } + (&ty::TyUint(_), &ty::TyFloat(_)) | (&ty::TyInt(_), &ty::TyFloat(_)) => { + if can_cast { err.span_suggestion(expr.span, - &format!("{} in a lossless way", + &format!("{}, rounding the float towards zero", msg), - suggestion); + cast_suggestion); + err.warn("casting here will cause undefined behavior if the rounded value \ + cannot be represented by the target integer type, including \ + `Inf` and `NaN` (this is a bug and will be fixed)"); } true } - (&ty::TyUint(_), &ty::TyFloat(_)) | (&ty::TyInt(_), &ty::TyFloat(_)) => { - err.span_suggestion(expr.span, - &format!("{}, rounding the float towards zero", - msg), - suggestion); - err.warn("casting here will cause Undefined Behavior if the rounded value \ - cannot be represented by the target integer type, including `Inf` \ - and `NaN` (this is a bug and will be fixed)"); + (&ty::TyFloat(ref exp), &ty::TyUint(ref found)) => { + 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", + msg), + into_suggestion); + } else if can_cast { + err.span_suggestion(expr.span, + &format!("{}, producing the floating point \ + representation of the integer, rounded if \ + necessary", + msg), + cast_suggestion); + } true } - (&ty::TyFloat(_), &ty::TyUint(_)) | (&ty::TyFloat(_), &ty::TyInt(_)) => { - err.span_suggestion(expr.span, - &format!("{}, producing the floating point representation \ - of the integer, rounded if necessary", - msg), - suggestion); + (&ty::TyFloat(ref exp), &ty::TyInt(ref found)) => { + 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", + msg), + into_suggestion); + } else if can_cast { + err.span_suggestion(expr.span, + &format!("{}, producing the floating point \ + representation of the integer, rounded if \ + necessary", + msg), + cast_suggestion); + } true } _ => false, diff --git a/src/test/ui/mismatched_types/issue-26480.stderr b/src/test/ui/mismatched_types/issue-26480.stderr index 36c5767fc0b..0641e09b552 100644 --- a/src/test/ui/mismatched_types/issue-26480.stderr +++ b/src/test/ui/mismatched_types/issue-26480.stderr @@ -8,7 +8,7 @@ error[E0308]: mismatched types | -------------- in this macro invocation help: you can cast an `usize` to `u64`, which will truncate or zero-extend depending on the bit width of `usize` | -26 | ($arr.len() * size_of($arr[0]) as )u64); //~ ERROR mismatched types +26 | ($arr.len() * size_of($arr[0])) as u64); //~ ERROR mismatched types | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0605]: non-primitive cast: `{integer}` as `()` diff --git a/src/test/ui/suggestions/numeric-cast-2.rs b/src/test/ui/suggestions/numeric-cast-2.rs index 24796fbe460..4c905a090a6 100644 --- a/src/test/ui/suggestions/numeric-cast-2.rs +++ b/src/test/ui/suggestions/numeric-cast-2.rs @@ -13,5 +13,7 @@ fn foo() -> i32 { } fn main() { let x: u32 = foo(); + //~^ ERROR mismatched types let z: i32 = x + x; + //~^ ERROR mismatched types } diff --git a/src/test/ui/suggestions/numeric-cast-2.stderr b/src/test/ui/suggestions/numeric-cast-2.stderr index c31095c102d..44703867b75 100644 --- a/src/test/ui/suggestions/numeric-cast-2.stderr +++ b/src/test/ui/suggestions/numeric-cast-2.stderr @@ -9,13 +9,13 @@ help: you can cast an `i32` to `u32`, which will sign-extend the source value | ^^^^^^^^^^^^ error[E0308]: mismatched types - --> $DIR/numeric-cast-2.rs:16:18 + --> $DIR/numeric-cast-2.rs:17:18 | -16 | let z: i32 = x + x; +17 | let z: i32 = x + x; | ^^^^^ expected i32, found u32 help: you can cast an `u32` to `i32`, which will truncate the source value | -16 | let z: i32 = (x + x as )i32; +17 | let z: i32 = (x + x) as i32; | ^^^^^^^^^^^^^^ error: aborting due to 2 previous errors diff --git a/src/test/ui/suggestions/numeric-cast.rs b/src/test/ui/suggestions/numeric-cast.rs index cca735d30d8..cd5e183ede9 100644 --- a/src/test/ui/suggestions/numeric-cast.rs +++ b/src/test/ui/suggestions/numeric-cast.rs @@ -331,6 +331,6 @@ fn main() { //~^ ERROR mismatched types foo::<f32>(x_f64); //~^ ERROR mismatched types - //~| WARN casting here will cause Undefined Behavior + //~| WARN casting here will cause undefined behavior foo::<f32>(x_f32); } diff --git a/src/test/ui/suggestions/numeric-cast.stderr b/src/test/ui/suggestions/numeric-cast.stderr index 4f4e4205f1f..f074a3bc280 100644 --- a/src/test/ui/suggestions/numeric-cast.stderr +++ b/src/test/ui/suggestions/numeric-cast.stderr @@ -94,7 +94,7 @@ error[E0308]: mismatched types 47 | foo::<usize>(x_f64); | ^^^^^ expected usize, found f64 | - = warning: Currently this will cause Undefined Behavior if the rounded value cannot be represented by the target integer type. This includes `Inf` and `NaN`. This is a bug and will be fixed. + = warning: casting here will cause undefined behavior if the rounded value cannot be represented by the target integer type, including `Inf` and `NaN` (this is a bug and will be fixed) help: you can cast an `f64` to `usize`, rounding the float towards zero | 47 | foo::<usize>(x_f64 as usize); @@ -106,7 +106,7 @@ error[E0308]: mismatched types 50 | foo::<usize>(x_f32); | ^^^^^ expected usize, found f32 | - = warning: Currently this will cause Undefined Behavior if the rounded value cannot be represented by the target integer type. This includes `Inf` and `NaN`. This is a bug and will be fixed. + = warning: casting here will cause undefined behavior if the rounded value cannot be represented by the target integer type, including `Inf` and `NaN` (this is a bug and will be fixed) help: you can cast an `f32` to `usize`, rounding the float towards zero | 50 | foo::<usize>(x_f32 as usize); @@ -208,7 +208,7 @@ error[E0308]: mismatched types 73 | foo::<isize>(x_f64); | ^^^^^ expected isize, found f64 | - = warning: Currently this will cause Undefined Behavior if the rounded value cannot be represented by the target integer type. This includes `Inf` and `NaN`. This is a bug and will be fixed. + = warning: casting here will cause undefined behavior if the rounded value cannot be represented by the target integer type, including `Inf` and `NaN` (this is a bug and will be fixed) help: you can cast an `f64` to `isize`, rounding the float towards zero | 73 | foo::<isize>(x_f64 as isize); @@ -220,7 +220,7 @@ error[E0308]: mismatched types 76 | foo::<isize>(x_f32); | ^^^^^ expected isize, found f32 | - = warning: Currently this will cause Undefined Behavior if the rounded value cannot be represented by the target integer type. This includes `Inf` and `NaN`. This is a bug and will be fixed. + = warning: casting here will cause undefined behavior if the rounded value cannot be represented by the target integer type, including `Inf` and `NaN` (this is a bug and will be fixed) help: you can cast an `f32` to `isize`, rounding the float towards zero | 76 | foo::<isize>(x_f32 as isize); @@ -243,7 +243,7 @@ error[E0308]: mismatched types | ^^^^^ expected u64, found u32 help: you can cast an `u32` to `u64`, which will zero-extend the source value | -83 | foo::<u64>(x_u32 as u64); +83 | foo::<u64>(x_u32.into()); | ^^^^^^^^^^^^ error[E0308]: mismatched types @@ -253,7 +253,7 @@ error[E0308]: mismatched types | ^^^^^ expected u64, found u16 help: you can cast an `u16` to `u64`, which will zero-extend the source value | -85 | foo::<u64>(x_u16 as u64); +85 | foo::<u64>(x_u16.into()); | ^^^^^^^^^^^^ error[E0308]: mismatched types @@ -263,7 +263,7 @@ error[E0308]: mismatched types | ^^^^ expected u64, found u8 help: you can cast an `u8` to `u64`, which will zero-extend the source value | -87 | foo::<u64>(x_u8 as u64); +87 | foo::<u64>(x_u8.into()); | ^^^^^^^^^^^ error[E0308]: mismatched types @@ -322,7 +322,7 @@ error[E0308]: mismatched types 99 | foo::<u64>(x_f64); | ^^^^^ expected u64, found f64 | - = warning: Currently this will cause Undefined Behavior if the rounded value cannot be represented by the target integer type. This includes `Inf` and `NaN`. This is a bug and will be fixed. + = warning: casting here will cause undefined behavior if the rounded value cannot be represented by the target integer type, including `Inf` and `NaN` (this is a bug and will be fixed) help: you can cast an `f64` to `u64`, rounding the float towards zero | 99 | foo::<u64>(x_f64 as u64); @@ -334,7 +334,7 @@ error[E0308]: mismatched types 102 | foo::<u64>(x_f32); | ^^^^^ expected u64, found f32 | - = warning: Currently this will cause Undefined Behavior if the rounded value cannot be represented by the target integer type. This includes `Inf` and `NaN`. This is a bug and will be fixed. + = warning: casting here will cause undefined behavior if the rounded value cannot be represented by the target integer type, including `Inf` and `NaN` (this is a bug and will be fixed) help: you can cast an `f32` to `u64`, rounding the float towards zero | 102 | foo::<u64>(x_f32 as u64); @@ -407,7 +407,7 @@ error[E0308]: mismatched types | ^^^^^ expected i64, found i32 help: you can cast an `i32` to `i64`, which will sign-extend the source value | -119 | foo::<i64>(x_i32 as i64); +119 | foo::<i64>(x_i32.into()); | ^^^^^^^^^^^^ error[E0308]: mismatched types @@ -417,7 +417,7 @@ error[E0308]: mismatched types | ^^^^^ expected i64, found i16 help: you can cast an `i16` to `i64`, which will sign-extend the source value | -121 | foo::<i64>(x_i16 as i64); +121 | foo::<i64>(x_i16.into()); | ^^^^^^^^^^^^ error[E0308]: mismatched types @@ -427,7 +427,7 @@ error[E0308]: mismatched types | ^^^^ expected i64, found i8 help: you can cast an `i8` to `i64`, which will sign-extend the source value | -123 | foo::<i64>(x_i8 as i64); +123 | foo::<i64>(x_i8.into()); | ^^^^^^^^^^^ error[E0308]: mismatched types @@ -436,7 +436,7 @@ error[E0308]: mismatched types 125 | foo::<i64>(x_f64); | ^^^^^ expected i64, found f64 | - = warning: Currently this will cause Undefined Behavior if the rounded value cannot be represented by the target integer type. This includes `Inf` and `NaN`. This is a bug and will be fixed. + = warning: casting here will cause undefined behavior if the rounded value cannot be represented by the target integer type, including `Inf` and `NaN` (this is a bug and will be fixed) help: you can cast an `f64` to `i64`, rounding the float towards zero | 125 | foo::<i64>(x_f64 as i64); @@ -448,7 +448,7 @@ error[E0308]: mismatched types 128 | foo::<i64>(x_f32); | ^^^^^ expected i64, found f32 | - = warning: Currently this will cause Undefined Behavior if the rounded value cannot be represented by the target integer type. This includes `Inf` and `NaN`. This is a bug and will be fixed. + = warning: casting here will cause undefined behavior if the rounded value cannot be represented by the target integer type, including `Inf` and `NaN` (this is a bug and will be fixed) help: you can cast an `f32` to `i64`, rounding the float towards zero | 128 | foo::<i64>(x_f32 as i64); @@ -481,7 +481,7 @@ error[E0308]: mismatched types | ^^^^^ expected u32, found u16 help: you can cast an `u16` to `u32`, which will zero-extend the source value | -137 | foo::<u32>(x_u16 as u32); +137 | foo::<u32>(x_u16.into()); | ^^^^^^^^^^^^ error[E0308]: mismatched types @@ -491,7 +491,7 @@ error[E0308]: mismatched types | ^^^^ expected u32, found u8 help: you can cast an `u8` to `u32`, which will zero-extend the source value | -139 | foo::<u32>(x_u8 as u32); +139 | foo::<u32>(x_u8.into()); | ^^^^^^^^^^^ error[E0308]: mismatched types @@ -550,7 +550,7 @@ error[E0308]: mismatched types 151 | foo::<u32>(x_f64); | ^^^^^ expected u32, found f64 | - = warning: Currently this will cause Undefined Behavior if the rounded value cannot be represented by the target integer type. This includes `Inf` and `NaN`. This is a bug and will be fixed. + = warning: casting here will cause undefined behavior if the rounded value cannot be represented by the target integer type, including `Inf` and `NaN` (this is a bug and will be fixed) help: you can cast an `f64` to `u32`, rounding the float towards zero | 151 | foo::<u32>(x_f64 as u32); @@ -562,7 +562,7 @@ error[E0308]: mismatched types 154 | foo::<u32>(x_f32); | ^^^^^ expected u32, found f32 | - = warning: Currently this will cause Undefined Behavior if the rounded value cannot be represented by the target integer type. This includes `Inf` and `NaN`. This is a bug and will be fixed. + = warning: casting here will cause undefined behavior if the rounded value cannot be represented by the target integer type, including `Inf` and `NaN` (this is a bug and will be fixed) help: you can cast an `f32` to `u32`, rounding the float towards zero | 154 | foo::<u32>(x_f32 as u32); @@ -645,7 +645,7 @@ error[E0308]: mismatched types | ^^^^^ expected i32, found i16 help: you can cast an `i16` to `i32`, which will sign-extend the source value | -173 | foo::<i32>(x_i16 as i32); +173 | foo::<i32>(x_i16.into()); | ^^^^^^^^^^^^ error[E0308]: mismatched types @@ -655,7 +655,7 @@ error[E0308]: mismatched types | ^^^^ expected i32, found i8 help: you can cast an `i8` to `i32`, which will sign-extend the source value | -175 | foo::<i32>(x_i8 as i32); +175 | foo::<i32>(x_i8.into()); | ^^^^^^^^^^^ error[E0308]: mismatched types @@ -664,7 +664,7 @@ error[E0308]: mismatched types 177 | foo::<i32>(x_f64); | ^^^^^ expected i32, found f64 | - = warning: Currently this will cause Undefined Behavior if the rounded value cannot be represented by the target integer type. This includes `Inf` and `NaN`. This is a bug and will be fixed. + = warning: casting here will cause undefined behavior if the rounded value cannot be represented by the target integer type, including `Inf` and `NaN` (this is a bug and will be fixed) help: you can cast an `f64` to `i32`, rounding the float towards zero | 177 | foo::<i32>(x_f64 as i32); @@ -676,7 +676,7 @@ error[E0308]: mismatched types 180 | foo::<i32>(x_f32); | ^^^^^ expected i32, found f32 | - = warning: Currently this will cause Undefined Behavior if the rounded value cannot be represented by the target integer type. This includes `Inf` and `NaN`. This is a bug and will be fixed. + = warning: casting here will cause undefined behavior if the rounded value cannot be represented by the target integer type, including `Inf` and `NaN` (this is a bug and will be fixed) help: you can cast an `f32` to `i32`, rounding the float towards zero | 180 | foo::<i32>(x_f32 as i32); @@ -719,7 +719,7 @@ error[E0308]: mismatched types | ^^^^ expected u16, found u8 help: you can cast an `u8` to `u16`, which will zero-extend the source value | -191 | foo::<u16>(x_u8 as u16); +191 | foo::<u16>(x_u8.into()); | ^^^^^^^^^^^ error[E0308]: mismatched types @@ -778,7 +778,7 @@ error[E0308]: mismatched types 203 | foo::<u16>(x_f64); | ^^^^^ expected u16, found f64 | - = warning: Currently this will cause Undefined Behavior if the rounded value cannot be represented by the target integer type. This includes `Inf` and `NaN`. This is a bug and will be fixed. + = warning: casting here will cause undefined behavior if the rounded value cannot be represented by the target integer type, including `Inf` and `NaN` (this is a bug and will be fixed) help: you can cast an `f64` to `u16`, rounding the float towards zero | 203 | foo::<u16>(x_f64 as u16); @@ -790,7 +790,7 @@ error[E0308]: mismatched types 206 | foo::<u16>(x_f32); | ^^^^^ expected u16, found f32 | - = warning: Currently this will cause Undefined Behavior if the rounded value cannot be represented by the target integer type. This includes `Inf` and `NaN`. This is a bug and will be fixed. + = warning: casting here will cause undefined behavior if the rounded value cannot be represented by the target integer type, including `Inf` and `NaN` (this is a bug and will be fixed) help: you can cast an `f32` to `u16`, rounding the float towards zero | 206 | foo::<u16>(x_f32 as u16); @@ -883,7 +883,7 @@ error[E0308]: mismatched types | ^^^^ expected i16, found i8 help: you can cast an `i8` to `i16`, which will sign-extend the source value | -227 | foo::<i16>(x_i8 as i16); +227 | foo::<i16>(x_i8.into()); | ^^^^^^^^^^^ error[E0308]: mismatched types @@ -892,7 +892,7 @@ error[E0308]: mismatched types 229 | foo::<i16>(x_f64); | ^^^^^ expected i16, found f64 | - = warning: Currently this will cause Undefined Behavior if the rounded value cannot be represented by the target integer type. This includes `Inf` and `NaN`. This is a bug and will be fixed. + = warning: casting here will cause undefined behavior if the rounded value cannot be represented by the target integer type, including `Inf` and `NaN` (this is a bug and will be fixed) help: you can cast an `f64` to `i16`, rounding the float towards zero | 229 | foo::<i16>(x_f64 as i16); @@ -904,7 +904,7 @@ error[E0308]: mismatched types 232 | foo::<i16>(x_f32); | ^^^^^ expected i16, found f32 | - = warning: Currently this will cause Undefined Behavior if the rounded value cannot be represented by the target integer type. This includes `Inf` and `NaN`. This is a bug and will be fixed. + = warning: casting here will cause undefined behavior if the rounded value cannot be represented by the target integer type, including `Inf` and `NaN` (this is a bug and will be fixed) help: you can cast an `f32` to `i16`, rounding the float towards zero | 232 | foo::<i16>(x_f32 as i16); @@ -1006,7 +1006,7 @@ error[E0308]: mismatched types 255 | foo::<u8>(x_f64); | ^^^^^ expected u8, found f64 | - = warning: Currently this will cause Undefined Behavior if the rounded value cannot be represented by the target integer type. This includes `Inf` and `NaN`. This is a bug and will be fixed. + = warning: casting here will cause undefined behavior if the rounded value cannot be represented by the target integer type, including `Inf` and `NaN` (this is a bug and will be fixed) help: you can cast an `f64` to `u8`, rounding the float towards zero | 255 | foo::<u8>(x_f64 as u8); @@ -1018,7 +1018,7 @@ error[E0308]: mismatched types 258 | foo::<u8>(x_f32); | ^^^^^ expected u8, found f32 | - = warning: Currently this will cause Undefined Behavior if the rounded value cannot be represented by the target integer type. This includes `Inf` and `NaN`. This is a bug and will be fixed. + = warning: casting here will cause undefined behavior if the rounded value cannot be represented by the target integer type, including `Inf` and `NaN` (this is a bug and will be fixed) help: you can cast an `f32` to `u8`, rounding the float towards zero | 258 | foo::<u8>(x_f32 as u8); @@ -1120,7 +1120,7 @@ error[E0308]: mismatched types 281 | foo::<i8>(x_f64); | ^^^^^ expected i8, found f64 | - = warning: Currently this will cause Undefined Behavior if the rounded value cannot be represented by the target integer type. This includes `Inf` and `NaN`. This is a bug and will be fixed. + = warning: casting here will cause undefined behavior if the rounded value cannot be represented by the target integer type, including `Inf` and `NaN` (this is a bug and will be fixed) help: you can cast an `f64` to `i8`, rounding the float towards zero | 281 | foo::<i8>(x_f64 as i8); @@ -1132,7 +1132,7 @@ error[E0308]: mismatched types 284 | foo::<i8>(x_f32); | ^^^^^ expected i8, found f32 | - = warning: Currently this will cause Undefined Behavior if the rounded value cannot be represented by the target integer type. This includes `Inf` and `NaN`. This is a bug and will be fixed. + = warning: casting here will cause undefined behavior if the rounded value cannot be represented by the target integer type, including `Inf` and `NaN` (this is a bug and will be fixed) help: you can cast an `f32` to `i8`, rounding the float towards zero | 284 | foo::<i8>(x_f32 as i8); @@ -1165,7 +1165,7 @@ error[E0308]: mismatched types | ^^^^^ expected f64, found u32 help: you can cast an `u32` to `f64`, producing the floating point representation of the integer, rounded if necessary | -292 | foo::<f64>(x_u32 as f64); +292 | foo::<f64>(x_u32.into()); | ^^^^^^^^^^^^ error[E0308]: mismatched types @@ -1175,7 +1175,7 @@ error[E0308]: mismatched types | ^^^^^ expected f64, found u16 help: you can cast an `u16` to `f64`, producing the floating point representation of the integer, rounded if necessary | -294 | foo::<f64>(x_u16 as f64); +294 | foo::<f64>(x_u16.into()); | ^^^^^^^^^^^^ error[E0308]: mismatched types @@ -1185,7 +1185,7 @@ error[E0308]: mismatched types | ^^^^ expected f64, found u8 help: you can cast an `u8` to `f64`, producing the floating point representation of the integer, rounded if necessary | -296 | foo::<f64>(x_u8 as f64); +296 | foo::<f64>(x_u8.into()); | ^^^^^^^^^^^ error[E0308]: mismatched types @@ -1215,7 +1215,7 @@ error[E0308]: mismatched types | ^^^^^ expected f64, found i32 help: you can cast an `i32` to `f64`, producing the floating point representation of the integer, rounded if necessary | -302 | foo::<f64>(x_i32 as f64); +302 | foo::<f64>(x_i32.into()); | ^^^^^^^^^^^^ error[E0308]: mismatched types @@ -1225,7 +1225,7 @@ error[E0308]: mismatched types | ^^^^^ expected f64, found i16 help: you can cast an `i16` to `f64`, producing the floating point representation of the integer, rounded if necessary | -304 | foo::<f64>(x_i16 as f64); +304 | foo::<f64>(x_i16.into()); | ^^^^^^^^^^^^ error[E0308]: mismatched types @@ -1235,7 +1235,7 @@ error[E0308]: mismatched types | ^^^^ expected f64, found i8 help: you can cast an `i8` to `f64`, producing the floating point representation of the integer, rounded if necessary | -306 | foo::<f64>(x_i8 as f64); +306 | foo::<f64>(x_i8.into()); | ^^^^^^^^^^^ error[E0308]: mismatched types @@ -1245,7 +1245,7 @@ error[E0308]: mismatched types | ^^^^^ expected f64, found f32 help: you can cast an `f32` to `f64` in a lossless way | -309 | foo::<f64>(x_f32 as f64); +309 | foo::<f64>(x_f32.into()); | ^^^^^^^^^^^^ error[E0308]: mismatched types @@ -1285,7 +1285,7 @@ error[E0308]: mismatched types | ^^^^^ expected f32, found u16 help: you can cast an `u16` to `f32`, producing the floating point representation of the integer, rounded if necessary | -318 | foo::<f32>(x_u16 as f32); +318 | foo::<f32>(x_u16.into()); | ^^^^^^^^^^^^ error[E0308]: mismatched types @@ -1295,7 +1295,7 @@ error[E0308]: mismatched types | ^^^^ expected f32, found u8 help: you can cast an `u8` to `f32`, producing the floating point representation of the integer, rounded if necessary | -320 | foo::<f32>(x_u8 as f32); +320 | foo::<f32>(x_u8.into()); | ^^^^^^^^^^^ error[E0308]: mismatched types @@ -1335,7 +1335,7 @@ error[E0308]: mismatched types | ^^^^^ expected f32, found i16 help: you can cast an `i16` to `f32`, producing the floating point representation of the integer, rounded if necessary | -328 | foo::<f32>(x_i16 as f32); +328 | foo::<f32>(x_i16.into()); | ^^^^^^^^^^^^ error[E0308]: mismatched types @@ -1345,7 +1345,7 @@ error[E0308]: mismatched types | ^^^^ expected f32, found i8 help: you can cast an `i8` to `f32`, producing the floating point representation of the integer, rounded if necessary | -330 | foo::<f32>(x_i8 as f32); +330 | foo::<f32>(x_i8.into()); | ^^^^^^^^^^^ error[E0308]: mismatched types @@ -1354,7 +1354,7 @@ error[E0308]: mismatched types 332 | foo::<f32>(x_f64); | ^^^^^ expected f32, found f64 | - = warning: Currently this will cause Undefined Behavior if the value is finite but larger or smaller than the largest or smallest finite value representable by `f32`. This is a bug and will be fixed. + = warning: casting here will cause undefined behavior if the value is finite but larger or smaller than the largest or smallest finite value representable by `f32` (this is a bug and will be fixed) help: you can cast an `f64` to `f32`, producing the closest possible value | 332 | foo::<f32>(x_f64 as f32); |
