Skip to content

hir: a static field initializer that constructs its own class gets the class's capture args in both copies - #15786

Open
proggeramlug wants to merge 3 commits into
mainfrom
nx-static-field-self-new
Open

proggeramlug wants to merge 3 commits into
mainfrom
nx-static-field-self-new

Conversation

@proggeramlug

@proggeramlug proggeramlug commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

A class whose static field initializer constructs the class itself (static mk = () => new Y(1)) crashed the compiler with "shared emitted closure capture layout changed" when the class is declared inside a function body (for example a CommonJS module wrapper).

The static field initializer is lowered before the class registers its captures, so the new Y(...) inside it gets its forwarded capture arguments from the end-of-body pass. That pass only rewrote the function body, but the initializer also lives in the class's static_fields, which is the arrow's one emitted definition. The two copies ended up with different capture lists.

append_new_args_in_declaring_body (class_captures.rs) now does the append on the body and on the pending class's static field initializers; both end-of-body passes (block bodies and function expressions) call it.

Minimal repro (CJS module required from main):

var F = 7;
class Y{static mk=()=>new Y(1);constructor(e){this.v=e+F}}
exports.r = [Y.mk().v];

Checks (qb10):

  • New test static_field_self_new (arrow and function-expression factory): passes with the fix, fails with it reverted.
  • 20 fixtures with the same pattern produce the same output as node.
  • perry-hir lib 941 ok; perry-wpfacts lib: 0 new reds vs main.
  • Whole-program rows c09b/c05/c08/c09: the only lost read (browser.ts:66:40) is lost identically on plain main 4a2ab6f against the gates' older main reference, so it is base drift, not this change. STOP c08/c09 and semver rc=0.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed a compiler crash when a class declared inside a function or CommonJS module constructs itself in a static field initializer.

…he class too

A static field initializer is lowered before its class registers its
captures, so `static mk = () => new Y(1)` in a class that captures `F`
gets no forwarded capture args. The end-of-body pass appends them to the
`new Y` sites of the lowered body only, on the premise that member code
lives in the pending class. A static field initializer is body code that
stands twice: in the body, where the class evaluation runs it, and in the
class's static_fields, which emitted_closures takes as the arrow's one
definition. The body copy captured [Y, F], the definition [Y], and the
facts walk stopped on "shared emitted closure capture layout changed"
(a CJS module or factory class with a static arrow constructing itself,
Next.js RenderResult once its unread static private is a static block).

Both end-of-body passes (arrow/function-declaration bodies and function
expressions, the CJS wrapper) now append to the pending class's static
field initializers as well, with the same outer ids: static field
initializers keep the outer ids, synthesize_class_captures remaps only
member bodies and instance fields.
@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Class-capture lowering now updates self-construction calls in declaring bodies and pending static field initializers. Regression tests cover classes declared in function-expression and arrow-function bodies.

Changes: Static self-construction capture refresh

Layer / File(s) Summary
Update class construction captures
crates/perry-hir/src/lower_decl/class_captures.rs, crates/perry-hir/src/lower_decl/mod.rs, crates/perry-hir/src/lower_decl/block.rs, crates/perry-hir/src/lower/expr_function.rs
The new helper updates matching constructions in declaring bodies and pending static field initializers. It skips capture arguments that are already present. Lowering call sites use the helper, and the class-capture lookup result is cloned into an owned vector.
Cover static self-construction cases
crates/perry-wpfacts/src/tests.rs, crates/perry-wpfacts/src/tests/static_field_self_new.rs, changelog.d/15786-static-field-self-new.md
Two regression tests check static initializer closure captures and run facts computation for classes declared in function-expression and arrow-function bodies. The changelog describes the compiler crash scenario.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Suggested labels: type:bug, area:compiler

Suggested reviewers: thehypnoo

Merge Risk: Minimal · up to d7a42

This fixes a compiler crash for classes declared inside functions whose static initializers construct the class itself. No merge-blocking risk was found in the changed behavior.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the change: a static field initializer that constructs its own class now gets the class's capture arguments in both copies. The raw summary and objectives confirm this is the main chan…
Description check Passed The description covers the problem, root cause, fix, minimal repro, and test results, so it is mostly complete. It does not follow the repository template. It omits the required 'Related issue' sectio…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the change: a static field initializer that constructs its own class now gets the class's capture arguments in both copies. The raw summary and objectives confirm this is the main change, which adds append_new_args_in_declaring_body to cover static_fields as well as the declaring body. The 'hir:' prefix is a scope marker and does not affect the assessment. The title is long at 106 characters, but it is specific and accurate.

Full details: Description check

Explanation

The description covers the problem, root cause, fix, minimal repro, and test results, so it is mostly complete. It does not follow the repository template. It omits the required 'Related issue' section, the 'Type of change' checkboxes, the 'Platforms' checkboxes with a 'Tested on' line, and the 'Changes' bullets. The Test plan has no fenced command output and does not tick the checklist items, including the changelog.d entry. The description also cites the changelog entry only indirectly. The raw summary shows changelog.d/15786-static-field-self-new.md exists, which covers that item.

Resolution

Consider adding the template sections the description skips: Related issue (for example, Refs #15786 or n/a), Type of change (Bug fix), Platforms with a Tested on line, and a Changes list. Also paste the actual commands and results into the bash Test plan block, and tick the applicable checklist items. The description says 'STOP c08/c09 and semver rc=0' and cites base drift; a reviewer may need the exact commands behind those results.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0d2a234f-515d-4929-8351-1cf7d3cddce6
Commits

Reviewing files that changed from the base of the PR and between d6db794 and d7a42ac.

Files selected for processing: 7

  • changelog.d/15786-static-field-self-new.md
  • crates/perry-hir/src/lower/expr_function.rs
  • crates/perry-hir/src/lower_decl/block.rs
  • crates/perry-hir/src/lower_decl/class_captures.rs
  • crates/perry-hir/src/lower_decl/mod.rs
  • crates/perry-wpfacts/src/tests.rs
  • crates/perry-wpfacts/src/tests/static_field_self_new.rs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@proggeramlug
proggeramlug marked this pull request as ready for review December 1, 2026 01:19

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants