]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/expr/stmt.rs
Fix two UI tests with locale-dependent output
[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, 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         match expr.kind {
24             ExprKind::Scope {
25                 region_scope,
26                 lint_level,
27                 value,
28             } => {
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)
32                 })
33             }
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;
38
39                 // Note: we evaluate assignments right-to-left. This
40                 // is better for borrowck interaction with overloaded
41                 // operators like x[j] = x[i].
42
43                 // Generate better code for things that don't need to be
44                 // dropped.
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));
49                     block.unit()
50                 } else {
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);
54                     block.unit()
55                 }
56             }
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)`?
65
66                 let lhs = this.hir.mirror(lhs);
67                 let lhs_ty = lhs.ty;
68
69                 // As above, RTL.
70                 let rhs = unpack!(block = this.as_local_operand(block, rhs));
71                 let lhs = unpack!(block = this.as_place(block, lhs));
72
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).
76                 let result = unpack!(
77                     block = this.build_binary_op(
78                         block,
79                         op,
80                         expr_span,
81                         lhs_ty,
82                         Operand::Copy(lhs.clone()),
83                         rhs
84                     )
85                 );
86                 this.cfg.push_assign(block, source_info, &lhs, result);
87
88                 block.unit()
89             }
90             ExprKind::Continue { label } => {
91                 let BreakableScope {
92                     continue_block,
93                     region_scope,
94                     ..
95                 } = *this.find_breakable_scope(expr_span, label);
96                 let continue_block = continue_block
97                     .expect("Attempted to continue in non-continuable breakable block");
98                 this.exit_scope(
99                     expr_span,
100                     (region_scope, source_info),
101                     block,
102                     continue_block,
103                 );
104                 this.cfg.start_new_block().unit()
105             }
106             ExprKind::Break { label, value } => {
107                 let (break_block, region_scope, destination) = {
108                     let BreakableScope {
109                         break_block,
110                         region_scope,
111                         ref break_destination,
112                         ..
113                     } = *this.find_breakable_scope(expr_span, label);
114                     (break_block, region_scope, break_destination.clone())
115                 };
116                 if let Some(value) = value {
117                     unpack!(block = this.into(&destination, block, value))
118                 } else {
119                     this.cfg.push_assign_unit(block, source_info, &destination)
120                 }
121                 this.exit_scope(expr_span, (region_scope, source_info), block, break_block);
122                 this.cfg.start_new_block().unit()
123             }
124             ExprKind::Return { value } => {
125                 block = match value {
126                     Some(value) => unpack!(this.into(&Place::Local(RETURN_PLACE), block, value)),
127                     None => {
128                         this.cfg
129                             .push_assign_unit(block, source_info, &Place::Local(RETURN_PLACE));
130                         block
131                     }
132                 };
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()
137             }
138             ExprKind::InlineAsm {
139                 asm,
140                 outputs,
141                 inputs,
142             } => {
143                 let outputs = outputs
144                     .into_iter()
145                     .map(|output| unpack!(block = this.as_place(block, output)))
146                     .collect::<Vec<_>>()
147                     .into_boxed_slice();
148                 let inputs = inputs
149                     .into_iter()
150                     .map(|input| unpack!(block = this.as_local_operand(block, input)))
151                     .collect::<Vec<_>>()
152                     .into_boxed_slice();
153                 this.cfg.push(
154                     block,
155                     Statement {
156                         source_info,
157                         kind: StatementKind::InlineAsm {
158                             asm: box asm.clone(),
159                             outputs,
160                             inputs,
161                         },
162                     },
163                 );
164                 block.unit()
165             }
166             _ => {
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));
171                 block.unit()
172             }
173         }
174     }
175 }