Skip to content

Commit 4b8eae1

Browse files
committed
use ty::Unevaluated instead of def substs pair
1 parent d823155 commit 4b8eae1

File tree

17 files changed

+64
-87
lines changed

17 files changed

+64
-87
lines changed

compiler/rustc_middle/src/query/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,11 @@ rustc_queries! {
297297
}
298298

299299
query try_unify_abstract_consts(key: (
300-
(ty::WithOptConstParam<DefId>, SubstsRef<'tcx>),
301-
(ty::WithOptConstParam<DefId>, SubstsRef<'tcx>)
300+
ty::Unevaluated<'tcx>, ty::Unevaluated<'tcx>
302301
)) -> bool {
303302
desc {
304303
|tcx| "trying to unify the generic constants {} and {}",
305-
tcx.def_path_str(key.0.0.did), tcx.def_path_str(key.1.0.did)
304+
tcx.def_path_str(key.0.def.did), tcx.def_path_str(key.1.def.did)
306305
}
307306
}
308307

compiler/rustc_middle/src/ty/consts/kind.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use super::ScalarInt;
1818
///
1919
/// We check for all possible substs in `fn default_anon_const_substs`,
2020
/// so refer to that check for more info.
21-
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable)]
21+
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Lift)]
2222
#[derive(Hash, HashStable)]
2323
pub struct Unevaluated<'tcx> {
2424
pub def: ty::WithOptConstParam<DefId>,

compiler/rustc_middle/src/ty/flags.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ impl FlagComputation {
248248
ty::PredicateKind::ClosureKind(_def_id, substs, _kind) => {
249249
self.add_substs(substs);
250250
}
251-
ty::PredicateKind::ConstEvaluatable(_def_id, substs) => {
252-
self.add_substs(substs);
251+
ty::PredicateKind::ConstEvaluatable(uv) => {
252+
self.add_unevaluated_const(uv);
253253
}
254254
ty::PredicateKind::ConstEquate(expected, found) => {
255255
self.add_const(expected);

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ crate struct PredicateInner<'tcx> {
401401
}
402402

403403
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
404-
static_assert_size!(PredicateInner<'_>, 48);
404+
static_assert_size!(PredicateInner<'_>, 56);
405405

406406
#[derive(Clone, Copy, Lift)]
407407
pub struct Predicate<'tcx> {
@@ -483,7 +483,7 @@ pub enum PredicateKind<'tcx> {
483483
Subtype(SubtypePredicate<'tcx>),
484484

485485
/// Constant initializer must evaluate successfully.
486-
ConstEvaluatable(ty::WithOptConstParam<DefId>, SubstsRef<'tcx>),
486+
ConstEvaluatable(ty::Unevaluated<'tcx>),
487487

488488
/// Constants must be equal. The first component is the const that is expected.
489489
ConstEquate(&'tcx Const<'tcx>, &'tcx Const<'tcx>),

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2220,8 +2220,8 @@ define_print_and_forward_display! {
22202220
print_value_path(closure_def_id, &[]),
22212221
write("` implements the trait `{}`", kind))
22222222
}
2223-
ty::PredicateKind::ConstEvaluatable(def, substs) => {
2224-
p!("the constant `", print_value_path(def.did, substs), "` can be evaluated")
2223+
ty::PredicateKind::ConstEvaluatable(uv) => {
2224+
p!("the constant `", print_value_path(uv.def.did, uv.substs_.map_or(&[], |x| x)), "` can be evaluated")
22252225
}
22262226
ty::PredicateKind::ConstEquate(c1, c2) => {
22272227
p!("the constant `", print(c1), "` equals `", print(c2), "`")

compiler/rustc_middle/src/ty/relate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
552552
(ty::ConstKind::Unevaluated(au), ty::ConstKind::Unevaluated(bu))
553553
if tcx.features().const_evaluatable_checked =>
554554
{
555-
tcx.try_unify_abstract_consts(((au.def, au.substs(tcx)), (bu.def, bu.substs(tcx))))
555+
tcx.try_unify_abstract_consts((au, bu))
556556
}
557557

558558
// While this is slightly incorrect, it shouldn't matter for `min_const_generics`

compiler/rustc_middle/src/ty/structural_impls.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ impl fmt::Debug for ty::PredicateKind<'tcx> {
191191
ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => {
192192
write!(f, "ClosureKind({:?}, {:?}, {:?})", closure_def_id, closure_substs, kind)
193193
}
194-
ty::PredicateKind::ConstEvaluatable(def_id, substs) => {
195-
write!(f, "ConstEvaluatable({:?}, {:?})", def_id, substs)
194+
ty::PredicateKind::ConstEvaluatable(uv) => {
195+
write!(f, "ConstEvaluatable({:?}, {:?})", uv.def, uv.substs_)
196196
}
197197
ty::PredicateKind::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2),
198198
ty::PredicateKind::TypeWellFormedFromEnv(ty) => {
@@ -441,8 +441,8 @@ impl<'a, 'tcx> Lift<'tcx> for ty::PredicateKind<'a> {
441441
ty::PredicateKind::ObjectSafe(trait_def_id) => {
442442
Some(ty::PredicateKind::ObjectSafe(trait_def_id))
443443
}
444-
ty::PredicateKind::ConstEvaluatable(def_id, substs) => {
445-
tcx.lift(substs).map(|substs| ty::PredicateKind::ConstEvaluatable(def_id, substs))
444+
ty::PredicateKind::ConstEvaluatable(uv) => {
445+
tcx.lift(uv).map(|uv| ty::PredicateKind::ConstEvaluatable(uv))
446446
}
447447
ty::PredicateKind::ConstEquate(c1, c2) => {
448448
tcx.lift((c1, c2)).map(|(c1, c2)| ty::PredicateKind::ConstEquate(c1, c2))

compiler/rustc_privacy/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ where
133133
ty.visit_with(self)
134134
}
135135
ty::PredicateKind::RegionOutlives(..) => ControlFlow::CONTINUE,
136-
ty::PredicateKind::ConstEvaluatable(defs, substs)
136+
ty::PredicateKind::ConstEvaluatable(uv)
137137
if self.def_id_visitor.tcx().features().const_evaluatable_checked =>
138138
{
139139
let tcx = self.def_id_visitor.tcx();
140-
if let Ok(Some(ct)) = AbstractConst::new(tcx, defs, substs) {
140+
if let Ok(Some(ct)) = AbstractConst::new(tcx, uv) {
141141
self.visit_abstract_const_expr(tcx, ct)?;
142142
}
143143
ControlFlow::CONTINUE

compiler/rustc_query_impl/src/keys.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,18 +217,13 @@ impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
217217
}
218218
}
219219

220-
impl<'tcx> Key
221-
for (
222-
(ty::WithOptConstParam<DefId>, SubstsRef<'tcx>),
223-
(ty::WithOptConstParam<DefId>, SubstsRef<'tcx>),
224-
)
225-
{
220+
impl<'tcx> Key for (ty::Unevaluated<'tcx>, ty::Unevaluated<'tcx>) {
226221
#[inline(always)]
227222
fn query_crate_is_local(&self) -> bool {
228-
(self.0).0.did.krate == LOCAL_CRATE
223+
(self.0).def.did.krate == LOCAL_CRATE
229224
}
230225
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
231-
(self.0).0.did.default_span(tcx)
226+
(self.0).def.did.default_span(tcx)
232227
}
233228
}
234229

compiler/rustc_trait_selection/src/traits/const_evaluatable.rs

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_middle::mir::{self, Rvalue, StatementKind, TerminatorKind};
1919
use rustc_middle::ty::subst::{Subst, SubstsRef};
2020
use rustc_middle::ty::{self, TyCtxt, TypeFoldable};
2121
use rustc_session::lint;
22-
use rustc_span::def_id::{DefId, LocalDefId};
22+
use rustc_span::def_id::LocalDefId;
2323
use rustc_span::Span;
2424

2525
use std::cmp;
@@ -29,26 +29,20 @@ use std::ops::ControlFlow;
2929
/// Check if a given constant can be evaluated.
3030
pub fn is_const_evaluatable<'cx, 'tcx>(
3131
infcx: &InferCtxt<'cx, 'tcx>,
32-
def: ty::WithOptConstParam<DefId>,
33-
substs: SubstsRef<'tcx>,
32+
uv: ty::Unevaluated<'tcx>,
3433
param_env: ty::ParamEnv<'tcx>,
3534
span: Span,
3635
) -> Result<(), NotConstEvaluatable> {
37-
debug!("is_const_evaluatable({:?}, {:?})", def, substs);
36+
debug!("is_const_evaluatable({:?})", uv);
3837
if infcx.tcx.features().const_evaluatable_checked {
3938
let tcx = infcx.tcx;
40-
match AbstractConst::new(tcx, def, substs)? {
39+
match AbstractConst::new(tcx, uv)? {
4140
// We are looking at a generic abstract constant.
4241
Some(ct) => {
4342
for pred in param_env.caller_bounds() {
4443
match pred.kind().skip_binder() {
45-
ty::PredicateKind::ConstEvaluatable(b_def, b_substs) => {
46-
if b_def == def && b_substs == substs {
47-
debug!("is_const_evaluatable: caller_bound ~~> ok");
48-
return Ok(());
49-
}
50-
51-
if let Some(b_ct) = AbstractConst::new(tcx, b_def, b_substs)? {
44+
ty::PredicateKind::ConstEvaluatable(uv) => {
45+
if let Some(b_ct) = AbstractConst::new(tcx, uv)? {
5246
// Try to unify with each subtree in the AbstractConst to allow for
5347
// `N + 1` being const evaluatable even if theres only a `ConstEvaluatable`
5448
// predicate for `(N + 1) * 2`
@@ -134,7 +128,7 @@ pub fn is_const_evaluatable<'cx, 'tcx>(
134128
}
135129

136130
let future_compat_lint = || {
137-
if let Some(local_def_id) = def.did.as_local() {
131+
if let Some(local_def_id) = uv.def.did.as_local() {
138132
infcx.tcx.struct_span_lint_hir(
139133
lint::builtin::CONST_EVALUATABLE_UNCHECKED,
140134
infcx.tcx.hir().local_def_id_to_hir_id(local_def_id),
@@ -155,13 +149,12 @@ pub fn is_const_evaluatable<'cx, 'tcx>(
155149
// and hopefully soon change this to an error.
156150
//
157151
// See #74595 for more details about this.
158-
let concrete =
159-
infcx.const_eval_resolve(param_env, ty::Unevaluated::new(def, substs), Some(span));
152+
let concrete = infcx.const_eval_resolve(param_env, uv, Some(span));
160153

161-
if concrete.is_ok() && substs.has_param_types_or_consts(infcx.tcx) {
162-
match infcx.tcx.def_kind(def.did) {
154+
if concrete.is_ok() && uv.substs(infcx.tcx).has_param_types_or_consts(infcx.tcx) {
155+
match infcx.tcx.def_kind(uv.def.did) {
163156
DefKind::AnonConst => {
164-
let mir_body = infcx.tcx.mir_for_ctfe_opt_const_arg(def);
157+
let mir_body = infcx.tcx.mir_for_ctfe_opt_const_arg(uv.def);
165158

166159
if mir_body.is_polymorphic {
167160
future_compat_lint();
@@ -173,7 +166,7 @@ pub fn is_const_evaluatable<'cx, 'tcx>(
173166

174167
debug!(?concrete, "is_const_evaluatable");
175168
match concrete {
176-
Err(ErrorHandled::TooGeneric) => Err(match substs.has_infer_types_or_consts() {
169+
Err(ErrorHandled::TooGeneric) => Err(match uv.has_infer_types_or_consts() {
177170
true => NotConstEvaluatable::MentionsInfer,
178171
false => NotConstEvaluatable::MentionsParam,
179172
}),
@@ -198,23 +191,22 @@ pub struct AbstractConst<'tcx> {
198191
pub substs: SubstsRef<'tcx>,
199192
}
200193

201-
impl AbstractConst<'tcx> {
194+
impl<'tcx> AbstractConst<'tcx> {
202195
pub fn new(
203196
tcx: TyCtxt<'tcx>,
204-
def: ty::WithOptConstParam<DefId>,
205-
substs: SubstsRef<'tcx>,
197+
uv: ty::Unevaluated<'tcx>,
206198
) -> Result<Option<AbstractConst<'tcx>>, ErrorReported> {
207-
let inner = tcx.mir_abstract_const_opt_const_arg(def)?;
208-
debug!("AbstractConst::new({:?}) = {:?}", def, inner);
209-
Ok(inner.map(|inner| AbstractConst { inner, substs }))
199+
let inner = tcx.mir_abstract_const_opt_const_arg(uv.def)?;
200+
debug!("AbstractConst::new({:?}) = {:?}", uv, inner);
201+
Ok(inner.map(|inner| AbstractConst { inner, substs: uv.substs(tcx) }))
210202
}
211203

212204
pub fn from_const(
213205
tcx: TyCtxt<'tcx>,
214206
ct: &ty::Const<'tcx>,
215207
) -> Result<Option<AbstractConst<'tcx>>, ErrorReported> {
216208
match ct.val {
217-
ty::ConstKind::Unevaluated(uv) => AbstractConst::new(tcx, uv.def, uv.substs(tcx)),
209+
ty::ConstKind::Unevaluated(uv) => AbstractConst::new(tcx, uv),
218210
ty::ConstKind::Error(_) => Err(ErrorReported),
219211
_ => Ok(None),
220212
}
@@ -564,14 +556,11 @@ pub(super) fn mir_abstract_const<'tcx>(
564556

565557
pub(super) fn try_unify_abstract_consts<'tcx>(
566558
tcx: TyCtxt<'tcx>,
567-
((a, a_substs), (b, b_substs)): (
568-
(ty::WithOptConstParam<DefId>, SubstsRef<'tcx>),
569-
(ty::WithOptConstParam<DefId>, SubstsRef<'tcx>),
570-
),
559+
(a, b): (ty::Unevaluated<'tcx>, ty::Unevaluated<'tcx>),
571560
) -> bool {
572561
(|| {
573-
if let Some(a) = AbstractConst::new(tcx, a, a_substs)? {
574-
if let Some(b) = AbstractConst::new(tcx, b, b_substs)? {
562+
if let Some(a) = AbstractConst::new(tcx, a)? {
563+
if let Some(b) = AbstractConst::new(tcx, b)? {
575564
return Ok(try_unify(tcx, a, b));
576565
}
577566
}

0 commit comments

Comments
 (0)