From 4ff22ddc9d0bec9b12a4a878ef4c4a89297081da Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 10 Nov 2024 18:34:59 +0100 Subject: [PATCH 1/2] Move some TypeVisitable/TypeFoldable impls to rustc_type_ir --- compiler/rustc_type_ir/src/const_kind.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_type_ir/src/const_kind.rs b/compiler/rustc_type_ir/src/const_kind.rs index 70a8509b51339..327a6671a4624 100644 --- a/compiler/rustc_type_ir/src/const_kind.rs +++ b/compiler/rustc_type_ir/src/const_kind.rs @@ -7,7 +7,10 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContext}; use rustc_type_ir_macros::{Lift_Generic, TypeFoldable_Generic, TypeVisitable_Generic}; -use crate::{self as ty, DebruijnIndex, Interner}; +use crate::{ + self as ty, DebruijnIndex, FallibleTypeFolder, Interner, TypeFoldable, TypeFolder, + TypeVisitable, TypeVisitor, VisitorResult, +}; /// Represents a constant in Rust. #[derive_where(Clone, Copy, Hash, PartialEq, Eq; I: Interner)] @@ -120,3 +123,19 @@ impl HashStable for InferConst { } } } + +impl TypeFoldable for InferConst { + fn try_fold_with>(self, _folder: &mut F) -> Result { + Ok(self) + } + + fn fold_with>(self, _folder: &mut F) -> Self { + self + } +} + +impl TypeVisitable for InferConst { + fn visit_with>(&self, _visitor: &mut V) -> V::Result { + V::Result::output() + } +} From 29799c2e21a9d6f946283ce6298a43d24716acdc Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 10 Nov 2024 18:35:41 +0100 Subject: [PATCH 2/2] Add a missing UpcastFrom impl in rustc_type_ir --- compiler/rustc_middle/src/ty/predicate.rs | 12 +--------- compiler/rustc_type_ir/src/const_kind.rs | 28 ++++++----------------- compiler/rustc_type_ir/src/predicate.rs | 9 ++++++++ 3 files changed, 17 insertions(+), 32 deletions(-) diff --git a/compiler/rustc_middle/src/ty/predicate.rs b/compiler/rustc_middle/src/ty/predicate.rs index 73a6f1829afe6..59e00f85957e7 100644 --- a/compiler/rustc_middle/src/ty/predicate.rs +++ b/compiler/rustc_middle/src/ty/predicate.rs @@ -6,8 +6,7 @@ use rustc_macros::{HashStable, extension}; use rustc_type_ir as ir; use crate::ty::{ - self, DebruijnIndex, EarlyBinder, PredicatePolarity, Ty, TyCtxt, TypeFlags, Upcast, UpcastFrom, - WithCachedTypeInfo, + self, DebruijnIndex, EarlyBinder, Ty, TyCtxt, TypeFlags, Upcast, UpcastFrom, WithCachedTypeInfo, }; pub type TraitRef<'tcx> = ir::TraitRef>; @@ -536,15 +535,6 @@ impl<'tcx> UpcastFrom, ty::Binder<'tcx, TraitRef<'tcx>>> for Clause } } -impl<'tcx> UpcastFrom, ty::Binder<'tcx, TraitRef<'tcx>>> for PolyTraitPredicate<'tcx> { - fn upcast_from(from: ty::Binder<'tcx, TraitRef<'tcx>>, _tcx: TyCtxt<'tcx>) -> Self { - from.map_bound(|trait_ref| TraitPredicate { - trait_ref, - polarity: PredicatePolarity::Positive, - }) - } -} - impl<'tcx> UpcastFrom, TraitPredicate<'tcx>> for Predicate<'tcx> { fn upcast_from(from: TraitPredicate<'tcx>, tcx: TyCtxt<'tcx>) -> Self { PredicateKind::Clause(ClauseKind::Trait(from)).upcast(tcx) diff --git a/compiler/rustc_type_ir/src/const_kind.rs b/compiler/rustc_type_ir/src/const_kind.rs index 327a6671a4624..4be38d4e702d7 100644 --- a/compiler/rustc_type_ir/src/const_kind.rs +++ b/compiler/rustc_type_ir/src/const_kind.rs @@ -7,10 +7,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContext}; use rustc_type_ir_macros::{Lift_Generic, TypeFoldable_Generic, TypeVisitable_Generic}; -use crate::{ - self as ty, DebruijnIndex, FallibleTypeFolder, Interner, TypeFoldable, TypeFolder, - TypeVisitable, TypeVisitor, VisitorResult, -}; +use crate::{self as ty, DebruijnIndex, Interner}; /// Represents a constant in Rust. #[derive_where(Clone, Copy, Hash, PartialEq, Eq; I: Interner)] @@ -95,10 +92,15 @@ rustc_index::newtype_index! { /// An inference variable for a const, for use in const generics. #[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)] +#[derive(TypeVisitable_Generic, TypeFoldable_Generic)] #[cfg_attr(feature = "nightly", derive(Encodable_NoContext, Decodable_NoContext))] pub enum InferConst { /// Infer the value of the const. - Var(ConstVid), + Var( + #[type_foldable(identity)] + #[type_visitable(ignore)] + ConstVid, + ), /// A fresh const variable. See `infer::freshen` for more details. Fresh(u32), } @@ -123,19 +125,3 @@ impl HashStable for InferConst { } } } - -impl TypeFoldable for InferConst { - fn try_fold_with>(self, _folder: &mut F) -> Result { - Ok(self) - } - - fn fold_with>(self, _folder: &mut F) -> Self { - self - } -} - -impl TypeVisitable for InferConst { - fn visit_with>(&self, _visitor: &mut V) -> V::Result { - V::Result::output() - } -} diff --git a/compiler/rustc_type_ir/src/predicate.rs b/compiler/rustc_type_ir/src/predicate.rs index c9489c98ccea1..9e4447ccd9939 100644 --- a/compiler/rustc_type_ir/src/predicate.rs +++ b/compiler/rustc_type_ir/src/predicate.rs @@ -184,6 +184,15 @@ impl UpcastFrom> for TraitPredicate { } } +impl UpcastFrom>> for ty::Binder> { + fn upcast_from(from: ty::Binder>, _tcx: I) -> Self { + from.map_bound(|trait_ref| TraitPredicate { + trait_ref, + polarity: PredicatePolarity::Positive, + }) + } +} + impl fmt::Debug for TraitPredicate { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "TraitPredicate({:?}, polarity:{:?})", self.trait_ref, self.polarity)