Auto merge of #68505 - skinny121:canonicalize-const-eval-inputs, r=nikomatsakis
Canonicalize inputs to const eval where needed Canonicalize inputs to const eval, so that they can contain inference variables. Which enables invoking const eval queries even if the current param env has inference variable within it, which can occur during trait selection. This is a reattempt of #67717, in a far less invasive way. Fixes #68477 r? @nikomatsakis cc @eddyb
This commit is contained in:
commit
bfc32dd106
8 changed files with 94 additions and 62 deletions
|
@ -13,13 +13,13 @@ impl<'tcx> TyCtxt<'tcx> {
|
||||||
pub fn const_eval_poly(self, def_id: DefId) -> ConstEvalResult<'tcx> {
|
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
|
// 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
|
// 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
|
// into `const_eval` which will return `ErrorHandled::ToGeneric` if any of them are
|
||||||
// encountered.
|
// encountered.
|
||||||
let substs = InternalSubsts::identity_for_item(self, def_id);
|
let substs = InternalSubsts::identity_for_item(self, def_id);
|
||||||
let instance = ty::Instance::new(def_id, substs);
|
let instance = ty::Instance::new(def_id, substs);
|
||||||
let cid = GlobalId { instance, promoted: None };
|
let cid = GlobalId { instance, promoted: None };
|
||||||
let param_env = self.param_env(def_id).with_reveal_all();
|
let param_env = self.param_env(def_id).with_reveal_all();
|
||||||
self.const_eval_validated(param_env.and(cid))
|
self.const_eval_global_id(param_env, cid, None)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resolves and evaluates a constant.
|
/// Resolves and evaluates a constant.
|
||||||
|
@ -41,11 +41,8 @@ impl<'tcx> TyCtxt<'tcx> {
|
||||||
) -> ConstEvalResult<'tcx> {
|
) -> ConstEvalResult<'tcx> {
|
||||||
let instance = ty::Instance::resolve(self, param_env, def_id, substs);
|
let instance = ty::Instance::resolve(self, param_env, def_id, substs);
|
||||||
if let Some(instance) = instance {
|
if let Some(instance) = instance {
|
||||||
if let Some(promoted) = promoted {
|
let cid = GlobalId { instance, promoted };
|
||||||
self.const_eval_promoted(param_env, instance, promoted)
|
self.const_eval_global_id(param_env, cid, span)
|
||||||
} else {
|
|
||||||
self.const_eval_instance(param_env, instance, span)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
Err(ErrorHandled::TooGeneric)
|
Err(ErrorHandled::TooGeneric)
|
||||||
}
|
}
|
||||||
|
@ -57,22 +54,23 @@ impl<'tcx> TyCtxt<'tcx> {
|
||||||
instance: ty::Instance<'tcx>,
|
instance: ty::Instance<'tcx>,
|
||||||
span: Option<Span>,
|
span: Option<Span>,
|
||||||
) -> ConstEvalResult<'tcx> {
|
) -> ConstEvalResult<'tcx> {
|
||||||
let cid = GlobalId { instance, promoted: None };
|
self.const_eval_global_id(param_env, GlobalId { instance, promoted: None }, span)
|
||||||
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.
|
/// Evaluate a constant.
|
||||||
pub fn const_eval_promoted(
|
pub fn const_eval_global_id(
|
||||||
self,
|
self,
|
||||||
param_env: ty::ParamEnv<'tcx>,
|
param_env: ty::ParamEnv<'tcx>,
|
||||||
instance: ty::Instance<'tcx>,
|
cid: GlobalId<'tcx>,
|
||||||
promoted: mir::Promoted,
|
span: Option<Span>,
|
||||||
) -> ConstEvalResult<'tcx> {
|
) -> ConstEvalResult<'tcx> {
|
||||||
let cid = GlobalId { instance, promoted: Some(promoted) };
|
// Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should
|
||||||
self.const_eval_validated(param_env.and(cid))
|
// improve caching of queries.
|
||||||
|
let inputs = self.erase_regions(¶m_env.and(cid));
|
||||||
|
if let Some(span) = span {
|
||||||
|
self.at(span).const_eval_validated(inputs)
|
||||||
|
} else {
|
||||||
|
self.const_eval_validated(inputs)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -513,7 +513,7 @@ rustc_queries! {
|
||||||
/// returns a proper constant that is usable by the rest of the compiler.
|
/// returns a proper constant that is usable by the rest of the compiler.
|
||||||
///
|
///
|
||||||
/// **Do not use this** directly, use one of the following wrappers: `tcx.const_eval_poly`,
|
/// **Do not use this** directly, use one of the following wrappers: `tcx.const_eval_poly`,
|
||||||
/// `tcx.const_eval_resolve`, `tcx.const_eval_instance`, or `tcx.const_eval_promoted`.
|
/// `tcx.const_eval_resolve`, `tcx.const_eval_instance`, or `tcx.const_eval_global_id`.
|
||||||
query const_eval_validated(key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>)
|
query const_eval_validated(key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>)
|
||||||
-> ConstEvalResult<'tcx> {
|
-> ConstEvalResult<'tcx> {
|
||||||
no_force
|
no_force
|
||||||
|
|
|
@ -2484,8 +2484,8 @@ impl<'tcx> Const<'tcx> {
|
||||||
// HACK(eddyb) when substs contain e.g. inference variables,
|
// HACK(eddyb) when substs contain e.g. inference variables,
|
||||||
// attempt using identity substs instead, that will succeed
|
// attempt using identity substs instead, that will succeed
|
||||||
// when the expression doesn't depend on any parameters.
|
// when the expression doesn't depend on any parameters.
|
||||||
// FIXME(eddyb) make `const_eval` a canonical query instead,
|
// FIXME(eddyb, skinny121) pass `InferCtxt` into here when it's available, so that
|
||||||
// that would properly handle inference variables in `substs`.
|
// we can call `infcx.const_eval_resolve` which handles inference variables.
|
||||||
if substs.has_local_value() {
|
if substs.has_local_value() {
|
||||||
let identity_substs = InternalSubsts::identity_for_item(tcx, did);
|
let identity_substs = InternalSubsts::identity_for_item(tcx, did);
|
||||||
// The `ParamEnv` needs to match the `identity_substs`.
|
// The `ParamEnv` needs to match the `identity_substs`.
|
||||||
|
|
|
@ -15,6 +15,8 @@ use rustc::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind, ToTy
|
||||||
use rustc::middle::free_region::RegionRelations;
|
use rustc::middle::free_region::RegionRelations;
|
||||||
use rustc::middle::lang_items;
|
use rustc::middle::lang_items;
|
||||||
use rustc::middle::region;
|
use rustc::middle::region;
|
||||||
|
use rustc::mir;
|
||||||
|
use rustc::mir::interpret::ConstEvalResult;
|
||||||
use rustc::session::config::BorrowckMode;
|
use rustc::session::config::BorrowckMode;
|
||||||
use rustc::ty::error::{ExpectedFound, TypeError, UnconstrainedNumeric};
|
use rustc::ty::error::{ExpectedFound, TypeError, UnconstrainedNumeric};
|
||||||
use rustc::ty::fold::{TypeFoldable, TypeFolder};
|
use rustc::ty::fold::{TypeFoldable, TypeFolder};
|
||||||
|
@ -63,6 +65,7 @@ pub mod resolve;
|
||||||
mod sub;
|
mod sub;
|
||||||
pub mod type_variable;
|
pub mod type_variable;
|
||||||
|
|
||||||
|
use crate::infer::canonical::OriginalQueryValues;
|
||||||
pub use rustc::infer::unify_key;
|
pub use rustc::infer::unify_key;
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
@ -1563,6 +1566,35 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
||||||
self.universe.set(u);
|
self.universe.set(u);
|
||||||
u
|
u
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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.
|
||||||
|
///
|
||||||
|
/// This handles inferences variables within both `param_env` and `substs` by
|
||||||
|
/// performing the operation on their respective canonical forms.
|
||||||
|
pub fn const_eval_resolve(
|
||||||
|
&self,
|
||||||
|
param_env: ty::ParamEnv<'tcx>,
|
||||||
|
def_id: DefId,
|
||||||
|
substs: SubstsRef<'tcx>,
|
||||||
|
promoted: Option<mir::Promoted>,
|
||||||
|
span: Option<Span>,
|
||||||
|
) -> ConstEvalResult<'tcx> {
|
||||||
|
let mut original_values = OriginalQueryValues::default();
|
||||||
|
let canonical = self.canonicalize_query(&(param_env, substs), &mut original_values);
|
||||||
|
|
||||||
|
let (param_env, substs) = canonical.value;
|
||||||
|
// The return value is the evaluated value which doesn't contain any reference to inference
|
||||||
|
// variables, thus we don't need to substitute back the original values.
|
||||||
|
self.tcx.const_eval_resolve(param_env, def_id, substs, promoted, span)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ShallowResolver<'a, 'tcx> {
|
pub struct ShallowResolver<'a, 'tcx> {
|
||||||
|
|
|
@ -510,27 +510,15 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
ty::Predicate::ConstEvaluatable(def_id, substs) => {
|
ty::Predicate::ConstEvaluatable(def_id, substs) => {
|
||||||
if obligation.param_env.has_local_value() {
|
match self.selcx.infcx().const_eval_resolve(
|
||||||
ProcessResult::Unchanged
|
obligation.param_env,
|
||||||
} else {
|
def_id,
|
||||||
if !substs.has_local_value() {
|
substs,
|
||||||
match self.selcx.tcx().const_eval_resolve(
|
None,
|
||||||
obligation.param_env,
|
Some(obligation.cause.span),
|
||||||
def_id,
|
) {
|
||||||
substs,
|
Ok(_) => ProcessResult::Changed(vec![]),
|
||||||
None,
|
Err(err) => ProcessResult::Error(CodeSelectionError(ConstEvalFailure(err))),
|
||||||
Some(obligation.cause.span),
|
|
||||||
) {
|
|
||||||
Ok(_) => ProcessResult::Changed(vec![]),
|
|
||||||
Err(err) => {
|
|
||||||
ProcessResult::Error(CodeSelectionError(ConstEvalFailure(err)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
pending_obligation.stalled_on =
|
|
||||||
substs.types().map(|ty| infer_ty(ty)).collect();
|
|
||||||
ProcessResult::Unchanged
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -532,20 +532,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
ty::Predicate::ConstEvaluatable(def_id, substs) => {
|
ty::Predicate::ConstEvaluatable(def_id, substs) => {
|
||||||
if !(obligation.param_env, substs).has_local_value() {
|
match self.tcx().const_eval_resolve(
|
||||||
match self.tcx().const_eval_resolve(
|
obligation.param_env,
|
||||||
obligation.param_env,
|
def_id,
|
||||||
def_id,
|
substs,
|
||||||
substs,
|
None,
|
||||||
None,
|
None,
|
||||||
None,
|
) {
|
||||||
) {
|
Ok(_) => Ok(EvaluatedToOk),
|
||||||
Ok(_) => Ok(EvaluatedToOk),
|
Err(_) => Ok(EvaluatedToErr),
|
||||||
Err(_) => Ok(EvaluatedToErr),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Inference variables still left in param_env or substs.
|
|
||||||
Ok(EvaluatedToAmbig)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -768,11 +768,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
||||||
} else {
|
} else {
|
||||||
self.param_env
|
self.param_env
|
||||||
};
|
};
|
||||||
let val = if let Some(promoted) = gid.promoted {
|
let val = self.tcx.const_eval_global_id(param_env, gid, Some(self.tcx.span))?;
|
||||||
self.tcx.const_eval_promoted(param_env, gid.instance, promoted)?
|
|
||||||
} else {
|
|
||||||
self.tcx.const_eval_instance(param_env, gid.instance, Some(self.tcx.span))?
|
|
||||||
};
|
|
||||||
|
|
||||||
// Even though `ecx.const_eval` is called from `eval_const_to_op` we can never have a
|
// Even though `ecx.const_eval` is called from `eval_const_to_op` we can never have a
|
||||||
// recursion deeper than one level, because the `tcx.const_eval` above is guaranteed to not
|
// recursion deeper than one level, because the `tcx.const_eval` above is guaranteed to not
|
||||||
|
|
23
src/test/incremental/const-generics/issue-68477.rs
Normal file
23
src/test/incremental/const-generics/issue-68477.rs
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
// edition:2018
|
||||||
|
// revisions:rpass1
|
||||||
|
#![feature(const_generics)]
|
||||||
|
|
||||||
|
const FOO: usize = 1;
|
||||||
|
|
||||||
|
struct Container<T> {
|
||||||
|
val: std::marker::PhantomData<T>,
|
||||||
|
blah: [(); FOO]
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn dummy() {}
|
||||||
|
|
||||||
|
async fn foo() {
|
||||||
|
let a: Container<&'static ()>;
|
||||||
|
dummy().await;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_send<T: Send>(_: T) {}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
is_send(foo());
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue