Skip to content

Commit bba1596

Browse files
committed
also print the expected type in the error message
1 parent 54b15c7 commit bba1596

File tree

5 files changed

+27
-13
lines changed

5 files changed

+27
-13
lines changed

src/librustc/middle/const_eval.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,8 @@ pub enum ErrKind {
465465
Math(ConstMathErr),
466466

467467
IntermediateUnsignedNegative,
468-
InferredWrongType(ConstInt),
468+
/// Expected, Got
469+
TypeMismatch(String, ConstInt),
469470
BadType(ConstVal),
470471
}
471472

@@ -528,7 +529,10 @@ impl ConstEvalErr {
528529
number was encountered. This is most likely a bug in\
529530
the constant evaluator".into_cow(),
530531

531-
InferredWrongType(ref i) => format!("inferred wrong type for {}", i).into_cow(),
532+
TypeMismatch(ref expected, ref got) => {
533+
format!("mismatched types: expected `{}`, found `{}`",
534+
expected, got.description()).into_cow()
535+
},
532536
BadType(ref i) => format!("value of wrong type: {:?}", i).into_cow(),
533537
}
534538
}
@@ -745,7 +749,7 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &TyCtxt<'tcx>,
745749

746750
let val = match eval_const_expr_partial(tcx, &base, base_hint, fn_args) {
747751
Ok(val) => val,
748-
Err(ConstEvalErr { kind: InferredWrongType(val), .. }) => {
752+
Err(ConstEvalErr { kind: TypeMismatch(_, val), .. }) => {
749753
// Something like `5i8 as usize` doesn't need a type hint for the base
750754
// instead take the type hint from the inner value
751755
let hint = match val.int_type() {
@@ -1085,8 +1089,8 @@ fn infer<'tcx>(
10851089
(&ty::TyUint(_), Infer(_)) => Err(err(Math(ConstMathErr::NotInRange))),
10861090
(&ty::TyUint(_), InferSigned(_)) => Err(err(IntermediateUnsignedNegative)),
10871091

1088-
(&ty::TyInt(_), i) |
1089-
(&ty::TyUint(_), i) => Err(err(InferredWrongType(i))),
1092+
(&ty::TyInt(ity), i) => Err(err(TypeMismatch(ity.to_string(), i))),
1093+
(&ty::TyUint(ity), i) => Err(err(TypeMismatch(ity.to_string(), i))),
10901094

10911095
(&ty::TyEnum(ref adt, _), i) => {
10921096
let hints = tcx.lookup_repr_hints(adt.did);

src/test/compile-fail/const-eval-overflow-3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
// self-hosted and a cross-compiled setup; therefore resorting to
1818
// error-pattern for now.
1919

20-
// error-pattern: expected constant integer for repeat count, but tried to add two integrals of
20+
// error-pattern: expected constant integer for repeat count, but attempted to add with overflow
2121

2222
#![allow(unused_imports)]
2323

src/test/compile-fail/const-eval-overflow-4b.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ use std::{u8, u16, u32, u64, usize};
2121

2222
const A_I8_T
2323
: [u32; (i8::MAX as i8 + 1u8) as usize]
24-
//~^ ERROR tried to add two integrals of different types [E0250]
24+
//~^ ERROR mismatched types:
25+
//~| expected `i8`,
26+
//~| found `u8` [E0250]
2527
= [0; (i8::MAX as usize) + 1];
2628

2729
fn main() {

src/test/compile-fail/issue-8761.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010

1111
enum Foo {
1212
A = 1i64,
13-
//~^ ERROR mismatched types: expected `isize` got `i64`
13+
//~^ ERROR mismatched types:
14+
//~| expected `isize`,
15+
//~| found `i64` [E0080]
1416
B = 2u8
15-
//~^ ERROR mismatched types: expected `isize` got `u8`
17+
//~^ ERROR mismatched types:
18+
//~| expected `isize`,
19+
//~| found `u8` [E0080]
1620
}
1721

1822
fn main() {}

src/test/compile-fail/repeat_count.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,17 @@ fn main() {
4343
let f = [0; -4_isize];
4444
//~^ ERROR mismatched types
4545
//~| expected `usize`
46-
//~| found `isize`
47-
//~| ERROR expected positive integer for repeat count, found isize [E0306]
46+
//~| found `isize` [E0308]
47+
//~| ERROR mismatched types:
48+
//~| expected `usize`,
49+
//~| found `isize` [E0307]
4850
let f = [0_usize; -1_isize];
4951
//~^ ERROR mismatched types
5052
//~| expected `usize`
51-
//~| found `isize`
52-
//~| ERROR expected positive integer for repeat count, found isize [E0306]
53+
//~| found `isize` [E0308]
54+
//~| ERROR mismatched types
55+
//~| expected `usize`
56+
//~| found `isize` [E0307]
5357
struct G {
5458
g: (),
5559
}

0 commit comments

Comments
 (0)