Unverified Commit f6c48e19 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

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 cfc7da30 (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
```
parent 02720587
...@@ -8,7 +8,7 @@ use std::{ ...@@ -8,7 +8,7 @@ use std::{
use anyhow::{Context, Result, bail}; use anyhow::{Context, Result, bail};
use bumpalo::boxed::Box as BumpBox; use bumpalo::boxed::Box as BumpBox;
use num_bigint::BigInt; use num_bigint::BigInt;
use smallvec::SmallVec; use smallvec::{SmallVec, smallvec};
use swc_core::ecma::{ast::Id, atoms::Atom}; use swc_core::ecma::{ast::Id, atoms::Atom};
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbopack_core::compile_time_info::{ use turbopack_core::compile_time_info::{
...@@ -1257,6 +1257,8 @@ impl<'a> JsValue<'a> { ...@@ -1257,6 +1257,8 @@ impl<'a> JsValue<'a> {
// Definable name management // Definable name management
impl JsValue<'_> { impl JsValue<'_> {
// Clippy is wrong. It's not the same lifetime
#[allow(mismatched_lifetime_syntaxes)]
/// When the value has a user-definable name, return it in segments. Otherwise /// When the value has a user-definable name, return it in segments. Otherwise
/// returns None. /// returns None.
/// It also returns a boolean whether the variable was potentially reassigned. /// It also returns a boolean whether the variable was potentially reassigned.
...@@ -1265,11 +1267,12 @@ impl JsValue<'_> { ...@@ -1265,11 +1267,12 @@ impl JsValue<'_> {
/// - some well-known objects/functions have a user-definable names: ["import"] /// - some well-known objects/functions have a user-definable names: ["import"]
/// - member calls without arguments also have a user-definable name: ["foo", Call("func")] /// - member calls without arguments also have a user-definable name: ["foo", Call("func")]
/// - typeof expressions add `typeof` after the argument's segments: ["foo", "typeof"] /// - typeof expressions add `typeof` after the argument's segments: ["foo", "typeof"]
pub fn get_definable_name( pub fn get_definable_name<'v>(
&self, &'v self,
var_graph: Option<&VarGraph<'_>>, var_graph: Option<&VarGraph<'_>>,
) -> Option<(DefinableNameSegmentRefs<'_>, bool)> { ) -> SmallVec<[Option<(DefinableNameSegmentRefs<'_>, bool)>; 1]> {
let mut current = self; let inner = |value: &'v JsValue| {
let mut current = value;
let mut segments = SmallVec::new(); let mut segments = SmallVec::new();
let mut potentially_reassigned = false; let mut potentially_reassigned = false;
loop { loop {
...@@ -1312,7 +1315,10 @@ impl JsValue<'_> { ...@@ -1312,7 +1315,10 @@ impl JsValue<'_> {
break; break;
} }
JsValue::MemberCall(_, call) if call.args().is_empty() => { JsValue::MemberCall(_, call) if call.args().is_empty() => {
segments.push(DefinableNameSegmentRef::Call(call.prop().as_str()?)); let Some(call_prop) = call.prop().as_str() else {
return Default::default();
};
segments.push(DefinableNameSegmentRef::Call(call_prop));
current = call.obj(); current = call.obj();
} }
JsValue::TypeOf(_, arg) => { JsValue::TypeOf(_, arg) => {
...@@ -1324,6 +1330,13 @@ impl JsValue<'_> { ...@@ -1324,6 +1330,13 @@ impl JsValue<'_> {
} }
segments.reverse(); segments.reverse();
Some((DefinableNameSegmentRefs(segments), potentially_reassigned)) Some((DefinableNameSegmentRefs(segments), potentially_reassigned))
};
if let JsValue::Alternatives { values, .. } = self {
values.iter().map(inner).collect()
} else {
smallvec![inner(self)]
}
} }
} }
......
...@@ -260,8 +260,8 @@ pub async fn get_constants( ...@@ -260,8 +260,8 @@ pub async fn get_constants(
value.clone_in(arena.get_or_default()), value.clone_in(arena.get_or_default()),
&|value| early_value_visitor(&arena, value), &|value| early_value_visitor(&arena, value),
&async |v| { &async |v| {
if let Some((name, _)) = v.get_definable_name(Some(&var_graph)) if let [Some((name, _))] = &*v.get_definable_name(Some(&var_graph))
&& let Some(value) = compile_time_info_ref.defines.get(&name).await? && let Some(value) = compile_time_info_ref.defines.get(name).await?
{ {
return Ok(( return Ok((
JsValue::from_compile_time_define_value_in( JsValue::from_compile_time_define_value_in(
......
...@@ -1299,8 +1299,12 @@ async fn analyze_ecmascript_module_internal( ...@@ -1299,8 +1299,12 @@ async fn analyze_ecmascript_module_internal(
.link_value(take(&mut *obj), ImportAttributes::empty_ref()) .link_value(take(&mut *obj), ImportAttributes::empty_ref())
.await?; .await?;
if let Some((name, false)) = if obj
obj.get_definable_name(Some(&analysis_state.var_graph)) .get_definable_name(Some(&analysis_state.var_graph))
.iter()
.flatten()
.any(|(name, reassigned)| {
!reassigned
&& matches!( && matches!(
name.0.as_slice(), name.0.as_slice(),
[ [
...@@ -1308,6 +1312,7 @@ async fn analyze_ecmascript_module_internal( ...@@ -1308,6 +1312,7 @@ async fn analyze_ecmascript_module_internal(
DefinableNameSegmentRef::Name("env") DefinableNameSegmentRef::Name("env")
] ]
) )
})
{ {
analysis.add_runtime_env_var_reference(RcStr::from(prop)); analysis.add_runtime_env_var_reference(RcStr::from(prop));
} }
...@@ -3443,6 +3448,8 @@ async fn handle_membership<'a>( ...@@ -3443,6 +3448,8 @@ async fn handle_membership<'a>(
let obj = link_obj.await?; let obj = link_obj.await?;
let obj_name = obj.get_definable_name(Some(&state.var_graph)); let obj_name = obj.get_definable_name(Some(&state.var_graph));
if let [obj_name] = &*obj_name {
// Exactly one name. We can potentially inline
if has_member && let Some((mut name, false)) = obj_name.clone() { if has_member && let Some((mut name, false)) = obj_name.clone() {
name.0.push(DefinableNameSegmentRef::Name(prop)); name.0.push(DefinableNameSegmentRef::Name(prop));
match ty { match ty {
...@@ -3454,7 +3461,8 @@ async fn handle_membership<'a>( ...@@ -3454,7 +3461,8 @@ async fn handle_membership<'a>(
.await? .await?
{ {
// Inline env var // Inline env var
handle_free_var_reference(ast_path, &value, span, state, analysis).await?; handle_free_var_reference(ast_path, &value, span, state, analysis)
.await?;
return Ok(()); return Ok(());
} }
} }
...@@ -3475,8 +3483,9 @@ async fn handle_membership<'a>( ...@@ -3475,8 +3483,9 @@ async fn handle_membership<'a>(
} }
} }
} }
if is_prop_cache
if is_prop_cache && let JsValue::WellKnownFunction(WellKnownFunctionKind::Require) = &obj { && let JsValue::WellKnownFunction(WellKnownFunctionKind::Require) = &obj
{
analysis.add_code_gen::<CodeGen>(match ty { analysis.add_code_gen::<CodeGen>(match ty {
MembershipType::Member => { MembershipType::Member => {
CjsRequireCacheAccess::new(ast_path.to_vec().into()).into() CjsRequireCacheAccess::new(ast_path.to_vec().into()).into()
...@@ -3489,8 +3498,11 @@ async fn handle_membership<'a>( ...@@ -3489,8 +3498,11 @@ async fn handle_membership<'a>(
}); });
return Ok(()); return Ok(());
} }
}
if let Some((name, false)) = &obj_name // Not inlined, potentially register as runtime env var.
if obj_name.iter().flatten().any(|(name, reassigned)| {
!reassigned
&& matches!( && matches!(
name.0.as_slice(), name.0.as_slice(),
[ [
...@@ -3498,13 +3510,11 @@ async fn handle_membership<'a>( ...@@ -3498,13 +3510,11 @@ async fn handle_membership<'a>(
DefinableNameSegmentRef::Name("env") DefinableNameSegmentRef::Name("env")
] ]
) )
{ }) {
// non-inlined env var
analysis.add_runtime_env_var_reference(RcStr::from(prop)); analysis.add_runtime_env_var_reference(RcStr::from(prop));
return Ok(()); return Ok(());
} }
} }
Ok(()) Ok(())
} }
...@@ -3515,7 +3525,11 @@ async fn handle_typeof<'a>( ...@@ -3515,7 +3525,11 @@ async fn handle_typeof<'a>(
state: &AnalysisState<'a>, state: &AnalysisState<'a>,
analysis: &mut AnalyzeEcmascriptModuleResultBuilder, analysis: &mut AnalyzeEcmascriptModuleResultBuilder,
) -> Result<()> { ) -> Result<()> {
if let Some((mut name, false)) = arg.get_definable_name(Some(&state.var_graph)) { let arg_name = arg.get_definable_name(Some(&state.var_graph));
if arg_name.len() == 1
&& let Some((mut name, false)) = arg_name.into_iter().next().unwrap()
{
// Exactly one name. We can potentially inline
name.0.push(DefinableNameSegmentRef::TypeOf); name.0.push(DefinableNameSegmentRef::TypeOf);
if let Some(value) = state if let Some(value) = state
.compile_time_info_ref .compile_time_info_ref
...@@ -3538,11 +3552,12 @@ async fn handle_free_var<'a>( ...@@ -3538,11 +3552,12 @@ async fn handle_free_var<'a>(
state: &AnalysisState<'a>, state: &AnalysisState<'a>,
analysis: &mut AnalyzeEcmascriptModuleResultBuilder, analysis: &mut AnalyzeEcmascriptModuleResultBuilder,
) -> Result<()> { ) -> Result<()> {
if let Some((name, _)) = var.get_definable_name(None) // Exactly one name. We can potentially inline
if let [Some((name, _))] = &*var.get_definable_name(None)
&& let Some(value) = state && let Some(value) = state
.compile_time_info_ref .compile_time_info_ref
.free_var_references .free_var_references
.get(&name) .get(name)
.await? .await?
{ {
handle_free_var_reference(ast_path, &value, span, state, analysis).await?; handle_free_var_reference(ast_path, &value, span, state, analysis).await?;
...@@ -3960,16 +3975,22 @@ async fn value_visitor_inner<'a>( ...@@ -3960,16 +3975,22 @@ async fn value_visitor_inner<'a>(
) -> Result<(JsValue<'a>, Modified)> { ) -> Result<(JsValue<'a>, Modified)> {
if let JsValue::In(_, left, right) = &v if let JsValue::In(_, left, right) = &v
&& let Some(left) = left.as_str() && let Some(left) = left.as_str()
&& let Some((mut name, _)) = right.get_definable_name(Some(var_graph)) && let right_name = right.get_definable_name(Some(var_graph))
&& right_name.len() == 1
&& let Some((mut right_name, false)) = right_name.into_iter().next().unwrap()
{
right_name.0.push(DefinableNameSegmentRef::Name(left));
if compile_time_info_ref
.defines
.contains_key(&right_name)
.await?
{ {
name.0.push(DefinableNameSegmentRef::Name(left));
if compile_time_info_ref.defines.contains_key(&name).await? {
return Ok((JsValue::Constant(JsConstantValue::True), Modified::Yes)); return Ok((JsValue::Constant(JsConstantValue::True), Modified::Yes));
} }
} }
if let Some((name, _)) = v.get_definable_name(Some(var_graph)) if let [Some((name, false))] = &*v.get_definable_name(Some(var_graph))
&& let Some(value) = compile_time_info_ref.defines.get(&name).await? && let Some(value) = compile_time_info_ref.defines.get(name).await?
{ {
return Ok(( return Ok((
JsValue::from_compile_time_define_value_in(arena.get_or_default(), &value)?, JsValue::from_compile_time_define_value_in(arena.get_or_default(), &value)?,
......
// TODO properly handle Pat::Assign
function readEnv({ FOO1, ...rest } = process.env) { function readEnv({ FOO1, ...rest } = process.env) {
return rest return rest
} }
......
// TODO properly handle Pat::Assign
function readEnv(env = process.env) { function readEnv(env = process.env) {
return env.FOO1 return env.FOO1
} }
......
...@@ -5,4 +5,6 @@ runtime: [ ...@@ -5,4 +5,6 @@ runtime: [
"FOO4", "FOO4",
"FOO5", "FOO5",
"FOO6", "FOO6",
"FOO7",
"FOO8",
] ]
...@@ -5,4 +5,6 @@ runtime: [ ...@@ -5,4 +5,6 @@ runtime: [
"FOO4", "FOO4",
"FOO5", "FOO5",
"FOO6", "FOO6",
"FOO7",
"FOO8",
] ]
...@@ -5,4 +5,6 @@ runtime: [ ...@@ -5,4 +5,6 @@ runtime: [
"FOO4", "FOO4",
"FOO5", "FOO5",
"FOO6", "FOO6",
"FOO7",
"FOO8",
] ]
...@@ -38,7 +38,6 @@ if (foo) { ...@@ -38,7 +38,6 @@ if (foo) {
} else { } else {
e = process.env e = process.env
} }
// TODO currently not tracked
console.log(e.FOO7) console.log(e.FOO7)
// --- // ---
...@@ -50,5 +49,4 @@ if (foo) { ...@@ -50,5 +49,4 @@ if (foo) {
} else { } else {
p2 = process p2 = process
} }
// TODO currently not tracked
console.log(p2.env.FOO8) console.log(p2.env.FOO8)
...@@ -5,4 +5,6 @@ runtime: [ ...@@ -5,4 +5,6 @@ runtime: [
"FOO4", "FOO4",
"FOO5", "FOO5",
"INLINED3", "INLINED3",
"FOO6",
"INLINED4",
] ]
...@@ -5,4 +5,6 @@ runtime: [ ...@@ -5,4 +5,6 @@ runtime: [
"FOO4", "FOO4",
"FOO5", "FOO5",
"INLINED3", "INLINED3",
"FOO6",
"INLINED4",
] ]
...@@ -5,4 +5,6 @@ runtime: [ ...@@ -5,4 +5,6 @@ runtime: [
"FOO4", "FOO4",
"FOO5", "FOO5",
"INLINED3", "INLINED3",
"FOO6",
"INLINED4",
] ]
...@@ -20,15 +20,6 @@ console.log(FOO4, renamed) ...@@ -20,15 +20,6 @@ console.log(FOO4, renamed)
// TODO this is actually not inlined yet // TODO this is actually not inlined yet
console.log(INLINED3) console.log(INLINED3)
// TODO not tracked yet
// The re-entered object pattern now receives:
// Alternatives [
// Argument(...),
// process.env
// ]
// add_object_pat_effects() therefore creates DestructuredMember with that Alternatives object.
// Later, Effect::DestructuredMember calls get_definable_name(). That function does not handle JsValue::Alternatives, so it returns None instead of recognizing the process.env alternative.
// Thus FOO6 and INLINED4 remain untracked in `function readEnv({ FOO6, INLINED4 } = process.env)`.
function readEnv({ FOO6, INLINED4 } = process.env) { function readEnv({ FOO6, INLINED4 } = process.env) {
// TODO this is actually not inlined yet // TODO this is actually not inlined yet
return FOO6 + INLINED4 return FOO6 + INLINED4
......
exports = () => 'hello' exports = () => 'hello'
if (typeof exports === 'object') throw 'oh no' if (typeof exports === 'object') {
throw new Error("exports-reassign: it's an object, so incorrectly inlined")
}
module.exports = 1234 module.exports = 1234
module = () => 'hello' module = () => 'hello'
if (typeof module === 'object') throw 'oh no' if (typeof module === 'object') {
throw new Error("module-reassign: it's an object, so incorrectly inlined")
}
exports.foo = 1234 exports.foo = 1234
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment