Skip to content

Commit bfc32dd

Browse files
committed
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
2 parents 7497d93 + ebfa2f4 commit bfc32dd

File tree

8 files changed

+94
-62
lines changed

8 files changed

+94
-62
lines changed

src/librustc/mir/interpret/queries.rs

+17-19
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ impl<'tcx> TyCtxt<'tcx> {
1313
pub fn const_eval_poly(self, def_id: DefId) -> ConstEvalResult<'tcx> {
1414
// In some situations def_id will have substitutions within scope, but they aren't allowed
1515
// to be used. So we can't use `Instance::mono`, instead we feed unresolved substitutions
16-
// into `const_eval` which will return `ErrorHandled::ToGeneric` if any og them are
16+
// into `const_eval` which will return `ErrorHandled::ToGeneric` if any of them are
1717
// encountered.
1818
let substs = InternalSubsts::identity_for_item(self, def_id);
1919
let instance = ty::Instance::new(def_id, substs);
2020
let cid = GlobalId { instance, promoted: None };
2121
let param_env = self.param_env(def_id).with_reveal_all();
22-
self.const_eval_validated(param_env.and(cid))
22+
self.const_eval_global_id(param_env, cid, None)
2323
}
2424

2525
/// Resolves and evaluates a constant.
@@ -41,11 +41,8 @@ impl<'tcx> TyCtxt<'tcx> {
4141
) -> ConstEvalResult<'tcx> {
4242
let instance = ty::Instance::resolve(self, param_env, def_id, substs);
4343
if let Some(instance) = instance {
44-
if let Some(promoted) = promoted {
45-
self.const_eval_promoted(param_env, instance, promoted)
46-
} else {
47-
self.const_eval_instance(param_env, instance, span)
48-
}
44+
let cid = GlobalId { instance, promoted };
45+
self.const_eval_global_id(param_env, cid, span)
4946
} else {
5047
Err(ErrorHandled::TooGeneric)
5148
}
@@ -57,22 +54,23 @@ impl<'tcx> TyCtxt<'tcx> {
5754
instance: ty::Instance<'tcx>,
5855
span: Option<Span>,
5956
) -> ConstEvalResult<'tcx> {
60-
let cid = GlobalId { instance, promoted: None };
61-
if let Some(span) = span {
62-
self.at(span).const_eval_validated(param_env.and(cid))
63-
} else {
64-
self.const_eval_validated(param_env.and(cid))
65-
}
57+
self.const_eval_global_id(param_env, GlobalId { instance, promoted: None }, span)
6658
}
6759

68-
/// Evaluate a promoted constant.
69-
pub fn const_eval_promoted(
60+
/// Evaluate a constant.
61+
pub fn const_eval_global_id(
7062
self,
7163
param_env: ty::ParamEnv<'tcx>,
72-
instance: ty::Instance<'tcx>,
73-
promoted: mir::Promoted,
64+
cid: GlobalId<'tcx>,
65+
span: Option<Span>,
7466
) -> ConstEvalResult<'tcx> {
75-
let cid = GlobalId { instance, promoted: Some(promoted) };
76-
self.const_eval_validated(param_env.and(cid))
67+
// Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should
68+
// improve caching of queries.
69+
let inputs = self.erase_regions(&param_env.and(cid));
70+
if let Some(span) = span {
71+
self.at(span).const_eval_validated(inputs)
72+
} else {
73+
self.const_eval_validated(inputs)
74+
}
7775
}
7876
}

src/librustc/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ rustc_queries! {
513513
/// returns a proper constant that is usable by the rest of the compiler.
514514
///
515515
/// **Do not use this** directly, use one of the following wrappers: `tcx.const_eval_poly`,
516-
/// `tcx.const_eval_resolve`, `tcx.const_eval_instance`, or `tcx.const_eval_promoted`.
516+
/// `tcx.const_eval_resolve`, `tcx.const_eval_instance`, or `tcx.const_eval_global_id`.
517517
query const_eval_validated(key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>)
518518
-> ConstEvalResult<'tcx> {
519519
no_force

src/librustc/ty/sty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2484,8 +2484,8 @@ impl<'tcx> Const<'tcx> {
24842484
// HACK(eddyb) when substs contain e.g. inference variables,
24852485
// attempt using identity substs instead, that will succeed
24862486
// when the expression doesn't depend on any parameters.
2487-
// FIXME(eddyb) make `const_eval` a canonical query instead,
2488-
// that would properly handle inference variables in `substs`.
2487+
// FIXME(eddyb, skinny121) pass `InferCtxt` into here when it's available, so that
2488+
// we can call `infcx.const_eval_resolve` which handles inference variables.
24892489
if substs.has_local_value() {
24902490
let identity_substs = InternalSubsts::identity_for_item(tcx, did);
24912491
// The `ParamEnv` needs to match the `identity_substs`.

src/librustc_infer/infer/mod.rs

+32
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ use rustc::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind, ToTy
1515
use rustc::middle::free_region::RegionRelations;
1616
use rustc::middle::lang_items;
1717
use rustc::middle::region;
18+
use rustc::mir;
19+
use rustc::mir::interpret::ConstEvalResult;
1820
use rustc::session::config::BorrowckMode;
1921
use rustc::ty::error::{ExpectedFound, TypeError, UnconstrainedNumeric};
2022
use rustc::ty::fold::{TypeFoldable, TypeFolder};
@@ -63,6 +65,7 @@ pub mod resolve;
6365
mod sub;
6466
pub mod type_variable;
6567

68+
use crate::infer::canonical::OriginalQueryValues;
6669
pub use rustc::infer::unify_key;
6770

6871
#[must_use]
@@ -1563,6 +1566,35 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15631566
self.universe.set(u);
15641567
u
15651568
}
1569+
1570+
/// Resolves and evaluates a constant.
1571+
///
1572+
/// The constant can be located on a trait like `<A as B>::C`, in which case the given
1573+
/// substitutions and environment are used to resolve the constant. Alternatively if the
1574+
/// constant has generic parameters in scope the substitutions are used to evaluate the value of
1575+
/// the constant. For example in `fn foo<T>() { let _ = [0; bar::<T>()]; }` the repeat count
1576+
/// constant `bar::<T>()` requires a substitution for `T`, if the substitution for `T` is still
1577+
/// too generic for the constant to be evaluated then `Err(ErrorHandled::TooGeneric)` is
1578+
/// returned.
1579+
///
1580+
/// This handles inferences variables within both `param_env` and `substs` by
1581+
/// performing the operation on their respective canonical forms.
1582+
pub fn const_eval_resolve(
1583+
&self,
1584+
param_env: ty::ParamEnv<'tcx>,
1585+
def_id: DefId,
1586+
substs: SubstsRef<'tcx>,
1587+
promoted: Option<mir::Promoted>,
1588+
span: Option<Span>,
1589+
) -> ConstEvalResult<'tcx> {
1590+
let mut original_values = OriginalQueryValues::default();
1591+
let canonical = self.canonicalize_query(&(param_env, substs), &mut original_values);
1592+
1593+
let (param_env, substs) = canonical.value;
1594+
// The return value is the evaluated value which doesn't contain any reference to inference
1595+
// variables, thus we don't need to substitute back the original values.
1596+
self.tcx.const_eval_resolve(param_env, def_id, substs, promoted, span)
1597+
}
15661598
}
15671599

15681600
pub struct ShallowResolver<'a, 'tcx> {

src/librustc_infer/traits/fulfill.rs

+9-21
Original file line numberDiff line numberDiff line change
@@ -510,27 +510,15 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
510510
}
511511

512512
ty::Predicate::ConstEvaluatable(def_id, substs) => {
513-
if obligation.param_env.has_local_value() {
514-
ProcessResult::Unchanged
515-
} else {
516-
if !substs.has_local_value() {
517-
match self.selcx.tcx().const_eval_resolve(
518-
obligation.param_env,
519-
def_id,
520-
substs,
521-
None,
522-
Some(obligation.cause.span),
523-
) {
524-
Ok(_) => ProcessResult::Changed(vec![]),
525-
Err(err) => {
526-
ProcessResult::Error(CodeSelectionError(ConstEvalFailure(err)))
527-
}
528-
}
529-
} else {
530-
pending_obligation.stalled_on =
531-
substs.types().map(|ty| infer_ty(ty)).collect();
532-
ProcessResult::Unchanged
533-
}
513+
match self.selcx.infcx().const_eval_resolve(
514+
obligation.param_env,
515+
def_id,
516+
substs,
517+
None,
518+
Some(obligation.cause.span),
519+
) {
520+
Ok(_) => ProcessResult::Changed(vec![]),
521+
Err(err) => ProcessResult::Error(CodeSelectionError(ConstEvalFailure(err))),
534522
}
535523
}
536524
}

src/librustc_infer/traits/select.rs

+9-14
Original file line numberDiff line numberDiff line change
@@ -532,20 +532,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
532532
}
533533

534534
ty::Predicate::ConstEvaluatable(def_id, substs) => {
535-
if !(obligation.param_env, substs).has_local_value() {
536-
match self.tcx().const_eval_resolve(
537-
obligation.param_env,
538-
def_id,
539-
substs,
540-
None,
541-
None,
542-
) {
543-
Ok(_) => Ok(EvaluatedToOk),
544-
Err(_) => Ok(EvaluatedToErr),
545-
}
546-
} else {
547-
// Inference variables still left in param_env or substs.
548-
Ok(EvaluatedToAmbig)
535+
match self.tcx().const_eval_resolve(
536+
obligation.param_env,
537+
def_id,
538+
substs,
539+
None,
540+
None,
541+
) {
542+
Ok(_) => Ok(EvaluatedToOk),
543+
Err(_) => Ok(EvaluatedToErr),
549544
}
550545
}
551546
}

src/librustc_mir/interpret/eval_context.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -768,11 +768,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
768768
} else {
769769
self.param_env
770770
};
771-
let val = if let Some(promoted) = gid.promoted {
772-
self.tcx.const_eval_promoted(param_env, gid.instance, promoted)?
773-
} else {
774-
self.tcx.const_eval_instance(param_env, gid.instance, Some(self.tcx.span))?
775-
};
771+
let val = self.tcx.const_eval_global_id(param_env, gid, Some(self.tcx.span))?;
776772

777773
// Even though `ecx.const_eval` is called from `eval_const_to_op` we can never have a
778774
// recursion deeper than one level, because the `tcx.const_eval` above is guaranteed to not
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// edition:2018
2+
// revisions:rpass1
3+
#![feature(const_generics)]
4+
5+
const FOO: usize = 1;
6+
7+
struct Container<T> {
8+
val: std::marker::PhantomData<T>,
9+
blah: [(); FOO]
10+
}
11+
12+
async fn dummy() {}
13+
14+
async fn foo() {
15+
let a: Container<&'static ()>;
16+
dummy().await;
17+
}
18+
19+
fn is_send<T: Send>(_: T) {}
20+
21+
fn main() {
22+
is_send(foo());
23+
}

0 commit comments

Comments
 (0)