Skip to content

Commit

Permalink
Auto merge of #60159 - estebank:type-mismatch-cast, r=oli-obk
Browse files Browse the repository at this point in the history
Suggest `try_into` when possible

CC #47168
  • Loading branch information
bors committed Apr 30, 2019
2 parents f843ad6 + 31eb5cc commit 862a878
Show file tree
Hide file tree
Showing 42 changed files with 3,515 additions and 733 deletions.
8 changes: 8 additions & 0 deletions src/librustc/hir/map/mod.rs
Expand Up @@ -734,6 +734,14 @@ impl<'hir> Map<'hir> {
}
}

pub fn is_const_scope(&self, hir_id: HirId) -> bool {
self.walk_parent_nodes(hir_id, |node| match *node {
Node::Item(Item { node: ItemKind::Const(_, _), .. }) => true,
Node::Item(Item { node: ItemKind::Fn(_, header, _, _), .. }) => header.is_const(),
_ => false,
}, |_| false).map(|id| id != CRATE_HIR_ID).unwrap_or(false)
}

/// If there is some error when walking the parents (e.g., a node does not
/// have a parent in the map or a node can't be found), then we return the
/// last good `NodeId` we found. Note that reaching the crate root (`id == 0`),
Expand Down
9 changes: 9 additions & 0 deletions src/librustc/hir/mod.rs
Expand Up @@ -2288,6 +2288,15 @@ pub struct FnHeader {
pub abi: Abi,
}

impl FnHeader {
pub fn is_const(&self) -> bool {
match &self.constness {
Constness::Const => true,
_ => false,
}
}
}

#[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable)]
pub enum ItemKind {
/// An `extern crate` item, with optional *original* crate name if the crate was renamed.
Expand Down
342 changes: 126 additions & 216 deletions src/librustc_typeck/check/demand.rs

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/test/ui/associated-types/associated-types-path-2.stderr
Expand Up @@ -3,6 +3,10 @@ error[E0308]: mismatched types
|
LL | f1(2i32, 4i32);
| ^^^^ expected u32, found i32
help: change the type of the numeric literal from `i32` to `u32`
|
LL | f1(2i32, 4u32);
| ^^^^

error[E0277]: the trait bound `u32: Foo` is not satisfied
--> $DIR/associated-types-path-2.rs:29:5
Expand Down Expand Up @@ -45,6 +49,10 @@ error[E0308]: mismatched types
|
LL | let _: i32 = f2(2i32);
| ^^^^^^^^ expected i32, found u32
help: you can convert an `u32` to `i32` or panic if it the converted value wouldn't fit
|
LL | let _: i32 = f2(2i32).try_into().unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 6 previous errors

Expand Down
32 changes: 32 additions & 0 deletions src/test/ui/discrim/discrim-ill-typed.stderr
Expand Up @@ -3,48 +3,80 @@ error[E0308]: mismatched types
|
LL | OhNo = 0_u8,
| ^^^^ expected i8, found u8
help: change the type of the numeric literal from `u8` to `i8`
|
LL | OhNo = 0_i8,
| ^^^^

error[E0308]: mismatched types
--> $DIR/discrim-ill-typed.rs:30:16
|
LL | OhNo = 0_i8,
| ^^^^ expected u8, found i8
help: change the type of the numeric literal from `i8` to `u8`
|
LL | OhNo = 0_u8,
| ^^^^

error[E0308]: mismatched types
--> $DIR/discrim-ill-typed.rs:43:16
|
LL | OhNo = 0_u16,
| ^^^^^ expected i16, found u16
help: change the type of the numeric literal from `u16` to `i16`
|
LL | OhNo = 0_i16,
| ^^^^^

error[E0308]: mismatched types
--> $DIR/discrim-ill-typed.rs:56:16
|
LL | OhNo = 0_i16,
| ^^^^^ expected u16, found i16
help: change the type of the numeric literal from `i16` to `u16`
|
LL | OhNo = 0_u16,
| ^^^^^

error[E0308]: mismatched types
--> $DIR/discrim-ill-typed.rs:69:16
|
LL | OhNo = 0_u32,
| ^^^^^ expected i32, found u32
help: change the type of the numeric literal from `u32` to `i32`
|
LL | OhNo = 0_i32,
| ^^^^^

error[E0308]: mismatched types
--> $DIR/discrim-ill-typed.rs:82:16
|
LL | OhNo = 0_i32,
| ^^^^^ expected u32, found i32
help: change the type of the numeric literal from `i32` to `u32`
|
LL | OhNo = 0_u32,
| ^^^^^

error[E0308]: mismatched types
--> $DIR/discrim-ill-typed.rs:95:16
|
LL | OhNo = 0_u64,
| ^^^^^ expected i64, found u64
help: change the type of the numeric literal from `u64` to `i64`
|
LL | OhNo = 0_i64,
| ^^^^^

error[E0308]: mismatched types
--> $DIR/discrim-ill-typed.rs:108:16
|
LL | OhNo = 0_i64,
| ^^^^^ expected u64, found i64
help: change the type of the numeric literal from `i64` to `u64`
|
LL | OhNo = 0_u64,
| ^^^^^

error: aborting due to 8 previous errors

Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/float-literal-inference-restrictions.stderr
Expand Up @@ -15,6 +15,10 @@ error[E0308]: mismatched types
|
LL | let y: f32 = 1f64;
| ^^^^ expected f32, found f64
help: change the type of the numeric literal from `f64` to `f32`
|
LL | let y: f32 = 1f32;
| ^^^^

error: aborting due to 2 previous errors

Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/indexing-requires-a-uint.stderr
Expand Up @@ -12,6 +12,10 @@ error[E0308]: mismatched types
|
LL | bar::<isize>(i); // i should not be re-coerced back to an isize
| ^ expected isize, found usize
help: you can convert an `usize` to `isize` or panic if it the converted value wouldn't fit
|
LL | bar::<isize>(i.try_into().unwrap()); // i should not be re-coerced back to an isize
| ^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

Expand Down

0 comments on commit 862a878

Please sign in to comment.