-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>), | ||
CharCast(ConstInt), | ||
} | ||
|
||
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,9 @@ impl ConstEvalErr { | |
}, | ||
BadType(ref i) => format!("value of wrong type: {:?}", i).into_cow(), | ||
ErroneousReferencedConstant(_) => "could not evaluate referenced constant".into_cow(), | ||
CharCast(ref got) => { | ||
format!("only `u8` can be cast as `char`, not `{}`", got.description()).into_cow() | ||
}, | ||
} | ||
} | ||
} | ||
|
@@ -824,7 +814,10 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &TyCtxt<'tcx>, | |
debug!("const call({:?})", call_args); | ||
eval_const_expr_partial(tcx, &result, ty_hint, Some(&call_args))? | ||
}, | ||
hir::ExprLit(ref lit) => lit_to_const(&lit.node, tcx, ety, lit.span)?, | ||
hir::ExprLit(ref lit) => match lit_to_const(&lit.node, tcx, ety, lit.span) { | ||
Ok(val) => val, | ||
Err(err) => signal!(e, err), | ||
}, | ||
hir::ExprBlock(ref block) => { | ||
match block.expr { | ||
Some(ref expr) => eval_const_expr_partial(tcx, &expr, ty_hint, fn_args)?, | ||
|
@@ -930,7 +923,10 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &TyCtxt<'tcx>, | |
}; | ||
|
||
match (ety.map(|t| &t.sty), result) { | ||
(Some(ref ty_hint), Integral(i)) => Ok(Integral(infer(i, tcx, ty_hint, e.span)?)), | ||
(Some(ref ty_hint), Integral(i)) => match infer(i, tcx, ty_hint) { | ||
Ok(inferred) => Ok(Integral(inferred)), | ||
Err(err) => signal!(e, err), | ||
}, | ||
(_, result) => Ok(result), | ||
} | ||
} | ||
|
@@ -939,15 +935,9 @@ fn infer<'tcx>( | |
i: ConstInt, | ||
tcx: &TyCtxt<'tcx>, | ||
ty_hint: &ty::TypeVariants<'tcx>, | ||
span: Span | ||
) -> Result<ConstInt, ConstEvalErr> { | ||
) -> Result<ConstInt, ErrKind> { | ||
use syntax::ast::*; | ||
|
||
let err = |e| ConstEvalErr { | ||
span: span, | ||
kind: e, | ||
}; | ||
|
||
match (ty_hint, i) { | ||
(&ty::TyInt(IntTy::I8), result @ I8(_)) => Ok(result), | ||
(&ty::TyInt(IntTy::I16), result @ I16(_)) => Ok(result), | ||
|
@@ -993,17 +983,17 @@ fn infer<'tcx>( | |
Err(_) => Ok(Usize(ConstUsize::Us32(i as u32))), | ||
} | ||
}, | ||
(&ty::TyUint(_), InferSigned(_)) => Err(err(IntermediateUnsignedNegative)), | ||
(&ty::TyUint(_), InferSigned(_)) => Err(IntermediateUnsignedNegative), | ||
|
||
(&ty::TyInt(ity), i) => Err(err(TypeMismatch(ity.to_string(), i))), | ||
(&ty::TyUint(ity), i) => Err(err(TypeMismatch(ity.to_string(), i))), | ||
(&ty::TyInt(ity), i) => Err(TypeMismatch(ity.to_string(), i)), | ||
(&ty::TyUint(ity), i) => Err(TypeMismatch(ity.to_string(), i)), | ||
|
||
(&ty::TyEnum(ref adt, _), i) => { | ||
let hints = tcx.lookup_repr_hints(adt.did); | ||
let int_ty = tcx.enum_repr_type(hints.iter().next()); | ||
infer(i, tcx, &int_ty.to_ty(tcx).sty, span) | ||
infer(i, tcx, &int_ty.to_ty(tcx).sty) | ||
}, | ||
(_, i) => Err(err(BadType(ConstVal::Integral(i)))), | ||
(_, i) => Err(BadType(ConstVal::Integral(i))), | ||
} | ||
} | ||
|
||
|
@@ -1089,23 +1079,22 @@ 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)), | ||
_ => bug!("ConstInt::erase_type returned something other than Infer/InferSigned"), | ||
}, | ||
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 |
||
_ => bug!("ConstInt::erase_type returned something other than Infer/InferSigned"), | ||
}, | ||
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 => match infer(val, tcx, &ty::TyUint(ast::UintTy::U8)) { | ||
Ok(U8(u)) => Ok(Char(u as char)), | ||
// can only occur before typeck, typeck blocks `T as char` for `T` != `u8` | ||
_ => Err(CharCast(val)), | ||
}, | ||
_ => Err(CannotCast), | ||
} | ||
} | ||
|
@@ -1136,36 +1125,36 @@ fn lit_to_const<'tcx>(lit: &ast::LitKind, | |
tcx: &TyCtxt<'tcx>, | ||
ty_hint: Option<Ty<'tcx>>, | ||
span: Span, | ||
) -> Result<ConstVal, ConstEvalErr> { | ||
) -> Result<ConstVal, ErrKind> { | ||
use syntax::ast::*; | ||
use syntax::ast::LitIntType::*; | ||
match *lit { | ||
LitKind::Str(ref s, _) => Ok(Str((*s).clone())), | ||
LitKind::ByteStr(ref data) => Ok(ByteStr(data.clone())), | ||
LitKind::Byte(n) => Ok(Integral(U8(n))), | ||
LitKind::Int(n, Signed(ity)) => { | ||
infer(InferSigned(n as i64), tcx, &ty::TyInt(ity), span).map(Integral) | ||
infer(InferSigned(n as i64), tcx, &ty::TyInt(ity)).map(Integral) | ||
}, | ||
|
||
LitKind::Int(n, Unsuffixed) => { | ||
match ty_hint.map(|t| &t.sty) { | ||
Some(&ty::TyInt(ity)) => { | ||
infer(InferSigned(n as i64), tcx, &ty::TyInt(ity), span).map(Integral) | ||
infer(InferSigned(n as i64), tcx, &ty::TyInt(ity)).map(Integral) | ||
}, | ||
Some(&ty::TyUint(uty)) => { | ||
infer(Infer(n), tcx, &ty::TyUint(uty), span).map(Integral) | ||
infer(Infer(n), tcx, &ty::TyUint(uty)).map(Integral) | ||
}, | ||
None => Ok(Integral(Infer(n))), | ||
Some(&ty::TyEnum(ref adt, _)) => { | ||
let hints = tcx.lookup_repr_hints(adt.did); | ||
let int_ty = tcx.enum_repr_type(hints.iter().next()); | ||
infer(Infer(n), tcx, &int_ty.to_ty(tcx).sty, span).map(Integral) | ||
infer(Infer(n), tcx, &int_ty.to_ty(tcx).sty).map(Integral) | ||
}, | ||
Some(ty_hint) => bug!("bad ty_hint: {:?}, {:?}", ty_hint, lit), | ||
} | ||
}, | ||
LitKind::Int(n, Unsigned(ity)) => { | ||
infer(Infer(n), tcx, &ty::TyUint(ity), span).map(Integral) | ||
infer(Infer(n), tcx, &ty::TyUint(ity)).map(Integral) | ||
}, | ||
|
||
LitKind::Float(ref n, _) | | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,6 @@ | |
|
||
#![allow(unused_imports)] | ||
|
||
use std::fmt; | ||
use std::{i8, i16, i32, i64, isize}; | ||
use std::{u8, u16, u32, u64, usize}; | ||
|
||
|
@@ -26,10 +25,15 @@ const A_I8_T | |
//~| found `u8` [E0250] | ||
= [0; (i8::MAX as usize) + 1]; | ||
|
||
fn main() { | ||
foo(&A_I8_T[..]); | ||
} | ||
|
||
fn foo<T:fmt::Debug>(x: T) { | ||
println!("{:?}", x); | ||
} | ||
const A_CHAR_USIZE | ||
: [u32; 5u8 as char as usize] | ||
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. Yay, more const casting possibilities. |
||
= [0; 5]; | ||
|
||
|
||
const A_BAD_CHAR_USIZE | ||
: [u32; 5i8 as char as usize] | ||
//~^ ERROR only `u8` can be cast as `char`, not `i8` | ||
= [0; 5]; | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2016 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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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