Unverified Commit 52ac89f6 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[turbopack] Consolodate logic for visiting functions to simplify state management (#82763)

Introduce a new helper function `enter_fn` in the analyzer to ensure state like `cur_fn_id` is consistently updated.  Follow the existing pattern of moving the state into a private module to control access.

Leverage the early return stack information to detect when functions implicitly return `undefined`

This fixes an small set of set of issues
* cur_fn_id was not consistently updated
* some functions were identified as returning undefined when they didn't

Mostly this sets us up for a future where we can track other metadata based on the accurate `cur_fn_id` field
parent d608590c
......@@ -1008,15 +1008,21 @@ impl JsValue {
func_ident: u32,
is_async: bool,
is_generator: bool,
mut return_value: Box<JsValue>,
return_value: JsValue,
) -> Self {
// Check generator first to handle async generators
if is_generator {
return_value = Box::new(JsValue::WellKnownObject(WellKnownObjectKind::Generator));
} else if is_async && !matches!(return_value, box JsValue::Promise(_, _)) {
return_value = Box::new(JsValue::promise(return_value));
}
Self::Function(1 + return_value.total_nodes(), func_ident, return_value)
let return_value = if is_generator {
JsValue::WellKnownObject(WellKnownObjectKind::Generator)
} else if is_async {
JsValue::promise(return_value)
} else {
return_value
};
Self::Function(
1 + return_value.total_nodes(),
func_ident,
Box::new(return_value),
)
}
pub fn object(list: Vec<ObjectPart>) -> Self {
......@@ -1072,12 +1078,12 @@ impl JsValue {
Self::Member(1 + o.total_nodes() + p.total_nodes(), o, p)
}
pub fn promise(operand: Box<JsValue>) -> Self {
pub fn promise(operand: JsValue) -> Self {
// In ecmascript Promise<Promise<T>> is equivalent to Promise<T>
if let JsValue::Promise(_, _) = &*operand {
return *operand;
if let JsValue::Promise(_, _) = operand {
return operand;
}
Self::Promise(1 + operand.total_nodes(), operand)
Self::Promise(1 + operand.total_nodes(), Box::new(operand))
}
pub fn awaited(operand: Box<JsValue>) -> Self {
......@@ -3665,10 +3671,10 @@ pub mod test_utils {
ref args,
) => match &args[0] {
JsValue::Constant(ConstantValue::Str(v)) => {
JsValue::promise(Box::new(JsValue::Module(ModuleValue {
JsValue::promise(JsValue::Module(ModuleValue {
module: v.as_atom().into_owned(),
annotations: ImportAnnotations::default(),
})))
}))
}
_ => v.into_unknown(true, "import() non constant"),
},
......
......@@ -345,10 +345,10 @@ fn path_dirname(mut args: Vec<JsValue>) -> JsValue {
pub fn import(args: Vec<JsValue>) -> JsValue {
match &args[..] {
[JsValue::Constant(ConstantValue::Str(v))] => {
JsValue::promise(Box::new(JsValue::Module(ModuleValue {
JsValue::promise(JsValue::Module(ModuleValue {
module: v.as_atom().into_owned(),
annotations: ImportAnnotations::default(),
})))
}))
}
_ => JsValue::unknown(
JsValue::call(
......
......@@ -8,6 +8,6 @@ c = FreeVar(import)(
((FreeVar(process)["env"]["NEXT_RUNTIME"] === "edge") ? "next/dist/compiled/@vercel/og/index.edge.js" : "next/dist/compiled/@vercel/og/index.node.js")
)
earlyExit = (...) => (undefined | hoisted)
earlyExit = (...) => (hoisted | undefined)
hoisted = (...) => undefined
......@@ -138,15 +138,15 @@
Alternatives {
total_nodes: 3,
values: [
Constant(
Undefined,
),
Variable(
(
"hoisted",
#7,
),
),
Constant(
Undefined,
),
],
logical_property: None,
},
......
......@@ -15,6 +15,6 @@ c = ???*0*
⚠️ nested operation
- *3* process.env: The Node.js process.env property: https://nodejs.org/api/process.html#processenv
earlyExit = (...) => (undefined | hoisted)
earlyExit = (...) => (hoisted | undefined)
hoisted = (...) => undefined
......@@ -12,7 +12,7 @@ inner = (...) => (a + b)
outer = (...) => inner("b")
r = (...) => (undefined | a | (r((a + 1)) + 1))
r = (...) => (a | (r((a + 1)) + 1))
v1 = x(1)(2)
......
......@@ -86,14 +86,11 @@
(
"r",
Function(
11,
10,
87,
Alternatives {
total_nodes: 10,
total_nodes: 9,
values: [
Constant(
Undefined,
),
Variable(
(
"a",
......
......@@ -20,7 +20,7 @@
- *0* unreachable
⚠️ This value might have side effects
6 -> 8 call = (...) => (undefined | a | (r((a + 1)) + 1))((???*0* + 1))
6 -> 8 call = (...) => (a | (r((a + 1)) + 1))((???*0* + 1))
- *0* arguments[0]
⚠️ function calls are not analysed yet
......@@ -32,7 +32,7 @@
- *0* unreachable
⚠️ This value might have side effects
0 -> 11 call = (...) => (undefined | a | (r((a + 1)) + 1))(2)
0 -> 11 call = (...) => (a | (r((a + 1)) + 1))(2)
0 -> 12 call = (...) => (a + b)("b")
......
......@@ -22,14 +22,14 @@ inner = (...) => (a + b)
outer = (...) => inner("b")
r = (...) => (undefined | a | (r((a + 1)) + 1))
r = (...) => (a | (r((a + 1)) + 1))
v1 = (???*0* + 2)
- *0* arguments[0]
⚠️ function calls are not analysed yet
v2 = (undefined | 2 | (???*0* + 1))
- *0* (...) => (undefined | a | (r((a + 1)) + 1))(3)
v2 = (2 | (???*0* + 1))
- *0* (...) => (a | (r((a + 1)) + 1))(3)
⚠️ recursive function call
⚠️ This value might have side effects
......
*arrow function 173* = (...) => (undefined | ???*0*)
*arrow function 173* = (...) => (???*0* | undefined)
- *0* spread in function calls is not supported
⚠️ This value might have side effects
......
......@@ -7,14 +7,14 @@
Alternatives {
total_nodes: 3,
values: [
Constant(
Undefined,
),
Unknown {
original_value: None,
reason: "spread in function calls is not supported",
has_side_effects: true,
},
Constant(
Undefined,
),
],
logical_property: None,
},
......
*arrow function 173* = (...) => (undefined | ???*0*)
*arrow function 173* = (...) => (???*0* | undefined)
- *0* spread in function calls is not supported
⚠️ This value might have side effects
......
......@@ -253,10 +253,10 @@ ctor = (...) => undefined
describeExpectation = (...) => DESCRIBE_EXPECTATION_FNS[expectation["type"]](expectation)
describeExpected = (...) => (
| undefined
| descriptions[0]
| `${descriptions[0]} or ${descriptions[1]}`
| `${descriptions["slice"](0, ???*0*)["join"](", ")}, or ${descriptions[???*1*]}`
| undefined
)
- *0* unsupported expression
⚠️ This value might have side effects
......@@ -928,7 +928,7 @@ peg$computeLocation = (...) => {
"end": {"offset": endPos, "line": endPosDetails["line"], "column": endPosDetails["column"]}
}
peg$computePosDetails = (...) => (undefined | details)
peg$computePosDetails = (...) => details
peg$currPos = (
| 0
......@@ -1083,7 +1083,7 @@ peg$maxFailPos = (0 | peg$currPos)
peg$otherExpectation = (...) => {"type": "other", "description": description}
peg$parse = (...) => (undefined | peg$result)
peg$parse = (...) => (peg$result | undefined)
peg$parse_ = (...) => s0
......
......@@ -39,10 +39,10 @@
⚠️ This value might have side effects
0 -> 23 call = (...) => (
| undefined
| descriptions[0]
| `${descriptions[0]} or ${descriptions[1]}`
| `${descriptions["slice"](0, ???*0*)["join"](", ")}, or ${descriptions[???*1*]}`
| undefined
)(???*2*)
- *0* unsupported expression
⚠️ This value might have side effects
......@@ -1265,11 +1265,11 @@
- *0* unreachable
⚠️ This value might have side effects
0 -> 372 call = (...) => (undefined | details)(???*0*)
0 -> 372 call = (...) => details(???*0*)
- *0* arguments[0]
⚠️ function calls are not analysed yet
0 -> 373 call = (...) => (undefined | details)(???*0*)
0 -> 373 call = (...) => details(???*0*)
- *0* arguments[1]
⚠️ function calls are not analysed yet
......
......@@ -297,10 +297,10 @@ ctor = (...) => undefined
describeExpectation = (...) => DESCRIBE_EXPECTATION_FNS[expectation["type"]](expectation)
describeExpected = (...) => (
| undefined
| descriptions[0]
| `${descriptions[0]} or ${descriptions[1]}`
| `${descriptions["slice"](0, ???*0*)["join"](", ")}, or ${descriptions[???*1*]}`
| undefined
)
- *0* unsupported expression
⚠️ This value might have side effects
......@@ -356,7 +356,7 @@ endPos = ???*0*
- *0* arguments[1]
⚠️ function calls are not analysed yet
endPosDetails = (undefined | {"line": 1, "column": 1} | ???*0* | {"line": ???*2*, "column": ???*4*})
endPosDetails = ({"line": 1, "column": 1} | ???*0* | {"line": ???*2*, "column": ???*4*})
- *0* [][???*1*]
⚠️ unknown array prototype methods or values
- *1* arguments[1]
......@@ -1207,7 +1207,7 @@ peg$computeLocation = (...) => {
"end": {"offset": endPos, "line": endPosDetails["line"], "column": endPosDetails["column"]}
}
peg$computePosDetails = (...) => (undefined | details)
peg$computePosDetails = (...) => details
peg$currPos = ???*0*
- *0* max number of linking steps reached
......@@ -1227,7 +1227,7 @@ peg$maxFailPos = ???*0*
peg$otherExpectation = (...) => {"type": "other", "description": description}
peg$parse = (...) => (undefined | peg$result)
peg$parse = (...) => (peg$result | undefined)
peg$parse_ = (...) => s0
......@@ -3596,7 +3596,7 @@ startPos = ???*0*
- *0* arguments[0]
⚠️ function calls are not analysed yet
startPosDetails = (undefined | {"line": 1, "column": 1} | ???*0* | {"line": ???*2*, "column": ???*4*})
startPosDetails = ({"line": 1, "column": 1} | ???*0* | {"line": ???*2*, "column": ???*4*})
- *0* [][???*1*]
⚠️ unknown array prototype methods or values
- *1* arguments[0]
......
......@@ -52,7 +52,6 @@ $k = (...) => ((bj(a) ? 1 : 0) | 11 | 14 | 2)
*anonymous function 114743* = (...) => null
*anonymous function 117815* = (...) => (
| undefined
| zj(a, b, c)
| b
| ???*0*
......@@ -60,6 +59,7 @@ $k = (...) => ((bj(a) ? 1 : 0) | 11 | 14 | 2)
| cj(a, b, b["type"], b["pendingProps"], c)
| yj(a, b, c)
| ej(a, b, c)
| undefined
)
- *0* dj(a, b, d, e, c)
⚠️ sequence with side effects
......@@ -93,7 +93,7 @@ $k = (...) => ((bj(a) ? 1 : 0) | 11 | 14 | 2)
*anonymous function 128133* = (...) => C
*anonymous function 128157* = (...) => (undefined | b())
*anonymous function 128157* = (...) => (b() | undefined)
*anonymous function 128216* = (...) => undefined
......@@ -427,11 +427,11 @@ Ce = (...) => undefined
Cf = (null | dd)
Cg = (...) => (
| undefined
| ((null !== b) ? ???*0* : !(1))
| ((null !== b) ? !(0) : !(1))
| ((null !== b) ? ???*1* : !(1))
| !(1)
| undefined
)
- *0* !(0)
⚠️ sequence with side effects
......@@ -466,7 +466,7 @@ Dc = (...) => (???*0* ? (???*1* ? ((0 !== ???*2*) ? 16 : 536870912) : 4) : 1)
Dd = rd(Cd)
De = (...) => (undefined | te(qe))
De = (...) => (te(qe) | undefined)
Df = (null | {"focusedElem": a, "selectionRange": c})
......@@ -498,7 +498,7 @@ Ec = (???*0* | *anonymous function 127654*)
Ed = A({}, ud, {"relatedTarget": 0})
Ee = (...) => (undefined | te(b))
Ee = (...) => (te(b) | undefined)
Ef = (...) => (
|| ("textarea" === a)
......@@ -546,7 +546,7 @@ Fc = (???*0* | *anonymous function 127923*)
Fd = rd(Ed)
Fe = (...) => (undefined | te(b))
Fe = (...) => (te(b) | undefined)
Ff = (("function" === typeof(FreeVar(setTimeout))) ? FreeVar(setTimeout) : undefined)
......@@ -556,7 +556,7 @@ Fh = Uf(Dh)
Fi = (...) => di()["memoizedState"]
Fj = (...) => (undefined | ???*0* | null | (???*1* ? b : null) | b | b["child"])
Fj = (...) => (???*0* | null | (???*1* ? b : null) | b | b["child"] | undefined)
- *0* null
⚠️ sequence with side effects
⚠️ This value might have side effects
......@@ -681,7 +681,7 @@ J#883 = (t["sibling"] | undefined)
Ja = FreeVar(Symbol)["iterator"]
Jb = (...) => (undefined | a(b, c) | Gb(a, b, c))
Jb = (...) => (a(b, c) | Gb(a, b, c) | undefined)
Jc = (!(1) | !(0))
......@@ -697,7 +697,7 @@ Jh = (...) => undefined
Ji = (...) => undefined
Jj = (...) => (undefined | ???*0* | (???*3* ? ???*4* : null) | null)
Jj = (...) => (???*0* | (???*3* ? ???*4* : null) | null | undefined)
- *0* (???*1* ? ???*2* : null)
⚠️ sequence with side effects
⚠️ This value might have side effects
......@@ -726,7 +726,7 @@ Kc = []
Kd = A({}, sd, {"data": 0})
Ke = (...) => (undefined | {"node": c, "offset": ???*0*})
Ke = (...) => ({"node": c, "offset": ???*0*} | undefined)
- *0* unsupported expression
⚠️ This value might have side effects
......@@ -921,7 +921,7 @@ Ok = (...) => a
P = (null | a | b | a)
Pa = (...) => (undefined | Ma(a["type"]) | Ma("Lazy") | Ma("Suspense") | Ma("SuspenseList") | ???*0* | "")
Pa = (...) => (Ma(a["type"]) | Ma("Lazy") | Ma("Suspense") | Ma("SuspenseList") | ???*0* | "" | undefined)
- *0* a
⚠️ sequence with side effects
⚠️ This value might have side effects
......@@ -1049,11 +1049,11 @@ Ri = (...) => c
Rj = (...) => undefined
Rk = (...) => (undefined | a(b))
Rk = (...) => (a(b) | undefined)
S = (...) => b
Sa = (...) => (undefined | a | "")
Sa = (...) => (a | "" | undefined)
Sb = {"onError": *anonymous function 17435*}
......@@ -1090,7 +1090,7 @@ Si = (new FreeVar(Set)([???*0*]) | null)
Sj = (...) => undefined
Sk = (...) => (undefined | a())
Sk = (...) => (a() | undefined)
T = (3 | 0 | 1 | 2 | 4 | 6 | 5)
......@@ -1126,12 +1126,12 @@ Tk = (...) => undefined
U = (!(1) | (U || (null !== c["memoizedState"])) | d | (U || m) | l | k | l)
Ua = (...) => (
| undefined
| {
"getValue": *anonymous function 10089*,
"setValue": *anonymous function 10119*,
"stopTracking": *anonymous function 10152*
}
| undefined
)
Ub = (...) => undefined
......@@ -1227,7 +1227,7 @@ Vh = (0 | ???*0*)
Vi = (...) => (a | null)
Vj = (...) => (undefined | null | a["stateNode"])
Vj = (...) => (null | a["stateNode"] | undefined)
Vk = (...) => undefined
......@@ -1274,7 +1274,7 @@ Wk = (???*0* | *anonymous function 117815*)
X = (null | d | c["stateNode"]["containerInfo"] | h["stateNode"] | h["stateNode"]["containerInfo"])
Xa = (...) => (undefined | null | (a["activeElement"] || a["body"]) | a["body"])
Xa = (...) => (null | (a["activeElement"] || a["body"]) | a["body"] | undefined)
Xb = (...) => undefined
......@@ -5203,7 +5203,7 @@ gc = ca["unstable_UserBlockingPriority"]
gd = (...) => undefined
ge = (...) => (undefined | (???*0* !== $d["indexOf"](b["keyCode"])) | (229 !== b["keyCode"]) | !(0) | !(1))
ge = (...) => ((???*0* !== $d["indexOf"](b["keyCode"])) | (229 !== b["keyCode"]) | !(0) | !(1) | undefined)
- *0* unsupported expression
⚠️ This value might have side effects
......@@ -5381,7 +5381,7 @@ hj = (...) => undefined
hk = (...) => undefined
hl = (...) => (undefined | null | a["child"]["stateNode"])
hl = (...) => (null | a["child"]["stateNode"] | undefined)
ia = !((
|| ("undefined" === typeof(FreeVar(window)))
......@@ -5415,9 +5415,9 @@ jb = (...) => undefined
jc = ca["unstable_IdlePriority"]
jd = (...) => (undefined | 1 | 4 | 16 | 536870912)
jd = (...) => (1 | 4 | 16 | 536870912 | undefined)
je = (...) => (undefined | he(b) | null | ee | (((a === ee) && fe) ? null : a))
je = (...) => (he(b) | null | ee | (((a === ee) && fe) ? null : a) | undefined)
jf = (hf["toLowerCase"]() | undefined)
......@@ -5580,10 +5580,10 @@ k#979 = cl(a, 0, !(1), null, null, !(1), !(1), "", ql)
ka = /^[:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD][:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\-.0-9\u00B7\u0300-\u036F\u203F-\u2040]*$/
kb = (...) => (
| undefined
| "http://www.w3.org/2000/svg"
| "http://www.w3.org/1998/Math/MathML"
| "http://www.w3.org/1999/xhtml"
| undefined
)
kc = (null | wl["inject"](vl))
......@@ -5591,12 +5591,12 @@ kc = (null | wl["inject"](vl))
kd = (null | e)
ke = (...) => (
| undefined
| ((("compositionend" === a) || (!(ae) && ge(a, b))) ? ???*0* : null)
| null
| b["char"]
| FreeVar(String)["fromCharCode"](b["which"])
| ((de && ("ko" !== b["locale"])) ? null : b["data"])
| undefined
)
- *0* a
⚠️ sequence with side effects
......@@ -5940,7 +5940,7 @@ og = []
oh = (...) => (("function" === typeof(a["shouldComponentUpdate"])) ? a["shouldComponentUpdate"](d, f, g) : ((b["prototype"] && b["prototype"]["isPureReactComponent"]) ? (!(Ie(c, d)) || !(Ie(e, f))) : !(0)))
oi = (...) => (undefined | !(He(a, c)) | !(0))
oi = (...) => (!(He(a, c)) | !(0) | undefined)
oj = (...) => {"baseLanes": a, "cachePool": null, "transitions": null}
......@@ -5953,7 +5953,7 @@ ol = (...) => !((
p = (...) => `Minified React error #${a}; visit ${b} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`
pa = (...) => (undefined | !(1) | !(0) | !(c["acceptsBooleans"]) | (("data-" !== a) && ("aria-" !== a)))
pa = (...) => (!(1) | !(0) | !(c["acceptsBooleans"]) | (("data-" !== a) && ("aria-" !== a)) | undefined)
pb = {
"animationIterationCount": !(0),
......@@ -6287,13 +6287,13 @@ tb = A(
}
)
tc = (...) => (undefined | 1 | 2 | 4 | 8 | 16 | 32 | ???*0* | 134217728 | 268435456 | 536870912 | 1073741824 | a)
tc = (...) => (1 | 2 | 4 | 8 | 16 | 32 | ???*0* | 134217728 | 268435456 | 536870912 | 1073741824 | a | undefined)
- *0* unsupported expression
⚠️ This value might have side effects
td = rd(sd)
te = (...) => (undefined | a)
te = (...) => (a | undefined)
tf = (...) => {"instance": a, "listener": b, "currentTarget": c}
......@@ -6333,7 +6333,7 @@ uc = (...) => (0 | b | d)
ud = A({}, sd, {"view": 0, "detail": 0})
ue = (...) => (undefined | a["stateNode"])
ue = (...) => (a["stateNode"] | undefined)
uf = `__reactContainer$${Nf}`
......@@ -6358,15 +6358,15 @@ v = (...) => undefined
va = FreeVar(Symbol)["for"]("react.element")
vb = (...) => (undefined | ("string" === typeof(b["is"])) | !(1) | !(0))
vb = (...) => (("string" === typeof(b["is"])) | !(1) | !(0) | undefined)
vc = (...) => (undefined | (b + 250) | (b + 5000) | ???*0*)
vc = (...) => ((b + 250) | (b + 5000) | ???*0* | undefined)
- *0* unsupported expression
⚠️ This value might have side effects
vd = rd(ud)
ve = (...) => (undefined | b)
ve = (...) => (b | undefined)
vf = (...) => (null | (a ? a : null))
......@@ -6572,7 +6572,7 @@ yh = (...) => (Ah(c["children"], e, f, b) | ???*0* | qj(c, e, f, b) | b)
⚠️ sequence with side effects
⚠️ This value might have side effects
yi = (...) => (undefined | ???*0*)
yi = (...) => (???*0* | undefined)
- *0* *anonymous function 69020*
⚠️ sequence with side effects
⚠️ This value might have side effects
......
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