Skip to content

Commit 10955ec

Browse files
committed
Move PatternKind and TypeWalker into rustc_type_ir
1 parent 00a0b6a commit 10955ec

File tree

12 files changed

+136
-98
lines changed

12 files changed

+136
-98
lines changed

compiler/rustc_middle/src/arena.rs

-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ macro_rules! arena_types {
8989
[] name_set: rustc_data_structures::unord::UnordSet<rustc_span::Symbol>,
9090
[] autodiff_item: rustc_ast::expand::autodiff_attrs::AutoDiffItem,
9191
[] ordered_name_set: rustc_data_structures::fx::FxIndexSet<rustc_span::Symbol>,
92-
[] pats: rustc_middle::ty::PatternKind<'tcx>,
9392
[] valtree: rustc_middle::ty::ValTreeKind<'tcx>,
9493

9594
// Note that this deliberately duplicates items in the `rustc_hir::arena`,

compiler/rustc_middle/src/ty/consts.rs

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::borrow::Cow;
33
use rustc_data_structures::intern::Interned;
44
use rustc_error_messages::MultiSpan;
55
use rustc_macros::HashStable;
6+
use rustc_type_ir::walk::TypeWalker;
67
use rustc_type_ir::{self as ir, TypeFlags, WithCachedTypeInfo};
78

89
use crate::ty::{self, Ty, TyCtxt};
@@ -243,4 +244,18 @@ impl<'tcx> Const<'tcx> {
243244
pub fn is_ct_infer(self) -> bool {
244245
matches!(self.kind(), ty::ConstKind::Infer(_))
245246
}
247+
248+
/// Iterator that walks `self` and any types reachable from
249+
/// `self`, in depth-first order. Note that just walks the types
250+
/// that appear in `self`, it does not descend into the fields of
251+
/// structs or variants. For example:
252+
///
253+
/// ```text
254+
/// isize => { isize }
255+
/// Foo<Bar<isize>> => { Foo<Bar<isize>>, Bar<isize>, isize }
256+
/// [isize] => { [isize], isize }
257+
/// ```
258+
pub fn walk(self) -> TypeWalker<TyCtxt<'tcx>> {
259+
TypeWalker::new(self.into())
260+
}
246261
}

compiler/rustc_middle/src/ty/generic_args.rs

+15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_hir::def_id::DefId;
1111
use rustc_macros::{HashStable, TyDecodable, TyEncodable, extension};
1212
use rustc_serialize::{Decodable, Encodable};
1313
use rustc_type_ir::WithCachedTypeInfo;
14+
use rustc_type_ir::walk::TypeWalker;
1415
use smallvec::SmallVec;
1516

1617
use crate::ty::codec::{TyDecoder, TyEncoder};
@@ -297,6 +298,20 @@ impl<'tcx> GenericArg<'tcx> {
297298
GenericArgKind::Const(ct) => ct.is_ct_infer(),
298299
}
299300
}
301+
302+
/// Iterator that walks `self` and any types reachable from
303+
/// `self`, in depth-first order. Note that just walks the types
304+
/// that appear in `self`, it does not descend into the fields of
305+
/// structs or variants. For example:
306+
///
307+
/// ```text
308+
/// isize => { isize }
309+
/// Foo<Bar<isize>> => { Foo<Bar<isize>>, Bar<isize>, isize }
310+
/// [isize] => { [isize], isize }
311+
/// ```
312+
pub fn walk(self) -> TypeWalker<TyCtxt<'tcx>> {
313+
TypeWalker::new(self)
314+
}
300315
}
301316

302317
impl<'a, 'tcx> Lift<TyCtxt<'tcx>> for GenericArg<'a> {

compiler/rustc_middle/src/ty/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ pub mod significant_drop_order;
127127
pub mod trait_def;
128128
pub mod util;
129129
pub mod vtable;
130-
pub mod walk;
131130

132131
mod adt;
133132
mod assoc;

compiler/rustc_middle/src/ty/pattern.rs

+20-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
use std::fmt;
22

33
use rustc_data_structures::intern::Interned;
4-
use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
5-
use rustc_type_ir::{FlagComputation, Flags};
4+
use rustc_macros::HashStable;
5+
use rustc_type_ir::ir_print::IrPrint;
6+
use rustc_type_ir::{
7+
FlagComputation, Flags, {self as ir},
8+
};
69

10+
use super::TyCtxt;
711
use crate::ty;
812

13+
pub type PatternKind<'tcx> = ir::PatternKind<TyCtxt<'tcx>>;
14+
915
#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable)]
1016
#[rustc_pass_by_value]
1117
pub struct Pattern<'tcx>(pub Interned<'tcx, PatternKind<'tcx>>);
@@ -43,9 +49,9 @@ impl<'tcx> fmt::Debug for Pattern<'tcx> {
4349
}
4450
}
4551

46-
impl<'tcx> fmt::Debug for PatternKind<'tcx> {
47-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48-
match *self {
52+
impl<'tcx> IrPrint<PatternKind<'tcx>> for TyCtxt<'tcx> {
53+
fn print(t: &PatternKind<'tcx>, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54+
match *t {
4955
PatternKind::Range { start, end } => {
5056
write!(f, "{start}")?;
5157

@@ -73,10 +79,15 @@ impl<'tcx> fmt::Debug for PatternKind<'tcx> {
7379
}
7480
}
7581
}
82+
83+
fn print_debug(t: &PatternKind<'tcx>, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
84+
Self::print(t, fmt)
85+
}
7686
}
7787

78-
#[derive(Clone, PartialEq, Eq, Hash)]
79-
#[derive(HashStable, TyEncodable, TyDecodable, TypeVisitable, TypeFoldable)]
80-
pub enum PatternKind<'tcx> {
81-
Range { start: ty::Const<'tcx>, end: ty::Const<'tcx> },
88+
impl<'tcx> rustc_type_ir::inherent::IntoKind for Pattern<'tcx> {
89+
type Kind = PatternKind<'tcx>;
90+
fn kind(self) -> Self::Kind {
91+
*self
92+
}
8293
}

compiler/rustc_middle/src/ty/sty.rs

+15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_hir::def_id::DefId;
1616
use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, extension};
1717
use rustc_span::{DUMMY_SP, Span, Symbol, sym};
1818
use rustc_type_ir::TyKind::*;
19+
use rustc_type_ir::walk::TypeWalker;
1920
use rustc_type_ir::{self as ir, BoundVar, CollectAndApply, DynKind, TypeVisitableExt, elaborate};
2021
use tracing::instrument;
2122
use ty::util::{AsyncDropGlueMorphology, IntTypeExt};
@@ -2029,6 +2030,20 @@ impl<'tcx> Ty<'tcx> {
20292030
pub fn is_known_rigid(self) -> bool {
20302031
self.kind().is_known_rigid()
20312032
}
2033+
2034+
/// Iterator that walks `self` and any types reachable from
2035+
/// `self`, in depth-first order. Note that just walks the types
2036+
/// that appear in `self`, it does not descend into the fields of
2037+
/// structs or variants. For example:
2038+
///
2039+
/// ```text
2040+
/// isize => { isize }
2041+
/// Foo<Bar<isize>> => { Foo<Bar<isize>>, Bar<isize>, isize }
2042+
/// [isize] => { [isize], isize }
2043+
/// ```
2044+
pub fn walk(self) -> TypeWalker<TyCtxt<'tcx>> {
2045+
TypeWalker::new(self.into())
2046+
}
20322047
}
20332048

20342049
impl<'tcx> rustc_type_ir::inherent::Tys<TyCtxt<'tcx>> for &'tcx ty::List<Ty<'tcx>> {

compiler/rustc_type_ir/src/inherent.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ pub trait Span<I: Interner>: Copy + Debug + Hash + Eq + TypeFoldable<I> {
583583

584584
pub trait SliceLike: Sized + Copy {
585585
type Item: Copy;
586-
type IntoIter: Iterator<Item = Self::Item>;
586+
type IntoIter: Iterator<Item = Self::Item> + DoubleEndedIterator;
587587

588588
fn iter(self) -> Self::IntoIter;
589589

compiler/rustc_type_ir/src/interner.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub trait Interner:
3131
+ IrPrint<ty::SubtypePredicate<Self>>
3232
+ IrPrint<ty::CoercePredicate<Self>>
3333
+ IrPrint<ty::FnSig<Self>>
34+
+ IrPrint<ty::PatternKind<Self>>
3435
{
3536
type DefId: DefId<Self>;
3637
type LocalDefId: Copy + Debug + Hash + Eq + Into<Self::DefId> + TypeFoldable<Self>;
@@ -104,7 +105,14 @@ pub trait Interner:
104105
type ErrorGuaranteed: Copy + Debug + Hash + Eq;
105106
type BoundExistentialPredicates: BoundExistentialPredicates<Self>;
106107
type AllocId: Copy + Debug + Hash + Eq;
107-
type Pat: Copy + Debug + Hash + Eq + Debug + Relate<Self> + Flags;
108+
type Pat: Copy
109+
+ Debug
110+
+ Hash
111+
+ Eq
112+
+ Debug
113+
+ Relate<Self>
114+
+ Flags
115+
+ IntoKind<Kind = ty::PatternKind<Self>>;
108116
type Safety: Safety<Self>;
109117
type Abi: Abi<Self>;
110118

compiler/rustc_type_ir/src/ir_print.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use std::fmt;
22

33
use crate::{
44
AliasTerm, AliasTy, Binder, CoercePredicate, ExistentialProjection, ExistentialTraitRef, FnSig,
5-
HostEffectPredicate, Interner, NormalizesTo, OutlivesPredicate, ProjectionPredicate,
6-
SubtypePredicate, TraitPredicate, TraitRef,
5+
HostEffectPredicate, Interner, NormalizesTo, OutlivesPredicate, PatternKind,
6+
ProjectionPredicate, SubtypePredicate, TraitPredicate, TraitRef,
77
};
88

99
pub trait IrPrint<T> {
@@ -57,9 +57,10 @@ define_display_via_print!(
5757
AliasTy,
5858
AliasTerm,
5959
FnSig,
60+
PatternKind,
6061
);
6162

62-
define_debug_via_print!(TraitRef, ExistentialTraitRef, ExistentialProjection);
63+
define_debug_via_print!(TraitRef, ExistentialTraitRef, ExistentialProjection, PatternKind);
6364

6465
impl<I: Interner, T> fmt::Display for OutlivesPredicate<I, T>
6566
where

compiler/rustc_type_ir/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub mod outlives;
3131
pub mod relate;
3232
pub mod search_graph;
3333
pub mod solve;
34+
pub mod walk;
3435

3536
// These modules are not `pub` since they are glob-imported.
3637
#[macro_use]
@@ -44,6 +45,7 @@ mod generic_arg;
4445
mod infer_ctxt;
4546
mod interner;
4647
mod opaque_ty;
48+
mod pattern;
4749
mod predicate;
4850
mod predicate_kind;
4951
mod region_kind;
@@ -67,6 +69,7 @@ pub use generic_arg::*;
6769
pub use infer_ctxt::*;
6870
pub use interner::*;
6971
pub use opaque_ty::*;
72+
pub use pattern::*;
7073
pub use predicate::*;
7174
pub use predicate_kind::*;
7275
pub use region_kind::*;

compiler/rustc_type_ir/src/pattern.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use derive_where::derive_where;
2+
#[cfg(feature = "nightly")]
3+
use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContext};
4+
use rustc_type_ir_macros::{Lift_Generic, TypeFoldable_Generic, TypeVisitable_Generic};
5+
6+
use crate::Interner;
7+
8+
#[derive_where(Clone, Copy, Hash, PartialEq, Eq; I: Interner)]
9+
#[derive(TypeVisitable_Generic, TypeFoldable_Generic, Lift_Generic)]
10+
#[cfg_attr(
11+
feature = "nightly",
12+
derive(Decodable_NoContext, Encodable_NoContext, HashStable_NoContext)
13+
)]
14+
pub enum PatternKind<I: Interner> {
15+
Range { start: I::Const, end: I::Const },
16+
}

0 commit comments

Comments
 (0)