Skip to content

Commit

Permalink
Rollup merge of rust-lang#63813 - estebank:int-from, r=varkor
Browse files Browse the repository at this point in the history
Do not suggest `.try_into()` on `i32::from(x)`

Fix rust-lang#63697.
  • Loading branch information
Centril authored Aug 25, 2019
2 parents 5761fc7 + 1acb537 commit ed8e13c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/librustc_typeck/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,30 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return false;
}
}
if let hir::ExprKind::Call(path, args) = &expr.node {
if let (
hir::ExprKind::Path(hir::QPath::TypeRelative(base_ty, path_segment)),
1,
) = (&path.node, args.len()) {
// `expr` is a conversion like `u32::from(val)`, do not suggest anything (#63697).
if let (
hir::TyKind::Path(hir::QPath::Resolved(None, base_ty_path)),
sym::from,
) = (&base_ty.node, path_segment.ident.name) {
if let Some(ident) = &base_ty_path.segments.iter().map(|s| s.ident).next() {
match ident.name {
sym::i128 | sym::i64 | sym::i32 | sym::i16 | sym::i8 |
sym::u128 | sym::u64 | sym::u32 | sym::u16 | sym::u8 |
sym::isize | sym::usize
if base_ty_path.segments.len() == 1 => {
return false;
}
_ => {}
}
}
}
}
}

let msg = format!("you can convert an `{}` to `{}`", checked_ty, expected_ty);
let cast_msg = format!("you can cast an `{} to `{}`", checked_ty, expected_ty);
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/suggestions/mismatched-types-numeric-from.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
let _: u32 = i32::from(0_u8); //~ ERROR mismatched types
}
9 changes: 9 additions & 0 deletions src/test/ui/suggestions/mismatched-types-numeric-from.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0308]: mismatched types
--> $DIR/mismatched-types-numeric-from.rs:2:18
|
LL | let _: u32 = i32::from(0_u8);
| ^^^^^^^^^^^^^^^ expected u32, found i32

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.

0 comments on commit ed8e13c

Please sign in to comment.