Skip to content

Commit 2e6bf09

Browse files
committed
const_eval_resolve
1 parent 58031c7 commit 2e6bf09

File tree

16 files changed

+96
-28
lines changed

16 files changed

+96
-28
lines changed

src/librustc_codegen_ssa/mir/constant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
2828
ty::ConstKind::Unevaluated(def, substs, promoted) => self
2929
.cx
3030
.tcx()
31-
.const_eval_resolve(ty::ParamEnv::reveal_all(), def.did, substs, promoted, None)
31+
.const_eval_resolve(ty::ParamEnv::reveal_all(), def, substs, promoted, None)
3232
.map_err(|err| {
3333
if promoted.is_none() {
3434
self.cx

src/librustc_infer/infer/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15361536
pub fn const_eval_resolve(
15371537
&self,
15381538
param_env: ty::ParamEnv<'tcx>,
1539-
def_id: DefId,
1539+
def: ty::WithOptParam<DefId>,
15401540
substs: SubstsRef<'tcx>,
15411541
promoted: Option<mir::Promoted>,
15421542
span: Option<Span>,
@@ -1547,7 +1547,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15471547
let (param_env, substs) = canonical.value;
15481548
// The return value is the evaluated value which doesn't contain any reference to inference
15491549
// variables, thus we don't need to substitute back the original values.
1550-
self.tcx.const_eval_resolve(param_env, def_id, substs, promoted, span)
1550+
self.tcx.const_eval_resolve(param_env, def, substs, promoted, span)
15511551
}
15521552

15531553
/// If `typ` is a type variable of some kind, resolve it one level

src/librustc_middle/mir/interpret/queries.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ impl<'tcx> TyCtxt<'tcx> {
3434
pub fn const_eval_resolve(
3535
self,
3636
param_env: ty::ParamEnv<'tcx>,
37-
def_id: DefId,
37+
def: ty::WithOptParam<DefId>,
3838
substs: SubstsRef<'tcx>,
3939
promoted: Option<mir::Promoted>,
4040
span: Option<Span>,
4141
) -> ConstEvalResult<'tcx> {
42-
match ty::Instance::resolve(self, param_env, def_id, substs) {
42+
match ty::Instance::resolve_const_arg(self, param_env, def, substs) {
4343
Ok(Some(instance)) => {
4444
let cid = GlobalId { instance, promoted };
4545
self.const_eval_global_id(param_env, cid, span)

src/librustc_middle/query/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,5 +1463,14 @@ rustc_queries! {
14631463
) -> Result<Option<ty::Instance<'tcx>>, ErrorReported> {
14641464
desc { "resolving instance `{}`", ty::Instance::new(key.value.0, key.value.1) }
14651465
}
1466+
1467+
query resolve_instance_of_const_arg(
1468+
key: ty::ParamEnvAnd<'tcx, (ty::WithOptParam<DefId>, SubstsRef<'tcx>)>
1469+
) -> Result<Option<ty::Instance<'tcx>>, ErrorReported> {
1470+
desc {
1471+
"resolving instance of the potential const argument `{}`",
1472+
ty::Instance::new(key.value.0.did, key.value.1),
1473+
}
1474+
}
14661475
}
14671476
}

src/librustc_middle/ty/instance.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,17 @@ impl<'tcx> Instance<'tcx> {
336336
tcx.resolve_instance(tcx.erase_regions(&param_env.and((def_id, substs))))
337337
}
338338

339+
// This should be kept up to date with `resolve`.
340+
pub fn resolve_const_arg(
341+
tcx: TyCtxt<'tcx>,
342+
param_env: ty::ParamEnv<'tcx>,
343+
def: ty::WithOptParam<DefId>,
344+
substs: SubstsRef<'tcx>,
345+
) -> Result<Option<Instance<'tcx>>, ErrorReported> {
346+
let substs = tcx.erase_regions(&substs);
347+
tcx.resolve_instance_of_const_arg(tcx.erase_regions(&param_env.and((def, substs))))
348+
}
349+
339350
pub fn resolve_for_fn_ptr(
340351
tcx: TyCtxt<'tcx>,
341352
param_env: ty::ParamEnv<'tcx>,

src/librustc_middle/ty/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ pub enum PredicateKind<'tcx> {
11001100
Subtype(PolySubtypePredicate<'tcx>),
11011101

11021102
/// Constant initializer must evaluate successfully.
1103-
ConstEvaluatable(DefId, SubstsRef<'tcx>),
1103+
ConstEvaluatable(ty::WithOptParam<DefId>, SubstsRef<'tcx>),
11041104

11051105
/// Constants must be equal. The first component is the const that is expected.
11061106
ConstEquate(&'tcx Const<'tcx>, &'tcx Const<'tcx>),
@@ -1571,7 +1571,7 @@ pub type PlaceholderType = Placeholder<BoundVar>;
15711571

15721572
pub type PlaceholderConst = Placeholder<BoundVar>;
15731573

1574-
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
1574+
#[derive(Copy, Clone, Debug, TypeFoldable, Lift, RustcEncodable, RustcDecodable)]
15751575
#[derive(PartialEq, Eq, PartialOrd, Ord)]
15761576
#[derive(Hash, HashStable)]
15771577
pub struct WithOptParam<T> {

src/librustc_middle/ty/print/pretty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,9 +2027,9 @@ define_print_and_forward_display! {
20272027
print_value_path(closure_def_id, &[]),
20282028
write("` implements the trait `{}`", kind))
20292029
}
2030-
&ty::PredicateKind::ConstEvaluatable(def_id, substs) => {
2030+
&ty::PredicateKind::ConstEvaluatable(def, substs) => {
20312031
p!(write("the constant `"),
2032-
print_value_path(def_id, substs),
2032+
print_value_path(def.did, substs),
20332033
write("` can be evaluated"))
20342034
}
20352035
ty::PredicateKind::ConstEquate(c1, c2) => {

src/librustc_middle/ty/query/keys.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,17 @@ impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
171171
}
172172
}
173173

174+
impl<'tcx> Key for (ty::WithOptParam<DefId>, SubstsRef<'tcx>) {
175+
type CacheSelector = DefaultCacheSelector;
176+
177+
fn query_crate(&self) -> CrateNum {
178+
LOCAL_CRATE
179+
}
180+
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
181+
self.0.did.default_span(tcx)
182+
}
183+
}
184+
174185
impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
175186
type CacheSelector = DefaultCacheSelector;
176187

src/librustc_middle/ty/sty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2373,7 +2373,7 @@ impl<'tcx> Const<'tcx> {
23732373
let (param_env, substs) = param_env_and_substs.into_parts();
23742374
// try to resolve e.g. associated constants to their definition on an impl, and then
23752375
// evaluate the const.
2376-
match tcx.const_eval_resolve(param_env, def.did, substs, promoted, None) {
2376+
match tcx.const_eval_resolve(param_env, def, substs, promoted, None) {
23772377
// NOTE(eddyb) `val` contains no lifetimes/types/consts,
23782378
// and we use the original type, so nothing from `substs`
23792379
// (which may be identity substs, see above),

src/librustc_mir/monomorphize/collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
623623
match substituted_constant.val {
624624
ty::ConstKind::Value(val) => collect_const_value(self.tcx, val, self.output),
625625
ty::ConstKind::Unevaluated(def, substs, promoted) => {
626-
match self.tcx.const_eval_resolve(param_env, def.did, substs, promoted, None) {
626+
match self.tcx.const_eval_resolve(param_env, def, substs, promoted, None) {
627627
Ok(val) => collect_const_value(self.tcx, val, self.output),
628628
Err(ErrorHandled::Reported(ErrorReported) | ErrorHandled::Linted) => {}
629629
Err(ErrorHandled::TooGeneric) => span_bug!(

0 commit comments

Comments
 (0)