11//! See docs in build/expr/mod.rs
22
33use crate :: build:: { BlockAnd , BlockAndExtension , Builder } ;
4+ use crate :: build:: scope:: { CachedBlock , DropKind } ;
45use crate :: hair:: * ;
56use rustc:: middle:: region;
67use rustc:: mir:: * ;
@@ -63,6 +64,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
6364 }
6465 this. local_decls . push ( local_decl)
6566 } ;
67+ let temp_place = & Place :: Base ( PlaceBase :: Local ( temp) ) ;
68+
6669 if !expr_ty. is_never ( ) {
6770 this. cfg . push (
6871 block,
@@ -71,25 +74,38 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
7174 kind : StatementKind :: StorageLive ( temp) ,
7275 } ,
7376 ) ;
77+
78+ // In constants, temp_lifetime is None for temporaries that live for the
79+ // 'static lifetime. Thus we do not drop these temporaries and simply leak them.
80+ // This is equivalent to what `let x = &foo();` does in functions. The temporary
81+ // is lifted to their surrounding scope. In a function that means the temporary lives
82+ // until just before the function returns. In constants that means it outlives the
83+ // constant's initialization value computation. Anything outliving a constant
84+ // must have the `'static` lifetime and live forever.
85+ // Anything with a shorter lifetime (e.g the `&foo()` in `bar(&foo())` or anything
86+ // within a block will keep the regular drops just like runtime code.
87+ if let Some ( temp_lifetime) = temp_lifetime {
88+ this. schedule_drop (
89+ expr_span,
90+ temp_lifetime,
91+ temp_place,
92+ expr_ty,
93+ DropKind :: Storage ,
94+ ) ;
95+ }
7496 }
7597
76- unpack ! ( block = this. into( & Place :: Base ( PlaceBase :: Local ( temp ) ) , block, expr) ) ;
98+ unpack ! ( block = this. into( temp_place , block, expr) ) ;
7799
78- // In constants, temp_lifetime is None for temporaries that live for the
79- // 'static lifetime. Thus we do not drop these temporaries and simply leak them.
80- // This is equivalent to what `let x = &foo();` does in functions. The temporary
81- // is lifted to their surrounding scope. In a function that means the temporary lives
82- // until just before the function returns. In constants that means it outlives the
83- // constant's initialization value computation. Anything outliving a constant
84- // must have the `'static` lifetime and live forever.
85- // Anything with a shorter lifetime (e.g the `&foo()` in `bar(&foo())` or anything
86- // within a block will keep the regular drops just like runtime code.
87100 if let Some ( temp_lifetime) = temp_lifetime {
88- this. schedule_drop_storage_and_value (
101+ this. schedule_drop (
89102 expr_span,
90103 temp_lifetime,
91- & Place :: Base ( PlaceBase :: Local ( temp ) ) ,
104+ temp_place ,
92105 expr_ty,
106+ DropKind :: Value {
107+ cached_block : CachedBlock :: default ( ) ,
108+ } ,
93109 ) ;
94110 }
95111
0 commit comments