-
-
Notifications
You must be signed in to change notification settings - Fork 161
fix(transform): stop cross-module inlining bundling a separately exported sibling by value #10630
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
Closed
+167
−1
Closed
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,37 @@ | ||
| ### Fixed | ||
|
|
||
| - **A function referenced inside its own module is a different object from | ||
| the same function imported elsewhere (#10554).** `function f(){}; export | ||
| function isSame(x){ return x === f; }; export { f };` gave `isSame(f)` | ||
| `false` for an importer's own `f` — identity checks, registries/caches | ||
| keyed by function, `removeEventListener`/`off(fn)`, and memoization all | ||
| silently took the wrong branch. | ||
|
|
||
| Root cause: the cross-module *function* inliner in | ||
| `perry-transform`'s `inline/cross_module.rs` harvests an exported | ||
| function's whole value-dependency graph — every function it transitively | ||
| references, including by value (`x === f`), not just as a call target — | ||
| and clones the entire graph into every importing module under fresh | ||
| `__perry_xmod_inline_<id>_<name>` symbols. When the referenced function | ||
| (`f`) is *also* independently exported, it got cloned alongside the | ||
| candidate instead of resolved through its own canonical wrapper. Every | ||
| function value materializes into a heap closure keyed by its wrapper | ||
| *symbol* (`js_closure_alloc_singleton`), so the clone's `f` and the | ||
| canonical `f` every importer resolves through produced two distinct | ||
| closures — an in-module identity check comparing them disagreed with | ||
| every importer's own view. | ||
|
|
||
| Fix: `gather_cross_module_functions` now refuses a candidate whose | ||
| dependency graph would need to bundle a *separately exported* sibling | ||
| function referenced by value — it falls back to an ordinary cross-module | ||
| call instead, which resolves through the shared canonical wrapper. | ||
| Self-recursion is unaffected. The directly-affected shape actually gets | ||
| **faster**, not slower: the unsound inline was paying for an extra | ||
| closure materialization on every call. | ||
|
|
||
| Validation: new `test_gap_10554_fn_identity_own_module` (function | ||
| declaration, function expression, arrow-in-const, named export, a barrel | ||
| re-export, a default export referencing an exported sibling, `Set` | ||
| membership, both identity directions) fails on the baseline and matches | ||
| Node on the fix; the existing `test_gap_10434`/export/import/cross-module/ | ||
| inline/module gap-test families (16 tests) are unaffected. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // Shared helpers for #10554: a function referenced inside its own module | ||
| // must be the SAME object an importer sees. | ||
| export function fnDecl() { | ||
| return 1; | ||
| } | ||
| export const fnExpr = function fnExprNamed() { | ||
| return 2; | ||
| }; | ||
| export const arrowFn = () => 3; | ||
|
|
||
| // Value comparisons made FROM WITHIN this module -- the defect's exact | ||
| // shape: `isSame*` is itself exported, so it is a candidate for | ||
| // cross-module inlining, and its body's reference to the sibling export is | ||
| // a plain in-module reference, not an import. | ||
| export function isSameDecl(x: unknown) { | ||
| return x === fnDecl; | ||
| } | ||
| export function isSameExpr(x: unknown) { | ||
| return x === fnExpr; | ||
| } | ||
| export function isSameArrow(x: unknown) { | ||
| return x === arrowFn; | ||
| } | ||
| export function bothSame(a: unknown, b: unknown) { | ||
| return a === b; | ||
| } | ||
| export function makeSet() { | ||
| return new Set<unknown>([fnDecl, fnExpr, arrowFn]); | ||
| } | ||
|
|
||
| // A default export that ALSO references an exported sibling by value -- | ||
| // #10548 fixed the export-ROW identity for `export default F`; this checks | ||
| // the (distinct) cross-module-inliner defect #10554 fixes doesn't resurface | ||
| // under a default export. | ||
| export default function useDecl(x: unknown) { | ||
| return x === fnDecl; | ||
| } |
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,13 @@ | ||
| // Barrel re-export -- a THIRD view of the same bindings, once removed from | ||
| // the declaring module. | ||
| export { | ||
| fnDecl, | ||
| fnExpr, | ||
| arrowFn, | ||
| isSameDecl, | ||
| isSameExpr, | ||
| isSameArrow, | ||
| bothSame, | ||
| makeSet, | ||
| } from "./lib.ts"; | ||
| export { default as useDeclDefault } from "./lib.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,50 @@ | ||
| // #10554: a function referenced inside its own module is a different | ||
| // object from the same function imported elsewhere. | ||
| import useDecl, { | ||
| fnDecl, | ||
| fnExpr, | ||
| arrowFn, | ||
| isSameDecl, | ||
| isSameExpr, | ||
| isSameArrow, | ||
| bothSame, | ||
| makeSet, | ||
| } from "./_helpers/fn_identity_10554/lib.ts"; | ||
| import * as ns from "./_helpers/fn_identity_10554/lib.ts"; | ||
| import { | ||
| fnDecl as reFnDecl, | ||
| isSameDecl as reIsSameDecl, | ||
| useDeclDefault, | ||
| } from "./_helpers/fn_identity_10554/reexport.ts"; | ||
|
|
||
| // 1. function declaration, function expression, arrow: in-module identity | ||
| // checked from a call made through the IMPORTED value. | ||
| console.log("decl:", isSameDecl(fnDecl)); | ||
| console.log("expr:", isSameExpr(fnExpr)); | ||
| console.log("arrow:", isSameArrow(arrowFn)); | ||
|
|
||
| // 2. Both directions: importer's namespace view vs named-import view vs | ||
| // in-module (through the exported checker functions). | ||
| console.log("ns decl:", isSameDecl(ns.fnDecl), ns.fnDecl === fnDecl, fnDecl === ns.fnDecl); | ||
| console.log("ns expr:", isSameExpr(ns.fnExpr), ns.fnExpr === fnExpr); | ||
| console.log("ns arrow:", isSameArrow(ns.arrowFn), ns.arrowFn === arrowFn); | ||
|
|
||
| // 3. Re-export (barrel): a third view, once removed. | ||
| console.log("reexport decl:", reIsSameDecl(reFnDecl), reFnDecl === fnDecl, isSameDecl(reFnDecl)); | ||
|
|
||
| // 4. default export whose body ALSO references an exported sibling by | ||
| // value (distinct from #10434/#10548's export-row identity; exercises the | ||
| // cross-module-inliner defect instead). | ||
| console.log("default:", useDecl(fnDecl), useDeclDefault(fnDecl), useDecl === useDeclDefault); | ||
|
|
||
| // 5. bothSame -- direct pass-through, no local materialization inside the | ||
| // callee (a control: unaffected by this defect, should always have passed). | ||
| console.log("bothSame decl:", bothSame(fnDecl, fnDecl), bothSame(fnDecl, ns.fnDecl)); | ||
| console.log("bothSame cross:", bothSame(fnDecl, fnExpr)); | ||
|
|
||
| // 6. Set membership -- identity through collection storage/lookup, built | ||
| // FROM WITHIN the module (the same defect shape via a different value | ||
| // consumer than `===`). | ||
| const set = makeSet(); | ||
| console.log("set has:", set.has(fnDecl), set.has(fnExpr), set.has(arrowFn)); | ||
| console.log("set has via ns:", set.has(ns.fnDecl)); |
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.
🚀 Performance & Scalability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 50369
Do not reject dependencies used only as direct call targets.
collect_func_refs_in_functionrecords theExpr::FuncRefused as anExpr::Callcallee. Therefore,export function outer() { return helper(); }is rejected whenhelperis also exported, even thoughouterdoes not usehelperas a function value. Track direct-call dependencies separately and apply this rejection only to non-callee value references. Add a regression test for an exported helper used only by a direct call.🤖 Prompt for AI Agents