-
-
Notifications
You must be signed in to change notification settings - Fork 161
fix(transform): give an exported dynamic-heritage class factory real per-evaluation identity #10622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
proggeramlug
wants to merge
2
commits into
main
Choose a base branch
from
fix/10455-class-expr-identity
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| ### Fixed | ||
|
|
||
| - **A class expression returned from a factory function had no | ||
| per-evaluation identity when the function lived in a non-entry module** | ||
| (#10455). `function withCommands(Base) { return class extends Base {}; }` | ||
| called twice with different `Base`s returned the SAME class object, | ||
| re-parented to the most recently passed `Base` — declared and called in | ||
| the entry module, the identical shape already worked because | ||
| `specialize_captured_class_factories` (`crates/perry-transform/src/inline/factory_specialize.rs`) | ||
| clones a distinct class per call site, but that pass only ever sees call | ||
| sites in the SAME module the factory Call expression appears in; a caller | ||
| in another module reaches the factory through an ordinary cross-module | ||
| call the pass never visits. redis's `commander.js` `attachConfig` (`Class | ||
| = class extends BaseClass {}`) hits this every time it's called with a | ||
| second `BaseClass`, from `RedisClient.factory` and | ||
| `Client.prototype.Multi`, and `new Client(options)` ran the wrong parent | ||
| constructor. New pass `fresh_export_dynamic_heritage_factories`: for an | ||
| **exported** factory whose body is exactly `return class extends <expr> | ||
| {};` (no other members), upgrades the class's shared-template `ClassRef` | ||
| to a fresh-per-evaluation `ClassExprFresh` object directly — exactly what | ||
| the same class expression would already lower to had it needed | ||
| per-evaluation statics/captures/a private brand — so every caller, local | ||
| or cross-module, gets a genuinely distinct class per call. Scoped to | ||
| exported functions and this single-statement shape only; a `Let`-bound | ||
| intermediate variable or Effect's object-literal wrapper factory shape | ||
| still rely on same-module specialization alone. | ||
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
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
11 changes: 11 additions & 0 deletions
11
test-files/gap_10455_class_expr_factory_identity_helper.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // Non-entry module half of test_gap_10455_class_expr_factory_identity.ts — | ||
| // the exact shape redis's `commander.js` `attachConfig` uses | ||
| // (`Class = class extends BaseClass {}`), reached through an ordinary | ||
| // cross-module call. | ||
| export function withCommands(Base: any) { | ||
| return class extends Base {}; | ||
| } | ||
|
|
||
| export function withCommandsB(Base: any) { | ||
| return class extends Base {}; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // #10455: a class expression returned from a function had no per-evaluation | ||
| // identity when the function lived in a NON-ENTRY module — every call | ||
| // returned the SAME class object, re-parented to the most recently passed | ||
| // `Base`. A mixin/factory declared and called in the entry module (or | ||
| // specialized per call site by `specialize_captured_class_factories`) always | ||
| // worked; the same shape reached through an ordinary cross-module call did | ||
| // not, because the per-module specialization pass never sees callers outside | ||
| // its own module. redis's `commander.js` `attachConfig` (`Class = class | ||
| // extends BaseClass {}`) hits exactly this from `RedisClient.factory` and | ||
| // `Client.prototype.Multi = MultiCommand.extend(config)`. | ||
| import { withCommands, withCommandsB } from "./gap_10455_class_expr_factory_identity_helper.ts"; | ||
|
|
||
| class A { | ||
| constructor() { | ||
| console.log(" A constructor"); | ||
| } | ||
| hello() { | ||
| return "A.hello"; | ||
| } | ||
| } | ||
| class B { | ||
| constructor() { | ||
| console.log(" B constructor"); | ||
| } | ||
| world() { | ||
| return "B.world"; | ||
| } | ||
| } | ||
|
|
||
| function localWithCommands(Base: any) { | ||
| return class extends Base {}; | ||
| } | ||
|
|
||
| // ── entry-module factory: two calls, two evaluations ── | ||
| const LocalA = localWithCommands(A); | ||
| const LocalB = localWithCommands(B); | ||
| const la = new LocalA(); | ||
| console.log( | ||
| "entry module: LocalA !== LocalB:", | ||
| LocalA !== LocalB, | ||
| "| instanceof A:", | ||
| la instanceof A, | ||
| "| typeof hello:", | ||
| typeof (la as any).hello, | ||
| ); | ||
|
|
||
| // ── non-entry-module factory: two calls, two evaluations ── | ||
| const ClientA = withCommands(A); | ||
| const ClientB = withCommands(B); | ||
| const ca = new ClientA(); | ||
| const cb = new ClientB(); | ||
| console.log( | ||
| "non-entry module: ClientA !== ClientB:", | ||
| ClientA !== ClientB, | ||
| "| instanceof A:", | ||
| ca instanceof A, | ||
| "| typeof hello:", | ||
| typeof (ca as any).hello, | ||
| ); | ||
| console.log( | ||
| "non-entry module: cb instanceof B:", | ||
| cb instanceof B, | ||
| "| cb instanceof A:", | ||
| cb instanceof A, | ||
| "| typeof world:", | ||
| typeof (cb as any).world, | ||
| ); | ||
| // ── three sequential calls at the SAME call site (loop) also stay distinct ── | ||
| const made: any[] = []; | ||
| for (let i = 0; i < 3; i++) { | ||
| made.push(withCommandsB(i % 2 === 0 ? A : B)); | ||
| } | ||
| console.log( | ||
| "loop calls pairwise distinct:", | ||
| made[0] !== made[1] && made[1] !== made[2] && made[0] !== made[2], | ||
| ); | ||
| console.log( | ||
| "loop instances match their own parent:", | ||
| new made[0]() instanceof A, | ||
| new made[1]() instanceof B, | ||
| new made[2]() instanceof A, | ||
| ); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 33372
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 17287
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 50369
Document the lowered-shape restriction.
fresh_export_dynamic_heritage_factoriesmatches an exported function whose return is a two-part sequence:RegisterClassParentDynamicfollowed by the matchingClassRef. Non-computed, non-private instance members that add no captured values still produce this sequence. Replace{};and “no other members” with a description of the required lowered sequence.🤖 Prompt for AI Agents