1
Fork 0

Clean up trivial traversal/lift impl generator macro calls.

We have four macros for generating trivial traversal (fold/visit) and
lift impls.
- `rustc_ir::TrivialTypeTraversalImpls`
- `rustc_middle::TrivialTypeTraversalImpls`
- `rustc_middle::TrivialLiftImpls`
- `rustc_middle::TrivialTypeTraversalAndLiftImpls`

The first two are very similar. The last one just combines the second
and third one.

The macros themselves are ok, but their use is a mess. This commit does
the following.
- Removes types that no longer need a lift and/or traversal impl from
  the macro calls.
- Consolidates the macro calls into the smallest number of calls
  possible, with each one mentioning as many types as possible.
- Orders the types within those macro calls alphabetically, and makes
  the module qualification more consistent.
- Eliminates `rustc_middle::mir::type_foldable`, because the macro calls
  were merged and the manual `TypeFoldable` impls are better placed in
  `structural_impls.rs`, alongside all the other ones.

This makes the code more concise. Moving forward, it also makes it more
obvious where new types should be added.
This commit is contained in:
Nicholas Nethercote 2025-02-05 12:02:04 +11:00
parent e5f11af042
commit d28678e621
11 changed files with 100 additions and 151 deletions

View file

@ -142,10 +142,6 @@ impl<'tcx, R> QueryResponse<'tcx, R> {
pub type QueryOutlivesConstraint<'tcx> =
(ty::OutlivesPredicate<'tcx, GenericArg<'tcx>>, ConstraintCategory<'tcx>);
TrivialTypeTraversalImpls! {
crate::infer::canonical::Certainty,
}
#[derive(Default)]
pub struct CanonicalParamEnvCache<'tcx> {
map: Lock<

View file

@ -163,6 +163,7 @@ impl<'tcx> graph::Predecessors for BasicBlocks<'tcx> {
}
}
// Done here instead of in `structural_impls.rs` because `Cache` is private, as is `basic_blocks`.
TrivialTypeTraversalImpls! { Cache }
impl<S: Encoder> Encodable<S> for Cache {

View file

@ -95,8 +95,6 @@ impl From<ReportedErrorInfo> for ErrorGuaranteed {
}
}
TrivialTypeTraversalImpls! { ErrorHandled }
pub type EvalToAllocationRawResult<'tcx> = Result<ConstAlloc<'tcx>, ErrorHandled>;
pub type EvalStaticInitializerRawResult<'tcx> = Result<ConstAllocation<'tcx>, ErrorHandled>;
pub type EvalToConstValueResult<'tcx> = Result<ConstValue<'tcx>, ErrorHandled>;

View file

@ -34,7 +34,6 @@ use self::visit::TyContext;
use crate::mir::interpret::{AllocRange, Scalar};
use crate::mir::visit::MirVisitable;
use crate::ty::codec::{TyDecoder, TyEncoder};
use crate::ty::fold::{FallibleTypeFolder, TypeFoldable};
use crate::ty::print::{FmtPrinter, Printer, pretty_print_const, with_no_trimmed_paths};
use crate::ty::visit::TypeVisitableExt;
use crate::ty::{
@ -59,7 +58,6 @@ pub mod tcx;
mod terminator;
pub mod traversal;
mod type_foldable;
pub mod visit;
pub use consts::*;
@ -927,8 +925,6 @@ pub enum BindingForm<'tcx> {
RefForGuard,
}
TrivialTypeTraversalImpls! { BindingForm<'tcx> }
mod binding_form_impl {
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_query_system::ich::StableHashingContext;

View file

@ -1,67 +0,0 @@
//! `TypeFoldable` implementations for MIR types
use rustc_ast::InlineAsmTemplatePiece;
use rustc_hir::UnsafeBinderCastKind;
use rustc_hir::def_id::LocalDefId;
use super::*;
TrivialTypeTraversalImpls! {
BlockTailInfo,
MirPhase,
SourceInfo,
FakeReadCause,
RetagKind,
SourceScope,
SourceScopeLocalData,
UserTypeAnnotationIndex,
BorrowKind,
RawPtrKind,
CastKind,
BasicBlock,
SwitchTargets,
CoroutineKind,
CoroutineSavedLocal,
UnsafeBinderCastKind,
}
TrivialTypeTraversalImpls! {
ConstValue<'tcx>,
NullOp<'tcx>,
}
impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for &'tcx [InlineAsmTemplatePiece] {
fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
self,
_folder: &mut F,
) -> Result<Self, F::Error> {
Ok(self)
}
}
impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for &'tcx [Span] {
fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
self,
_folder: &mut F,
) -> Result<Self, F::Error> {
Ok(self)
}
}
impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for &'tcx ty::List<LocalDefId> {
fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
self,
_folder: &mut F,
) -> Result<Self, F::Error> {
Ok(self)
}
}
impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for &'tcx ty::List<PlaceElem<'tcx>> {
fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
self,
folder: &mut F,
) -> Result<Self, F::Error> {
ty::util::fold_list(self, folder, |tcx, v| tcx.mk_place_elems(v))
}
}

View file

@ -427,10 +427,6 @@ pub enum IsConstable {
Ctor,
}
TrivialTypeTraversalAndLiftImpls! {
IsConstable,
}
/// The 'location' at which we try to perform HIR-based wf checking.
/// This information is used to obtain an `hir::Ty`, which
/// we can walk in order to obtain precise spans for any

View file

@ -260,8 +260,6 @@ impl From<ErrorGuaranteed> for OverflowError {
}
}
TrivialTypeTraversalImpls! { OverflowError }
impl<'tcx> From<OverflowError> for SelectionError<'tcx> {
fn from(overflow_error: OverflowError) -> SelectionError<'tcx> {
match overflow_error {

View file

@ -30,8 +30,6 @@ impl From<ErrorGuaranteed> for NotConstEvaluatable {
}
}
TrivialTypeTraversalImpls! { NotConstEvaluatable }
pub type BoundAbstractConst<'tcx> =
Result<Option<EarlyBinder<'tcx, ty::Const<'tcx>>>, ErrorGuaranteed>;

View file

@ -76,11 +76,11 @@ use crate::traits::solve::{
};
use crate::ty::predicate::ExistentialPredicateStableCmpExt as _;
use crate::ty::{
self, AdtDef, AdtDefData, AdtKind, Binder, BoundConstness, Clause, Clauses, Const, GenericArg,
GenericArgs, GenericArgsRef, GenericParamDefKind, ImplPolarity, List, ListWithCachedTypeInfo,
ParamConst, ParamTy, Pattern, PatternKind, PolyExistentialPredicate, PolyFnSig, Predicate,
PredicateKind, PredicatePolarity, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty,
TyKind, TyVid, Visibility,
self, AdtDef, AdtDefData, AdtKind, Binder, Clause, Clauses, Const, GenericArg, GenericArgs,
GenericArgsRef, GenericParamDefKind, List, ListWithCachedTypeInfo, ParamConst, ParamTy,
Pattern, PatternKind, PolyExistentialPredicate, PolyFnSig, Predicate, PredicateKind,
PredicatePolarity, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyVid,
Visibility,
};
#[allow(rustc::usage_of_ty_tykind)]
@ -2279,10 +2279,6 @@ macro_rules! nop_slice_lift {
nop_slice_lift! {ty::ValTree<'a> => ty::ValTree<'tcx>}
TrivialLiftImpls! {
ImplPolarity, PredicatePolarity, Promoted, BoundConstness,
}
macro_rules! sty_debug_print {
($fmt: expr, $ctxt: expr, $($variant: ident),*) => {{
// Curious inner module to allow variant names to be used as

View file

@ -6,15 +6,18 @@
use std::fmt::{self, Debug};
use rustc_abi::TyAndLayout;
use rustc_ast::InlineAsmTemplatePiece;
use rustc_ast_ir::try_visit;
use rustc_ast_ir::visit::VisitorResult;
use rustc_hir::def::Namespace;
use rustc_hir::def_id::LocalDefId;
use rustc_span::Span;
use rustc_span::source_map::Spanned;
use rustc_type_ir::ConstKind;
use super::print::PrettyPrinter;
use super::{GenericArg, GenericArgKind, Pattern, Region};
use crate::mir::interpret;
use crate::mir::PlaceElem;
use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeSuperFoldable};
use crate::ty::print::{FmtPrinter, Printer, with_no_trimmed_paths};
use crate::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor};
@ -221,76 +224,83 @@ impl<'tcx> fmt::Debug for Region<'tcx> {
// copy...), just add them to one of these lists as appropriate.
// For things for which the type library provides traversal implementations
// for all Interners, we only need to provide a Lift implementation:
// for all Interners, we only need to provide a Lift implementation.
TrivialLiftImpls! {
(),
bool,
usize,
u64,
crate::mir::Promoted,
crate::mir::interpret::AllocId,
crate::mir::interpret::Scalar,
rustc_abi::ExternAbi,
rustc_abi::Size,
rustc_hir::Safety,
rustc_type_ir::BoundConstness,
rustc_type_ir::PredicatePolarity,
}
// For some things about which the type library does not know, or does not
// provide any traversal implementations, we need to provide a traversal
// implementation (only for TyCtxt<'_> interners).
TrivialTypeTraversalImpls! {
::rustc_abi::FieldIdx,
::rustc_abi::VariantIdx,
crate::middle::region::Scope,
::rustc_ast::InlineAsmOptions,
::rustc_ast::InlineAsmTemplatePiece,
::rustc_ast::NodeId,
::rustc_hir::def::Res,
::rustc_hir::def_id::LocalDefId,
::rustc_hir::ByRef,
::rustc_hir::HirId,
::rustc_hir::MatchSource,
::rustc_target::asm::InlineAsmRegOrRegClass,
crate::mir::coverage::BlockMarkerId,
crate::mir::coverage::CounterId,
crate::mir::coverage::ExpressionId,
crate::mir::coverage::ConditionId,
crate::infer::canonical::Certainty,
crate::mir::BasicBlock,
crate::mir::BindingForm<'tcx>,
crate::mir::BlockTailInfo,
crate::mir::BorrowKind,
crate::mir::CastKind,
crate::mir::ConstValue<'tcx>,
crate::mir::CoroutineSavedLocal,
crate::mir::FakeReadCause,
crate::mir::Local,
crate::mir::MirPhase,
crate::mir::NullOp<'tcx>,
crate::mir::Promoted,
crate::ty::adjustment::AutoBorrowMutability,
crate::mir::RawPtrKind,
crate::mir::RetagKind,
crate::mir::SourceInfo,
crate::mir::SourceScope,
crate::mir::SourceScopeLocalData,
crate::mir::SwitchTargets,
crate::traits::IsConstable,
crate::traits::OverflowError,
crate::ty::AdtKind,
crate::ty::BoundRegion,
// Including `BoundRegionKind` is a *bit* dubious, but direct
// references to bound region appear in `ty::Error`, and aren't
// really meant to be folded. In general, we can only fold a fully
// general `Region`.
crate::ty::BoundRegionKind,
crate::ty::AssocItem,
crate::ty::AssocKind,
crate::ty::BoundRegion,
crate::ty::BoundVar,
crate::ty::Placeholder<crate::ty::BoundRegion>,
crate::ty::Placeholder<crate::ty::BoundTy>,
crate::ty::Placeholder<ty::BoundVar>,
crate::ty::LateParamRegion,
crate::ty::UserTypeAnnotationIndex,
crate::ty::ValTree<'tcx>,
crate::ty::abstract_const::NotConstEvaluatable,
crate::ty::adjustment::AutoBorrowMutability,
crate::ty::adjustment::PointerCoercion,
::rustc_span::Ident,
::rustc_span::Span,
::rustc_span::Symbol,
ty::BoundVar,
ty::ValTree<'tcx>,
rustc_abi::FieldIdx,
rustc_abi::VariantIdx,
rustc_ast::InlineAsmOptions,
rustc_ast::InlineAsmTemplatePiece,
rustc_hir::CoroutineKind,
rustc_hir::HirId,
rustc_hir::MatchSource,
rustc_hir::def_id::LocalDefId,
rustc_span::Ident,
rustc_span::Span,
rustc_span::Symbol,
rustc_target::asm::InlineAsmRegOrRegClass,
}
// For some things about which the type library does not know, or does not
// provide any traversal implementations, we need to provide a traversal
// implementation and a lift implementation (the former only for TyCtxt<'_>
// interners).
TrivialTypeTraversalAndLiftImpls! {
::rustc_hir::def_id::DefId,
crate::ty::ClosureKind,
crate::ty::ParamConst,
crate::ty::ParamTy,
crate::ty::instance::ReifyReason,
interpret::AllocId,
interpret::CtfeProvenance,
interpret::Scalar,
rustc_abi::Size,
}
TrivialLiftImpls! {
::rustc_hir::Safety,
::rustc_abi::ExternAbi,
rustc_hir::def_id::DefId,
}
///////////////////////////////////////////////////////////////////////////
@ -672,3 +682,39 @@ impl<'tcx, T: TypeFoldable<TyCtxt<'tcx>> + Debug + Clone> TypeFoldable<TyCtxt<'t
})
}
}
impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for &'tcx [InlineAsmTemplatePiece] {
fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
self,
_folder: &mut F,
) -> Result<Self, F::Error> {
Ok(self)
}
}
impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for &'tcx [Span] {
fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
self,
_folder: &mut F,
) -> Result<Self, F::Error> {
Ok(self)
}
}
impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for &'tcx ty::List<LocalDefId> {
fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
self,
_folder: &mut F,
) -> Result<Self, F::Error> {
Ok(self)
}
}
impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for &'tcx ty::List<PlaceElem<'tcx>> {
fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
self,
folder: &mut F,
) -> Result<Self, F::Error> {
ty::util::fold_list(self, folder, |tcx, v| tcx.mk_place_elems(v))
}
}

View file

@ -47,23 +47,14 @@ TrivialTypeTraversalImpls! {
u16,
u32,
u64,
String,
crate::AliasRelationDirection,
crate::AliasTyKind,
crate::BoundConstness,
crate::DebruijnIndex,
crate::FloatTy,
crate::InferTy,
crate::IntVarValue,
crate::PredicatePolarity,
crate::RegionVid,
crate::UniverseIndex,
crate::Variance,
crate::solve::BuiltinImplSource,
crate::solve::Certainty,
crate::solve::GoalSource,
crate::solve::MaybeCause,
crate::solve::NoSolution,
crate::UniverseIndex,
crate::Variance,
rustc_ast_ir::Movability,
rustc_ast_ir::Mutability,
}