@@ -47,8 +47,18 @@ which is useful to keep peak memory in check. Having a THIR representation of
47
47
all bodies of a crate in memory at the same time would be very heavy.
48
48
49
49
You can get a debug representation of the THIR by passing the ` -Zunpretty=thir-tree ` flag
50
- to ` rustc ` . Here is how a function with just the statement ` let x = 1 + 2; ` gets represented in
51
- THIR:
50
+ to ` rustc ` .
51
+
52
+ To demonstrate, let's use the following example:
53
+
54
+ ``` rust
55
+ fn main () {
56
+ let x = 1 + 2 ;
57
+ }
58
+ ```
59
+
60
+ Here is how that gets represented in THIR (as of <!-- date-check --> Aug 2022):
61
+
52
62
``` rust
53
63
Thir {
54
64
// no match arms
@@ -57,65 +67,83 @@ Thir {
57
67
// expression 0, a literal with a value of 1
58
68
Expr {
59
69
ty : i32 ,
60
- temp_lifetime : Some (Node (6 )),
70
+ temp_lifetime : Some (
71
+ Node (1 ),
72
+ ),
61
73
span : oneplustwo . rs: 2 : 13 : 2 : 14 (#0 ),
62
74
kind : Literal {
63
- literal : Const {
64
- ty : i32 ,
65
- val : Value (Scalar (0x00000001 )),
75
+ lit : Spanned {
76
+ node : Int (
77
+ 1 ,
78
+ Unsuffixed ,
79
+ ),
80
+ span : oneplustwo . rs: 2 : 13 : 2 : 14 (#0 ),
66
81
},
67
- user_ty : None ,
68
- const_id : None ,
82
+ neg : false ,
69
83
},
70
84
},
71
85
// expression 1, scope surronding literal 1
72
86
Expr {
73
87
ty : i32 ,
74
- temp_lifetime : Some (Node (6 )),
88
+ temp_lifetime : Some (
89
+ Node (1 ),
90
+ ),
75
91
span : oneplustwo . rs: 2 : 13 : 2 : 14 (#0 ),
76
92
kind : Scope {
77
- region_scope : Node (1 ),
78
- lint_level : Explicit (HirId {
79
- owner : DefId (0 : 3 ~ oneplustwo [6ccc ]:: main ),
80
- local_id : 1 ,
81
- }),
82
93
// reference to expression 0 above
94
+ region_scope : Node (3 ),
95
+ lint_level : Explicit (
96
+ HirId {
97
+ owner : DefId (0 : 3 ~ oneplustwo [6932 ]:: main ),
98
+ local_id : 3 ,
99
+ },
100
+ ),
83
101
value : e0 ,
84
102
},
85
103
},
86
104
// expression 2, literal 2
87
105
Expr {
88
106
ty : i32 ,
89
- temp_lifetime : Some (Node (6 )),
107
+ temp_lifetime : Some (
108
+ Node (1 ),
109
+ ),
90
110
span : oneplustwo . rs: 2 : 17 : 2 : 18 (#0 ),
91
111
kind : Literal {
92
- literal : Const {
93
- ty : i32 ,
94
- val : Value (Scalar (0x00000002 )),
112
+ lit : Spanned {
113
+ node : Int (
114
+ 2 ,
115
+ Unsuffixed ,
116
+ ),
117
+ span : oneplustwo . rs: 2 : 17 : 2 : 18 (#0 ),
95
118
},
96
- user_ty : None ,
97
- const_id : None ,
119
+ neg : false ,
98
120
},
99
121
},
100
122
// expression 3, scope surrounding literal 2
101
123
Expr {
102
124
ty : i32 ,
103
- temp_lifetime : Some (Node (6 )),
125
+ temp_lifetime : Some (
126
+ Node (1 ),
127
+ ),
104
128
span : oneplustwo . rs: 2 : 17 : 2 : 18 (#0 ),
105
129
kind : Scope {
106
- region_scope : Node (2 ),
107
- lint_level : Explicit (HirId {
108
- owner : DefId (0 : 3 ~ oneplustwo [6ccc ]:: main ),
109
- local_id : 2 ,
110
- }),
130
+ region_scope : Node (4 ),
131
+ lint_level : Explicit (
132
+ HirId {
133
+ owner : DefId (0 : 3 ~ oneplustwo [6932 ]:: main ),
134
+ local_id : 4 ,
135
+ },
136
+ ),
111
137
// reference to expression 2 above
112
138
value : e2 ,
113
139
},
114
140
},
115
141
// expression 4, represents 1 + 2
116
142
Expr {
117
143
ty : i32 ,
118
- temp_lifetime : Some (Node (6 )),
144
+ temp_lifetime : Some (
145
+ Node (1 ),
146
+ ),
119
147
span : oneplustwo . rs: 2 : 13 : 2 : 18 (#0 ),
120
148
kind : Binary {
121
149
op : Add ,
@@ -127,30 +155,38 @@ Thir {
127
155
// expression 5, scope surronding expression 4
128
156
Expr {
129
157
ty : i32 ,
130
- temp_lifetime : Some (Node (6 )),
158
+ temp_lifetime : Some (
159
+ Node (1 ),
160
+ ),
131
161
span : oneplustwo . rs: 2 : 13 : 2 : 18 (#0 ),
132
162
kind : Scope {
133
- region_scope : Node (3 ),
134
- lint_level : Explicit (HirId {
135
- owner : DefId (0 : 3 ~ oneplustwo [6ccc ]:: main ),
136
- local_id : 3 ,
137
- }),
163
+ region_scope : Node (5 ),
164
+ lint_level : Explicit (
165
+ HirId {
166
+ owner : DefId (0 : 3 ~ oneplustwo [6932 ]:: main ),
167
+ local_id : 5 ,
168
+ },
169
+ ),
138
170
value : e4 ,
139
171
},
140
172
},
141
173
// expression 6, block around statement
142
174
Expr {
143
175
ty : (),
144
- temp_lifetime : Some (Node (8 )),
176
+ temp_lifetime : Some (
177
+ Node (9 ),
178
+ ),
145
179
span : oneplustwo . rs: 1 : 11 : 3 : 2 (#0 ),
146
180
kind : Block {
147
181
body : Block {
148
182
targeted_by_break : false ,
149
- region_scope : Node (7 ),
183
+ region_scope : Node (8 ),
150
184
opt_destruction_scope : None ,
151
185
span : oneplustwo . rs: 1 : 11 : 3 : 2 (#0 ),
152
186
// reference to statement 0 below
153
- stmts : [ s0 ],
187
+ stmts : [
188
+ s0 ,
189
+ ],
154
190
expr : None ,
155
191
safety_mode : Safe ,
156
192
},
@@ -160,25 +196,29 @@ Thir {
160
196
Expr {
161
197
ty : (),
162
198
temp_lifetime : Some (
163
- Node (8 ),
199
+ Node (9 ),
164
200
),
165
201
span : oneplustwo . rs: 1 : 11 : 3 : 2 (#0 ),
166
202
kind : Scope {
167
- region_scope : Node (8 ),
168
- lint_level : Explicit (HirId {
169
- owner : DefId (0 : 3 ~ oneplustwo [6ccc ]:: main ),
170
- local_id : 8 ,
171
- }),
203
+ region_scope : Node (9 ),
204
+ lint_level : Explicit (
205
+ HirId {
206
+ owner : DefId (0 : 3 ~ oneplustwo [6932 ]:: main ),
207
+ local_id : 9 ,
208
+ },
209
+ ),
172
210
value : e6 ,
173
211
},
174
212
},
175
213
// destruction scope around expression 7
176
214
Expr {
177
215
ty : (),
178
- temp_lifetime : Some (Node (8 )),
216
+ temp_lifetime : Some (
217
+ Node (9 ),
218
+ ),
179
219
span : oneplustwo . rs: 1 : 11 : 3 : 2 (#0 ),
180
220
kind : Scope {
181
- region_scope : Destruction (8 ),
221
+ region_scope : Destruction (9 ),
182
222
lint_level : Inherited ,
183
223
value : e7 ,
184
224
},
@@ -188,31 +228,40 @@ Thir {
188
228
// let statement
189
229
Stmt {
190
230
kind : Let {
191
- remainder_scope : Remainder { block : 7 , first_statement_index : 0 },
192
- init_scope : Node (6 ),
231
+ remainder_scope : Remainder { block : 8 , first_statement_index : 0 },
232
+ init_scope : Node (1 ),
193
233
pattern : Pat {
194
234
ty : i32 ,
195
235
span : oneplustwo . rs: 2 : 9 : 2 : 10 (#0 ),
196
236
kind : Binding {
197
237
mutability : Not ,
198
238
name : " x" ,
199
239
mode : ByValue ,
200
- var : HirId {
201
- owner : DefId (0 : 3 ~ oneplustwo [6ccc ]:: main ),
202
- local_id : 5 ,
203
- },
240
+ var : LocalVarId (
241
+ HirId {
242
+ owner : DefId (0 : 3 ~ oneplustwo [6932 ]:: main ),
243
+ local_id : 7 ,
244
+ },
245
+ ),
204
246
ty : i32 ,
205
247
subpattern : None ,
206
248
is_primary : true ,
207
249
},
208
250
},
209
- initializer : Some (e5 ),
210
- lint_level : Explicit (HirId {
211
- owner : DefId (0 : 3 ~ oneplustwo [6ccc ]:: main ),
212
- local_id : 4 ,
213
- }),
251
+ initializer : Some (
252
+ e5 ,
253
+ ),
254
+ else_block : None ,
255
+ lint_level : Explicit (
256
+ HirId {
257
+ owner : DefId (0 : 3 ~ oneplustwo [6932 ]:: main ),
258
+ local_id : 6 ,
259
+ },
260
+ ),
214
261
},
215
- opt_destruction_scope : Some (Destruction (6 )),
262
+ opt_destruction_scope : Some (
263
+ Destruction (1 ),
264
+ ),
216
265
},
217
266
],
218
267
}
0 commit comments