@@ -21,7 +21,7 @@ tcx: TyCtxt<'a, 'gcx, 'tcx>
2121
2222As you can see, the ` TyCtxt ` type takes three lifetime parameters.
2323These lifetimes are perhaps the most complex thing to understand about
24- the tcx. During rust compilation, we allocate most of our memory in
24+ the tcx. During Rust compilation, we allocate most of our memory in
2525** arenas** , which are basically pools of memory that get freed all at
2626once. When you see a reference with a lifetime like ` 'tcx ` or ` 'gcx ` ,
2727you know that it refers to arena-allocated data (or data that lives as
@@ -70,18 +70,24 @@ fn maybe_in_inference<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>, def_id: DefId
7070
7171### Allocating and working with types
7272
73- Rust types are represented using the ` ty::Ty<'tcx> ` type. This is in fact a simple type alias
74- for a reference with ` 'tcx ` lifetime:
73+ Rust types are represented using the ` Ty<'tcx> ` defined in the ` ty `
74+ module (not to be confused with the ` Ty ` struct from [ the HIR] ). This
75+ is in fact a simple type alias for a reference with ` 'tcx ` lifetime:
7576
7677``` rust
7778pub type Ty <'tcx > = & 'tcx TyS <'tcx >;
7879```
7980
80- The ` TyS ` struct defines the actual details of how a type is
81- represented. The most interesting part of it is the ` sty ` field, which
82- contains an enum that lets us test what sort of type this is. For
83- example, it is very common to see code that tests what sort of type you have
84- that looks roughly like so:
81+ [ the HIR ] : ../hir/README.md
82+
83+ You can basically ignore the ` TyS ` struct -- you will basically never
84+ access it explicitly. We always pass it by reference using the
85+ ` Ty<'tcx> ` alias -- the only exception I think is to define inherent
86+ methods on types. Instances of ` TyS ` are only ever allocated in one of
87+ the rustc arenas (never e.g. on the stack).
88+
89+ One common operation on types is to ** match** and see what kinds of
90+ types they are. This is done by doing ` match ty.sty ` , sort of like this:
8591
8692``` rust
8793fn test_type <'tcx >(ty : Ty <'tcx >) {
@@ -92,10 +98,14 @@ fn test_type<'tcx>(ty: Ty<'tcx>) {
9298}
9399```
94100
95- (Note though that doing such low-level tests on types during inference
96- can be risky, as there are may be inference variables and other things
97- to consider, or sometimes types are not yet known that will become
98- known later.).
101+ The ` sty ` field (the origin of this name is unclear to me; perhaps
102+ structural type?) is of type ` TypeVariants<'tcx> ` , which is an enum
103+ definined all of the different kinds of types in the compiler.
104+
105+ > NB: inspecting the ` sty ` field on types during type inference can be
106+ > risky, as there are may be inference variables and other things to
107+ > consider, or sometimes types are not yet known that will become
108+ > known later.).
99109
100110To allocate a new type, you can use the various ` mk_ ` methods defined
101111on the ` tcx ` . These have names that correpond mostly to the various kinds
@@ -114,13 +124,13 @@ any inference variables or other "temporary" types, they will be
114124allocated in the global arena). However, the lifetime ` 'tcx ` is always
115125a safe approximation, so that is what you get back.
116126
117- NB. Because types are interned, it is possible to compare them for
118- equality efficiently using ` == ` -- however, this is almost never what
119- you want to do unless you happen to be hashing and looking for
120- duplicates. This is because often in Rust there are multiple ways to
121- represent the same type, particularly once inference is involved. If
122- you are going to be testing for type equality, you probably need to
123- start looking into the inference code to do it right.
127+ > NB. Because types are interned, it is possible to compare them for
128+ > equality efficiently using ` == ` -- however, this is almost never what
129+ > you want to do unless you happen to be hashing and looking for
130+ > duplicates. This is because often in Rust there are multiple ways to
131+ > represent the same type, particularly once inference is involved. If
132+ > you are going to be testing for type equality, you probably need to
133+ > start looking into the inference code to do it right.
124134
125135You can also find various common types in the tcx itself by accessing
126136` tcx.types.bool ` , ` tcx.types.char ` , etc (see ` CommonTypes ` for more).
@@ -153,7 +163,3 @@ In particular, since they are so common, the `Ty` and `TyCtxt` types
153163are imported directly. Other types are often referenced with an
154164explicit ` ty:: ` prefix (e.g., ` ty::TraitRef<'tcx> ` ). But some modules
155165choose to import a larger or smaller set of names explicitly.
156-
157-
158-
159-
0 commit comments