Skip to content

Commit bc0156b

Browse files
committed
shrink ty::PredicateKind again
1 parent 283e0e6 commit bc0156b

File tree

12 files changed

+68
-22
lines changed

12 files changed

+68
-22
lines changed

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
675675
/// canonicalizing the consts.
676676
pub fn try_unify_abstract_consts(
677677
&self,
678-
a: ty::Unevaluated<'tcx>,
679-
b: ty::Unevaluated<'tcx>,
678+
a: ty::Unevaluated<'tcx, ()>,
679+
b: ty::Unevaluated<'tcx, ()>,
680680
) -> bool {
681681
let canonical = self.canonicalize_query((a, b), &mut OriginalQueryValues::default());
682682
debug!("canonical consts: {:?}", &canonical.value);

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ rustc_queries! {
303303
}
304304

305305
query try_unify_abstract_consts(key: (
306-
ty::Unevaluated<'tcx>, ty::Unevaluated<'tcx>
306+
ty::Unevaluated<'tcx, ()>, ty::Unevaluated<'tcx, ()>
307307
)) -> bool {
308308
desc {
309309
|tcx| "trying to unify the generic constants {} and {}",

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::convert::TryInto;
2+
use std::fmt;
23

34
use crate::mir::interpret::{AllocId, ConstValue, Scalar};
45
use crate::mir::Promoted;
@@ -20,20 +21,38 @@ use super::ScalarInt;
2021
/// so refer to that check for more info.
2122
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Lift)]
2223
#[derive(Hash, HashStable)]
23-
pub struct Unevaluated<'tcx> {
24+
pub struct Unevaluated<'tcx, P = Option<Promoted>> {
2425
pub def: ty::WithOptConstParam<DefId>,
2526
pub substs_: Option<SubstsRef<'tcx>>,
26-
pub promoted: Option<Promoted>,
27+
pub promoted: P,
2728
}
2829

2930
impl<'tcx> Unevaluated<'tcx> {
30-
pub fn new(def: ty::WithOptConstParam<DefId>, substs: SubstsRef<'tcx>) -> Unevaluated<'tcx> {
31-
Unevaluated { def, substs_: Some(substs), promoted: None }
31+
pub fn shrink(self) -> Unevaluated<'tcx, ()> {
32+
debug_assert_eq!(self.promoted, None);
33+
Unevaluated { def: self.def, substs_: self.substs_, promoted: () }
3234
}
35+
}
36+
37+
impl<'tcx> Unevaluated<'tcx, ()> {
38+
pub fn expand(self) -> Unevaluated<'tcx> {
39+
Unevaluated { def: self.def, substs_: self.substs_, promoted: None }
40+
}
41+
}
42+
43+
impl<'tcx, P: Default> Unevaluated<'tcx, P> {
44+
pub fn new(def: ty::WithOptConstParam<DefId>, substs: SubstsRef<'tcx>) -> Unevaluated<'tcx, P> {
45+
Unevaluated { def, substs_: Some(substs), promoted: Default::default() }
46+
}
47+
}
3348

49+
impl<'tcx, P: Default + PartialEq + fmt::Debug> Unevaluated<'tcx, P> {
3450
pub fn substs(self, tcx: TyCtxt<'tcx>) -> SubstsRef<'tcx> {
3551
self.substs_.unwrap_or_else(|| {
36-
debug_assert_eq!(self.promoted, None);
52+
// We must not use the parents default substs for promoted constants
53+
// as that can result in incorrect substs and calls the `default_anon_const_substs`
54+
// for something that might not actually be a constant.
55+
debug_assert_eq!(self.promoted, Default::default());
3756
tcx.default_anon_const_substs(self.def.did)
3857
})
3958
}

compiler/rustc_middle/src/ty/flags.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ impl FlagComputation {
310310
}
311311
}
312312

313-
fn add_unevaluated_const(&mut self, ct: ty::Unevaluated<'tcx>) {
313+
fn add_unevaluated_const<P>(&mut self, ct: ty::Unevaluated<'tcx, P>) {
314314
if let Some(substs) = ct.substs_ {
315315
self.add_substs(substs);
316316
} else {

compiler/rustc_middle/src/ty/mod.rs

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

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

411411
#[derive(Clone, Copy, Lift)]
412412
pub struct Predicate<'tcx> {
@@ -502,7 +502,7 @@ pub enum PredicateKind<'tcx> {
502502
Coerce(CoercePredicate<'tcx>),
503503

504504
/// Constant initializer must evaluate successfully.
505-
ConstEvaluatable(ty::Unevaluated<'tcx>),
505+
ConstEvaluatable(ty::Unevaluated<'tcx, ()>),
506506

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

compiler/rustc_middle/src/ty/relate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
579579
(ty::ConstKind::Unevaluated(au), ty::ConstKind::Unevaluated(bu))
580580
if tcx.features().const_evaluatable_checked =>
581581
{
582-
tcx.try_unify_abstract_consts((au, bu))
582+
tcx.try_unify_abstract_consts((au.shrink(), bu.shrink()))
583583
}
584584

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

compiler/rustc_middle/src/ty/structural_impls.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,3 +1105,28 @@ impl<'tcx> TypeFoldable<'tcx> for ty::Unevaluated<'tcx> {
11051105
}
11061106
}
11071107
}
1108+
1109+
impl<'tcx> TypeFoldable<'tcx> for ty::Unevaluated<'tcx, ()> {
1110+
fn super_fold_with<F: TypeFolder<'tcx>>(self, folder: &mut F) -> Self {
1111+
ty::Unevaluated {
1112+
def: self.def,
1113+
substs_: Some(self.substs(folder.tcx()).fold_with(folder)),
1114+
promoted: self.promoted,
1115+
}
1116+
}
1117+
1118+
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1119+
visitor.visit_unevaluated_const(self.expand())
1120+
}
1121+
1122+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
1123+
if let Some(tcx) = visitor.tcx_for_anon_const_substs() {
1124+
self.substs(tcx).visit_with(visitor)
1125+
} else if let Some(substs) = self.substs_ {
1126+
substs.visit_with(visitor)
1127+
} else {
1128+
debug!("ignoring default substs of `{:?}`", self.def);
1129+
ControlFlow::CONTINUE
1130+
}
1131+
}
1132+
}

compiler/rustc_query_impl/src/keys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
217217
}
218218
}
219219

220-
impl<'tcx> Key for (ty::Unevaluated<'tcx>, ty::Unevaluated<'tcx>) {
220+
impl<'tcx> Key for (ty::Unevaluated<'tcx, ()>, ty::Unevaluated<'tcx, ()>) {
221221
#[inline(always)]
222222
fn query_crate_is_local(&self) -> bool {
223223
(self.0).def.did.krate == LOCAL_CRATE

compiler/rustc_trait_selection/src/traits/const_evaluatable.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ 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-
uv: ty::Unevaluated<'tcx>,
32+
uv: ty::Unevaluated<'tcx, ()>,
3333
param_env: ty::ParamEnv<'tcx>,
3434
span: Span,
3535
) -> Result<(), NotConstEvaluatable> {
@@ -149,7 +149,7 @@ pub fn is_const_evaluatable<'cx, 'tcx>(
149149
// and hopefully soon change this to an error.
150150
//
151151
// See #74595 for more details about this.
152-
let concrete = infcx.const_eval_resolve(param_env, uv, Some(span));
152+
let concrete = infcx.const_eval_resolve(param_env, uv.expand(), Some(span));
153153

154154
if concrete.is_ok() && uv.substs(infcx.tcx).has_param_types_or_consts(infcx.tcx) {
155155
match infcx.tcx.def_kind(uv.def.did) {
@@ -194,7 +194,7 @@ pub struct AbstractConst<'tcx> {
194194
impl<'tcx> AbstractConst<'tcx> {
195195
pub fn new(
196196
tcx: TyCtxt<'tcx>,
197-
uv: ty::Unevaluated<'tcx>,
197+
uv: ty::Unevaluated<'tcx, ()>,
198198
) -> Result<Option<AbstractConst<'tcx>>, ErrorReported> {
199199
let inner = tcx.mir_abstract_const_opt_const_arg(uv.def)?;
200200
debug!("AbstractConst::new({:?}) = {:?}", uv, inner);
@@ -206,7 +206,7 @@ impl<'tcx> AbstractConst<'tcx> {
206206
ct: &ty::Const<'tcx>,
207207
) -> Result<Option<AbstractConst<'tcx>>, ErrorReported> {
208208
match ct.val {
209-
ty::ConstKind::Unevaluated(uv) => AbstractConst::new(tcx, uv),
209+
ty::ConstKind::Unevaluated(uv) => AbstractConst::new(tcx, uv.shrink()),
210210
ty::ConstKind::Error(_) => Err(ErrorReported),
211211
_ => Ok(None),
212212
}
@@ -556,7 +556,7 @@ pub(super) fn mir_abstract_const<'tcx>(
556556

557557
pub(super) fn try_unify_abstract_consts<'tcx>(
558558
tcx: TyCtxt<'tcx>,
559-
(a, b): (ty::Unevaluated<'tcx>, ty::Unevaluated<'tcx>),
559+
(a, b): (ty::Unevaluated<'tcx, ()>, ty::Unevaluated<'tcx, ()>),
560560
) -> bool {
561561
(|| {
562562
if let Some(a) = AbstractConst::new(tcx, a)? {

compiler/rustc_trait_selection/src/traits/fulfill.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ impl<'a, 'b, 'tcx> FulfillProcessor<'a, 'b, 'tcx> {
580580
if let (ty::ConstKind::Unevaluated(a), ty::ConstKind::Unevaluated(b)) =
581581
(c1.val, c2.val)
582582
{
583-
if infcx.try_unify_abstract_consts(a, b) {
583+
if infcx.try_unify_abstract_consts(a.shrink(), b.shrink()) {
584584
return ProcessResult::Changed(vec![]);
585585
}
586586
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
623623
if let (ty::ConstKind::Unevaluated(a), ty::ConstKind::Unevaluated(b)) =
624624
(c1.val, c2.val)
625625
{
626-
if self.infcx.try_unify_abstract_consts(a, b) {
626+
if self.infcx.try_unify_abstract_consts(a.shrink(), b.shrink()) {
627627
return Ok(EvaluatedToOk);
628628
}
629629
}

compiler/rustc_typeck/src/collect.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,8 +2355,10 @@ fn const_evaluatable_predicates_of<'tcx>(
23552355
if let ty::ConstKind::Unevaluated(uv) = ct.val {
23562356
assert_eq!(uv.promoted, None);
23572357
let span = self.tcx.hir().span(c.hir_id);
2358-
self.preds
2359-
.insert((ty::PredicateKind::ConstEvaluatable(uv).to_predicate(self.tcx), span));
2358+
self.preds.insert((
2359+
ty::PredicateKind::ConstEvaluatable(uv.shrink()).to_predicate(self.tcx),
2360+
span,
2361+
));
23602362
}
23612363
}
23622364

0 commit comments

Comments
 (0)