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();
248 this
.record_operands_moved(&args
);
253 TerminatorKind
::Call
{
256 cleanup
: Some(cleanup
),
257 // FIXME(varkor): replace this with an uninhabitedness-based check.
258 // This requires getting access to the current module to call
259 // `tcx.is_ty_uninhabited_from`, which is currently tricky to do.
260 destination
: if expr
.ty
.is_never() {
263 Some((destination
.clone(), success
))
271 ExprKind
::Use { source }
=> {
272 this
.into(destination
, block
, source
)
275 // These cases don't actually need a destination
276 ExprKind
::Assign { .. }
277 | ExprKind
::AssignOp { .. }
278 | ExprKind
::Continue { .. }
279 | ExprKind
::Break { .. }
280 | ExprKind
::InlineAsm { .. }
281 | ExprKind
::Return { .. }
=> {
282 unpack
!(block
= this
.stmt_expr(block
, expr
, None
));
283 this
.cfg
.push_assign_unit(block
, source_info
, destination
);
287 // Avoid creating a temporary
288 ExprKind
::VarRef { .. }
|
290 ExprKind
::StaticRef { .. }
|
291 ExprKind
::PlaceTypeAscription { .. }
|
292 ExprKind
::ValueTypeAscription { .. }
=> {
293 debug_assert
!(Category
::of(&expr
.kind
) == Some(Category
::Place
));
295 let place
= unpack
!(block
= this
.as_place(block
, expr
));
296 let rvalue
= Rvalue
::Use(this
.consume_by_copy_or_move(place
));
298 .push_assign(block
, source_info
, destination
, rvalue
);
301 ExprKind
::Index { .. }
| ExprKind
::Deref { .. }
| ExprKind
::Field { .. }
=> {
302 debug_assert
!(Category
::of(&expr
.kind
) == Some(Category
::Place
));
304 // Create a "fake" temporary variable so that we check that the
305 // value is Sized. Usually, this is caught in type checking, but
306 // in the case of box expr there is no such check.
307 if !destination
.projection
.is_empty() {
309 .push(LocalDecl
::new_temp(expr
.ty
, expr
.span
));
312 debug_assert
!(Category
::of(&expr
.kind
) == Some(Category
::Place
));
314 let place
= unpack
!(block
= this
.as_place(block
, expr
));
315 let rvalue
= Rvalue
::Use(this
.consume_by_copy_or_move(place
));
317 .push_assign(block
, source_info
, destination
, rvalue
);
321 // these are the cases that are more naturally handled by some other mode
322 ExprKind
::Unary { .. }
323 | ExprKind
::Binary { .. }
324 | ExprKind
::Box { .. }
325 | ExprKind
::Cast { .. }
326 | ExprKind
::Pointer { .. }
327 | ExprKind
::Repeat { .. }
328 | ExprKind
::Borrow { .. }
329 | ExprKind
::Array { .. }
330 | ExprKind
::Tuple { .. }
331 | ExprKind
::Adt { .. }
332 | ExprKind
::Closure { .. }
333 | ExprKind
::Literal { .. }
334 | ExprKind
::Yield { .. }
=> {
335 debug_assert
!(match Category
::of(&expr
.kind
).unwrap() {
336 // should be handled above
337 Category
::Rvalue(RvalueFunc
::Into
) => false,
339 // must be handled above or else we get an
340 // infinite loop in the builder; see
341 // e.g., `ExprKind::VarRef` above
342 Category
::Place
=> false,
347 let rvalue
= unpack
!(block
= this
.as_local_rvalue(block
, expr
));
348 this
.cfg
.push_assign(block
, source_info
, destination
, rvalue
);
353 if !expr_is_block_or_scope
{
354 let popped
= this
.block_context
.pop();
355 assert
!(popped
.is_some());