Skip to content

Commit

Permalink
Auto merge of #68505 - skinny121:canonicalize-const-eval-inputs, r=ni…
Browse files Browse the repository at this point in the history
…komatsakis

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
  • Loading branch information
bors committed Feb 28, 2020
2 parents 7497d93 + ebfa2f4 commit bfc32dd
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 62 deletions.
36 changes: 17 additions & 19 deletions src/librustc/mir/interpret/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ impl<'tcx> TyCtxt<'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
// 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.
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).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.
Expand All @@ -41,11 +41,8 @@ impl<'tcx> TyCtxt<'tcx> {
) -> ConstEvalResult<'tcx> {
let instance = ty::Instance::resolve(self, param_env, def_id, substs);
if let Some(instance) = instance {
if let Some(promoted) = promoted {
self.const_eval_promoted(param_env, instance, promoted)
} else {
self.const_eval_instance(param_env, instance, span)
}
let cid = GlobalId { instance, promoted };
self.const_eval_global_id(param_env, cid, span)
} else {
Err(ErrorHandled::TooGeneric)
}
Expand All @@ -57,22 +54,23 @@ impl<'tcx> TyCtxt<'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))
}
self.const_eval_global_id(param_env, GlobalId { instance, promoted: None }, span)
}

/// Evaluate a promoted constant.
pub fn const_eval_promoted(
/// Evaluate a constant.
pub fn const_eval_global_id(
self,
param_env: ty::ParamEnv<'tcx>,
instance: ty::Instance<'tcx>,
promoted: mir::Promoted,
cid: GlobalId<'tcx>,
span: Option<Span>,
) -> ConstEvalResult<'tcx> {
let cid = GlobalId { instance, promoted: Some(promoted) };
self.const_eval_validated(param_env.and(cid))
// Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should
// improve caching of queries.
let inputs = self.erase_regions(&param_env.and(cid));
if let Some(span) = span {
self.at(span).const_eval_validated(inputs)
} else {
self.const_eval_validated(inputs)
}
}
}
2 changes: 1 addition & 1 deletion src/librustc/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ rustc_queries! {
/// 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`,
/// `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>>)
-> ConstEvalResult<'tcx> {
no_force
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2484,8 +2484,8 @@ impl<'tcx> Const<'tcx> {
// HACK(eddyb) when substs contain e.g. inference variables,
// attempt using identity substs instead, that will succeed
// when the expression doesn't depend on any parameters.
// FIXME(eddyb) make `const_eval` a canonical query instead,
// that would properly handle inference variables in `substs`.
// FIXME(eddyb, skinny121) pass `InferCtxt` into here when it's available, so that
// we can call `infcx.const_eval_resolve` which handles inference variables.
if substs.has_local_value() {
let identity_substs = InternalSubsts::identity_for_item(tcx, did);
// The `ParamEnv` needs to match the `identity_substs`.
Expand Down
32 changes: 32 additions & 0 deletions src/librustc_infer/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use rustc::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind, ToTy
use rustc::middle::free_region::RegionRelations;
use rustc::middle::lang_items;
use rustc::middle::region;
use rustc::mir;
use rustc::mir::interpret::ConstEvalResult;
use rustc::session::config::BorrowckMode;
use rustc::ty::error::{ExpectedFound, TypeError, UnconstrainedNumeric};
use rustc::ty::fold::{TypeFoldable, TypeFolder};
Expand Down Expand Up @@ -63,6 +65,7 @@ pub mod resolve;
mod sub;
pub mod type_variable;

use crate::infer::canonical::OriginalQueryValues;
pub use rustc::infer::unify_key;

#[must_use]
Expand Down Expand Up @@ -1563,6 +1566,35 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
self.universe.set(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> {
Expand Down
30 changes: 9 additions & 21 deletions src/librustc_infer/traits/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,27 +510,15 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
}

ty::Predicate::ConstEvaluatable(def_id, substs) => {
if obligation.param_env.has_local_value() {
ProcessResult::Unchanged
} else {
if !substs.has_local_value() {
match self.selcx.tcx().const_eval_resolve(
obligation.param_env,
def_id,
substs,
None,
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
}
match self.selcx.infcx().const_eval_resolve(
obligation.param_env,
def_id,
substs,
None,
Some(obligation.cause.span),
) {
Ok(_) => ProcessResult::Changed(vec![]),
Err(err) => ProcessResult::Error(CodeSelectionError(ConstEvalFailure(err))),
}
}
}
Expand Down
23 changes: 9 additions & 14 deletions src/librustc_infer/traits/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,20 +532,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
}

ty::Predicate::ConstEvaluatable(def_id, substs) => {
if !(obligation.param_env, substs).has_local_value() {
match self.tcx().const_eval_resolve(
obligation.param_env,
def_id,
substs,
None,
None,
) {
Ok(_) => Ok(EvaluatedToOk),
Err(_) => Ok(EvaluatedToErr),
}
} else {
// Inference variables still left in param_env or substs.
Ok(EvaluatedToAmbig)
match self.tcx().const_eval_resolve(
obligation.param_env,
def_id,
substs,
None,
None,
) {
Ok(_) => Ok(EvaluatedToOk),
Err(_) => Ok(EvaluatedToErr),
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_mir/interpret/eval_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,11 +768,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
} else {
self.param_env
};
let val = if let Some(promoted) = gid.promoted {
self.tcx.const_eval_promoted(param_env, gid.instance, promoted)?
} else {
self.tcx.const_eval_instance(param_env, gid.instance, Some(self.tcx.span))?
};
let val = self.tcx.const_eval_global_id(param_env, gid, Some(self.tcx.span))?;

// 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
Expand Down
23 changes: 23 additions & 0 deletions src/test/incremental/const-generics/issue-68477.rs
Original file line number Diff line number Diff line change
@@ -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());
}

0 comments on commit bfc32dd

Please sign in to comment.