-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #67532 - Centril:rollup-3duj42d, r=Centril
Rollup of 8 pull requests Successful merges: - #66877 (Add simpler entry points to const eval for common usages.) - #67299 (Add `ImmTy::try_from_(u)int` methods) - #67487 (Rustdoc mutability removal) - #67499 (Misc MIR building cleanups) - #67506 (Remove iter_private.rs) - #67508 (Fix typo in path parser name) - #67519 (Document why Any is not an unsafe trait) - #67525 (Utilize rust-lang/rust commit hashes in toolstate) Failed merges: r? @ghost
- Loading branch information
Showing
41 changed files
with
320 additions
and
428 deletions.
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use super::{ConstEvalResult, ErrorHandled, GlobalId}; | ||
|
||
use crate::mir; | ||
use crate::hir::def_id::DefId; | ||
use crate::ty::{self, TyCtxt}; | ||
use crate::ty::subst::{InternalSubsts, SubstsRef}; | ||
use syntax_pos::Span; | ||
|
||
|
||
impl<'tcx> TyCtxt<'tcx> { | ||
|
||
/// Evaluates a constant without providing any substitutions. This is useful to evaluate consts | ||
/// that can't take any generic arguments like statics, const items or enum discriminants. If a | ||
/// generic parameter is used within the constant `ErrorHandled::ToGeneric` will be returned. | ||
pub fn const_eval_poly(self, def_id: DefId) -> ConstEvalResult<'tcx> { | ||
// In some situations def_id will have substitutions within scope, but they aren't allowed | ||
// to be used. So we can't use `Instance::mono`, instead we feed unresolved substitutions | ||
// into `const_eval` which will return `ErrorHandled::ToGeneric` if any og them are | ||
// encountered. | ||
let substs = InternalSubsts::identity_for_item(self, def_id); | ||
let instance = ty::Instance::new(def_id, substs); | ||
let cid = GlobalId { | ||
instance, | ||
promoted: None, | ||
}; | ||
let param_env = self.param_env(def_id); | ||
self.const_eval_validated(param_env.and(cid)) | ||
} | ||
|
||
/// Resolves and evaluates a constant. | ||
/// | ||
/// The constant can be located on a trait like `<A as B>::C`, in which case the given | ||
/// substitutions and environment are used to resolve the constant. Alternatively if the | ||
/// constant has generic parameters in scope the substitutions are used to evaluate the value of | ||
/// the constant. For example in `fn foo<T>() { let _ = [0; bar::<T>()]; }` the repeat count | ||
/// constant `bar::<T>()` requires a substitution for `T`, if the substitution for `T` is still | ||
/// too generic for the constant to be evaluated then `Err(ErrorHandled::TooGeneric)` is | ||
/// returned. | ||
pub fn const_eval_resolve( | ||
self, | ||
param_env: ty::ParamEnv<'tcx>, | ||
def_id: DefId, | ||
substs: SubstsRef<'tcx>, | ||
span: Option<Span> | ||
) -> ConstEvalResult<'tcx> { | ||
let instance = ty::Instance::resolve( | ||
self, | ||
param_env, | ||
def_id, | ||
substs, | ||
); | ||
if let Some(instance) = instance { | ||
self.const_eval_instance(param_env, instance, span) | ||
} else { | ||
Err(ErrorHandled::TooGeneric) | ||
} | ||
} | ||
|
||
pub fn const_eval_instance( | ||
self, | ||
param_env: ty::ParamEnv<'tcx>, | ||
instance: ty::Instance<'tcx>, | ||
span: Option<Span> | ||
) -> ConstEvalResult<'tcx> { | ||
let cid = GlobalId { | ||
instance, | ||
promoted: None, | ||
}; | ||
if let Some(span) = span { | ||
self.at(span).const_eval_validated(param_env.and(cid)) | ||
} else { | ||
self.const_eval_validated(param_env.and(cid)) | ||
} | ||
} | ||
|
||
/// Evaluate a promoted constant. | ||
pub fn const_eval_promoted( | ||
self, | ||
instance: ty::Instance<'tcx>, | ||
promoted: mir::Promoted | ||
) -> ConstEvalResult<'tcx> { | ||
let cid = GlobalId { | ||
instance, | ||
promoted: Some(promoted), | ||
}; | ||
let param_env = ty::ParamEnv::reveal_all(); | ||
self.const_eval_validated(param_env.and(cid)) | ||
} | ||
} |
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
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.