Skip to content

Commit 147b854

Browse files
committed
Remove TyS
1 parent 87a04f5 commit 147b854

File tree

6 files changed

+65
-97
lines changed

6 files changed

+65
-97
lines changed

compiler/rustc_middle/src/arena.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(rustc::usage_of_ty_tykind)]
2+
13
/// This higher-order macro declares a list of types which can be allocated by `Arena`.
24
///
35
/// Specifying the `decode` modifier will add decode impls for `&T` and `&[T]` where `T` is the type
@@ -88,7 +90,7 @@ macro_rules! arena_types {
8890
[] hir_id_set: rustc_hir::HirIdSet,
8991

9092
// Interned types
91-
[] tys: rustc_type_ir::WithCachedTypeInfo<rustc_middle::ty::TyS<'tcx>>,
93+
[] tys: rustc_type_ir::WithCachedTypeInfo<rustc_middle::ty::TyKind<'tcx>>,
9294
[] predicates: rustc_type_ir::WithCachedTypeInfo<rustc_middle::ty::PredicateS<'tcx>>,
9395
[] consts: rustc_middle::ty::ConstS<'tcx>,
9496

compiler/rustc_middle/src/ty/context.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Type context book-keeping.
22
3+
#![allow(rustc::usage_of_ty_tykind)]
4+
35
use crate::arena::Arena;
46
use crate::dep_graph::{DepGraph, DepKindStruct};
57
use crate::hir::place::Place as HirPlace;
@@ -20,7 +22,7 @@ use crate::ty::{
2022
ClosureSizeProfileData, Const, ConstS, DefIdTree, FloatTy, FloatVar, FloatVid,
2123
GenericParamDefKind, InferTy, IntTy, IntVar, IntVid, List, ParamConst, ParamTy,
2224
PolyExistentialPredicate, PolyFnSig, Predicate, PredicateKind, PredicateS, ProjectionTy,
23-
Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar, TyVid, TypeAndMut,
25+
Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyVar, TyVid, TypeAndMut,
2426
UintTy, Visibility,
2527
};
2628
use crate::ty::{GenericArg, GenericArgKind, InternalSubsts, SubstsRef, UserSubsts};
@@ -137,7 +139,7 @@ pub struct CtxtInterners<'tcx> {
137139

138140
// Specifically use a speedy hash algorithm for these hash sets, since
139141
// they're accessed quite often.
140-
type_: InternedSet<'tcx, WithCachedTypeInfo<TyS<'tcx>>>,
142+
type_: InternedSet<'tcx, WithCachedTypeInfo<TyKind<'tcx>>>,
141143
const_lists: InternedSet<'tcx, List<ty::Const<'tcx>>>,
142144
substs: InternedSet<'tcx, InternalSubsts<'tcx>>,
143145
canonical_var_infos: InternedSet<'tcx, List<CanonicalVarInfo<'tcx>>>,
@@ -194,15 +196,12 @@ impl<'tcx> CtxtInterners<'tcx> {
194196
let stable_hash =
195197
self.stable_hash(&flags, sess, definitions, cstore, source_span, &kind);
196198

197-
let ty_struct = TyS {
198-
kind,
199+
InternedInSet(self.arena.alloc(WithCachedTypeInfo {
200+
internee: kind,
201+
stable_hash,
199202
flags: flags.flags,
200203
outer_exclusive_binder: flags.outer_exclusive_binder,
201-
};
202-
203-
InternedInSet(
204-
self.arena.alloc(WithCachedTypeInfo { internee: ty_struct, stable_hash }),
205-
)
204+
}))
206205
})
207206
.0,
208207
))
@@ -2058,7 +2057,7 @@ macro_rules! sty_debug_print {
20582057
let shards = tcx.interners.type_.lock_shards();
20592058
let types = shards.iter().flat_map(|shard| shard.keys());
20602059
for &InternedInSet(t) in types {
2061-
let variant = match t.kind {
2060+
let variant = match t.internee {
20622061
ty::Bool | ty::Char | ty::Int(..) | ty::Uint(..) |
20632062
ty::Float(..) | ty::Str | ty::Never => continue,
20642063
ty::Error(_) => /* unimportant */ continue,
@@ -2168,26 +2167,26 @@ impl<'tcx, T: 'tcx + ?Sized> IntoPointer for InternedInSet<'tcx, T> {
21682167
}
21692168

21702169
#[allow(rustc::usage_of_ty_tykind)]
2171-
impl<'tcx> Borrow<TyKind<'tcx>> for InternedInSet<'tcx, WithCachedTypeInfo<TyS<'tcx>>> {
2170+
impl<'tcx> Borrow<TyKind<'tcx>> for InternedInSet<'tcx, WithCachedTypeInfo<TyKind<'tcx>>> {
21722171
fn borrow<'a>(&'a self) -> &'a TyKind<'tcx> {
2173-
&self.0.kind
2172+
&self.0.internee
21742173
}
21752174
}
21762175

2177-
impl<'tcx> PartialEq for InternedInSet<'tcx, WithCachedTypeInfo<TyS<'tcx>>> {
2178-
fn eq(&self, other: &InternedInSet<'tcx, WithCachedTypeInfo<TyS<'tcx>>>) -> bool {
2176+
impl<'tcx> PartialEq for InternedInSet<'tcx, WithCachedTypeInfo<TyKind<'tcx>>> {
2177+
fn eq(&self, other: &InternedInSet<'tcx, WithCachedTypeInfo<TyKind<'tcx>>>) -> bool {
21792178
// The `Borrow` trait requires that `x.borrow() == y.borrow()` equals
21802179
// `x == y`.
2181-
self.0.kind == other.0.kind
2180+
self.0.internee == other.0.internee
21822181
}
21832182
}
21842183

2185-
impl<'tcx> Eq for InternedInSet<'tcx, WithCachedTypeInfo<TyS<'tcx>>> {}
2184+
impl<'tcx> Eq for InternedInSet<'tcx, WithCachedTypeInfo<TyKind<'tcx>>> {}
21862185

2187-
impl<'tcx> Hash for InternedInSet<'tcx, WithCachedTypeInfo<TyS<'tcx>>> {
2186+
impl<'tcx> Hash for InternedInSet<'tcx, WithCachedTypeInfo<TyKind<'tcx>>> {
21882187
fn hash<H: Hasher>(&self, s: &mut H) {
21892188
// The `Borrow` trait requires that `x.borrow().hash(s) == x.hash(s)`.
2190-
self.0.kind.hash(s)
2189+
self.0.internee.hash(s)
21912190
}
21922191
}
21932192

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 9 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
//!
1010
//! ["The `ty` module: representing types"]: https://rustc-dev-guide.rust-lang.org/ty.html
1111
12+
#![allow(rustc::usage_of_ty_tykind)]
13+
1214
pub use self::fold::{FallibleTypeFolder, TypeFoldable, TypeFolder, TypeSuperFoldable};
1315
pub use self::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor};
1416
pub use self::AssocItemContainer::*;
@@ -446,89 +448,24 @@ pub struct CReaderCacheKey {
446448
pub pos: usize,
447449
}
448450

449-
/// Represents a type.
450-
///
451-
/// IMPORTANT:
452-
/// - This is a very "dumb" struct (with no derives and no `impls`).
453-
/// - Values of this type are always interned and thus unique, and are stored
454-
/// as an `Interned<TyS>`.
455-
/// - `Ty` (which contains a reference to a `Interned<TyS>`) or `Interned<TyS>`
456-
/// should be used everywhere instead of `TyS`. In particular, `Ty` has most
457-
/// of the relevant methods.
458-
#[derive(PartialEq, Eq, PartialOrd, Ord)]
459-
#[allow(rustc::usage_of_ty_tykind)]
460-
pub(crate) struct TyS<'tcx> {
461-
/// This field shouldn't be used directly and may be removed in the future.
462-
/// Use `Ty::kind()` instead.
463-
kind: TyKind<'tcx>,
464-
465-
/// This field provides fast access to information that is also contained
466-
/// in `kind`.
467-
///
468-
/// This field shouldn't be used directly and may be removed in the future.
469-
/// Use `Ty::flags()` instead.
470-
flags: TypeFlags,
471-
472-
/// This field provides fast access to information that is also contained
473-
/// in `kind`.
474-
///
475-
/// This is a kind of confusing thing: it stores the smallest
476-
/// binder such that
477-
///
478-
/// (a) the binder itself captures nothing but
479-
/// (b) all the late-bound things within the type are captured
480-
/// by some sub-binder.
481-
///
482-
/// So, for a type without any late-bound things, like `u32`, this
483-
/// will be *innermost*, because that is the innermost binder that
484-
/// captures nothing. But for a type `&'D u32`, where `'D` is a
485-
/// late-bound region with De Bruijn index `D`, this would be `D + 1`
486-
/// -- the binder itself does not capture `D`, but `D` is captured
487-
/// by an inner binder.
488-
///
489-
/// We call this concept an "exclusive" binder `D` because all
490-
/// De Bruijn indices within the type are contained within `0..D`
491-
/// (exclusive).
492-
outer_exclusive_binder: ty::DebruijnIndex,
493-
}
494-
495451
/// Use this rather than `TyS`, whenever possible.
496452
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable)]
497453
#[rustc_diagnostic_item = "Ty"]
498454
#[rustc_pass_by_value]
499-
pub struct Ty<'tcx>(Interned<'tcx, WithCachedTypeInfo<TyS<'tcx>>>);
455+
pub struct Ty<'tcx>(Interned<'tcx, WithCachedTypeInfo<TyKind<'tcx>>>);
500456

501457
impl<'tcx> TyCtxt<'tcx> {
502458
/// A "bool" type used in rustc_mir_transform unit tests when we
503459
/// have not spun up a TyCtxt.
504460
pub const BOOL_TY_FOR_UNIT_TESTING: Ty<'tcx> =
505461
Ty(Interned::new_unchecked(&WithCachedTypeInfo {
506-
internee: TyS {
507-
kind: ty::Bool,
508-
flags: TypeFlags::empty(),
509-
outer_exclusive_binder: DebruijnIndex::from_usize(0),
510-
},
462+
internee: ty::Bool,
511463
stable_hash: Fingerprint::ZERO,
464+
flags: TypeFlags::empty(),
465+
outer_exclusive_binder: DebruijnIndex::from_usize(0),
512466
}));
513467
}
514468

515-
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for TyS<'tcx> {
516-
#[inline]
517-
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
518-
let TyS {
519-
kind,
520-
521-
// The other fields just provide fast access to information that is
522-
// also contained in `kind`, so no need to hash them.
523-
flags: _,
524-
525-
outer_exclusive_binder: _,
526-
} = self;
527-
528-
kind.hash_stable(hcx, hasher)
529-
}
530-
}
531-
532469
impl ty::EarlyBoundRegion {
533470
/// Does this early bound region have a name? Early bound regions normally
534471
/// always have names except when using anonymous lifetimes (`'_`).
@@ -1030,7 +967,7 @@ impl<'tcx> Term<'tcx> {
1030967
unsafe {
1031968
match ptr & TAG_MASK {
1032969
TYPE_TAG => TermKind::Ty(Ty(Interned::new_unchecked(
1033-
&*((ptr & !TAG_MASK) as *const WithCachedTypeInfo<ty::TyS<'tcx>>),
970+
&*((ptr & !TAG_MASK) as *const WithCachedTypeInfo<ty::TyKind<'tcx>>),
1034971
))),
1035972
CONST_TAG => TermKind::Const(ty::Const(Interned::new_unchecked(
1036973
&*((ptr & !TAG_MASK) as *const ty::ConstS<'tcx>),
@@ -1074,7 +1011,7 @@ impl<'tcx> TermKind<'tcx> {
10741011
TermKind::Ty(ty) => {
10751012
// Ensure we can use the tag bits.
10761013
assert_eq!(mem::align_of_val(&*ty.0.0) & TAG_MASK, 0);
1077-
(TYPE_TAG, ty.0.0 as *const WithCachedTypeInfo<ty::TyS<'tcx>> as usize)
1014+
(TYPE_TAG, ty.0.0 as *const WithCachedTypeInfo<ty::TyKind<'tcx>> as usize)
10781015
}
10791016
TermKind::Const(ct) => {
10801017
// Ensure we can use the tag bits.
@@ -2695,7 +2632,6 @@ mod size_asserts {
26952632
use rustc_data_structures::static_assert_size;
26962633
// tidy-alphabetical-start
26972634
static_assert_size!(PredicateS<'_>, 48);
2698-
static_assert_size!(TyS<'_>, 40);
2699-
static_assert_size!(WithCachedTypeInfo<TyS<'_>>, 56);
2635+
static_assert_size!(WithCachedTypeInfo<TyKind<'_>>, 56);
27002636
// tidy-alphabetical-end
27012637
}

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1602,7 +1602,7 @@ impl<'tcx> Region<'tcx> {
16021602
impl<'tcx> Ty<'tcx> {
16031603
#[inline(always)]
16041604
pub fn kind(self) -> &'tcx TyKind<'tcx> {
1605-
&self.0.0.kind
1605+
&self.0.0
16061606
}
16071607

16081608
#[inline(always)]

compiler/rustc_middle/src/ty/subst.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl<'tcx> GenericArgKind<'tcx> {
8585
GenericArgKind::Type(ty) => {
8686
// Ensure we can use the tag bits.
8787
assert_eq!(mem::align_of_val(&*ty.0.0) & TAG_MASK, 0);
88-
(TYPE_TAG, ty.0.0 as *const WithCachedTypeInfo<ty::TyS<'tcx>> as usize)
88+
(TYPE_TAG, ty.0.0 as *const WithCachedTypeInfo<ty::TyKind<'tcx>> as usize)
8989
}
9090
GenericArgKind::Const(ct) => {
9191
// Ensure we can use the tag bits.
@@ -163,7 +163,7 @@ impl<'tcx> GenericArg<'tcx> {
163163
&*((ptr & !TAG_MASK) as *const ty::RegionKind<'tcx>),
164164
))),
165165
TYPE_TAG => GenericArgKind::Type(Ty(Interned::new_unchecked(
166-
&*((ptr & !TAG_MASK) as *const WithCachedTypeInfo<ty::TyS<'tcx>>),
166+
&*((ptr & !TAG_MASK) as *const WithCachedTypeInfo<ty::TyKind<'tcx>>),
167167
))),
168168
CONST_TAG => GenericArgKind::Const(ty::Const(Interned::new_unchecked(
169169
&*((ptr & !TAG_MASK) as *const ty::ConstS<'tcx>),

compiler/rustc_type_ir/src/ty_info.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ use rustc_data_structures::{
99
stable_hasher::{HashStable, StableHasher},
1010
};
1111

12+
use crate::{DebruijnIndex, TypeFlags};
13+
1214
/// A helper type that you can wrap round your own type in order to automatically
13-
/// cache the stable hash on creation and not recompute it whenever the stable hash
14-
/// of the type is computed.
15+
/// cache the stable hash, type flags and debruijn index on creation and
16+
/// not recompute it whenever the information is needed.
1517
/// This is only done in incremental mode. You can also opt out of caching by using
1618
/// StableHash::ZERO for the hash, in which case the hash gets computed each time.
1719
/// This is useful if you have values that you intern but never (can?) use for stable
@@ -20,6 +22,35 @@ use rustc_data_structures::{
2022
pub struct WithCachedTypeInfo<T> {
2123
pub internee: T,
2224
pub stable_hash: Fingerprint,
25+
26+
/// This field provides fast access to information that is also contained
27+
/// in `kind`.
28+
///
29+
/// This field shouldn't be used directly and may be removed in the future.
30+
/// Use `Ty::flags()` instead.
31+
pub flags: TypeFlags,
32+
33+
/// This field provides fast access to information that is also contained
34+
/// in `kind`.
35+
///
36+
/// This is a kind of confusing thing: it stores the smallest
37+
/// binder such that
38+
///
39+
/// (a) the binder itself captures nothing but
40+
/// (b) all the late-bound things within the type are captured
41+
/// by some sub-binder.
42+
///
43+
/// So, for a type without any late-bound things, like `u32`, this
44+
/// will be *innermost*, because that is the innermost binder that
45+
/// captures nothing. But for a type `&'D u32`, where `'D` is a
46+
/// late-bound region with De Bruijn index `D`, this would be `D + 1`
47+
/// -- the binder itself does not capture `D`, but `D` is captured
48+
/// by an inner binder.
49+
///
50+
/// We call this concept an "exclusive" binder `D` because all
51+
/// De Bruijn indices within the type are contained within `0..D`
52+
/// (exclusive).
53+
pub outer_exclusive_binder: DebruijnIndex,
2354
}
2455

2556
impl<T: PartialEq> PartialEq for WithCachedTypeInfo<T> {

0 commit comments

Comments
 (0)