From bba1596c71989bdbad19ec06b27fa0d81d5bc58f Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Thu, 25 Feb 2016 11:12:18 +0100 Subject: [PATCH] also print the expected type in the error message --- src/librustc/middle/const_eval.rs | 14 +++++++++----- src/test/compile-fail/const-eval-overflow-3.rs | 2 +- src/test/compile-fail/const-eval-overflow-4b.rs | 4 +++- src/test/compile-fail/issue-8761.rs | 8 ++++++-- src/test/compile-fail/repeat_count.rs | 12 ++++++++---- 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/librustc/middle/const_eval.rs b/src/librustc/middle/const_eval.rs index f4c5196d0cd..f56c42738e7 100644 --- a/src/librustc/middle/const_eval.rs +++ b/src/librustc/middle/const_eval.rs @@ -465,7 +465,8 @@ pub enum ErrKind { Math(ConstMathErr), IntermediateUnsignedNegative, - InferredWrongType(ConstInt), + /// Expected, Got + TypeMismatch(String, ConstInt), BadType(ConstVal), } @@ -528,7 +529,10 @@ impl ConstEvalErr { number was encountered. This is most likely a bug in\ the constant evaluator".into_cow(), - InferredWrongType(ref i) => format!("inferred wrong type for {}", i).into_cow(), + TypeMismatch(ref expected, ref got) => { + format!("mismatched types: expected `{}`, found `{}`", + expected, got.description()).into_cow() + }, BadType(ref i) => format!("value of wrong type: {:?}", i).into_cow(), } } @@ -745,7 +749,7 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &TyCtxt<'tcx>, let val = match eval_const_expr_partial(tcx, &base, base_hint, fn_args) { Ok(val) => val, - Err(ConstEvalErr { kind: InferredWrongType(val), .. }) => { + Err(ConstEvalErr { kind: TypeMismatch(_, val), .. }) => { // Something like `5i8 as usize` doesn't need a type hint for the base // instead take the type hint from the inner value let hint = match val.int_type() { @@ -1085,8 +1089,8 @@ fn infer<'tcx>( (&ty::TyUint(_), Infer(_)) => Err(err(Math(ConstMathErr::NotInRange))), (&ty::TyUint(_), InferSigned(_)) => Err(err(IntermediateUnsignedNegative)), - (&ty::TyInt(_), i) | - (&ty::TyUint(_), i) => Err(err(InferredWrongType(i))), + (&ty::TyInt(ity), i) => Err(err(TypeMismatch(ity.to_string(), i))), + (&ty::TyUint(ity), i) => Err(err(TypeMismatch(ity.to_string(), i))), (&ty::TyEnum(ref adt, _), i) => { let hints = tcx.lookup_repr_hints(adt.did); diff --git a/src/test/compile-fail/const-eval-overflow-3.rs b/src/test/compile-fail/const-eval-overflow-3.rs index 33fa4aae611..c90ae045f96 100644 --- a/src/test/compile-fail/const-eval-overflow-3.rs +++ b/src/test/compile-fail/const-eval-overflow-3.rs @@ -17,7 +17,7 @@ // self-hosted and a cross-compiled setup; therefore resorting to // error-pattern for now. -// error-pattern: expected constant integer for repeat count, but tried to add two integrals of +// error-pattern: expected constant integer for repeat count, but attempted to add with overflow #![allow(unused_imports)] diff --git a/src/test/compile-fail/const-eval-overflow-4b.rs b/src/test/compile-fail/const-eval-overflow-4b.rs index 68ef1b47751..5aa93cf6383 100644 --- a/src/test/compile-fail/const-eval-overflow-4b.rs +++ b/src/test/compile-fail/const-eval-overflow-4b.rs @@ -21,7 +21,9 @@ use std::{u8, u16, u32, u64, usize}; const A_I8_T : [u32; (i8::MAX as i8 + 1u8) as usize] - //~^ ERROR tried to add two integrals of different types [E0250] + //~^ ERROR mismatched types: + //~| expected `i8`, + //~| found `u8` [E0250] = [0; (i8::MAX as usize) + 1]; fn main() { diff --git a/src/test/compile-fail/issue-8761.rs b/src/test/compile-fail/issue-8761.rs index da8bd1dc28d..1c98abce030 100644 --- a/src/test/compile-fail/issue-8761.rs +++ b/src/test/compile-fail/issue-8761.rs @@ -10,9 +10,13 @@ enum Foo { A = 1i64, - //~^ ERROR mismatched types: expected `isize` got `i64` + //~^ ERROR mismatched types: + //~| expected `isize`, + //~| found `i64` [E0080] B = 2u8 - //~^ ERROR mismatched types: expected `isize` got `u8` + //~^ ERROR mismatched types: + //~| expected `isize`, + //~| found `u8` [E0080] } fn main() {} diff --git a/src/test/compile-fail/repeat_count.rs b/src/test/compile-fail/repeat_count.rs index c0913be1354..10b722946a8 100644 --- a/src/test/compile-fail/repeat_count.rs +++ b/src/test/compile-fail/repeat_count.rs @@ -43,13 +43,17 @@ fn main() { let f = [0; -4_isize]; //~^ ERROR mismatched types //~| expected `usize` - //~| found `isize` - //~| ERROR expected positive integer for repeat count, found isize [E0306] + //~| found `isize` [E0308] + //~| ERROR mismatched types: + //~| expected `usize`, + //~| found `isize` [E0307] let f = [0_usize; -1_isize]; //~^ ERROR mismatched types //~| expected `usize` - //~| found `isize` - //~| ERROR expected positive integer for repeat count, found isize [E0306] + //~| found `isize` [E0308] + //~| ERROR mismatched types + //~| expected `usize` + //~| found `isize` [E0307] struct G { g: (), }