]> git.proxmox.com Git - rustc.git/blob - src/librustc_mir_build/build/expr/as_rvalue.rs
New upstream version 1.42.0+dfsg1
[rustc.git] / src / librustc_mir_build / build / expr / as_rvalue.rs
1 //! See docs in `build/expr/mod.rs`.
2
3 use rustc_index::vec::Idx;
4
5 use crate::build::expr::category::{Category, RvalueFunc};
6 use crate::build::{BlockAnd, BlockAndExtension, Builder};
7 use crate::hair::*;
8 use rustc::middle::region;
9 use rustc::mir::interpret::PanicInfo;
10 use rustc::mir::*;
11 use rustc::ty::{self, Ty, UpvarSubsts};
12 use rustc_span::Span;
13
14 impl<'a, 'tcx> Builder<'a, 'tcx> {
15 /// Returns an rvalue suitable for use until the end of the current
16 /// scope expression.
17 ///
18 /// The operand returned from this function will *not be valid* after
19 /// an ExprKind::Scope is passed, so please do *not* return it from
20 /// functions to avoid bad miscompiles.
21 crate fn as_local_rvalue<M>(&mut self, block: BasicBlock, expr: M) -> BlockAnd<Rvalue<'tcx>>
22 where
23 M: Mirror<'tcx, Output = Expr<'tcx>>,
24 {
25 let local_scope = self.local_scope();
26 self.as_rvalue(block, local_scope, expr)
27 }
28
29 /// Compile `expr`, yielding an rvalue.
30 fn as_rvalue<M>(
31 &mut self,
32 block: BasicBlock,
33 scope: Option<region::Scope>,
34 expr: M,
35 ) -> BlockAnd<Rvalue<'tcx>>
36 where
37 M: Mirror<'tcx, Output = Expr<'tcx>>,
38 {
39 let expr = self.hir.mirror(expr);
40 self.expr_as_rvalue(block, scope, expr)
41 }
42
43 fn expr_as_rvalue(
44 &mut self,
45 mut block: BasicBlock,
46 scope: Option<region::Scope>,
47 expr: Expr<'tcx>,
48 ) -> BlockAnd<Rvalue<'tcx>> {
49 debug!("expr_as_rvalue(block={:?}, scope={:?}, expr={:?})", block, scope, expr);
50
51 let this = self;
52 let expr_span = expr.span;
53 let source_info = this.source_info(expr_span);
54
55 match expr.kind {
56 ExprKind::Scope { region_scope, lint_level, value } => {
57 let region_scope = (region_scope, source_info);
58 this.in_scope(region_scope, lint_level, |this| this.as_rvalue(block, scope, value))
59 }
60 ExprKind::Repeat { value, count } => {
61 let value_operand = unpack!(block = this.as_operand(block, scope, value));
62 block.and(Rvalue::Repeat(value_operand, count))
63 }
64 ExprKind::Binary { op, lhs, rhs } => {
65 let lhs = unpack!(block = this.as_operand(block, scope, lhs));
66 let rhs = unpack!(block = this.as_operand(block, scope, rhs));
67 this.build_binary_op(block, op, expr_span, expr.ty, lhs, rhs)
68 }
69 ExprKind::Unary { op, arg } => {
70 let arg = unpack!(block = this.as_operand(block, scope, arg));
71 // Check for -MIN on signed integers
72 if this.hir.check_overflow() && op == UnOp::Neg && expr.ty.is_signed() {
73 let bool_ty = this.hir.bool_ty();
74
75 let minval = this.minval_literal(expr_span, expr.ty);
76 let is_min = this.temp(bool_ty, expr_span);
77
78 this.cfg.push_assign(
79 block,
80 source_info,
81 &is_min,
82 Rvalue::BinaryOp(BinOp::Eq, arg.to_copy(), minval),
83 );
84
85 block = this.assert(
86 block,
87 Operand::Move(is_min),
88 false,
89 PanicInfo::OverflowNeg,
90 expr_span,
91 );
92 }
93 block.and(Rvalue::UnaryOp(op, arg))
94 }
95 ExprKind::Box { value } => {
96 let value = this.hir.mirror(value);
97 // The `Box<T>` temporary created here is not a part of the HIR,
98 // and therefore is not considered during generator OIBIT
99 // determination. See the comment about `box` at `yield_in_scope`.
100 let result = this.local_decls.push(LocalDecl::new_internal(expr.ty, expr_span));
101 this.cfg.push(
102 block,
103 Statement { source_info, kind: StatementKind::StorageLive(result) },
104 );
105 if let Some(scope) = scope {
106 // schedule a shallow free of that memory, lest we unwind:
107 this.schedule_drop_storage_and_value(expr_span, scope, result);
108 }
109
110 // malloc some memory of suitable type (thus far, uninitialized):
111 let box_ = Rvalue::NullaryOp(NullOp::Box, value.ty);
112 this.cfg.push_assign(block, source_info, &Place::from(result), box_);
113
114 // initialize the box contents:
115 unpack!(
116 block = this.into(
117 &this.hir.tcx().mk_place_deref(Place::from(result)),
118 block,
119 value
120 )
121 );
122 block.and(Rvalue::Use(Operand::Move(Place::from(result))))
123 }
124 ExprKind::Cast { source } => {
125 let source = unpack!(block = this.as_operand(block, scope, source));
126 block.and(Rvalue::Cast(CastKind::Misc, source, expr.ty))
127 }
128 ExprKind::Pointer { cast, source } => {
129 let source = unpack!(block = this.as_operand(block, scope, source));
130 block.and(Rvalue::Cast(CastKind::Pointer(cast), source, expr.ty))
131 }
132 ExprKind::Array { fields } => {
133 // (*) We would (maybe) be closer to codegen if we
134 // handled this and other aggregate cases via
135 // `into()`, not `as_rvalue` -- in that case, instead
136 // of generating
137 //
138 // let tmp1 = ...1;
139 // let tmp2 = ...2;
140 // dest = Rvalue::Aggregate(Foo, [tmp1, tmp2])
141 //
142 // we could just generate
143 //
144 // dest.f = ...1;
145 // dest.g = ...2;
146 //
147 // The problem is that then we would need to:
148 //
149 // (a) have a more complex mechanism for handling
150 // partial cleanup;
151 // (b) distinguish the case where the type `Foo` has a
152 // destructor, in which case creating an instance
153 // as a whole "arms" the destructor, and you can't
154 // write individual fields; and,
155 // (c) handle the case where the type Foo has no
156 // fields. We don't want `let x: ();` to compile
157 // to the same MIR as `let x = ();`.
158
159 // first process the set of fields
160 let el_ty = expr.ty.sequence_element_type(this.hir.tcx());
161 let fields: Vec<_> = fields
162 .into_iter()
163 .map(|f| unpack!(block = this.as_operand(block, scope, f)))
164 .collect();
165
166 block.and(Rvalue::Aggregate(box AggregateKind::Array(el_ty), fields))
167 }
168 ExprKind::Tuple { fields } => {
169 // see (*) above
170 // first process the set of fields
171 let fields: Vec<_> = fields
172 .into_iter()
173 .map(|f| unpack!(block = this.as_operand(block, scope, f)))
174 .collect();
175
176 block.and(Rvalue::Aggregate(box AggregateKind::Tuple, fields))
177 }
178 ExprKind::Closure { closure_id, substs, upvars, movability } => {
179 // see (*) above
180 let operands: Vec<_> = upvars
181 .into_iter()
182 .map(|upvar| {
183 let upvar = this.hir.mirror(upvar);
184 match Category::of(&upvar.kind) {
185 // Use as_place to avoid creating a temporary when
186 // moving a variable into a closure, so that
187 // borrowck knows which variables to mark as being
188 // used as mut. This is OK here because the upvar
189 // expressions have no side effects and act on
190 // disjoint places.
191 // This occurs when capturing by copy/move, while
192 // by reference captures use as_operand
193 Some(Category::Place) => {
194 let place = unpack!(block = this.as_place(block, upvar));
195 this.consume_by_copy_or_move(place)
196 }
197 _ => {
198 // Turn mutable borrow captures into unique
199 // borrow captures when capturing an immutable
200 // variable. This is sound because the mutation
201 // that caused the capture will cause an error.
202 match upvar.kind {
203 ExprKind::Borrow {
204 borrow_kind:
205 BorrowKind::Mut { allow_two_phase_borrow: false },
206 arg,
207 } => unpack!(
208 block = this.limit_capture_mutability(
209 upvar.span, upvar.ty, scope, block, arg,
210 )
211 ),
212 _ => unpack!(block = this.as_operand(block, scope, upvar)),
213 }
214 }
215 }
216 })
217 .collect();
218 let result = match substs {
219 UpvarSubsts::Generator(substs) => {
220 // We implicitly set the discriminant to 0. See
221 // librustc_mir/transform/deaggregator.rs for details.
222 let movability = movability.unwrap();
223 box AggregateKind::Generator(closure_id, substs, movability)
224 }
225 UpvarSubsts::Closure(substs) => box AggregateKind::Closure(closure_id, substs),
226 };
227 block.and(Rvalue::Aggregate(result, operands))
228 }
229 ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
230 block = unpack!(this.stmt_expr(block, expr, None));
231 block.and(this.unit_rvalue())
232 }
233 ExprKind::Yield { value } => {
234 let value = unpack!(block = this.as_operand(block, scope, value));
235 let resume = this.cfg.start_new_block();
236 let cleanup = this.generator_drop_cleanup();
237 this.cfg.terminate(
238 block,
239 source_info,
240 TerminatorKind::Yield { value: value, resume: resume, drop: cleanup },
241 );
242 resume.and(this.unit_rvalue())
243 }
244 ExprKind::Literal { .. }
245 | ExprKind::StaticRef { .. }
246 | ExprKind::Block { .. }
247 | ExprKind::Match { .. }
248 | ExprKind::NeverToAny { .. }
249 | ExprKind::Use { .. }
250 | ExprKind::Borrow { .. }
251 | ExprKind::AddressOf { .. }
252 | ExprKind::Adt { .. }
253 | ExprKind::Loop { .. }
254 | ExprKind::LogicalOp { .. }
255 | ExprKind::Call { .. }
256 | ExprKind::Field { .. }
257 | ExprKind::Deref { .. }
258 | ExprKind::Index { .. }
259 | ExprKind::VarRef { .. }
260 | ExprKind::SelfRef
261 | ExprKind::Break { .. }
262 | ExprKind::Continue { .. }
263 | ExprKind::Return { .. }
264 | ExprKind::InlineAsm { .. }
265 | ExprKind::PlaceTypeAscription { .. }
266 | ExprKind::ValueTypeAscription { .. } => {
267 // these do not have corresponding `Rvalue` variants,
268 // so make an operand and then return that
269 debug_assert!(match Category::of(&expr.kind) {
270 Some(Category::Rvalue(RvalueFunc::AsRvalue)) => false,
271 _ => true,
272 });
273 let operand = unpack!(block = this.as_operand(block, scope, expr));
274 block.and(Rvalue::Use(operand))
275 }
276 }
277 }
278
279 crate fn build_binary_op(
280 &mut self,
281 mut block: BasicBlock,
282 op: BinOp,
283 span: Span,
284 ty: Ty<'tcx>,
285 lhs: Operand<'tcx>,
286 rhs: Operand<'tcx>,
287 ) -> BlockAnd<Rvalue<'tcx>> {
288 let source_info = self.source_info(span);
289 let bool_ty = self.hir.bool_ty();
290 if self.hir.check_overflow() && op.is_checkable() && ty.is_integral() {
291 let result_tup = self.hir.tcx().intern_tup(&[ty, bool_ty]);
292 let result_value = self.temp(result_tup, span);
293
294 self.cfg.push_assign(
295 block,
296 source_info,
297 &result_value,
298 Rvalue::CheckedBinaryOp(op, lhs, rhs),
299 );
300 let val_fld = Field::new(0);
301 let of_fld = Field::new(1);
302
303 let tcx = self.hir.tcx();
304 let val = tcx.mk_place_field(result_value.clone(), val_fld, ty);
305 let of = tcx.mk_place_field(result_value, of_fld, bool_ty);
306
307 let err = PanicInfo::Overflow(op);
308
309 block = self.assert(block, Operand::Move(of), false, err, span);
310
311 block.and(Rvalue::Use(Operand::Move(val)))
312 } else {
313 if ty.is_integral() && (op == BinOp::Div || op == BinOp::Rem) {
314 // Checking division and remainder is more complex, since we 1. always check
315 // and 2. there are two possible failure cases, divide-by-zero and overflow.
316
317 let zero_err = if op == BinOp::Div {
318 PanicInfo::DivisionByZero
319 } else {
320 PanicInfo::RemainderByZero
321 };
322 let overflow_err = PanicInfo::Overflow(op);
323
324 // Check for / 0
325 let is_zero = self.temp(bool_ty, span);
326 let zero = self.zero_literal(span, ty);
327 self.cfg.push_assign(
328 block,
329 source_info,
330 &is_zero,
331 Rvalue::BinaryOp(BinOp::Eq, rhs.to_copy(), zero),
332 );
333
334 block = self.assert(block, Operand::Move(is_zero), false, zero_err, span);
335
336 // We only need to check for the overflow in one case:
337 // MIN / -1, and only for signed values.
338 if ty.is_signed() {
339 let neg_1 = self.neg_1_literal(span, ty);
340 let min = self.minval_literal(span, ty);
341
342 let is_neg_1 = self.temp(bool_ty, span);
343 let is_min = self.temp(bool_ty, span);
344 let of = self.temp(bool_ty, span);
345
346 // this does (rhs == -1) & (lhs == MIN). It could short-circuit instead
347
348 self.cfg.push_assign(
349 block,
350 source_info,
351 &is_neg_1,
352 Rvalue::BinaryOp(BinOp::Eq, rhs.to_copy(), neg_1),
353 );
354 self.cfg.push_assign(
355 block,
356 source_info,
357 &is_min,
358 Rvalue::BinaryOp(BinOp::Eq, lhs.to_copy(), min),
359 );
360
361 let is_neg_1 = Operand::Move(is_neg_1);
362 let is_min = Operand::Move(is_min);
363 self.cfg.push_assign(
364 block,
365 source_info,
366 &of,
367 Rvalue::BinaryOp(BinOp::BitAnd, is_neg_1, is_min),
368 );
369
370 block = self.assert(block, Operand::Move(of), false, overflow_err, span);
371 }
372 }
373
374 block.and(Rvalue::BinaryOp(op, lhs, rhs))
375 }
376 }
377
378 fn limit_capture_mutability(
379 &mut self,
380 upvar_span: Span,
381 upvar_ty: Ty<'tcx>,
382 temp_lifetime: Option<region::Scope>,
383 mut block: BasicBlock,
384 arg: ExprRef<'tcx>,
385 ) -> BlockAnd<Operand<'tcx>> {
386 let this = self;
387
388 let source_info = this.source_info(upvar_span);
389 let temp = this.local_decls.push(LocalDecl::new_temp(upvar_ty, upvar_span));
390
391 this.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(temp) });
392
393 let arg_place = unpack!(block = this.as_place(block, arg));
394
395 let mutability = match arg_place.as_ref() {
396 PlaceRef { local, projection: &[] } => this.local_decls[*local].mutability,
397 PlaceRef { local, projection: &[ProjectionElem::Deref] } => {
398 debug_assert!(
399 this.local_decls[*local].is_ref_for_guard(),
400 "Unexpected capture place",
401 );
402 this.local_decls[*local].mutability
403 }
404 PlaceRef {
405 ref local,
406 projection: &[ref proj_base @ .., ProjectionElem::Field(upvar_index, _)],
407 }
408 | PlaceRef {
409 ref local,
410 projection:
411 &[ref proj_base @ .., ProjectionElem::Field(upvar_index, _), ProjectionElem::Deref],
412 } => {
413 let place = PlaceRef { local, projection: proj_base };
414
415 // Not projected from the implicit `self` in a closure.
416 debug_assert!(
417 match place.local_or_deref_local() {
418 Some(local) => local == Local::new(1),
419 None => false,
420 },
421 "Unexpected capture place"
422 );
423 // Not in a closure
424 debug_assert!(
425 this.upvar_mutbls.len() > upvar_index.index(),
426 "Unexpected capture place"
427 );
428 this.upvar_mutbls[upvar_index.index()]
429 }
430 _ => bug!("Unexpected capture place"),
431 };
432
433 let borrow_kind = match mutability {
434 Mutability::Not => BorrowKind::Unique,
435 Mutability::Mut => BorrowKind::Mut { allow_two_phase_borrow: false },
436 };
437
438 this.cfg.push_assign(
439 block,
440 source_info,
441 &Place::from(temp),
442 Rvalue::Ref(this.hir.tcx().lifetimes.re_erased, borrow_kind, arg_place),
443 );
444
445 // In constants, temp_lifetime is None. We should not need to drop
446 // anything because no values with a destructor can be created in
447 // a constant at this time, even if the type may need dropping.
448 if let Some(temp_lifetime) = temp_lifetime {
449 this.schedule_drop_storage_and_value(upvar_span, temp_lifetime, temp);
450 }
451
452 block.and(Operand::Move(Place::from(temp)))
453 }
454
455 // Helper to get a `-1` value of the appropriate type
456 fn neg_1_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
457 let param_ty = ty::ParamEnv::empty().and(ty);
458 let bits = self.hir.tcx().layout_of(param_ty).unwrap().size.bits();
459 let n = (!0u128) >> (128 - bits);
460 let literal = ty::Const::from_bits(self.hir.tcx(), n, param_ty);
461
462 self.literal_operand(span, literal)
463 }
464
465 // Helper to get the minimum value of the appropriate type
466 fn minval_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
467 assert!(ty.is_signed());
468 let param_ty = ty::ParamEnv::empty().and(ty);
469 let bits = self.hir.tcx().layout_of(param_ty).unwrap().size.bits();
470 let n = 1 << (bits - 1);
471 let literal = ty::Const::from_bits(self.hir.tcx(), n, param_ty);
472
473 self.literal_operand(span, literal)
474 }
475 }