1 use build::scope::BreakableScope;
2 use build::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
6 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
7 /// Builds a block of MIR statements to evaluate the HAIR `expr`.
8 /// If the original expression was an AST statement,
9 /// (e.g., `some().code(&here());`) then `opt_stmt_span` is the
10 /// span of that statement (including its semicolon, if any).
11 /// Diagnostics use this span (which may be larger than that of
12 /// `expr`) to identify when statement temporaries are dropped.
13 pub fn stmt_expr(&mut self,
14 mut block: BasicBlock,
16 opt_stmt_span: Option<StatementSpan>)
20 let expr_span = expr.span;
21 let source_info = this.source_info(expr.span);
22 // Handle a number of expressions that don't need a destination at all. This
23 // avoids needing a mountain of temporary `()` variables.
24 let expr2 = expr.clone();
31 let value = this.hir.mirror(value);
32 this.in_scope((region_scope, source_info), lint_level, block, |this| {
33 this.stmt_expr(block, value, opt_stmt_span)
36 ExprKind::Assign { lhs, rhs } => {
37 let lhs = this.hir.mirror(lhs);
38 let rhs = this.hir.mirror(rhs);
39 let lhs_span = lhs.span;
41 // Note: we evaluate assignments right-to-left. This
42 // is better for borrowck interaction with overloaded
43 // operators like x[j] = x[i].
45 debug!("stmt_expr Assign block_context.push(SubExpr) : {:?}", expr2);
46 this.block_context.push(BlockFrame::SubExpr);
48 // Generate better code for things that don't need to be
50 if this.hir.needs_drop(lhs.ty) {
51 let rhs = unpack!(block = this.as_local_operand(block, rhs));
52 let lhs = unpack!(block = this.as_place(block, lhs));
53 unpack!(block = this.build_drop_and_replace(block, lhs_span, lhs, rhs));
55 let rhs = unpack!(block = this.as_local_rvalue(block, rhs));
56 let lhs = unpack!(block = this.as_place(block, lhs));
57 this.cfg.push_assign(block, source_info, &lhs, rhs);
60 this.block_context.pop();
63 ExprKind::AssignOp { op, lhs, rhs } => {
64 // FIXME(#28160) there is an interesting semantics
65 // question raised here -- should we "freeze" the
66 // value of the lhs here? I'm inclined to think not,
67 // since it seems closer to the semantics of the
68 // overloaded version, which takes `&mut self`. This
69 // only affects weird things like `x += {x += 1; x}`
70 // -- is that equal to `x + (x + 1)` or `2*(x+1)`?
72 let lhs = this.hir.mirror(lhs);
75 debug!("stmt_expr AssignOp block_context.push(SubExpr) : {:?}", expr2);
76 this.block_context.push(BlockFrame::SubExpr);
79 let rhs = unpack!(block = this.as_local_operand(block, rhs));
80 let lhs = unpack!(block = this.as_place(block, lhs));
82 // we don't have to drop prior contents or anything
83 // because AssignOp is only legal for Copy types
84 // (overloaded ops should be desugared into a call).
86 block = this.build_binary_op(
91 Operand::Copy(lhs.clone()),
95 this.cfg.push_assign(block, source_info, &lhs, result);
97 this.block_context.pop();
100 ExprKind::Continue { label } => {
105 } = *this.find_breakable_scope(expr_span, label);
106 let continue_block = continue_block
107 .expect("Attempted to continue in non-continuable breakable block");
110 (region_scope, source_info),
114 this.cfg.start_new_block().unit()
116 ExprKind::Break { label, value } => {
117 let (break_block, region_scope, destination) = {
121 ref break_destination,
123 } = *this.find_breakable_scope(expr_span, label);
124 (break_block, region_scope, break_destination.clone())
126 if let Some(value) = value {
127 debug!("stmt_expr Break val block_context.push(SubExpr) : {:?}", expr2);
128 this.block_context.push(BlockFrame::SubExpr);
129 unpack!(block = this.into(&destination, block, value));
130 this.block_context.pop();
132 this.cfg.push_assign_unit(block, source_info, &destination)
134 this.exit_scope(expr_span, (region_scope, source_info), block, break_block);
135 this.cfg.start_new_block().unit()
137 ExprKind::Return { value } => {
138 block = match value {
140 debug!("stmt_expr Return val block_context.push(SubExpr) : {:?}", expr2);
141 this.block_context.push(BlockFrame::SubExpr);
142 let result = unpack!(this.into(&Place::Local(RETURN_PLACE), block, value));
143 this.block_context.pop();
148 .push_assign_unit(block, source_info, &Place::Local(RETURN_PLACE));
152 let region_scope = this.region_scope_of_return_scope();
153 let return_block = this.return_block();
154 this.exit_scope(expr_span, (region_scope, source_info), block, return_block);
155 this.cfg.start_new_block().unit()
157 ExprKind::InlineAsm {
162 debug!("stmt_expr InlineAsm block_context.push(SubExpr) : {:?}", expr2);
163 this.block_context.push(BlockFrame::SubExpr);
164 let outputs = outputs
166 .map(|output| unpack!(block = this.as_place(block, output)))
174 unpack!(block = this.as_local_operand(block, input)),
176 }).collect::<Vec<_>>()
182 kind: StatementKind::InlineAsm {
183 asm: box asm.clone(),
189 this.block_context.pop();
193 let expr_ty = expr.ty;
195 // Issue #54382: When creating temp for the value of
198 // `{ side_effects(); { let l = stuff(); the_value } }`
200 // it is usually better to focus on `the_value` rather
201 // than the entirety of block(s) surrounding it.
202 let mut temp_span = expr_span;
203 let mut temp_in_tail_of_block = false;
204 if let ExprKind::Block { body } = expr.kind {
205 if let Some(tail_expr) = &body.expr {
206 let mut expr = tail_expr;
207 while let rustc::hir::ExprKind::Block(subblock, _label) = &expr.node {
208 if let Some(subtail_expr) = &subblock.expr {
214 temp_span = expr.span;
215 temp_in_tail_of_block = true;
220 let mut local_decl = LocalDecl::new_temp(expr.ty.clone(), temp_span);
221 if temp_in_tail_of_block {
222 if this.block_context.currently_ignores_tail_results() {
223 local_decl = local_decl.block_tail(BlockTailInfo {
224 tail_result_is_ignored: true
228 let temp = this.local_decls.push(local_decl);
229 let place = Place::Local(temp);
230 debug!("created temp {:?} for expr {:?} in block_context: {:?}",
231 temp, expr, this.block_context);
234 unpack!(block = this.into(&temp, block, expr));
236 // Attribute drops of the statement's temps to the
237 // semicolon at the statement's end.
238 let drop_point = this.hir.tcx().sess.source_map().end_point(match opt_stmt_span {
240 Some(StatementSpan(span)) => span,
243 unpack!(block = this.build_drop(block, drop_point, temp, expr_ty));