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, Builder};
16 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
17 pub fn stmt_expr(&mut self, mut block: BasicBlock, expr: Expr<'tcx>) -> BlockAnd<()> {
19 let expr_span = expr.span;
20 let source_info = this.source_info(expr.span);
21 // Handle a number of expressions that don't need a destination at all. This
22 // avoids needing a mountain of temporary `()` variables.
29 let value = this.hir.mirror(value);
30 this.in_scope((region_scope, source_info), lint_level, block, |this| {
31 this.stmt_expr(block, value)
34 ExprKind::Assign { lhs, rhs } => {
35 let lhs = this.hir.mirror(lhs);
36 let rhs = this.hir.mirror(rhs);
37 let lhs_span = lhs.span;
39 // Note: we evaluate assignments right-to-left. This
40 // is better for borrowck interaction with overloaded
41 // operators like x[j] = x[i].
43 // Generate better code for things that don't need to be
45 if this.hir.needs_drop(lhs.ty) {
46 let rhs = unpack!(block = this.as_local_operand(block, rhs));
47 let lhs = unpack!(block = this.as_place(block, lhs));
48 unpack!(block = this.build_drop_and_replace(block, lhs_span, lhs, rhs));
51 let rhs = unpack!(block = this.as_local_rvalue(block, rhs));
52 let lhs = unpack!(block = this.as_place(block, lhs));
53 this.cfg.push_assign(block, source_info, &lhs, rhs);
57 ExprKind::AssignOp { op, lhs, rhs } => {
58 // FIXME(#28160) there is an interesting semantics
59 // question raised here -- should we "freeze" the
60 // value of the lhs here? I'm inclined to think not,
61 // since it seems closer to the semantics of the
62 // overloaded version, which takes `&mut self`. This
63 // only affects weird things like `x += {x += 1; x}`
64 // -- is that equal to `x + (x + 1)` or `2*(x+1)`?
66 let lhs = this.hir.mirror(lhs);
70 let rhs = unpack!(block = this.as_local_operand(block, rhs));
71 let lhs = unpack!(block = this.as_place(block, lhs));
73 // we don't have to drop prior contents or anything
74 // because AssignOp is only legal for Copy types
75 // (overloaded ops should be desugared into a call).
77 block = this.build_binary_op(
82 Operand::Copy(lhs.clone()),
86 this.cfg.push_assign(block, source_info, &lhs, result);
90 ExprKind::Continue { label } => {
95 } = *this.find_breakable_scope(expr_span, label);
96 let continue_block = continue_block
97 .expect("Attempted to continue in non-continuable breakable block");
100 (region_scope, source_info),
104 this.cfg.start_new_block().unit()
106 ExprKind::Break { label, value } => {
107 let (break_block, region_scope, destination) = {
111 ref break_destination,
113 } = *this.find_breakable_scope(expr_span, label);
114 (break_block, region_scope, break_destination.clone())
116 if let Some(value) = value {
117 unpack!(block = this.into(&destination, block, value))
119 this.cfg.push_assign_unit(block, source_info, &destination)
121 this.exit_scope(expr_span, (region_scope, source_info), block, break_block);
122 this.cfg.start_new_block().unit()
124 ExprKind::Return { value } => {
125 block = match value {
126 Some(value) => unpack!(this.into(&Place::Local(RETURN_PLACE), block, value)),
129 .push_assign_unit(block, source_info, &Place::Local(RETURN_PLACE));
133 let region_scope = this.region_scope_of_return_scope();
134 let return_block = this.return_block();
135 this.exit_scope(expr_span, (region_scope, source_info), block, return_block);
136 this.cfg.start_new_block().unit()
138 ExprKind::InlineAsm {
143 let outputs = outputs
145 .map(|output| unpack!(block = this.as_place(block, output)))
150 .map(|input| unpack!(block = this.as_local_operand(block, input)))
157 kind: StatementKind::InlineAsm {
158 asm: box asm.clone(),
167 let expr_ty = expr.ty;
168 let temp = this.temp(expr.ty.clone(), expr_span);
169 unpack!(block = this.into(&temp, block, expr));
170 unpack!(block = this.build_drop(block, expr_span, temp, expr_ty));