TypeMismatch(String, ConstInt),
BadType(ConstVal),
ErroneousReferencedConstant(Box<ConstEvalErr>),
+ BadCharValue,
}
impl From<ConstMathErr> for ErrKind {
},
BadType(ref i) => format!("value of wrong type: {:?}", i).into_cow(),
ErroneousReferencedConstant(_) => "could not evaluate referenced constant".into_cow(),
+ BadCharValue => "invalid numeric value for char".into_cow(),
}
}
}
Err(_) => Ok(Integral(Usize(ConstUsize::Us32(v as u32)))),
}
},
- ty::TyFloat(ast::FloatTy::F64) if val.is_negative() => {
- // FIXME: this could probably be prettier
- // there's no easy way to turn an `Infer` into a f64
- let val = (-val).map_err(Math)?;
- let val = val.to_u64().unwrap() as f64;
- let val = -val;
- Ok(Float(val))
+ ty::TyFloat(ast::FloatTy::F64) => match val.erase_type() {
+ Infer(u) => Ok(Float(u as f64)),
+ InferSigned(i) => Ok(Float(i as f64)),
+ _ => unreachable!(),
},
- ty::TyFloat(ast::FloatTy::F64) => Ok(Float(val.to_u64().unwrap() as f64)),
- ty::TyFloat(ast::FloatTy::F32) if val.is_negative() => {
- let val = (-val).map_err(Math)?;
- let val = val.to_u64().unwrap() as f32;
- let val = -val;
- Ok(Float(val as f64))
+ ty::TyFloat(ast::FloatTy::F32) => match val.erase_type() {
+ Infer(u) => Ok(Float(u as f32 as f64)),
+ InferSigned(i) => Ok(Float(i as f32 as f64)),
+ _ => unreachable!(),
},
- ty::TyFloat(ast::FloatTy::F32) => Ok(Float(val.to_u64().unwrap() as f32 as f64)),
ty::TyRawPtr(_) => Err(ErrKind::UnimplementedConstVal("casting an address to a raw ptr")),
+ ty::TyChar if v as u32 as u64 == v => ::std::char::from_u32(v as u32).map(Char)
+ .ok_or(BadCharValue),
_ => Err(CannotCast),
}
}
--- /dev/null
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// check for const_err regressions
+
+#![deny(const_err)]
+
+
+fn main() {
+ let _ = ((-1 as i8) << 8 - 1) as f32;
+ let _ = 0u8 as char;
+}