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

+2-3
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

+1-1
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

+2-2
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

+2-2
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

+2-2
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

+1-1
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

+4-4
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

+2-2
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

+3-8
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

+21-32
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
}

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -804,10 +804,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
804804
}
805805

806806
match obligation.predicate.kind().skip_binder() {
807-
ty::PredicateKind::ConstEvaluatable(def, _) => {
807+
ty::PredicateKind::ConstEvaluatable(uv) => {
808808
let mut err =
809809
self.tcx.sess.struct_span_err(span, "unconstrained generic constant");
810-
let const_span = self.tcx.def_span(def.did);
810+
let const_span = self.tcx.def_span(uv.def.did);
811811
match self.tcx.sess.source_map().span_to_snippet(const_span) {
812812
Ok(snippet) => err.help(&format!(
813813
"try adding a `where` bound using this expression: `where [(); {}]:`",

compiler/rustc_trait_selection/src/traits/fulfill.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -489,19 +489,20 @@ impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> {
489489
}
490490
}
491491

492-
ty::PredicateKind::ConstEvaluatable(def_id, substs) => {
492+
ty::PredicateKind::ConstEvaluatable(uv) => {
493493
match const_evaluatable::is_const_evaluatable(
494494
self.selcx.infcx(),
495-
def_id,
496-
substs,
495+
uv,
497496
obligation.param_env,
498497
obligation.cause.span,
499498
) {
500499
Ok(()) => ProcessResult::Changed(vec![]),
501500
Err(NotConstEvaluatable::MentionsInfer) => {
502501
pending_obligation.stalled_on.clear();
503502
pending_obligation.stalled_on.extend(
504-
substs.iter().filter_map(TyOrConstInferVar::maybe_from_generic_arg),
503+
uv.substs(infcx.tcx)
504+
.iter()
505+
.filter_map(TyOrConstInferVar::maybe_from_generic_arg),
505506
);
506507
ProcessResult::Unchanged
507508
}
@@ -525,10 +526,7 @@ impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> {
525526
if let (ty::ConstKind::Unevaluated(a), ty::ConstKind::Unevaluated(b)) =
526527
(c1.val, c2.val)
527528
{
528-
if tcx.try_unify_abstract_consts((
529-
(a.def, a.substs(tcx)),
530-
(b.def, b.substs(tcx)),
531-
)) {
529+
if tcx.try_unify_abstract_consts((a, b)) {
532530
return ProcessResult::Changed(vec![]);
533531
}
534532
}

compiler/rustc_trait_selection/src/traits/object_safety.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -852,12 +852,12 @@ fn contains_illegal_self_type_reference<'tcx, T: TypeFoldable<'tcx>>(
852852
}
853853

854854
fn visit_predicate(&mut self, pred: ty::Predicate<'tcx>) -> ControlFlow<Self::BreakTy> {
855-
if let ty::PredicateKind::ConstEvaluatable(def, substs) = pred.kind().skip_binder() {
855+
if let ty::PredicateKind::ConstEvaluatable(ct) = pred.kind().skip_binder() {
856856
// FIXME(const_evaluatable_checked): We should probably deduplicate the logic for
857857
// `AbstractConst`s here, it might make sense to change `ConstEvaluatable` to
858858
// take a `ty::Const` instead.
859859
use rustc_middle::mir::abstract_const::Node;
860-
if let Ok(Some(ct)) = AbstractConst::new(self.tcx, def, substs) {
860+
if let Ok(Some(ct)) = AbstractConst::new(self.tcx, ct) {
861861
const_evaluatable::walk_abstract_const(self.tcx, ct, |node| match node.root() {
862862
Node::Leaf(leaf) => {
863863
let leaf = leaf.subst(self.tcx, ct.substs);

compiler/rustc_trait_selection/src/traits/select/mod.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -547,11 +547,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
547547
}
548548
}
549549

550-
ty::PredicateKind::ConstEvaluatable(def_id, substs) => {
550+
ty::PredicateKind::ConstEvaluatable(uv) => {
551551
match const_evaluatable::is_const_evaluatable(
552552
self.infcx,
553-
def_id,
554-
substs,
553+
uv,
555554
obligation.param_env,
556555
obligation.cause.span,
557556
) {
@@ -573,10 +572,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
573572
if let (ty::ConstKind::Unevaluated(a), ty::ConstKind::Unevaluated(b)) =
574573
(c1.val, c2.val)
575574
{
576-
if self.tcx().try_unify_abstract_consts((
577-
(a.def, a.substs(self.tcx())),
578-
(b.def, b.substs(self.tcx())),
579-
)) {
575+
if self.tcx().try_unify_abstract_consts((a, b)) {
580576
return Ok(EvaluatedToOk);
581577
}
582578
}

compiler/rustc_trait_selection/src/traits/wf.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,9 @@ pub fn predicate_obligations<'a, 'tcx>(
128128
wf.compute(a.into());
129129
wf.compute(b.into());
130130
}
131-
ty::PredicateKind::ConstEvaluatable(def, substs) => {
132-
let obligations = wf.nominal_obligations(def.did, substs);
131+
ty::PredicateKind::ConstEvaluatable(uv) => {
132+
let substs = uv.substs(wf.tcx());
133+
let obligations = wf.nominal_obligations(uv.def.did, substs);
133134
wf.out.extend(obligations);
134135

135136
for arg in substs.iter() {
@@ -438,8 +439,10 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
438439
let obligations = self.nominal_obligations(uv.def.did, substs);
439440
self.out.extend(obligations);
440441

441-
let predicate = ty::PredicateKind::ConstEvaluatable(uv.def, substs)
442-
.to_predicate(self.tcx());
442+
let predicate = ty::PredicateKind::ConstEvaluatable(
443+
ty::Unevaluated::new(uv.def, substs),
444+
)
445+
.to_predicate(self.tcx());
443446
let cause = self.cause(traits::MiscObligation);
444447
self.out.push(traits::Obligation::with_depth(
445448
cause,

compiler/rustc_typeck/src/check/wfcheck.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -531,10 +531,10 @@ fn check_type_defn<'tcx, F>(
531531
fcx.register_predicate(traits::Obligation::new(
532532
cause,
533533
fcx.param_env,
534-
ty::PredicateKind::ConstEvaluatable(
534+
ty::PredicateKind::ConstEvaluatable(ty::Unevaluated::new(
535535
ty::WithOptConstParam::unknown(discr_def_id.to_def_id()),
536536
discr_substs,
537-
)
537+
))
538538
.to_predicate(tcx),
539539
));
540540
}

compiler/rustc_typeck/src/collect.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -2304,11 +2304,8 @@ fn const_evaluatable_predicates_of<'tcx>(
23042304
if let ty::ConstKind::Unevaluated(uv) = ct.val {
23052305
assert_eq!(uv.promoted, None);
23062306
let span = self.tcx.hir().span(c.hir_id);
2307-
self.preds.insert((
2308-
ty::PredicateKind::ConstEvaluatable(uv.def, uv.substs(self.tcx))
2309-
.to_predicate(self.tcx),
2310-
span,
2311-
));
2307+
self.preds
2308+
.insert((ty::PredicateKind::ConstEvaluatable(uv).to_predicate(self.tcx), span));
23122309
}
23132310
}
23142311

0 commit comments

Comments
 (0)