next.js
f6c48e19 - Turbopack: allow `get_definable_name` to return a list (#97984)

Commit
2 days ago
Turbopack: allow `get_definable_name` to return a list (#97984) Followup to #95310 Now we can correctly track env vars in cases like these were a `JsValue::Alternative` is involved ```js // An alternative (process|unknown).env let p2 if (foo) { p2 = foo } else { p2 = process } console.log(p2.env.FOO8) ``` The inlining logic behaves the same as before: - If `get_definable_name` returns exactly one result, we can inline. - Otherwise, we can't inline (previously this case returned `None`) A bit of trivia why we need that boolean in the return type in the first place (because conceptually it should be possible to just model it via `JsValue::Alternative` anyway: > but why is that a problem? if the value is reassigned, then there should just an alternative, then get_definable_name returns multiple values and it should still not be inlining > > Because module is a `FreeVar`, not a normal `Variable`. > > The assignment is stored separately in `VarGraph`: > > ``` > free_var_ids["module"] -> id > values[id] -> assigned function > ``` > > But later reads still evaluate to: > > `JsValue::FreeVar("module")` > > The linker expands JsValue::Variable, but not JsValue::FreeVar. Therefore there is no JsValue::Alternatives at this call site. > > Instead, `get_definable_name()` detects the graph entry and returns: > > `[Some((["module", TypeOf], true))]` > > That boolean is how reassignment is represented for free variables. Cardinality remains one, so ignoring the boolean at references/mod.rs:4007 incorrectly permits inlining. > > Actual `Alternatives` primarily represent multiple values assigned to tracked variables. For free globals, the original ambient value plus reassignment is represented by `FreeVar` plus `potentially_reassigned`. No perf impact: ``` commit cfc7da3049f91669a1b61a0f7a5edef503cddc53 (HEAD -> canary, origin/canary, origin/HEAD) 393.17s user 31.34s system 722% cpu 58.775 total 391.91s user 35.37s system 746% cpu 57.256 total 389.48s user 30.40s system 770% cpu 54.513 total commit 38f2708659be31538c951b12ba806ef53710f074 (HEAD -> mischnic/get-definable-name-list) 392.86s user 34.00s system 745% cpu 57.221 total 388.10s user 32.72s system 766% cpu 54.912 total 391.84s user 37.82s system 717% cpu 59.865 total ```
Author
Parents
Loading