1 use crate::build::scope::BreakableScope;
2 use crate::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!(
144 &Place::RETURN_PLACE,
149 this.block_context.pop();
153 this.cfg.push_assign_unit(
156 &Place::RETURN_PLACE,
161 let region_scope = this.region_scope_of_return_scope();
162 let return_block = this.return_block();
163 this.exit_scope(expr_span, (region_scope, source_info), block, return_block);
164 this.cfg.start_new_block().unit()
166 ExprKind::InlineAsm {
171 debug!("stmt_expr InlineAsm block_context.push(SubExpr) : {:?}", expr2);
172 this.block_context.push(BlockFrame::SubExpr);
173 let outputs = outputs
175 .map(|output| unpack!(block = this.as_place(block, output)))
183 unpack!(block = this.as_local_operand(block, input)),
185 }).collect::<Vec<_>>()
191 kind: StatementKind::InlineAsm(box InlineAsm {
198 this.block_context.pop();
202 let expr_ty = expr.ty;
204 // Issue #54382: When creating temp for the value of
207 // `{ side_effects(); { let l = stuff(); the_value } }`
209 // it is usually better to focus on `the_value` rather
210 // than the entirety of block(s) surrounding it.
211 let mut temp_span = expr_span;
212 let mut temp_in_tail_of_block = false;
213 if let ExprKind::Block { body } = expr.kind {
214 if let Some(tail_expr) = &body.expr {
215 let mut expr = tail_expr;
216 while let rustc::hir::ExprKind::Block(subblock, _label) = &expr.node {
217 if let Some(subtail_expr) = &subblock.expr {
223 temp_span = expr.span;
224 temp_in_tail_of_block = true;
229 let mut local_decl = LocalDecl::new_temp(expr.ty.clone(), temp_span);
230 if temp_in_tail_of_block {
231 if this.block_context.currently_ignores_tail_results() {
232 local_decl = local_decl.block_tail(BlockTailInfo {
233 tail_result_is_ignored: true
237 let temp = this.local_decls.push(local_decl);
238 let place = Place::Base(PlaceBase::Local(temp));
239 debug!("created temp {:?} for expr {:?} in block_context: {:?}",
240 temp, expr, this.block_context);
243 unpack!(block = this.into(&temp, block, expr));
245 // Attribute drops of the statement's temps to the
246 // semicolon at the statement's end.
247 let drop_point = this.hir.tcx().sess.source_map().end_point(match opt_stmt_span {
249 Some(StatementSpan(span)) => span,
252 unpack!(block = this.build_drop(block, drop_point, temp, expr_ty));