1 //! See docs in build/expr/mod.rs
3 use crate::build::expr::category::{Category, RvalueFunc};
4 use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
9 use rustc_target::spec::abi::Abi;
11 impl<'a, 'tcx> Builder<'a, 'tcx> {
12 /// Compile `expr`, storing the result into `destination`, which
13 /// is assumed to be uninitialized.
16 destination: &Place<'tcx>,
17 mut block: BasicBlock,
21 "into_expr(destination={:?}, block={:?}, expr={:?})",
22 destination, block, expr
25 // since we frequently have to reference `self` from within a
26 // closure, where `self` would be shadowed, it's easier to
27 // just use the name `this` uniformly
29 let expr_span = expr.span;
30 let source_info = this.source_info(expr_span);
32 let expr_is_block_or_scope = match expr.kind {
33 ExprKind::Block { .. } => true,
34 ExprKind::Scope { .. } => true,
38 if !expr_is_block_or_scope {
39 this.block_context.push(BlockFrame::SubExpr);
42 let block_and = match expr.kind {
48 let region_scope = (region_scope, source_info);
49 this.in_scope(region_scope, lint_level, |this| {
50 this.into(destination, block, value)
53 ExprKind::Block { body: ast_block } => {
54 this.ast_block(destination, block, ast_block, source_info)
56 ExprKind::Match { scrutinee, arms } => {
57 this.match_expr(destination, expr_span, block, scrutinee, arms)
59 ExprKind::NeverToAny { source } => {
60 let source = this.hir.mirror(source);
61 let is_call = match source.kind {
62 ExprKind::Call { .. } => true,
66 unpack!(block = this.as_local_rvalue(block, source));
68 // This is an optimization. If the expression was a call then we already have an
69 // unreachable block. Don't bother to terminate it and create a new one.
74 .terminate(block, source_info, TerminatorKind::Unreachable);
75 let end_block = this.cfg.start_new_block();
79 ExprKind::LogicalOp { op, lhs, rhs } => {
82 // [block: If(lhs)] -true-> [else_block: If(rhs)] -true-> [true_block]
84 // +----------false-----------+------------------> [false_block]
88 // [block: If(lhs)] -false-> [else_block: If(rhs)] -true-> [true_block]
90 // [true_block] [false_block]
92 let (true_block, false_block, mut else_block, join_block) = (
93 this.cfg.start_new_block(),
94 this.cfg.start_new_block(),
95 this.cfg.start_new_block(),
96 this.cfg.start_new_block(),
99 let lhs = unpack!(block = this.as_local_operand(block, lhs));
100 let blocks = match op {
101 LogicalOp::And => (else_block, false_block),
102 LogicalOp::Or => (true_block, else_block),
104 let term = TerminatorKind::if_(this.hir.tcx(), lhs, blocks.0, blocks.1);
105 this.cfg.terminate(block, source_info, term);
107 let rhs = unpack!(else_block = this.as_local_operand(else_block, rhs));
108 let term = TerminatorKind::if_(this.hir.tcx(), rhs, true_block, false_block);
109 this.cfg.terminate(else_block, source_info, term);
111 this.cfg.push_assign_constant(
118 literal: this.hir.true_literal(),
122 this.cfg.push_assign_constant(
129 literal: this.hir.false_literal(),
136 TerminatorKind::Goto { target: join_block },
141 TerminatorKind::Goto { target: join_block },
146 ExprKind::Loop { body } => {
149 // [loop_block] -> [body_block] -/eval. body/-> [body_block_end]
152 // | +-----------------------------------------+
153 // +-> [diverge_cleanup]
154 // The false link is required to make sure borrowck considers unwinds through the
155 // body, even when the exact code in the body cannot unwind
157 let loop_block = this.cfg.start_new_block();
158 let exit_block = this.cfg.start_new_block();
164 TerminatorKind::Goto { target: loop_block },
167 this.in_breakable_scope(
172 // conduct the test, if necessary
173 let body_block = this.cfg.start_new_block();
174 let diverge_cleanup = this.diverge_cleanup();
178 TerminatorKind::FalseUnwind {
179 real_target: body_block,
180 unwind: Some(diverge_cleanup),
184 // The “return” value of the loop body must always be an unit. We therefore
185 // introduce a unit temporary as the destination for the loop body.
186 let tmp = this.get_unit_temp();
187 // Execute the body, branching back to the test.
188 let body_block_end = unpack!(this.into(&tmp, body_block, body));
192 TerminatorKind::Goto { target: loop_block },
198 ExprKind::Call { ty, fun, args, from_hir_call } => {
199 let intrinsic = match ty.sty {
200 ty::FnDef(def_id, _) => {
201 let f = ty.fn_sig(this.hir.tcx());
202 if f.abi() == Abi::RustIntrinsic || f.abi() == Abi::PlatformIntrinsic {
203 Some(this.hir.tcx().item_name(def_id).as_str())
210 let intrinsic = intrinsic.as_ref().map(|s| &s[..]);
211 let fun = unpack!(block = this.as_local_operand(block, fun));
212 if intrinsic == Some("move_val_init") {
213 // `move_val_init` has "magic" semantics - the second argument is
214 // always evaluated "directly" into the first one.
216 let mut args = args.into_iter();
217 let ptr = args.next().expect("0 arguments to `move_val_init`");
218 let val = args.next().expect("1 argument to `move_val_init`");
219 assert!(args.next().is_none(), ">2 arguments to `move_val_init`");
221 let ptr = this.hir.mirror(ptr);
223 // Create an *internal* temp for the pointer, so that unsafety
224 // checking won't complain about the raw pointer assignment.
225 let ptr_temp = this.local_decls.push(LocalDecl {
226 mutability: Mutability::Mut,
228 user_ty: UserTypeProjections::none(),
231 visibility_scope: source_info.scope,
233 is_user_variable: None,
236 let ptr_temp = Place::from(ptr_temp);
237 let block = unpack!(this.into(&ptr_temp, block, ptr));
238 this.into(&ptr_temp.deref(), block, val)
240 let args: Vec<_> = args
242 .map(|arg| unpack!(block = this.as_local_operand(block, arg)))
245 let success = this.cfg.start_new_block();
246 let cleanup = this.diverge_cleanup();
250 TerminatorKind::Call {
253 cleanup: Some(cleanup),
254 // FIXME(varkor): replace this with an uninhabitedness-based check.
255 // This requires getting access to the current module to call
256 // `tcx.is_ty_uninhabited_from`, which is currently tricky to do.
257 destination: if expr.ty.is_never() {
260 Some((destination.clone(), success))
268 ExprKind::Use { source } => {
269 this.into(destination, block, source)
272 // These cases don't actually need a destination
273 ExprKind::Assign { .. }
274 | ExprKind::AssignOp { .. }
275 | ExprKind::Continue { .. }
276 | ExprKind::Break { .. }
277 | ExprKind::InlineAsm { .. }
278 | ExprKind::Return { .. } => {
279 unpack!(block = this.stmt_expr(block, expr, None));
280 this.cfg.push_assign_unit(block, source_info, destination);
284 // Avoid creating a temporary
285 ExprKind::VarRef { .. } |
287 ExprKind::StaticRef { .. } |
288 ExprKind::PlaceTypeAscription { .. } |
289 ExprKind::ValueTypeAscription { .. } => {
290 debug_assert!(Category::of(&expr.kind) == Some(Category::Place));
292 let place = unpack!(block = this.as_place(block, expr));
293 let rvalue = Rvalue::Use(this.consume_by_copy_or_move(place));
295 .push_assign(block, source_info, destination, rvalue);
298 ExprKind::Index { .. } | ExprKind::Deref { .. } | ExprKind::Field { .. } => {
299 debug_assert!(Category::of(&expr.kind) == Some(Category::Place));
301 // Create a "fake" temporary variable so that we check that the
302 // value is Sized. Usually, this is caught in type checking, but
303 // in the case of box expr there is no such check.
304 if !destination.projection.is_empty() {
306 .push(LocalDecl::new_temp(expr.ty, expr.span));
309 debug_assert!(Category::of(&expr.kind) == Some(Category::Place));
311 let place = unpack!(block = this.as_place(block, expr));
312 let rvalue = Rvalue::Use(this.consume_by_copy_or_move(place));
314 .push_assign(block, source_info, destination, rvalue);
318 // these are the cases that are more naturally handled by some other mode
319 ExprKind::Unary { .. }
320 | ExprKind::Binary { .. }
321 | ExprKind::Box { .. }
322 | ExprKind::Cast { .. }
323 | ExprKind::Pointer { .. }
324 | ExprKind::Repeat { .. }
325 | ExprKind::Borrow { .. }
326 | ExprKind::Array { .. }
327 | ExprKind::Tuple { .. }
328 | ExprKind::Adt { .. }
329 | ExprKind::Closure { .. }
330 | ExprKind::Literal { .. }
331 | ExprKind::Yield { .. } => {
332 debug_assert!(match Category::of(&expr.kind).unwrap() {
333 // should be handled above
334 Category::Rvalue(RvalueFunc::Into) => false,
336 // must be handled above or else we get an
337 // infinite loop in the builder; see
338 // e.g., `ExprKind::VarRef` above
339 Category::Place => false,
344 let rvalue = unpack!(block = this.as_local_rvalue(block, expr));
345 this.cfg.push_assign(block, source_info, destination, rvalue);
350 if !expr_is_block_or_scope {
351 let popped = this.block_context.pop();
352 assert!(popped.is_some());