@@ -4,7 +4,7 @@ use rustc_index::vec::Idx;
4
4
5
5
use crate :: build:: expr:: as_place:: PlaceBase ;
6
6
use crate :: build:: expr:: category:: { Category , RvalueFunc } ;
7
- use crate :: build:: { BlockAnd , BlockAndExtension , Builder } ;
7
+ use crate :: build:: { BlockAnd , BlockAndExtension , Builder , NeedsTemporary } ;
8
8
use rustc_hir:: lang_items:: LangItem ;
9
9
use rustc_middle:: middle:: region;
10
10
use rustc_middle:: mir:: AssertKind ;
@@ -52,17 +52,28 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
52
52
} )
53
53
}
54
54
ExprKind :: Repeat { value, count } => {
55
- let value_operand =
56
- unpack ! ( block = this. as_operand( block, scope, & this. thir[ value] , None ) ) ;
55
+ let value_operand = unpack ! (
56
+ block =
57
+ this. as_operand( block, scope, & this. thir[ value] , None , NeedsTemporary :: No )
58
+ ) ;
57
59
block. and ( Rvalue :: Repeat ( value_operand, count) )
58
60
}
59
61
ExprKind :: Binary { op, lhs, rhs } => {
60
- let lhs = unpack ! ( block = this. as_operand( block, scope, & this. thir[ lhs] , None ) ) ;
61
- let rhs = unpack ! ( block = this. as_operand( block, scope, & this. thir[ rhs] , None ) ) ;
62
+ let lhs = unpack ! (
63
+ block =
64
+ this. as_operand( block, scope, & this. thir[ lhs] , None , NeedsTemporary :: Maybe )
65
+ ) ;
66
+ let rhs = unpack ! (
67
+ block =
68
+ this. as_operand( block, scope, & this. thir[ rhs] , None , NeedsTemporary :: No )
69
+ ) ;
62
70
this. build_binary_op ( block, op, expr_span, expr. ty , lhs, rhs)
63
71
}
64
72
ExprKind :: Unary { op, arg } => {
65
- let arg = unpack ! ( block = this. as_operand( block, scope, & this. thir[ arg] , None ) ) ;
73
+ let arg = unpack ! (
74
+ block =
75
+ this. as_operand( block, scope, & this. thir[ arg] , None , NeedsTemporary :: No )
76
+ ) ;
66
77
// Check for -MIN on signed integers
67
78
if this. check_overflow && op == UnOp :: Neg && expr. ty . is_signed ( ) {
68
79
let bool_ty = this. tcx . types . bool ;
@@ -167,13 +178,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
167
178
block. and ( Rvalue :: Use ( Operand :: Move ( Place :: from ( result) ) ) )
168
179
}
169
180
ExprKind :: Cast { source } => {
170
- let source =
171
- unpack ! ( block = this. as_operand( block, scope, & this. thir[ source] , None ) ) ;
181
+ let source = unpack ! (
182
+ block =
183
+ this. as_operand( block, scope, & this. thir[ source] , None , NeedsTemporary :: No )
184
+ ) ;
172
185
block. and ( Rvalue :: Cast ( CastKind :: Misc , source, expr. ty ) )
173
186
}
174
187
ExprKind :: Pointer { cast, source } => {
175
- let source =
176
- unpack ! ( block = this. as_operand( block, scope, & this. thir[ source] , None ) ) ;
188
+ let source = unpack ! (
189
+ block =
190
+ this. as_operand( block, scope, & this. thir[ source] , None , NeedsTemporary :: No )
191
+ ) ;
177
192
block. and ( Rvalue :: Cast ( CastKind :: Pointer ( cast) , source, expr. ty ) )
178
193
}
179
194
ExprKind :: Array { ref fields } => {
@@ -208,7 +223,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
208
223
let fields: Vec < _ > = fields
209
224
. into_iter ( )
210
225
. copied ( )
211
- . map ( |f| unpack ! ( block = this. as_operand( block, scope, & this. thir[ f] , None ) ) )
226
+ . map ( |f| {
227
+ unpack ! (
228
+ block = this. as_operand(
229
+ block,
230
+ scope,
231
+ & this. thir[ f] ,
232
+ None ,
233
+ NeedsTemporary :: Maybe
234
+ )
235
+ )
236
+ } )
212
237
. collect ( ) ;
213
238
214
239
block. and ( Rvalue :: Aggregate ( Box :: new ( AggregateKind :: Array ( el_ty) ) , fields) )
@@ -219,7 +244,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
219
244
let fields: Vec < _ > = fields
220
245
. into_iter ( )
221
246
. copied ( )
222
- . map ( |f| unpack ! ( block = this. as_operand( block, scope, & this. thir[ f] , None ) ) )
247
+ . map ( |f| {
248
+ unpack ! (
249
+ block = this. as_operand(
250
+ block,
251
+ scope,
252
+ & this. thir[ f] ,
253
+ None ,
254
+ NeedsTemporary :: Maybe
255
+ )
256
+ )
257
+ } )
223
258
. collect ( ) ;
224
259
225
260
block. and ( Rvalue :: Aggregate ( Box :: new ( AggregateKind :: Tuple ) , fields) )
@@ -296,7 +331,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
296
331
)
297
332
) ,
298
333
_ => {
299
- unpack ! ( block = this. as_operand( block, scope, upvar, None ) )
334
+ unpack ! (
335
+ block = this. as_operand(
336
+ block,
337
+ scope,
338
+ upvar,
339
+ None ,
340
+ NeedsTemporary :: Maybe
341
+ )
342
+ )
300
343
}
301
344
}
302
345
}
@@ -325,13 +368,18 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
325
368
literal : ConstantKind :: zero_sized ( this. tcx . types . unit ) ,
326
369
} ) ) ) )
327
370
}
328
- ExprKind :: Yield { .. }
329
- | ExprKind :: Literal { .. }
371
+
372
+ ExprKind :: Literal { .. }
330
373
| ExprKind :: NamedConst { .. }
331
374
| ExprKind :: NonHirLiteral { .. }
332
375
| ExprKind :: ConstParam { .. }
333
376
| ExprKind :: ConstBlock { .. }
334
- | ExprKind :: StaticRef { .. }
377
+ | ExprKind :: StaticRef { .. } => {
378
+ let constant = this. as_constant ( expr) ;
379
+ block. and ( Rvalue :: Use ( Operand :: Constant ( Box :: new ( constant) ) ) )
380
+ }
381
+
382
+ ExprKind :: Yield { .. }
335
383
| ExprKind :: Block { .. }
336
384
| ExprKind :: Match { .. }
337
385
| ExprKind :: If { .. }
@@ -359,9 +407,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
359
407
// so make an operand and then return that
360
408
debug_assert ! ( !matches!(
361
409
Category :: of( & expr. kind) ,
362
- Some ( Category :: Rvalue ( RvalueFunc :: AsRvalue ) )
410
+ Some ( Category :: Rvalue ( RvalueFunc :: AsRvalue ) | Category :: Constant )
363
411
) ) ;
364
- let operand = unpack ! ( block = this. as_operand( block, scope, expr, None ) ) ;
412
+ let operand =
413
+ unpack ! ( block = this. as_operand( block, scope, expr, None , NeedsTemporary :: No ) ) ;
365
414
block. and ( Rvalue :: Use ( operand) )
366
415
}
367
416
}
0 commit comments