]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/expr/stmt.rs
Rollup merge of #55490 - petrochenkov:resolveice, r=eddyb
[rust.git] / src / librustc_mir / build / expr / stmt.rs
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.
4 //
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.
10
11 use build::scope::BreakableScope;
12 use build::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
13 use hair::*;
14 use rustc::mir::*;
15
16 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
17     pub fn stmt_expr(&mut self, mut block: BasicBlock, expr: Expr<'tcx>) -> BlockAnd<()> {
18         let this = self;
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.
23         let expr2 = expr.clone();
24         match expr.kind {
25             ExprKind::Scope {
26                 region_scope,
27                 lint_level,
28                 value,
29             } => {
30                 let value = this.hir.mirror(value);
31                 this.in_scope((region_scope, source_info), lint_level, block, |this| {
32                     this.stmt_expr(block, value)
33                 })
34             }
35             ExprKind::Assign { lhs, rhs } => {
36                 let lhs = this.hir.mirror(lhs);
37                 let rhs = this.hir.mirror(rhs);
38                 let lhs_span = lhs.span;
39
40                 // Note: we evaluate assignments right-to-left. This
41                 // is better for borrowck interaction with overloaded
42                 // operators like x[j] = x[i].
43
44                 debug!("stmt_expr Assign block_context.push(SubExpr) : {:?}", expr2);
45                 this.block_context.push(BlockFrame::SubExpr);
46
47                 // Generate better code for things that don't need to be
48                 // dropped.
49                 if this.hir.needs_drop(lhs.ty) {
50                     let rhs = unpack!(block = this.as_local_operand(block, rhs));
51                     let lhs = unpack!(block = this.as_place(block, lhs));
52                     unpack!(block = this.build_drop_and_replace(block, lhs_span, lhs, rhs));
53                 } else {
54                     let rhs = unpack!(block = this.as_local_rvalue(block, rhs));
55                     let lhs = unpack!(block = this.as_place(block, lhs));
56                     this.cfg.push_assign(block, source_info, &lhs, rhs);
57                 }
58
59                 this.block_context.pop();
60                 block.unit()
61             }
62             ExprKind::AssignOp { op, lhs, rhs } => {
63                 // FIXME(#28160) there is an interesting semantics
64                 // question raised here -- should we "freeze" the
65                 // value of the lhs here?  I'm inclined to think not,
66                 // since it seems closer to the semantics of the
67                 // overloaded version, which takes `&mut self`.  This
68                 // only affects weird things like `x += {x += 1; x}`
69                 // -- is that equal to `x + (x + 1)` or `2*(x+1)`?
70
71                 let lhs = this.hir.mirror(lhs);
72                 let lhs_ty = lhs.ty;
73
74                 debug!("stmt_expr AssignOp block_context.push(SubExpr) : {:?}", expr2);
75                 this.block_context.push(BlockFrame::SubExpr);
76
77                 // As above, RTL.
78                 let rhs = unpack!(block = this.as_local_operand(block, rhs));
79                 let lhs = unpack!(block = this.as_place(block, lhs));
80
81                 // we don't have to drop prior contents or anything
82                 // because AssignOp is only legal for Copy types
83                 // (overloaded ops should be desugared into a call).
84                 let result = unpack!(
85                     block = this.build_binary_op(
86                         block,
87                         op,
88                         expr_span,
89                         lhs_ty,
90                         Operand::Copy(lhs.clone()),
91                         rhs
92                     )
93                 );
94                 this.cfg.push_assign(block, source_info, &lhs, result);
95
96                 this.block_context.pop();
97                 block.unit()
98             }
99             ExprKind::Continue { label } => {
100                 let BreakableScope {
101                     continue_block,
102                     region_scope,
103                     ..
104                 } = *this.find_breakable_scope(expr_span, label);
105                 let continue_block = continue_block
106                     .expect("Attempted to continue in non-continuable breakable block");
107                 this.exit_scope(
108                     expr_span,
109                     (region_scope, source_info),
110                     block,
111                     continue_block,
112                 );
113                 this.cfg.start_new_block().unit()
114             }
115             ExprKind::Break { label, value } => {
116                 let (break_block, region_scope, destination) = {
117                     let BreakableScope {
118                         break_block,
119                         region_scope,
120                         ref break_destination,
121                         ..
122                     } = *this.find_breakable_scope(expr_span, label);
123                     (break_block, region_scope, break_destination.clone())
124                 };
125                 if let Some(value) = value {
126                     debug!("stmt_expr Break val block_context.push(SubExpr) : {:?}", expr2);
127                     this.block_context.push(BlockFrame::SubExpr);
128                     unpack!(block = this.into(&destination, block, value));
129                     this.block_context.pop();
130                 } else {
131                     this.cfg.push_assign_unit(block, source_info, &destination)
132                 }
133                 this.exit_scope(expr_span, (region_scope, source_info), block, break_block);
134                 this.cfg.start_new_block().unit()
135             }
136             ExprKind::Return { value } => {
137                 block = match value {
138                     Some(value) => {
139                         debug!("stmt_expr Return val block_context.push(SubExpr) : {:?}", expr2);
140                         this.block_context.push(BlockFrame::SubExpr);
141                         let result = unpack!(this.into(&Place::Local(RETURN_PLACE), block, value));
142                         this.block_context.pop();
143                         result
144                     }
145                     None => {
146                         this.cfg
147                             .push_assign_unit(block, source_info, &Place::Local(RETURN_PLACE));
148                         block
149                     }
150                 };
151                 let region_scope = this.region_scope_of_return_scope();
152                 let return_block = this.return_block();
153                 this.exit_scope(expr_span, (region_scope, source_info), block, return_block);
154                 this.cfg.start_new_block().unit()
155             }
156             ExprKind::InlineAsm {
157                 asm,
158                 outputs,
159                 inputs,
160             } => {
161                 debug!("stmt_expr InlineAsm block_context.push(SubExpr) : {:?}", expr2);
162                 this.block_context.push(BlockFrame::SubExpr);
163                 let outputs = outputs
164                     .into_iter()
165                     .map(|output| unpack!(block = this.as_place(block, output)))
166                     .collect::<Vec<_>>()
167                     .into_boxed_slice();
168                 let inputs = inputs
169                     .into_iter()
170                     .map(|input| {
171                         (
172                             input.span(),
173                             unpack!(block = this.as_local_operand(block, input)),
174                         )
175                     }).collect::<Vec<_>>()
176                     .into_boxed_slice();
177                 this.cfg.push(
178                     block,
179                     Statement {
180                         source_info,
181                         kind: StatementKind::InlineAsm {
182                             asm: box asm.clone(),
183                             outputs,
184                             inputs,
185                         },
186                     },
187                 );
188                 this.block_context.pop();
189                 block.unit()
190             }
191             _ => {
192                 let expr_ty = expr.ty;
193                 let temp = this.temp(expr.ty.clone(), expr_span);
194                 unpack!(block = this.into(&temp, block, expr));
195                 unpack!(block = this.build_drop(block, expr_span, temp, expr_ty));
196                 block.unit()
197             }
198         }
199     }
200 }