Skip to content

Commit 8549953

Browse files
committed
Reintroduce TypeError::FixedArraySize
1 parent f13317c commit 8549953

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

src/librustc/ty/error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub enum TypeError<'tcx> {
2323
AbiMismatch(ExpectedFound<abi::Abi>),
2424
Mutability,
2525
TupleSize(ExpectedFound<usize>),
26+
FixedArraySize(ExpectedFound<u64>),
2627
ArgCount,
2728

2829
RegionsDoesNotOutlive(Region<'tcx>, Region<'tcx>),
@@ -99,6 +100,12 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
99100
values.expected,
100101
values.found)
101102
}
103+
FixedArraySize(values) => {
104+
write!(f, "expected an array with a fixed size of {} elements, \
105+
found one with {} elements",
106+
values.expected,
107+
values.found)
108+
}
102109
ArgCount => {
103110
write!(f, "incorrect number of function parameters")
104111
}

src/librustc/ty/relate.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,22 @@ pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R,
472472
(&ty::Array(a_t, sz_a), &ty::Array(b_t, sz_b)) =>
473473
{
474474
let t = relation.relate(&a_t, &b_t)?;
475-
let sz = relation.relate(&sz_a, &sz_b)?;
476-
Ok(tcx.mk_ty(ty::Array(t, sz)))
475+
match relation.relate(&sz_a, &sz_b) {
476+
Ok(sz) => Ok(tcx.mk_ty(ty::Array(t, sz))),
477+
Err(err) => {
478+
// Check whether the lengths are both `usize`s,
479+
// but differ in value, for better diagnostics.
480+
// FIXME(eddyb) get the right param_env.
481+
match (sz_a.assert_usize(tcx), sz_b.assert_usize(tcx)) {
482+
(Some(sz_a_val), Some(sz_b_val)) => {
483+
Err(TypeError::FixedArraySize(
484+
expected_found(relation, &sz_a_val, &sz_b_val)
485+
))
486+
}
487+
_ => return Err(err),
488+
}
489+
}
490+
}
477491
}
478492

479493
(&ty::Slice(a_t), &ty::Slice(b_t)) =>

src/librustc/ty/structural_impls.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,7 @@ impl<'a, 'tcx> Lift<'tcx> for ty::error::TypeError<'a> {
716716
AbiMismatch(x) => AbiMismatch(x),
717717
Mutability => Mutability,
718718
TupleSize(x) => TupleSize(x),
719+
FixedArraySize(x) => FixedArraySize(x),
719720
ArgCount => ArgCount,
720721
RegionsDoesNotOutlive(a, b) => {
721722
return tcx.lift(&(a, b)).map(|(a, b)| RegionsDoesNotOutlive(a, b))
@@ -1294,6 +1295,7 @@ EnumTypeFoldableImpl! {
12941295
(ty::error::TypeError::AbiMismatch)(x),
12951296
(ty::error::TypeError::Mutability),
12961297
(ty::error::TypeError::TupleSize)(x),
1298+
(ty::error::TypeError::FixedArraySize)(x),
12971299
(ty::error::TypeError::ArgCount),
12981300
(ty::error::TypeError::RegionsDoesNotOutlive)(a, b),
12991301
(ty::error::TypeError::RegionsInsufficientlyPolymorphic)(a, b),

src/test/ui/consts/const-array-oob-arith.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/const-array-oob-arith.rs:7:45
33
|
44
LL | const BLUB: [i32; (ARR[0] - 40) as usize] = [5];
5-
| ^^^ expected `2usize`, found `1usize`
5+
| ^^^ expected an array with a fixed size of 2 elements, found one with 1 elements
66
|
77
= note: expected type `[i32; 2]`
88
found type `[i32; 1]`
@@ -11,7 +11,7 @@ error[E0308]: mismatched types
1111
--> $DIR/const-array-oob-arith.rs:8:44
1212
|
1313
LL | const BOO: [i32; (ARR[0] - 41) as usize] = [5, 99];
14-
| ^^^^^^^ expected `1usize`, found `2usize`
14+
| ^^^^^^^ expected an array with a fixed size of 1 elements, found one with 2 elements
1515
|
1616
= note: expected type `[i32; 1]`
1717
found type `[i32; 2]`

0 commit comments

Comments
 (0)