fix(repsel): contain guarded for-of element facts - #7899
Conversation
📝 WalkthroughWalkthroughThe change updates array element-shape analysis for compiler-generated guarded ChangesGuarded for-of element facts
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant ArrayWalk
participant ArrayIterationPatched
participant LazyGetIterator
participant IndexedFastArm
participant ElementShapeFacts
ArrayWalk->>ArrayIterationPatched: recognize guarded for-of structure
ArrayIterationPatched->>LazyGetIterator: inspect iterator escape branch
ArrayIterationPatched->>IndexedFastArm: inspect indexed branch and aliases
ArrayWalk->>ElementShapeFacts: retain terminal indexed facts
LazyGetIterator->>ElementShapeFacts: block facts when later uses or group reuse exist
Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
crates/perry-codegen/src/collectors/ptr_shape_elements_tests.rs (1)
960-986: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAdd a case that covers the group-member alias fixpoint.
The fixpoint at lines 609-619 of
crates/perry-codegen/src/collectors/ptr_shape_elements.rsextendsgroup_membersacrossalias_edges. No test here exercises it: every group member is referenced by its own id. Delete that loop and this file still passes. Add a case that bindsconst row_alias = row;before the guard and then referencesrow_aliasafter the join, and assert the facts are empty.🧪 Proposed additional test
/// `ptr_shape` promotes an immutable alias with its root, so an alias of a /// group member reused after the join observes the same reshaped object. /// /// Sabotage: delete the `alias_edges` fixpoint that extends `group_members` /// and the producer keeps its fact across the iterator escape. #[test] fn guarded_for_of_boundary_follows_group_member_aliases() { let c = class_c(); let cs = [c]; let classes = classes_of(&cs); let stmts = vec![ let_arr(1, "rows"), let_c(2, "row"), push(1, Expr::LocalGet(2)), Stmt::Let { id: 3, name: "row_alias".to_string(), ty: Type::Named("C".to_string()), mutable: false, init: Some(Expr::LocalGet(2)), }, guarded_for_of( 1, 20, 30, 31, Vec::new(), vec![let_elem(32, "guarded", 30, 31), read_x(32)], ), read_x(3), ]; assert!(elements(&stmts, &classes).is_empty()); assert!(promote(&stmts, &classes).is_empty()); }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@crates/perry-codegen/src/collectors/ptr_shape_elements_tests.rs` around lines 960 - 986, Add a regression test in ptr_shape_elements_tests.rs covering the alias_edges group_members fixpoint: bind an immutable row_alias to the group member row before guarded_for_of, reference row_alias after the join, and assert both elements(...) and promote(...) return empty. Follow the existing guarded_for_of test structure and use the visible symbols let_arr, let_c, guarded_for_of, read_x, and Stmt::Let.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@crates/perry-codegen/src/collectors/ptr_shape_elements_tests.rs`:
- Around line 960-986: Add a regression test in ptr_shape_elements_tests.rs
covering the alias_edges group_members fixpoint: bind an immutable row_alias to
the group member row before guarded_for_of, reference row_alias after the join,
and assert both elements(...) and promote(...) return empty. Follow the existing
guarded_for_of test structure and use the visible symbols let_arr, let_c,
guarded_for_of, read_x, and Stmt::Let.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 0d633c9a-9460-4e9c-b65e-56173aa4b817
📒 Files selected for processing (3)
changelog.d/7899-guarded-for-of-element-facts.mdcrates/perry-codegen/src/collectors/ptr_shape_elements.rscrates/perry-codegen/src/collectors/ptr_shape_elements_tests.rs
Summary
Restore the
Ptr<Shape>element facts lost when #7761 wrapped proven-arrayfor…ofloops in an iterator-patch guard, without allowing a patched iterator to export stale shape facts across the branch join.Changes
ArrayIterationPatchedguard and retain facts in its mutually exclusive index arm.Related issue
Fixes #7777
Test plan
cargo test -p perry-codegen --lib ptr_shape_elements::tests— 35 passedcargo test -p perry-codegen— full crate and doc tests passedCensus OK; fixture recovered from 0/0 to exactly 3 selected / 3 consumedtest_gap_repsel_ptr_shape_elements.ts— byte-exact with Node, both exit 0cargo fmt --all -- --checkgit diff --checkbash scripts/check_file_size.shcargo test -p perry-codegen)Screenshots / output
Before:
After:
Checklist
Summary by CodeRabbit
Bug Fixes
for…ofloops so array element information is preserved when the indexed path is the final use.Tests