@@ -21,7 +21,7 @@ tcx: TyCtxt<'a, 'gcx, 'tcx>
21
21
22
22
As you can see, the ` TyCtxt ` type takes three lifetime parameters.
23
23
These 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
25
25
** arenas** , which are basically pools of memory that get freed all at
26
26
once. When you see a reference with a lifetime like ` 'tcx ` or ` 'gcx ` ,
27
27
you 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
70
70
71
71
### Allocating and working with types
72
72
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:
75
76
76
77
``` rust
77
78
pub type Ty <'tcx > = & 'tcx TyS <'tcx >;
78
79
```
79
80
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:
85
91
86
92
``` rust
87
93
fn test_type <'tcx >(ty : Ty <'tcx >) {
@@ -92,10 +98,14 @@ fn test_type<'tcx>(ty: Ty<'tcx>) {
92
98
}
93
99
```
94
100
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.).
99
109
100
110
To allocate a new type, you can use the various ` mk_ ` methods defined
101
111
on 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
114
124
allocated in the global arena). However, the lifetime ` 'tcx ` is always
115
125
a safe approximation, so that is what you get back.
116
126
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.
124
134
125
135
You can also find various common types in the tcx itself by accessing
126
136
` 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
153
163
are imported directly. Other types are often referenced with an
154
164
explicit ` ty:: ` prefix (e.g., ` ty::TraitRef<'tcx> ` ). But some modules
155
165
choose to import a larger or smaller set of names explicitly.
156
-
157
-
158
-
159
-
0 commit comments