Skip to content

Commit 10737a5

Browse files
committed
* as char assumes * to be of type u8
1 parent bf51eaf commit 10737a5

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/librustc_const_eval/eval.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ pub enum ErrKind {
408408
TypeMismatch(String, ConstInt),
409409
BadType(ConstVal),
410410
ErroneousReferencedConstant(Box<ConstEvalErr>),
411-
BadCharValue,
411+
CharCast(ConstInt),
412412
}
413413

414414
impl From<ConstMathErr> for ErrKind {
@@ -469,7 +469,9 @@ impl ConstEvalErr {
469469
},
470470
BadType(ref i) => format!("value of wrong type: {:?}", i).into_cow(),
471471
ErroneousReferencedConstant(_) => "could not evaluate referenced constant".into_cow(),
472-
BadCharValue => "invalid numeric value for char".into_cow(),
472+
CharCast(ref got) => {
473+
format!("only `u8` can be cast as `char`, not `{}`", got.description()).into_cow()
474+
},
473475
}
474476
}
475477
}
@@ -1080,16 +1082,19 @@ fn cast_const_int<'tcx>(tcx: &TyCtxt<'tcx>, val: ConstInt, ty: ty::Ty) -> CastRe
10801082
ty::TyFloat(ast::FloatTy::F64) => match val.erase_type() {
10811083
Infer(u) => Ok(Float(u as f64)),
10821084
InferSigned(i) => Ok(Float(i as f64)),
1083-
_ => unreachable!(),
1085+
_ => bug!("ConstInt::erase_type returned something other than Infer/InferSigned"),
10841086
},
10851087
ty::TyFloat(ast::FloatTy::F32) => match val.erase_type() {
10861088
Infer(u) => Ok(Float(u as f32 as f64)),
10871089
InferSigned(i) => Ok(Float(i as f32 as f64)),
1088-
_ => unreachable!(),
1090+
_ => bug!("ConstInt::erase_type returned something other than Infer/InferSigned"),
10891091
},
10901092
ty::TyRawPtr(_) => Err(ErrKind::UnimplementedConstVal("casting an address to a raw ptr")),
1091-
ty::TyChar if v as u32 as u64 == v => ::std::char::from_u32(v as u32).map(Char)
1092-
.ok_or(BadCharValue),
1093+
ty::TyChar => match infer(val, tcx, &ty::TyUint(ast::UintTy::U8)) {
1094+
Ok(U8(u)) => Ok(Char(u as char)),
1095+
// can only occur before typeck, typeck blocks `T as char` for `T` != `u8`
1096+
_ => Err(CharCast(val)),
1097+
},
10931098
_ => Err(CannotCast),
10941099
}
10951100
}

0 commit comments

Comments
 (0)