-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix various const eval errors #33339
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -377,13 +377,6 @@ pub enum ErrKind { | |
NotOn(ConstVal), | ||
CallOn(ConstVal), | ||
|
||
NegateWithOverflow(i64), | ||
AddiWithOverflow(i64, i64), | ||
SubiWithOverflow(i64, i64), | ||
MuliWithOverflow(i64, i64), | ||
AdduWithOverflow(u64, u64), | ||
SubuWithOverflow(u64, u64), | ||
MuluWithOverflow(u64, u64), | ||
DivideByZero, | ||
DivideWithOverflow, | ||
ModuloByZero, | ||
|
@@ -415,6 +408,7 @@ pub enum ErrKind { | |
TypeMismatch(String, ConstInt), | ||
BadType(ConstVal), | ||
ErroneousReferencedConstant(Box<ConstEvalErr>), | ||
BadCharValue, | ||
} | ||
|
||
impl From<ConstMathErr> for ErrKind { | ||
|
@@ -439,13 +433,6 @@ impl ConstEvalErr { | |
NotOn(ref const_val) => format!("not on {}", const_val.description()).into_cow(), | ||
CallOn(ref const_val) => format!("call on {}", const_val.description()).into_cow(), | ||
|
||
NegateWithOverflow(..) => "attempted to negate with overflow".into_cow(), | ||
AddiWithOverflow(..) => "attempted to add with overflow".into_cow(), | ||
SubiWithOverflow(..) => "attempted to sub with overflow".into_cow(), | ||
MuliWithOverflow(..) => "attempted to mul with overflow".into_cow(), | ||
AdduWithOverflow(..) => "attempted to add with overflow".into_cow(), | ||
SubuWithOverflow(..) => "attempted to sub with overflow".into_cow(), | ||
MuluWithOverflow(..) => "attempted to mul with overflow".into_cow(), | ||
DivideByZero => "attempted to divide by zero".into_cow(), | ||
DivideWithOverflow => "attempted to divide with overflow".into_cow(), | ||
ModuloByZero => "attempted remainder with a divisor of zero".into_cow(), | ||
|
@@ -482,6 +469,7 @@ impl ConstEvalErr { | |
}, | ||
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(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add tests for this warning? Probably |
||
} | ||
} | ||
} | ||
|
@@ -1089,23 +1077,19 @@ fn cast_const_int<'tcx>(tcx: &TyCtxt<'tcx>, val: ConstInt, ty: ty::Ty) -> CastRe | |
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!(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I think there's a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or |
||
}, | ||
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)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This reminds me, we need 2 variants, one for |
||
_ => 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), | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: year |
||
// 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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are gone because they were unused?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea, just a drive-by-cleanup