1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use build::scope::BreakableScope;
12 use build::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
16 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
17 /// Builds a block of MIR statements to evaluate the HAIR `expr`.
18 /// If the original expression was an AST statement,
19 /// (e.g. `some().code(&here());`) then `opt_stmt_span` is the
20 /// span of that statement (including its semicolon, if any).
21 /// Diagnostics use this span (which may be larger than that of
22 /// `expr`) to identify when statement temporaries are dropped.
23 pub fn stmt_expr(&mut self,
24 mut block: BasicBlock,
26 opt_stmt_span: Option<StatementSpan>)
30 let expr_span = expr.span;
31 let source_info = this.source_info(expr.span);
32 // Handle a number of expressions that don't need a destination at all. This
33 // avoids needing a mountain of temporary `()` variables.
34 let expr2 = expr.clone();
41 let value = this.hir.mirror(value);
42 this.in_scope((region_scope, source_info), lint_level, block, |this| {
43 this.stmt_expr(block, value, opt_stmt_span)
46 ExprKind::Assign { lhs, rhs } => {
47 let lhs = this.hir.mirror(lhs);
48 let rhs = this.hir.mirror(rhs);
49 let lhs_span = lhs.span;
51 // Note: we evaluate assignments right-to-left. This
52 // is better for borrowck interaction with overloaded
53 // operators like x[j] = x[i].
55 debug!("stmt_expr Assign block_context.push(SubExpr) : {:?}", expr2);
56 this.block_context.push(BlockFrame::SubExpr);
58 // Generate better code for things that don't need to be
60 if this.hir.needs_drop(lhs.ty) {
61 let rhs = unpack!(block = this.as_local_operand(block, rhs));
62 let lhs = unpack!(block = this.as_place(block, lhs));
63 unpack!(block = this.build_drop_and_replace(block, lhs_span, lhs, rhs));
65 let rhs = unpack!(block = this.as_local_rvalue(block, rhs));
66 let lhs = unpack!(block = this.as_place(block, lhs));
67 this.cfg.push_assign(block, source_info, &lhs, rhs);
70 this.block_context.pop();
73 ExprKind::AssignOp { op, lhs, rhs } => {
74 // FIXME(#28160) there is an interesting semantics
75 // question raised here -- should we "freeze" the
76 // value of the lhs here? I'm inclined to think not,
77 // since it seems closer to the semantics of the
78 // overloaded version, which takes `&mut self`. This
79 // only affects weird things like `x += {x += 1; x}`
80 // -- is that equal to `x + (x + 1)` or `2*(x+1)`?
82 let lhs = this.hir.mirror(lhs);
85 debug!("stmt_expr AssignOp block_context.push(SubExpr) : {:?}", expr2);
86 this.block_context.push(BlockFrame::SubExpr);
89 let rhs = unpack!(block = this.as_local_operand(block, rhs));
90 let lhs = unpack!(block = this.as_place(block, lhs));
92 // we don't have to drop prior contents or anything
93 // because AssignOp is only legal for Copy types
94 // (overloaded ops should be desugared into a call).
96 block = this.build_binary_op(
101 Operand::Copy(lhs.clone()),
105 this.cfg.push_assign(block, source_info, &lhs, result);
107 this.block_context.pop();
110 ExprKind::Continue { label } => {
115 } = *this.find_breakable_scope(expr_span, label);
116 let continue_block = continue_block
117 .expect("Attempted to continue in non-continuable breakable block");
120 (region_scope, source_info),
124 this.cfg.start_new_block().unit()
126 ExprKind::Break { label, value } => {
127 let (break_block, region_scope, destination) = {
131 ref break_destination,
133 } = *this.find_breakable_scope(expr_span, label);
134 (break_block, region_scope, break_destination.clone())
136 if let Some(value) = value {
137 debug!("stmt_expr Break val block_context.push(SubExpr) : {:?}", expr2);
138 this.block_context.push(BlockFrame::SubExpr);
139 unpack!(block = this.into(&destination, block, value));
140 this.block_context.pop();
142 this.cfg.push_assign_unit(block, source_info, &destination)
144 this.exit_scope(expr_span, (region_scope, source_info), block, break_block);
145 this.cfg.start_new_block().unit()
147 ExprKind::Return { value } => {
148 block = match value {
150 debug!("stmt_expr Return val block_context.push(SubExpr) : {:?}", expr2);
151 this.block_context.push(BlockFrame::SubExpr);
152 let result = unpack!(this.into(&Place::Local(RETURN_PLACE), block, value));
153 this.block_context.pop();
158 .push_assign_unit(block, source_info, &Place::Local(RETURN_PLACE));
162 let region_scope = this.region_scope_of_return_scope();
163 let return_block = this.return_block();
164 this.exit_scope(expr_span, (region_scope, source_info), block, return_block);
165 this.cfg.start_new_block().unit()
167 ExprKind::InlineAsm {
172 debug!("stmt_expr InlineAsm block_context.push(SubExpr) : {:?}", expr2);
173 this.block_context.push(BlockFrame::SubExpr);
174 let outputs = outputs
176 .map(|output| unpack!(block = this.as_place(block, output)))
184 unpack!(block = this.as_local_operand(block, input)),
186 }).collect::<Vec<_>>()
192 kind: StatementKind::InlineAsm {
193 asm: box asm.clone(),
199 this.block_context.pop();
203 let expr_ty = expr.ty;
205 // Issue #54382: When creating temp for the value of
208 // `{ side_effects(); { let l = stuff(); the_value } }`
210 // it is usually better to focus on `the_value` rather
211 // than the entirety of block(s) surrounding it.
212 let mut temp_span = expr_span;
213 let mut temp_in_tail_of_block = false;
214 if let ExprKind::Block { body } = expr.kind {
215 if let Some(tail_expr) = &body.expr {
216 let mut expr = tail_expr;
217 while let rustc::hir::ExprKind::Block(subblock, _label) = &expr.node {
218 if let Some(subtail_expr) = &subblock.expr {
224 temp_span = expr.span;
225 temp_in_tail_of_block = true;
230 let mut local_decl = LocalDecl::new_temp(expr.ty.clone(), temp_span);
231 if temp_in_tail_of_block {
232 if this.block_context.currently_ignores_tail_results() {
233 local_decl = local_decl.block_tail(BlockTailInfo {
234 tail_result_is_ignored: true
238 let temp = this.local_decls.push(local_decl);
239 let place = Place::Local(temp);
240 debug!("created temp {:?} for expr {:?} in block_context: {:?}",
241 temp, expr, this.block_context);
244 unpack!(block = this.into(&temp, block, expr));
246 // Attribute drops of the statement's temps to the
247 // semicolon at the statement's end.
248 let drop_point = this.hir.tcx().sess.source_map().end_point(match opt_stmt_span {
250 Some(StatementSpan(span)) => span,
253 unpack!(block = this.build_drop(block, drop_point, temp, expr_ty));