charon_lib/transform/resugar/reconstruct_fallible_operations.rs
1//! # Micro-pass: remove the dynamic checks for array/slice bounds, overflow, and division by zero.
2//! Note that from a semantic point of view, an out-of-bound access or a division by zero
3//! must lead to a panic in Rust (which is why those checks are always present, even when
4//! compiling for release). In our case, we take this into account in the semantics of our
5//! array/slice manipulation and arithmetic functions, on the verification side.
6
7use rustc_hash::FxHashSet as HashSet;
8
9use crate::transform::TransformCtx;
10use crate::transform::ctx::UllbcPass;
11use crate::ullbc_ast::*;
12
13type LocalUses = IndexVec<BlockId, HashSet<LocalId>>;
14
15/// Compute for each block the locals that are assumed to have been initialized/defined before entering it.
16fn compute_uses(body: &ExprBody) -> LocalUses {
17 #[derive(Visitor)]
18 struct UsedLocalsVisitor<'a>(&'a mut HashSet<LocalId>);
19
20 impl VisitBody for UsedLocalsVisitor<'_> {
21 fn visit_place(&mut self, x: &Place) -> ::std::ops::ControlFlow<Self::Break> {
22 if let Some(local_id) = x.as_local() {
23 self.0.insert(local_id);
24 }
25 self.visit_inner(x)
26 }
27 }
28
29 body.body.map_ref(|block| {
30 let mut uses = HashSet::default();
31 let mut visitor = UsedLocalsVisitor(&mut uses);
32
33 // do a simple live variable analysis by walking the block backwards
34 for statement in block.statements.iter().rev() {
35 match &statement.kind {
36 StatementKind::Assign(place, rval) => {
37 // We clear the assigned place, but it may be re-added
38 // if it's used in rval
39 if let Some(local_id) = place.as_local() {
40 visitor.0.remove(&local_id);
41 }
42 let _ = rval.drive_body(&mut visitor);
43 }
44 StatementKind::StorageLive(local) | StatementKind::StorageDead(local) => {
45 // A `StorageLive` re-sets the local to be uninitialised,
46 // so any usage after this point doesn't matter
47 // Similarly, a `StorageDead` means the local is de-initialised,
48 // so we can ignore any usage after this point
49 visitor.0.remove(local);
50 }
51 _ => {
52 let _ = statement.drive_body(&mut visitor);
53 }
54 }
55 }
56
57 uses
58 })
59}
60
61/// Whether the value uses the given local in a place.
62fn uses_local<T: BodyVisitable>(x: &T, local: LocalId) -> bool {
63 struct FoundIt;
64 struct UsesLocalVisitor(LocalId);
65
66 impl Visitor for UsesLocalVisitor {
67 type Break = FoundIt;
68 }
69 impl VisitBody for UsesLocalVisitor {
70 fn visit_place(&mut self, x: &Place) -> ::std::ops::ControlFlow<Self::Break> {
71 if let Some(local_id) = x.as_local()
72 && local_id == self.0
73 {
74 return ControlFlow::Break(FoundIt);
75 }
76 self.visit_inner(x)
77 }
78
79 fn visit_ullbc_statement(
80 &mut self,
81 x: &ullbc_ast::Statement,
82 ) -> ::std::ops::ControlFlow<Self::Break> {
83 match x.kind {
84 StatementKind::StorageDead(_) | StatementKind::StorageLive(_) => {
85 ControlFlow::Continue(())
86 }
87 _ => self.visit_inner(x),
88 }
89 }
90 }
91
92 x.drive_body(&mut UsesLocalVisitor(local)).is_break()
93}
94
95fn make_binop_overflow_panic<T: BodyVisitable>(
96 x: &mut [T],
97 matches: impl Fn(&BinOp, &Operand, &Operand) -> bool,
98) -> bool {
99 let mut found = false;
100 for y in x.iter_mut() {
101 y.dyn_visit_in_body_mut(|rv: &mut Rvalue| {
102 if let Rvalue::BinaryOp(binop, op_l, op_r) = rv
103 && matches(binop, op_l, op_r)
104 {
105 *binop = binop.with_overflow(OverflowMode::Panic);
106 found = true;
107 }
108 });
109 }
110 found
111}
112
113fn make_unop_overflow_panic<T: BodyVisitable>(
114 x: &mut [T],
115 matches: impl Fn(&UnOp, &Operand) -> bool,
116) -> bool {
117 let mut found = false;
118 for y in x.iter_mut() {
119 y.dyn_visit_in_body_mut(|rv: &mut Rvalue| {
120 if let Rvalue::UnaryOp(unop, op) = rv
121 && matches(unop, op)
122 {
123 *unop = unop.with_overflow(OverflowMode::Panic);
124 found = true;
125 }
126 });
127 }
128 found
129}
130
131/// Check if the two operands are equivalent: either they're the same constant, or they represent
132/// the same place (regardless of whether the operand is a move or a copy).
133fn equiv_op(op_l: &Operand, op_r: &Operand) -> bool {
134 match (op_l, op_r) {
135 (Operand::Copy(l, _) | Operand::Move(l), Operand::Copy(r, _) | Operand::Move(r)) => l == r,
136 (Operand::Const(l), Operand::Const(r)) => l == r,
137 _ => false,
138 }
139}
140
141/// A shift overflow check whose shift operation is not in the same block: rustc evaluates the
142/// shift operands (which is when it emits the overflow check) before computing the destination
143/// place, so when that computation involves a function call (e.g. `out[i] = x >> y` through an
144/// overloaded `IndexMut`), the check ends up separated from the shift by the call terminator.
145/// See issue #1041. These asserts are only ever emitted by the compiler, so instead of following
146/// the control flow we record the check and find the matching shift over the whole body in
147/// [resolve_pending_shift_check].
148struct PendingShiftCheck {
149 /// The block containing the check.
150 block: BlockId,
151 /// The checked shift amount; used to find the shift.
152 amount: Operand,
153 /// The comparison result asserted by the check. Identifies the check's statements to remove.
154 cond_local: LocalId,
155 /// The `y as uN` cast temporary, when the amount is cast before the comparison; also removed.
156 cast_local: Option<LocalId>,
157}
158
159/// Rustc inserts dynamic checks during MIR lowering. They all end in an `Assert` statement (and
160/// this is the only use of this statement).
161fn remove_dynamic_checks(
162 uses: &LocalUses,
163 block_id: BlockId,
164 statements: &mut [Statement],
165 pending_shift_checks: &mut Vec<PendingShiftCheck>,
166) {
167 // Whether this local was used in another block
168 let used_outside_block = |local: LocalId| {
169 uses.iter_enumerated()
170 .any(|(bid, used)| bid != block_id && used.contains(&local))
171 };
172
173 // We return the statements we want to keep, which must be a prefix of `block.statements`.
174 let statements_to_keep = match statements {
175 // Bounds checks for slices. They look like:
176 // l := use(copy a.metadata)
177 // b := copy x < copy l
178 // assert(move b == true)
179 [
180 Statement {
181 kind: StatementKind::Assign(len, Rvalue::Use(Operand::Copy(len_op, _), _)),
182 ..
183 },
184 Statement {
185 kind:
186 StatementKind::Assign(
187 is_in_bounds,
188 Rvalue::BinaryOp(BinOp::Lt, _, Operand::Copy(lt_op2, _)),
189 ),
190 ..
191 },
192 Statement {
193 kind:
194 StatementKind::Assert {
195 assert:
196 Assert {
197 cond: Operand::Move(cond),
198 expected: true,
199 ..
200 },
201 ..
202 },
203 ..
204 },
205 rest @ ..,
206 ] if lt_op2 == len
207 && cond == is_in_bounds
208 && let Some((_, ProjectionElem::PtrMetadata)) = len_op.as_projection() =>
209 {
210 rest
211 }
212 // Sometimes that instead looks like:
213 // a := &raw const *z
214 // l := use(copy a.metadata)
215 // b := copy x < copy l
216 // assert(move b == true)
217 [
218 Statement {
219 kind:
220 StatementKind::Assign(
221 reborrow,
222 Rvalue::RawPtr {
223 kind: RefKind::Shared,
224 ..
225 },
226 ),
227 ..
228 },
229 Statement {
230 kind: StatementKind::Assign(len, Rvalue::Use(Operand::Copy(len_op, _), _)),
231 ..
232 },
233 Statement {
234 kind:
235 StatementKind::Assign(
236 is_in_bounds,
237 Rvalue::BinaryOp(BinOp::Lt, _, Operand::Copy(lt_op2, _)),
238 ),
239 ..
240 },
241 Statement {
242 kind:
243 StatementKind::Assert {
244 assert:
245 Assert {
246 cond: Operand::Move(cond),
247 expected: true,
248 check_kind: Some(BuiltinAssertKind::BoundsCheck { .. }),
249 ..
250 },
251 ..
252 },
253 ..
254 },
255 rest @ ..,
256 ] if lt_op2 == len
257 && cond == is_in_bounds
258 && let Some((slice_place, ProjectionElem::PtrMetadata)) = len_op.as_projection()
259 && reborrow == slice_place =>
260 {
261 rest
262 }
263
264 // Zero checks for division and remainder. They look like:
265 // b := copy y == const 0
266 // assert(move b == false)
267 // ...
268 // res := x {/,%} move y;
269 // ... or ...
270 // b := const y == const 0
271 // assert(move b == false)
272 // ...
273 // res := x {/,%} const y;
274 //
275 // This also overlaps with overflow checks for negation, which looks like:
276 // is_min := x == INT::min
277 // assert(move is_min == false)
278 // ...
279 // res := -x;
280 [
281 Statement {
282 kind:
283 StatementKind::Assign(
284 is_zero,
285 Rvalue::BinaryOp(BinOp::Eq, y_op, Operand::Const(_zero)),
286 ),
287 ..
288 },
289 Statement {
290 kind:
291 StatementKind::Assert {
292 assert:
293 Assert {
294 cond: Operand::Move(cond),
295 expected: false,
296 check_kind:
297 Some(
298 BuiltinAssertKind::DivisionByZero(_)
299 | BuiltinAssertKind::RemainderByZero(_)
300 | BuiltinAssertKind::OverflowNeg(_),
301 ),
302 ..
303 },
304 ..
305 },
306 ..
307 },
308 rest @ ..,
309 ] if cond == is_zero => {
310 let found = make_binop_overflow_panic(rest, |bop, _, r| {
311 matches!(bop, BinOp::Div(_) | BinOp::Rem(_)) && equiv_op(r, y_op)
312 }) || make_unop_overflow_panic(rest, |unop, o| {
313 matches!(unop, UnOp::Neg(_)) && equiv_op(o, y_op)
314 });
315 if found {
316 rest
317 } else {
318 return;
319 }
320 }
321
322 // Overflow checks for signed division and remainder. They look like:
323 // is_neg_1 := y == (-1)
324 // is_min := x == INT::min
325 // has_overflow := move (is_neg_1) & move (is_min)
326 // assert(move has_overflow == false)
327 // Note here we don't need to update the operand to panic, as this was already done
328 // by the previous pass for division by zero.
329 [
330 Statement {
331 kind: StatementKind::Assign(is_neg_1, Rvalue::BinaryOp(BinOp::Eq, _y_op, _minus_1)),
332 ..
333 },
334 Statement {
335 kind: StatementKind::Assign(is_min, Rvalue::BinaryOp(BinOp::Eq, _x_op, _int_min)),
336 ..
337 },
338 Statement {
339 kind:
340 StatementKind::Assign(
341 has_overflow,
342 Rvalue::BinaryOp(
343 BinOp::BitAnd,
344 Operand::Move(and_op1),
345 Operand::Move(and_op2),
346 ),
347 ),
348 ..
349 },
350 Statement {
351 kind:
352 StatementKind::Assert {
353 assert:
354 Assert {
355 cond: Operand::Move(cond),
356 expected: false,
357 check_kind: Some(BuiltinAssertKind::Overflow(..)),
358 ..
359 },
360 ..
361 },
362 ..
363 },
364 rest @ ..,
365 ] if and_op1 == is_neg_1 && and_op2 == is_min && cond == has_overflow => rest,
366
367 // Overflow checks for right/left shift. They can look like:
368 // a := y as u32; // or another type
369 // b := move a < const 32; // or another constant
370 // assert(move b == true);
371 // ...
372 // res := x {<<,>>} y;
373 [
374 Statement {
375 kind: StatementKind::Assign(cast, Rvalue::UnaryOp(UnOp::Cast(_), y_op)),
376 ..
377 },
378 Statement {
379 kind:
380 StatementKind::Assign(
381 has_overflow,
382 Rvalue::BinaryOp(BinOp::Lt, Operand::Move(lhs), Operand::Const(..)),
383 ),
384 ..
385 },
386 Statement {
387 kind:
388 StatementKind::Assert {
389 assert:
390 Assert {
391 cond: Operand::Move(cond),
392 expected: true,
393 check_kind: Some(BuiltinAssertKind::Overflow(..)),
394 ..
395 },
396 ..
397 },
398 ..
399 },
400 rest @ ..,
401 ] if cond == has_overflow
402 && lhs == cast
403 && let Some(cast_local) = cast.as_local()
404 && !rest.iter().any(|st| uses_local(st, cast_local)) =>
405 {
406 let found = make_binop_overflow_panic(rest, |bop, _, r| {
407 matches!(bop, BinOp::Shl(_) | BinOp::Shr(_)) && equiv_op(r, y_op)
408 });
409 if found {
410 rest
411 } else {
412 // The shift is not in this block; it may live in a later one (see
413 // [PendingShiftCheck]). Record it and resolve it over the whole body afterwards.
414 if let Some(cond_local) = has_overflow.as_local() {
415 pending_shift_checks.push(PendingShiftCheck {
416 block: block_id,
417 amount: y_op.clone(),
418 cond_local,
419 cast_local: Some(cast_local),
420 });
421 }
422 return;
423 }
424 }
425 // or like:
426 // b := y < const 32; // or another constant
427 // assert(move b == true);
428 // ...
429 // res := x {<<,>>} y;
430 //
431 // this also overlaps with out of bounds checks for arrays, so we check for either;
432 // these look like:
433 // b := copy y < const _
434 // assert(move b == true)
435 // ...
436 // res := a[y];
437 [
438 Statement {
439 kind:
440 StatementKind::Assign(
441 has_overflow,
442 Rvalue::BinaryOp(BinOp::Lt, y_op, Operand::Const(..)),
443 ),
444 ..
445 },
446 Statement {
447 kind:
448 StatementKind::Assert {
449 assert:
450 Assert {
451 cond: Operand::Move(cond),
452 expected: true,
453 check_kind:
454 check_kind @ Some(
455 BuiltinAssertKind::Overflow(..)
456 | BuiltinAssertKind::BoundsCheck { .. },
457 ),
458 ..
459 },
460 ..
461 },
462 ..
463 },
464 rest @ ..,
465 ] if cond == has_overflow => {
466 // look for a shift operation
467 let mut found = make_binop_overflow_panic(rest, |bop, _, r| {
468 matches!(bop, BinOp::Shl(_) | BinOp::Shr(_)) && equiv_op(r, y_op)
469 });
470 if !found {
471 // otherwise, look for an array access
472 for stmt in rest.iter_mut() {
473 stmt.dyn_visit_in_body(|p: &Place| {
474 if let Some((_, ProjectionElem::Index { offset, .. })) = p.as_projection()
475 && equiv_op(offset, y_op)
476 {
477 found = true;
478 }
479 });
480 }
481 }
482
483 if found {
484 rest
485 } else {
486 // The shift may be in a later block (see [PendingShiftCheck]). This only applies
487 // to overflow checks: a bounds check never guards a shift.
488 if matches!(check_kind, Some(BuiltinAssertKind::Overflow(..)))
489 && let Some(cond_local) = has_overflow.as_local()
490 {
491 pending_shift_checks.push(PendingShiftCheck {
492 block: block_id,
493 amount: y_op.clone(),
494 cond_local,
495 cast_local: None,
496 });
497 }
498 return;
499 }
500 }
501
502 _ => return,
503 };
504
505 // Remove the statements we're not keeping.
506 let keep_len = statements_to_keep.len();
507 let removed_len = statements.len() - keep_len;
508 for i in 0..removed_len {
509 // If the statement we're removing assigns to a local that
510 // is used elsewhere (in the leftover statements or in another block),
511 // we don't remove it.
512 if let StatementKind::Assign(place, _) = &statements[i].kind
513 && let Some(local) = place.as_local()
514 && let mut statements_to_keep = statements[removed_len..].as_ref().iter()
515 && (used_outside_block(local) || statements_to_keep.any(|st| uses_local(st, local)))
516 {
517 continue;
518 };
519 statements[i].kind = StatementKind::Nop;
520 }
521}
522
523/// Resolve a [PendingShiftCheck] whose shift landed in a different block. We remove the check and
524/// compute the shift early, promoted to panic-on-overflow, into a fresh local placed where the
525/// check used to be; the fresh local then carries the result to the original destination.
526///
527/// This is correct because rustc has already evaluated both shift operands by the time control
528/// reaches the check: the only effects of the shift are that evaluation and the panic-on-overflow,
529/// and the overflow check already performs both at that point. Storing the result in a fresh local
530/// lets us keep the write to the (not-yet-computed) destination place at the original shift site.
531fn resolve_pending_shift_check(body: &mut ExprBody, check: PendingShiftCheck) {
532 let PendingShiftCheck {
533 block,
534 amount,
535 cond_local,
536 cast_local,
537 } = check;
538
539 // Locate the assert we will repurpose to hold the early, promoted shift. Pairing it with the
540 // shift below (via `zip`) means we bail before mutating anything if either is unexpectedly
541 // absent, so the shift is never rewired to a fresh local we never assigned.
542 let assert_pos = body.body[block].statements.iter().position(|stmt| {
543 matches!(
544 &stmt.kind,
545 StatementKind::Assert {
546 assert: Assert {
547 cond: Operand::Move(cond),
548 ..
549 },
550 ..
551 } if cond.as_local() == Some(cond_local)
552 )
553 });
554
555 // rustc only emits these asserts for its own shifts, so we don't follow the control flow: we
556 // look for the (still-wrapping) shift by its amount anywhere in the body.
557 let Some(((shift_loc, dest, lhs, rhs, panic_binop, ty), assert_pos)) = body
558 .body
559 .iter_enumerated()
560 .find_map(|(sb, block_data)| {
561 block_data
562 .statements
563 .iter()
564 .enumerate()
565 .find_map(|(i, stmt)| {
566 if let StatementKind::Assign(dest, Rvalue::BinaryOp(binop, lhs, rhs)) =
567 &stmt.kind
568 && matches!(
569 binop,
570 BinOp::Shl(OverflowMode::Wrap) | BinOp::Shr(OverflowMode::Wrap)
571 )
572 && equiv_op(rhs, &amount)
573 {
574 Some((
575 StmtLoc::new(sb, i),
576 dest.clone(),
577 lhs.clone(),
578 rhs.clone(),
579 binop.with_overflow(OverflowMode::Panic),
580 dest.ty.clone(),
581 ))
582 } else {
583 None
584 }
585 })
586 })
587 .zip(assert_pos)
588 else {
589 return;
590 };
591
592 // Every mutation below now happens together, so the fresh local is always assigned and used.
593 let fresh = body.locals.new_var(None, ty);
594
595 // Drop the check's comparison and optional cast; they are dead once the assert is gone.
596 body.body[block].statements.iter_mut().for_each(|stmt| {
597 let is_check_temp = if let StatementKind::Assign(place, _) = &stmt.kind {
598 let local = place.as_local();
599 local == Some(cond_local) || cast_local.is_some_and(|c| local == Some(c))
600 } else {
601 false
602 };
603 if is_check_temp {
604 stmt.kind = StatementKind::Nop;
605 }
606 });
607
608 // Compute the shift early, promoted to panic-on-overflow, where the check used to be.
609 body[StmtLoc::new(block, assert_pos)].kind =
610 StatementKind::Assign(fresh.clone(), Rvalue::BinaryOp(panic_binop, lhs, rhs));
611
612 // Forward the result at the original shift site.
613 body[shift_loc].kind =
614 StatementKind::Assign(dest, Rvalue::Use(Operand::Move(fresh), WithRetag::No));
615}
616
617/// Overflow checks for addition/subtraction/multiplication. They look like:
618/// ```text
619/// r := x checked.+ y;
620/// assert(move r.1 == false);
621/// ...
622/// z := move r.0;
623/// ```
624/// We replace that with:
625/// ```text
626/// z := x panic.+ y;
627/// ```
628///
629/// Also sometimes, because of constant promotion, we end up with a lone checked operation without
630/// assert. In that case we replace it with its wrapping equivalent.
631fn reconstruct_overflow_calls(ctx: &TransformCtx, body: &mut ExprBody) {
632 let mut predecessors = body.body.map_ref(|_| 0usize);
633 // Count the uses of `r` and `r.0`.
634 let mut tuple_mentions = body.locals.locals.map_ref(|_| 0usize);
635 let mut integer_uses = body.locals.locals.map_ref(|_| 0usize);
636 body.dyn_visit_in_body(|place: &Place| {
637 if let Some(local) = place.as_local() {
638 tuple_mentions[local] += 1;
639 }
640 });
641 for block in &body.body {
642 for target in block.terminator.targets() {
643 predecessors[target] += 1;
644 }
645 block.dyn_visit_in_body(|operand: &Operand| {
646 if let Operand::Move(place) | Operand::Copy(place, _) = operand
647 && let Some((base, ProjectionElem::Field(None, field))) = place.as_projection()
648 && let Some(local) = base.as_local()
649 && field.index() == 0
650 {
651 integer_uses[local] += 1;
652 }
653 });
654 }
655
656 let mut rewritten_locals = HashSet::default();
657 for id in body.body.indices() {
658 let block = &body.body[id];
659 let TerminatorKind::Call {
660 call, dest, target, ..
661 } = &block.terminator.kind
662 else {
663 continue;
664 };
665
666 let Some(local) = dest.as_local() else {
667 continue;
668 };
669 let target = *target;
670 // Return and argument locals must keep the types from the function signature.
671 if body.locals.is_return_or_arg(local) {
672 continue;
673 }
674
675 let FnOperand::Regular(fn_ptr) = &call.func else {
676 continue;
677 };
678 let FnPtrKind::Fun(fun_id) = fn_ptr.kind.as_ref() else {
679 continue;
680 };
681 let Some(decl) = ctx.translated.fun_decls.get(*fun_id) else {
682 continue;
683 };
684 let Body::Intrinsic { name, .. } = &decl.body else {
685 continue;
686 };
687 let binop = match name.as_str() {
688 "add_with_overflow" => BinOp::Add(OverflowMode::Wrap),
689 "sub_with_overflow" => BinOp::Sub(OverflowMode::Wrap),
690 "mul_with_overflow" => BinOp::Mul(OverflowMode::Wrap),
691 _ => continue,
692 };
693 let [left, right] = call.args.as_slice() else {
694 continue;
695 };
696
697 let continuation = &body.body[target];
698 let followed_by_assert = predecessors[target] == 1
699 && matches!(
700 continuation.statements.first(),
701 Some(Statement {
702 kind: StatementKind::Assert {
703 assert: Assert {
704 cond: Operand::Move(cond),
705 expected: false,
706 check_kind: Some(BuiltinAssertKind::Overflow(..)),
707 ..
708 },
709 ..
710 },
711 ..
712 }) if matches!(cond.as_projection(), Some((base, ProjectionElem::Field(None, field)))
713 if base == dest && field.index() == 1)
714 );
715 // Every use of the integer field also counts as a mention of the tuple local. The other
716 // mentions allowed are: its assignment, and the assert. If these are the only uses, we can
717 // replace the operation.
718 if tuple_mentions[local] != integer_uses[local] + 1 + usize::from(followed_by_assert) {
719 continue;
720 }
721 let binop = if followed_by_assert {
722 binop.with_overflow(OverflowMode::Panic)
723 } else {
724 binop
725 };
726 let rvalue = Rvalue::BinaryOp(binop, left.clone(), right.clone());
727
728 // All uses of the tuple are accounted for, so we can give its local the scalar type.
729 body.locals.locals[local].ty = left.ty().clone();
730 let scalar = body.locals.place_for_var(local);
731 rewritten_locals.insert(local);
732
733 let block = &mut body.body[id];
734 let span = block.terminator.span;
735 block.statements.push(Statement {
736 span,
737 kind: StatementKind::Assign(scalar, rvalue),
738 comments_before: std::mem::take(&mut block.terminator.comments_before),
739 });
740 block.terminator.kind = TerminatorKind::Goto { target };
741 if followed_by_assert {
742 let continuation = &mut body.body[target];
743 continuation.statements[0].kind = StatementKind::Nop;
744 }
745 }
746 body.body.dyn_visit_in_body_mut(|place: &mut Place| {
747 if let Some((base, ProjectionElem::Field(None, field))) = place.as_projection()
748 && let Some(local) = base.as_local()
749 && rewritten_locals.contains(&local)
750 && field.index() == 0
751 {
752 // Recompute the place because it has changed type.
753 *place = body.locals.place_for_var(local);
754 }
755 });
756}
757
758pub struct Transform;
759impl UllbcPass for Transform {
760 fn should_run(&self, options: &crate::options::TranslateOptions) -> bool {
761 options.reconstruct_fallible_operations
762 }
763
764 fn transform_body(&self, ctx: &mut TransformCtx, b: &mut ExprBody) {
765 reconstruct_overflow_calls(ctx, b);
766 let local_uses: LocalUses = compute_uses(b);
767 let mut pending_shift_checks = Vec::new();
768 b.transform_sequences_fwd(|id, _locals, seq| {
769 remove_dynamic_checks(&local_uses, id, seq, &mut pending_shift_checks);
770 Vec::new()
771 });
772 // Resolve the checks whose shift lives in a later block (see [PendingShiftCheck]).
773 pending_shift_checks
774 .into_iter()
775 .for_each(|check| resolve_pending_shift_check(b, check));
776 }
777}