Skip to content

Commit bf51eaf

Browse files
committed
check for wrong const_err warnings
1 parent 12c4a19 commit bf51eaf

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

src/librustc_const_eval/eval.rs

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

413414
impl From<ConstMathErr> for ErrKind {
@@ -468,6 +469,7 @@ impl ConstEvalErr {
468469
},
469470
BadType(ref i) => format!("value of wrong type: {:?}", i).into_cow(),
470471
ErroneousReferencedConstant(_) => "could not evaluate referenced constant".into_cow(),
472+
BadCharValue => "invalid numeric value for char".into_cow(),
471473
}
472474
}
473475
}
@@ -1075,23 +1077,19 @@ fn cast_const_int<'tcx>(tcx: &TyCtxt<'tcx>, val: ConstInt, ty: ty::Ty) -> CastRe
10751077
Err(_) => Ok(Integral(Usize(ConstUsize::Us32(v as u32)))),
10761078
}
10771079
},
1078-
ty::TyFloat(ast::FloatTy::F64) if val.is_negative() => {
1079-
// FIXME: this could probably be prettier
1080-
// there's no easy way to turn an `Infer` into a f64
1081-
let val = (-val).map_err(Math)?;
1082-
let val = val.to_u64().unwrap() as f64;
1083-
let val = -val;
1084-
Ok(Float(val))
1080+
ty::TyFloat(ast::FloatTy::F64) => match val.erase_type() {
1081+
Infer(u) => Ok(Float(u as f64)),
1082+
InferSigned(i) => Ok(Float(i as f64)),
1083+
_ => unreachable!(),
10851084
},
1086-
ty::TyFloat(ast::FloatTy::F64) => Ok(Float(val.to_u64().unwrap() as f64)),
1087-
ty::TyFloat(ast::FloatTy::F32) if val.is_negative() => {
1088-
let val = (-val).map_err(Math)?;
1089-
let val = val.to_u64().unwrap() as f32;
1090-
let val = -val;
1091-
Ok(Float(val as f64))
1085+
ty::TyFloat(ast::FloatTy::F32) => match val.erase_type() {
1086+
Infer(u) => Ok(Float(u as f32 as f64)),
1087+
InferSigned(i) => Ok(Float(i as f32 as f64)),
1088+
_ => unreachable!(),
10921089
},
1093-
ty::TyFloat(ast::FloatTy::F32) => Ok(Float(val.to_u64().unwrap() as f32 as f64)),
10941090
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),
10951093
_ => Err(CannotCast),
10961094
}
10971095
}

src/test/run-pass/const-err.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// check for const_err regressions
12+
13+
#![deny(const_err)]
14+
15+
16+
fn main() {
17+
let _ = ((-1 as i8) << 8 - 1) as f32;
18+
let _ = 0u8 as char;
19+
}

0 commit comments

Comments
 (0)