-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Evaluate fixed-length array length expressions lazily. #44275
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
50076b0
rustc: intern ConstVal's in TyCtxt.
eddyb 932289c
rustc: introduce ty::Const { ConstVal, Ty }.
eddyb 3ce31eb
rustc: replace usize with u64 and ConstUsize.
eddyb 8a9b78f
rustc: use ty::Const for the length of TyArray.
eddyb 8821761
rustc: remove obsolete const_val::ErrKind::{Negate,Not}On.
eddyb 74349fa
rustc: evaluate fixed-length array length expressions lazily.
eddyb 57ebd28
rustc: use ConstVal::Unevaluated instead of mir::Literal::Item.
eddyb 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
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
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
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
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 |
---|---|---|
|
@@ -8,64 +8,66 @@ | |
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
use self::ConstVal::*; | ||
pub use rustc_const_math::ConstInt; | ||
|
||
use hir; | ||
use hir::def::Def; | ||
use hir::def_id::DefId; | ||
use traits::Reveal; | ||
use ty::{self, TyCtxt, layout}; | ||
use ty::subst::Substs; | ||
use util::common::ErrorReported; | ||
use rustc_const_math::*; | ||
|
||
use graphviz::IntoCow; | ||
use errors::DiagnosticBuilder; | ||
use serialize::{self, Encodable, Encoder, Decodable, Decoder}; | ||
use syntax::symbol::InternedString; | ||
use syntax::ast; | ||
use syntax_pos::Span; | ||
|
||
use std::borrow::Cow; | ||
use std::collections::BTreeMap; | ||
use std::rc::Rc; | ||
|
||
pub type EvalResult<'tcx> = Result<ConstVal<'tcx>, ConstEvalErr<'tcx>>; | ||
pub type EvalResult<'tcx> = Result<&'tcx ty::Const<'tcx>, ConstEvalErr<'tcx>>; | ||
|
||
#[derive(Clone, Debug, Hash, RustcEncodable, RustcDecodable, Eq, PartialEq)] | ||
#[derive(Copy, Clone, Debug, Hash, RustcEncodable, RustcDecodable, Eq, PartialEq)] | ||
pub enum ConstVal<'tcx> { | ||
Float(ConstFloat), | ||
Integral(ConstInt), | ||
Float(ConstFloat), | ||
Str(InternedString), | ||
ByteStr(Rc<Vec<u8>>), | ||
ByteStr(ByteArray<'tcx>), | ||
Bool(bool), | ||
Char(char), | ||
Variant(DefId), | ||
Function(DefId, &'tcx Substs<'tcx>), | ||
Struct(BTreeMap<ast::Name, ConstVal<'tcx>>), | ||
Tuple(Vec<ConstVal<'tcx>>), | ||
Array(Vec<ConstVal<'tcx>>), | ||
Repeat(Box<ConstVal<'tcx>>, u64), | ||
Aggregate(ConstAggregate<'tcx>), | ||
Unevaluated(DefId, &'tcx Substs<'tcx>), | ||
} | ||
|
||
impl<'tcx> ConstVal<'tcx> { | ||
pub fn description(&self) -> &'static str { | ||
match *self { | ||
Float(f) => f.description(), | ||
Integral(i) => i.description(), | ||
Str(_) => "string literal", | ||
ByteStr(_) => "byte string literal", | ||
Bool(_) => "boolean", | ||
Char(..) => "char", | ||
Variant(_) => "enum variant", | ||
Struct(_) => "struct", | ||
Tuple(_) => "tuple", | ||
Function(..) => "function definition", | ||
Array(..) => "array", | ||
Repeat(..) => "repeat", | ||
} | ||
#[derive(Copy, Clone, Debug, Hash, RustcEncodable, Eq, PartialEq)] | ||
pub struct ByteArray<'tcx> { | ||
pub data: &'tcx [u8], | ||
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. Making this a |
||
} | ||
|
||
impl<'tcx> serialize::UseSpecializedDecodable for ByteArray<'tcx> {} | ||
|
||
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] | ||
pub enum ConstAggregate<'tcx> { | ||
Struct(&'tcx [(ast::Name, &'tcx ty::Const<'tcx>)]), | ||
Tuple(&'tcx [&'tcx ty::Const<'tcx>]), | ||
Array(&'tcx [&'tcx ty::Const<'tcx>]), | ||
Repeat(&'tcx ty::Const<'tcx>, u64), | ||
} | ||
|
||
impl<'tcx> Encodable for ConstAggregate<'tcx> { | ||
fn encode<S: Encoder>(&self, _: &mut S) -> Result<(), S::Error> { | ||
bug!("should never encode ConstAggregate::{:?}", self) | ||
} | ||
} | ||
|
||
impl<'tcx> Decodable for ConstAggregate<'tcx> { | ||
fn decode<D: Decoder>(_: &mut D) -> Result<Self, D::Error> { | ||
bug!("should never decode ConstAggregate") | ||
} | ||
} | ||
|
||
impl<'tcx> ConstVal<'tcx> { | ||
pub fn to_const_int(&self) -> Option<ConstInt> { | ||
match *self { | ||
ConstVal::Integral(i) => Some(i), | ||
|
@@ -86,8 +88,6 @@ pub struct ConstEvalErr<'tcx> { | |
pub enum ErrKind<'tcx> { | ||
CannotCast, | ||
MissingStructField, | ||
NegateOn(ConstVal<'tcx>), | ||
NotOn(ConstVal<'tcx>), | ||
|
||
NonConstPath, | ||
UnimplementedConstVal(&'static str), | ||
|
@@ -146,9 +146,6 @@ impl<'a, 'gcx, 'tcx> ConstEvalErr<'tcx> { | |
|
||
match self.kind { | ||
CannotCast => simple!("can't cast this type"), | ||
NegateOn(ref const_val) => simple!("negate on {}", const_val.description()), | ||
NotOn(ref const_val) => simple!("not on {}", const_val.description()), | ||
|
||
MissingStructField => simple!("nonexistent struct field"), | ||
NonConstPath => simple!("non-constant path in constant expression"), | ||
UnimplementedConstVal(what) => | ||
|
@@ -221,37 +218,3 @@ impl<'a, 'gcx, 'tcx> ConstEvalErr<'tcx> { | |
self.struct_error(tcx, primary_span, primary_kind).emit(); | ||
} | ||
} | ||
|
||
/// Returns the value of the length-valued expression | ||
pub fn eval_length(tcx: TyCtxt, | ||
count: hir::BodyId, | ||
reason: &str) | ||
-> Result<usize, ErrorReported> | ||
{ | ||
let count_expr = &tcx.hir.body(count).value; | ||
let count_def_id = tcx.hir.body_owner_def_id(count); | ||
let param_env = ty::ParamEnv::empty(Reveal::UserFacing); | ||
let substs = Substs::identity_for_item(tcx.global_tcx(), count_def_id); | ||
match tcx.at(count_expr.span).const_eval(param_env.and((count_def_id, substs))) { | ||
Ok(Integral(Usize(count))) => { | ||
let val = count.as_u64(tcx.sess.target.uint_type); | ||
assert_eq!(val as usize as u64, val); | ||
Ok(val as usize) | ||
}, | ||
Ok(_) | | ||
Err(ConstEvalErr { kind: ErrKind::TypeckError, .. }) => Err(ErrorReported), | ||
Err(err) => { | ||
let mut diag = err.struct_error(tcx, count_expr.span, reason); | ||
|
||
if let hir::ExprPath(hir::QPath::Resolved(None, ref path)) = count_expr.node { | ||
if let Def::Local(..) = path.def { | ||
diag.note(&format!("`{}` is a variable", | ||
tcx.hir.node_to_pretty_string(count_expr.id))); | ||
} | ||
} | ||
|
||
diag.emit(); | ||
Err(ErrorReported) | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.
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.
Idle speculation: I wonder if we should call this
requires_normalization
, at some point.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.
needs_normalizing
?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.
also fine