Skip to content

Commit 862a878

Browse files
committed
Auto merge of #60159 - estebank:type-mismatch-cast, r=oli-obk
Suggest `try_into` when possible CC #47168
2 parents f843ad6 + 31eb5cc commit 862a878

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+3515
-733
lines changed

src/librustc/hir/map/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,14 @@ impl<'hir> Map<'hir> {
734734
}
735735
}
736736

737+
pub fn is_const_scope(&self, hir_id: HirId) -> bool {
738+
self.walk_parent_nodes(hir_id, |node| match *node {
739+
Node::Item(Item { node: ItemKind::Const(_, _), .. }) => true,
740+
Node::Item(Item { node: ItemKind::Fn(_, header, _, _), .. }) => header.is_const(),
741+
_ => false,
742+
}, |_| false).map(|id| id != CRATE_HIR_ID).unwrap_or(false)
743+
}
744+
737745
/// If there is some error when walking the parents (e.g., a node does not
738746
/// have a parent in the map or a node can't be found), then we return the
739747
/// last good `NodeId` we found. Note that reaching the crate root (`id == 0`),

src/librustc/hir/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -2288,6 +2288,15 @@ pub struct FnHeader {
22882288
pub abi: Abi,
22892289
}
22902290

2291+
impl FnHeader {
2292+
pub fn is_const(&self) -> bool {
2293+
match &self.constness {
2294+
Constness::Const => true,
2295+
_ => false,
2296+
}
2297+
}
2298+
}
2299+
22912300
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable)]
22922301
pub enum ItemKind {
22932302
/// An `extern crate` item, with optional *original* crate name if the crate was renamed.

src/librustc_typeck/check/demand.rs

+126-216
Large diffs are not rendered by default.

src/test/ui/associated-types/associated-types-path-2.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ error[E0308]: mismatched types
33
|
44
LL | f1(2i32, 4i32);
55
| ^^^^ expected u32, found i32
6+
help: change the type of the numeric literal from `i32` to `u32`
7+
|
8+
LL | f1(2i32, 4u32);
9+
| ^^^^
610

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

4957
error: aborting due to 6 previous errors
5058

src/test/ui/discrim/discrim-ill-typed.stderr

+32
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,80 @@ error[E0308]: mismatched types
33
|
44
LL | OhNo = 0_u8,
55
| ^^^^ expected i8, found u8
6+
help: change the type of the numeric literal from `u8` to `i8`
7+
|
8+
LL | OhNo = 0_i8,
9+
| ^^^^
610

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

1321
error[E0308]: mismatched types
1422
--> $DIR/discrim-ill-typed.rs:43:16
1523
|
1624
LL | OhNo = 0_u16,
1725
| ^^^^^ expected i16, found u16
26+
help: change the type of the numeric literal from `u16` to `i16`
27+
|
28+
LL | OhNo = 0_i16,
29+
| ^^^^^
1830

1931
error[E0308]: mismatched types
2032
--> $DIR/discrim-ill-typed.rs:56:16
2133
|
2234
LL | OhNo = 0_i16,
2335
| ^^^^^ expected u16, found i16
36+
help: change the type of the numeric literal from `i16` to `u16`
37+
|
38+
LL | OhNo = 0_u16,
39+
| ^^^^^
2440

2541
error[E0308]: mismatched types
2642
--> $DIR/discrim-ill-typed.rs:69:16
2743
|
2844
LL | OhNo = 0_u32,
2945
| ^^^^^ expected i32, found u32
46+
help: change the type of the numeric literal from `u32` to `i32`
47+
|
48+
LL | OhNo = 0_i32,
49+
| ^^^^^
3050

3151
error[E0308]: mismatched types
3252
--> $DIR/discrim-ill-typed.rs:82:16
3353
|
3454
LL | OhNo = 0_i32,
3555
| ^^^^^ expected u32, found i32
56+
help: change the type of the numeric literal from `i32` to `u32`
57+
|
58+
LL | OhNo = 0_u32,
59+
| ^^^^^
3660

3761
error[E0308]: mismatched types
3862
--> $DIR/discrim-ill-typed.rs:95:16
3963
|
4064
LL | OhNo = 0_u64,
4165
| ^^^^^ expected i64, found u64
66+
help: change the type of the numeric literal from `u64` to `i64`
67+
|
68+
LL | OhNo = 0_i64,
69+
| ^^^^^
4270

4371
error[E0308]: mismatched types
4472
--> $DIR/discrim-ill-typed.rs:108:16
4573
|
4674
LL | OhNo = 0_i64,
4775
| ^^^^^ expected u64, found i64
76+
help: change the type of the numeric literal from `i64` to `u64`
77+
|
78+
LL | OhNo = 0_u64,
79+
| ^^^^^
4880

4981
error: aborting due to 8 previous errors
5082

src/test/ui/float-literal-inference-restrictions.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ error[E0308]: mismatched types
1515
|
1616
LL | let y: f32 = 1f64;
1717
| ^^^^ expected f32, found f64
18+
help: change the type of the numeric literal from `f64` to `f32`
19+
|
20+
LL | let y: f32 = 1f32;
21+
| ^^^^
1822

1923
error: aborting due to 2 previous errors
2024

src/test/ui/indexing-requires-a-uint.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ error[E0308]: mismatched types
1212
|
1313
LL | bar::<isize>(i); // i should not be re-coerced back to an isize
1414
| ^ expected isize, found usize
15+
help: you can convert an `usize` to `isize` or panic if it the converted value wouldn't fit
16+
|
17+
LL | bar::<isize>(i.try_into().unwrap()); // i should not be re-coerced back to an isize
18+
| ^^^^^^^^^^^^^^^^^^^^^
1519

1620
error: aborting due to 2 previous errors
1721

0 commit comments

Comments
 (0)