Skip to content

Commit da4eb11

Browse files
committed
---
yaml --- r: 273121 b: refs/heads/beta c: 54b15c7 h: refs/heads/master i: 273119: 574c651
1 parent 1035afe commit da4eb11

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ refs/tags/0.9: 36870b185fc5f5486636d4515f0e22677493f225
2323
refs/tags/0.10: ac33f2b15782272ae348dbd7b14b8257b2148b5a
2424
refs/tags/0.11.0: e1247cb1d0d681be034adb4b558b5a0c0d5720f9
2525
refs/tags/0.12.0: f0c419429ef30723ceaf6b42f9b5a2aeb5d2e2d1
26-
refs/heads/beta: 6ee60037f9cee27d9de70195d7e63d4946465a2f
26+
refs/heads/beta: 54b15c7160e165621f0a7ae6a71db30dded254df
2727
refs/tags/1.0.0-alpha: e42bd6d93a1d3433c486200587f8f9e12590a4d7
2828
refs/heads/tmp: e06d2ad9fcd5027bcaac5b08fc9aa39a49d0ecd3
2929
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f

branches/beta/src/librustc/middle/const_eval.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,21 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &TyCtxt<'tcx>,
743743
}
744744
};
745745

746-
let val = try!(eval_const_expr_partial(tcx, &base, base_hint, fn_args));
746+
let val = match eval_const_expr_partial(tcx, &base, base_hint, fn_args) {
747+
Ok(val) => val,
748+
Err(ConstEvalErr { kind: InferredWrongType(val), .. }) => {
749+
// Something like `5i8 as usize` doesn't need a type hint for the base
750+
// instead take the type hint from the inner value
751+
let hint = match val.int_type() {
752+
Some(IntType::UnsignedInt(ty)) => ty_hint.checked_or(tcx.mk_mach_uint(ty)),
753+
Some(IntType::SignedInt(ty)) => ty_hint.checked_or(tcx.mk_mach_int(ty)),
754+
// we had a type hint, so we can't have an unknown type
755+
None => unreachable!(),
756+
};
757+
try!(eval_const_expr_partial(tcx, &base, hint, fn_args))
758+
},
759+
Err(e) => return Err(e),
760+
};
747761
match cast_const(tcx, val, ety) {
748762
Ok(val) => val,
749763
Err(kind) => return Err(ConstEvalErr { span: e.span, kind: kind }),

branches/beta/src/librustc_const_eval/int.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
// except according to those terms.
1010

1111
use std::cmp::Ordering;
12+
use syntax::attr::IntType;
13+
use syntax::ast::{IntTy, UintTy};
1214

1315
use super::is::*;
1416
use super::us::*;
@@ -258,6 +260,22 @@ impl ConstInt {
258260
ConstInt::Infer(_) | ConstInt::InferSigned(_) => panic!("no type info for const int"),
259261
}
260262
}
263+
264+
pub fn int_type(self) -> Option<IntType> {
265+
match self {
266+
ConstInt::I8(_) => Some(IntType::SignedInt(IntTy::I8)),
267+
ConstInt::I16(_) => Some(IntType::SignedInt(IntTy::I16)),
268+
ConstInt::I32(_) => Some(IntType::SignedInt(IntTy::I32)),
269+
ConstInt::I64(_) => Some(IntType::SignedInt(IntTy::I64)),
270+
ConstInt::Isize(_) => Some(IntType::SignedInt(IntTy::Is)),
271+
ConstInt::U8(_) => Some(IntType::UnsignedInt(UintTy::U8)),
272+
ConstInt::U16(_) => Some(IntType::UnsignedInt(UintTy::U16)),
273+
ConstInt::U32(_) => Some(IntType::UnsignedInt(UintTy::U32)),
274+
ConstInt::U64(_) => Some(IntType::UnsignedInt(UintTy::U64)),
275+
ConstInt::Usize(_) => Some(IntType::UnsignedInt(UintTy::Us)),
276+
_ => None,
277+
}
278+
}
261279
}
262280

263281
impl ::std::cmp::PartialOrd for ConstInt {

0 commit comments

Comments
 (0)