fix(hir): run static class blocks at declaration source position (#2278)#2285
Merged
Conversation
`static { ... }` blocks now emit a synthetic
`Class.__perry_static_init_N()` call at the class declaration's
source position in `lower/stmt.rs::Decl::Class`, mirroring how
static field initializers are already emitted there. Pre-fix the
only emitter was `init_static_fields_late`, which runs AFTER every
module-level stmt — so a `console.log(C.staticField)` on the line
after a class declaration read the field's zero default instead of
the value the block had assigned.
The late pass keeps calling static blocks for any class that
*didn't* get an inline call emitted (inner classes hoisted into
`hir.classes` from a function body still hit this fallback). A new
`collect_classes_with_inline_static_block_calls` helper scans
`hir.init` for the synthetic calls and dedupes them out of the late
pass so we don't double-execute.
Closes #2278; clears the residual gap entry in `test_gap_class_advanced.ts`.
ada35c3 to
f5aac4d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Update (post-rebase): the equivalent HIR + codegen fix for #2278 landed on
mainvia a parallel PR while this branch was in CI. Rebased onto currentmain, kept the parts that are still uniquely additive:test-parity/node-suite/object/class-static-block.ts— a dedicated parity test with five variations (basic boolean, field+block override, multi-assignment in a single block, two blocks on one class, self-reference) byte-identical againstnode --experimental-strip-types. The existing fix only hadtest_gap_class_advanced.tscovering one shape.init_static_fields_lateand the newinit_calls_static_blockhelper so the rationale for the dedup loop links back to Static class block assigningtrueto a static boolean field reads back0(init missed) #2278 directly.The original commit body (HIR emission at class-decl source position + late-pass dedup) is now redundant with
main; the gap test is the residual carrier of the fix's intent for the test suite.Test plan
cargo fmt --all -- --checkclean.cargo test --release -p perry-codegen -p perry-hirgreen.test-parity/node-suite/object/class-static-block.tsbyte-identical against Node.test-files/test_gap_class_advanced.tsstill byte-identical (already removed fromknown_failures.jsonupstream).Refs #2278.