@@ -4,7 +4,7 @@ use rustc_index::vec::Idx;
44
55use crate :: build:: expr:: as_place:: PlaceBase ;
66use crate :: build:: expr:: category:: { Category , RvalueFunc } ;
7- use crate :: build:: { BlockAnd , BlockAndExtension , Builder } ;
7+ use crate :: build:: { BlockAnd , BlockAndExtension , Builder , NeedsTemporary } ;
88use rustc_hir:: lang_items:: LangItem ;
99use rustc_middle:: middle:: region;
1010use rustc_middle:: mir:: AssertKind ;
@@ -52,17 +52,28 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
5252 } )
5353 }
5454 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+ ) ;
5759 block. and ( Rvalue :: Repeat ( value_operand, count) )
5860 }
5961 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+ ) ;
6270 this. build_binary_op ( block, op, expr_span, expr. ty , lhs, rhs)
6371 }
6472 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+ ) ;
6677 // Check for -MIN on signed integers
6778 if this. check_overflow && op == UnOp :: Neg && expr. ty . is_signed ( ) {
6879 let bool_ty = this. tcx . types . bool ;
@@ -167,13 +178,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
167178 block. and ( Rvalue :: Use ( Operand :: Move ( Place :: from ( result) ) ) )
168179 }
169180 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+ ) ;
172185 block. and ( Rvalue :: Cast ( CastKind :: Misc , source, expr. ty ) )
173186 }
174187 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+ ) ;
177192 block. and ( Rvalue :: Cast ( CastKind :: Pointer ( cast) , source, expr. ty ) )
178193 }
179194 ExprKind :: Array { ref fields } => {
@@ -208,7 +223,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
208223 let fields: Vec < _ > = fields
209224 . into_iter ( )
210225 . 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+ } )
212237 . collect ( ) ;
213238
214239 block. and ( Rvalue :: Aggregate ( Box :: new ( AggregateKind :: Array ( el_ty) ) , fields) )
@@ -219,7 +244,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
219244 let fields: Vec < _ > = fields
220245 . into_iter ( )
221246 . 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+ } )
223258 . collect ( ) ;
224259
225260 block. and ( Rvalue :: Aggregate ( Box :: new ( AggregateKind :: Tuple ) , fields) )
@@ -296,7 +331,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
296331 )
297332 ) ,
298333 _ => {
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+ )
300343 }
301344 }
302345 }
@@ -325,13 +368,18 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
325368 literal : ConstantKind :: zero_sized ( this. tcx . types . unit ) ,
326369 } ) ) ) )
327370 }
328- ExprKind :: Yield { .. }
329- | ExprKind :: Literal { .. }
371+
372+ ExprKind :: Literal { .. }
330373 | ExprKind :: NamedConst { .. }
331374 | ExprKind :: NonHirLiteral { .. }
332375 | ExprKind :: ConstParam { .. }
333376 | 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 { .. }
335383 | ExprKind :: Block { .. }
336384 | ExprKind :: Match { .. }
337385 | ExprKind :: If { .. }
@@ -359,9 +407,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
359407 // so make an operand and then return that
360408 debug_assert ! ( !matches!(
361409 Category :: of( & expr. kind) ,
362- Some ( Category :: Rvalue ( RvalueFunc :: AsRvalue ) )
410+ Some ( Category :: Rvalue ( RvalueFunc :: AsRvalue ) | Category :: Constant )
363411 ) ) ;
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 ) ) ;
365414 block. and ( Rvalue :: Use ( operand) )
366415 }
367416 }
0 commit comments