1 //! See docs in build/expr/mod.rs
3 use crate::build::{BlockAnd, BlockAndExtension, Builder};
5 use rustc::middle::region;
8 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
9 /// Compile `expr` into a fresh temporary. This is used when building
10 /// up rvalues so as to freeze the value that will be consumed.
14 temp_lifetime: Option<region::Scope>,
16 mutability: Mutability,
19 M: Mirror<'tcx, Output = Expr<'tcx>>,
21 let expr = self.hir.mirror(expr);
22 self.expr_as_temp(block, temp_lifetime, expr, mutability)
27 mut block: BasicBlock,
28 temp_lifetime: Option<region::Scope>,
30 mutability: Mutability,
31 ) -> BlockAnd<Local> {
33 "expr_as_temp(block={:?}, temp_lifetime={:?}, expr={:?}, mutability={:?})",
34 block, temp_lifetime, expr, mutability
38 let expr_span = expr.span;
39 let source_info = this.source_info(expr_span);
40 if let ExprKind::Scope {
46 return this.in_scope((region_scope, source_info), lint_level, |this| {
47 this.as_temp(block, temp_lifetime, value, mutability)
51 let expr_ty = expr.ty;
53 let mut local_decl = LocalDecl::new_temp(expr_ty, expr_span);
54 if mutability == Mutability::Not {
55 local_decl = local_decl.immutable();
58 debug!("creating temp {:?} with block_context: {:?}", local_decl, this.block_context);
59 // Find out whether this temp is being created within the
60 // tail expression of a block whose result is ignored.
61 if let Some(tail_info) = this.block_context.currently_in_block_tail() {
62 local_decl = local_decl.block_tail(tail_info);
64 this.local_decls.push(local_decl)
66 if !expr_ty.is_never() {
71 kind: StatementKind::StorageLive(temp),
76 unpack!(block = this.into(&Place::Base(PlaceBase::Local(temp)), block, expr));
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_storage_and_value(
91 &Place::Base(PlaceBase::Local(temp)),