Skip to content

Commit c5c6ef7

Browse files
committed
stop folding UnevaluatedConst
1 parent 116d35d commit c5c6ef7

File tree

6 files changed

+9
-91
lines changed

6 files changed

+9
-91
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use super::ScalarInt;
1414

1515
/// An unevaluated (potentially generic) constant used in the type-system.
1616
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Lift)]
17-
#[derive(Hash, HashStable)]
17+
#[derive(Hash, HashStable, TypeFoldable, TypeVisitable)]
1818
pub struct UnevaluatedConst<'tcx> {
1919
pub def: ty::WithOptConstParam<DefId>,
2020
pub substs: SubstsRef<'tcx>,

compiler/rustc_middle/src/ty/flags.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ impl FlagComputation {
3434
result.flags
3535
}
3636

37-
pub fn for_unevaluated_const(uv: ty::UnevaluatedConst<'_>) -> TypeFlags {
38-
let mut result = FlagComputation::new();
39-
result.add_unevaluated_const(uv);
40-
result.flags
41-
}
42-
4337
fn add_flags(&mut self, flags: TypeFlags) {
4438
self.flags = self.flags | flags;
4539
}
@@ -289,7 +283,10 @@ impl FlagComputation {
289283
fn add_const(&mut self, c: ty::Const<'_>) {
290284
self.add_ty(c.ty());
291285
match c.kind() {
292-
ty::ConstKind::Unevaluated(unevaluated) => self.add_unevaluated_const(unevaluated),
286+
ty::ConstKind::Unevaluated(uv) => {
287+
self.add_substs(uv.substs);
288+
self.add_flags(TypeFlags::HAS_CT_PROJECTION);
289+
}
293290
ty::ConstKind::Infer(infer) => {
294291
self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
295292
match infer {
@@ -313,11 +310,6 @@ impl FlagComputation {
313310
}
314311
}
315312

316-
fn add_unevaluated_const(&mut self, ct: ty::UnevaluatedConst<'_>) {
317-
self.add_substs(ct.substs);
318-
self.add_flags(TypeFlags::HAS_CT_PROJECTION);
319-
}
320-
321313
fn add_existential_projection(&mut self, projection: &ty::ExistentialProjection<'_>) {
322314
self.add_substs(projection.substs);
323315
match projection.term.unpack() {

compiler/rustc_middle/src/ty/fold.rs

-21
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,6 @@ pub trait TypeFolder<'tcx>: FallibleTypeFolder<'tcx, Error = !> {
126126
c.super_fold_with(self)
127127
}
128128

129-
fn fold_ty_unevaluated(
130-
&mut self,
131-
uv: ty::UnevaluatedConst<'tcx>,
132-
) -> ty::UnevaluatedConst<'tcx> {
133-
uv.super_fold_with(self)
134-
}
135-
136129
fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
137130
p.super_fold_with(self)
138131
}
@@ -169,13 +162,6 @@ pub trait FallibleTypeFolder<'tcx>: Sized {
169162
c.try_super_fold_with(self)
170163
}
171164

172-
fn try_fold_ty_unevaluated(
173-
&mut self,
174-
c: ty::UnevaluatedConst<'tcx>,
175-
) -> Result<ty::UnevaluatedConst<'tcx>, Self::Error> {
176-
c.try_super_fold_with(self)
177-
}
178-
179165
fn try_fold_predicate(
180166
&mut self,
181167
p: ty::Predicate<'tcx>,
@@ -215,13 +201,6 @@ where
215201
Ok(self.fold_const(c))
216202
}
217203

218-
fn try_fold_ty_unevaluated(
219-
&mut self,
220-
c: ty::UnevaluatedConst<'tcx>,
221-
) -> Result<ty::UnevaluatedConst<'tcx>, !> {
222-
Ok(self.fold_ty_unevaluated(c))
223-
}
224-
225204
fn try_fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> Result<ty::Predicate<'tcx>, !> {
226205
Ok(self.fold_predicate(p))
227206
}

compiler/rustc_middle/src/ty/structural_impls.rs

-21
Original file line numberDiff line numberDiff line change
@@ -832,27 +832,6 @@ impl<'tcx> TypeVisitable<'tcx> for InferConst<'tcx> {
832832
}
833833
}
834834

835-
impl<'tcx> TypeFoldable<'tcx> for ty::UnevaluatedConst<'tcx> {
836-
fn try_fold_with<F: FallibleTypeFolder<'tcx>>(self, folder: &mut F) -> Result<Self, F::Error> {
837-
folder.try_fold_ty_unevaluated(self)
838-
}
839-
}
840-
841-
impl<'tcx> TypeVisitable<'tcx> for ty::UnevaluatedConst<'tcx> {
842-
fn visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
843-
visitor.visit_ty_unevaluated(*self)
844-
}
845-
}
846-
847-
impl<'tcx> TypeSuperFoldable<'tcx> for ty::UnevaluatedConst<'tcx> {
848-
fn try_super_fold_with<F: FallibleTypeFolder<'tcx>>(
849-
self,
850-
folder: &mut F,
851-
) -> Result<Self, F::Error> {
852-
Ok(ty::UnevaluatedConst { def: self.def, substs: self.substs.try_fold_with(folder)? })
853-
}
854-
}
855-
856835
impl<'tcx> TypeSuperVisitable<'tcx> for ty::UnevaluatedConst<'tcx> {
857836
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> ControlFlow<V::BreakTy> {
858837
self.substs.visit_with(visitor)

compiler/rustc_middle/src/ty/visit.rs

-22
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,6 @@ pub trait TypeVisitor<'tcx>: Sized {
197197
c.super_visit_with(self)
198198
}
199199

200-
fn visit_ty_unevaluated(
201-
&mut self,
202-
uv: ty::UnevaluatedConst<'tcx>,
203-
) -> ControlFlow<Self::BreakTy> {
204-
uv.super_visit_with(self)
205-
}
206-
207200
fn visit_predicate(&mut self, p: ty::Predicate<'tcx>) -> ControlFlow<Self::BreakTy> {
208201
p.super_visit_with(self)
209202
}
@@ -592,21 +585,6 @@ impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor {
592585
}
593586
}
594587

595-
#[inline]
596-
#[instrument(level = "trace", ret)]
597-
fn visit_ty_unevaluated(
598-
&mut self,
599-
uv: ty::UnevaluatedConst<'tcx>,
600-
) -> ControlFlow<Self::BreakTy> {
601-
let flags = FlagComputation::for_unevaluated_const(uv);
602-
trace!(r.flags=?flags);
603-
if flags.intersects(self.flags) {
604-
ControlFlow::Break(FoundFlags)
605-
} else {
606-
ControlFlow::CONTINUE
607-
}
608-
}
609-
610588
#[inline]
611589
#[instrument(level = "trace", ret)]
612590
fn visit_predicate(&mut self, predicate: ty::Predicate<'tcx>) -> ControlFlow<Self::BreakTy> {

compiler/rustc_trait_selection/src/traits/object_safety.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -837,24 +837,14 @@ fn contains_illegal_self_type_reference<'tcx, T: TypeVisitable<'tcx>>(
837837
}
838838
}
839839

840-
fn visit_ty_unevaluated(
841-
&mut self,
842-
uv: ty::UnevaluatedConst<'tcx>,
843-
) -> ControlFlow<Self::BreakTy> {
840+
fn visit_const(&mut self, ct: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
844841
// Constants can only influence object safety if they reference `Self`.
845842
// This is only possible for unevaluated constants, so we walk these here.
846843
//
847-
// If `AbstractConst::new` returned an error we already failed compilation
844+
// If `AbstractConst::from_const` returned an error we already failed compilation
848845
// so we don't have to emit an additional error here.
849-
//
850-
// We currently recurse into abstract consts here but do not recurse in
851-
// `is_const_evaluatable`. This means that the object safety check is more
852-
// liberal than the const eval check.
853-
//
854-
// This shouldn't really matter though as we can't really use any
855-
// constants which are not considered const evaluatable.
856846
use rustc_middle::ty::abstract_const::Node;
857-
if let Ok(Some(ct)) = AbstractConst::new(self.tcx, uv) {
847+
if let Ok(Some(ct)) = AbstractConst::from_const(self.tcx, ct) {
858848
walk_abstract_const(self.tcx, ct, |node| match node.root(self.tcx) {
859849
Node::Leaf(leaf) => self.visit_const(leaf),
860850
Node::Cast(_, _, ty) => self.visit_ty(ty),
@@ -863,7 +853,7 @@ fn contains_illegal_self_type_reference<'tcx, T: TypeVisitable<'tcx>>(
863853
}
864854
})
865855
} else {
866-
ControlFlow::CONTINUE
856+
ct.super_visit_with(self)
867857
}
868858
}
869859
}

0 commit comments

Comments
 (0)