From 53af1453c6280caad0f1a4e8cd2cb71c6bffad36 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Wed, 31 Jul 2024 16:27:36 +0200 Subject: [PATCH] fix case when both paths return --- crates/turbopack-ecmascript/src/analyzer/graph.rs | 14 +++++++------- .../snapshot/comptime/early-return/input/module.js | 10 ++++++++++ ...napshot_comptime_early-return_input_99b3d5._.js | 9 +++++++++ ...hot_comptime_early-return_input_99b3d5._.js.map | 8 ++++---- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/crates/turbopack-ecmascript/src/analyzer/graph.rs b/crates/turbopack-ecmascript/src/analyzer/graph.rs index 7259f81996e457..93f9c46a23fac4 100644 --- a/crates/turbopack-ecmascript/src/analyzer/graph.rs +++ b/crates/turbopack-ecmascript/src/analyzer/graph.rs @@ -1979,12 +1979,6 @@ impl<'a> Analyzer<'a> { return; } match (early_return_when_true, early_return_when_false) { - (true, true) => { - self.early_return_stack.push(EarlyReturn::Always { - prev_effects: take(&mut self.effects), - start_ast_path: as_parent_path(ast_path), - }); - } (true, false) => { self.early_return_stack.push(EarlyReturn::Conditional { prev_effects: take(&mut self.effects), @@ -2011,7 +2005,7 @@ impl<'a> Analyzer<'a> { early_return_condition_value: false, }); } - (false, false) => { + (false, false) | (true, true) => { let kind = match (then, r#else) { (Some(then), Some(r#else)) => ConditionalKind::IfElse { then, r#else }, (Some(then), None) => ConditionalKind::If { then }, @@ -2025,6 +2019,12 @@ impl<'a> Analyzer<'a> { span, in_try: is_in_try(ast_path), }); + if early_return_when_false && early_return_when_true { + self.early_return_stack.push(EarlyReturn::Always { + prev_effects: take(&mut self.effects), + start_ast_path: as_parent_path(ast_path), + }); + } } } } diff --git a/crates/turbopack-tests/tests/snapshot/comptime/early-return/input/module.js b/crates/turbopack-tests/tests/snapshot/comptime/early-return/input/module.js index 685efcb6791568..75d6a24c365014 100644 --- a/crates/turbopack-tests/tests/snapshot/comptime/early-return/input/module.js +++ b/crates/turbopack-tests/tests/snapshot/comptime/early-return/input/module.js @@ -147,6 +147,16 @@ class K { } n5(); }; + + o() { + if (something) { + require("./module"); + return; + } else { + require("./module"); + return; + } + } } z1(); diff --git a/crates/turbopack-tests/tests/snapshot/comptime/early-return/output/crates_turbopack-tests_tests_snapshot_comptime_early-return_input_99b3d5._.js b/crates/turbopack-tests/tests/snapshot/comptime/early-return/output/crates_turbopack-tests_tests_snapshot_comptime_early-return_input_99b3d5._.js index a0011d05a7127a..bbf5c4f3ec0e4b 100644 --- a/crates/turbopack-tests/tests/snapshot/comptime/early-return/output/crates_turbopack-tests_tests_snapshot_comptime_early-return_input_99b3d5._.js +++ b/crates/turbopack-tests/tests/snapshot/comptime/early-return/output/crates_turbopack-tests_tests_snapshot_comptime_early-return_input_99b3d5._.js @@ -143,6 +143,15 @@ class K { } n5(); }; + o() { + if (something) { + __turbopack_require__("[project]/crates/turbopack-tests/tests/snapshot/comptime/early-return/input/module.js [test] (ecmascript)"); + return; + } else { + __turbopack_require__("[project]/crates/turbopack-tests/tests/snapshot/comptime/early-return/input/module.js [test] (ecmascript)"); + return; + } + } } z1(); return; diff --git a/crates/turbopack-tests/tests/snapshot/comptime/early-return/output/crates_turbopack-tests_tests_snapshot_comptime_early-return_input_99b3d5._.js.map b/crates/turbopack-tests/tests/snapshot/comptime/early-return/output/crates_turbopack-tests_tests_snapshot_comptime_early-return_input_99b3d5._.js.map index 510e79008e7d4e..643f12b3bacf00 100644 --- a/crates/turbopack-tests/tests/snapshot/comptime/early-return/output/crates_turbopack-tests_tests_snapshot_comptime_early-return_input_99b3d5._.js.map +++ b/crates/turbopack-tests/tests/snapshot/comptime/early-return/output/crates_turbopack-tests_tests_snapshot_comptime_early-return_input_99b3d5._.js.map @@ -2,8 +2,8 @@ "version": 3, "sources": [], "sections": [ - {"offset": {"line": 5, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/comptime/early-return/input/module.js"],"sourcesContent":["export function a() {\n if (true) {\n a1();\n return;\n }\n a2();\n var a3 = 3;\n function a4() {\n var a5;\n }\n (function a6() {\n var a7;\n });\n const a8 = () => {\n var a9;\n };\n class a10 {}\n let a11 = 11;\n}\n\nexport function b() {\n if (true) {\n b1();\n return;\n } else {\n b2();\n }\n b3();\n}\n\nexport function c() {\n if (true) {\n return;\n }\n c1();\n}\n\nexport function d() {\n if (true) {\n return;\n } else {\n d1();\n }\n d2();\n}\n\nexport function e() {\n if (false) {\n e1();\n } else {\n return;\n }\n e2();\n}\n\nexport function f() {\n if (false) {\n } else {\n return;\n }\n f1();\n}\n\nexport function g() {\n if (false) {\n g1();\n } else {\n g2();\n return;\n }\n g3();\n}\n\nexport function h() {\n if (false) {\n } else {\n h1();\n return;\n }\n h2();\n}\n\nexport function i(j) {\n if (j < 1) return i1();\n return i2();\n}\n\nexport function j(j) {\n if (j < 1) {\n return i1();\n }\n return i2();\n}\n\nclass K {\n constructor() {\n try {\n k1();\n } catch (e) {\n k2();\n return;\n k3();\n } finally {\n k4();\n }\n k5();\n }\n\n l() {\n try {\n l1();\n } catch (e) {\n l2();\n } finally {\n l3();\n return;\n l4();\n }\n l5();\n }\n\n get m() {\n if (true) {\n m1();\n return;\n }\n m2();\n }\n\n set m(value) {\n m1();\n return m2();\n m3();\n }\n\n n = () => {\n switch (42) {\n case 1:\n n1();\n return;\n n2();\n case 2:\n n3();\n break;\n default:\n n4();\n }\n n5();\n };\n}\n\nz1();\n\nreturn;\n\nz2();\n"],"names":[],"mappings":";;;;;;;;;;;;AAAO,SAAS;IACd,wCAAU;QACR;QACA;IACF;;IAEA,IAAI;IACJ,SAAS;QACP,IAAI;IACN;IAIA,MAAM;IAGA,IAAA;IACN,IAAI;AACN;AAEO,SAAS;IACd,wCAAU;QACR;QACA;IACF,OAAO;;IAEP;;AAEF;AAEO,SAAS;IACd,wCAAU;QACR;IACF;;AAEF;AAEO,SAAS;IACd,wCAAU;QACR;IACF,OAAO;;IAEP;;AAEF;AAEO,SAAS;IACd,uCAAW;;IAEX,OAAO;QACL;IACF;;AAEF;AAEO,SAAS;IACd,uCAAW,CACX,OAAO;QACL;IACF;;AAEF;AAEO,SAAS;IACd,uCAAW;;IAEX,OAAO;QACL;QACA;IACF;;AAEF;AAEO,SAAS;IACd,uCAAW,CACX,OAAO;QACL;QACA;IACF;;AAEF;AAEO,SAAS,EAAE,CAAC;IACjB,IAAI,IAAI,GAAG,OAAO;IAClB,OAAO;AACT;AAEO,SAAS,EAAE,CAAC;IACjB,IAAI,IAAI,GAAG;QACT,OAAO;IACT;IACA,OAAO;AACT;AAEA,MAAM;IACJ,aAAc;QACZ,IAAI;YACF;QACF,EAAE,OAAO,GAAG;YACV;YACA;;QAEF,SAAU;YACR;QACF;QACA;IACF;IAEA,IAAI;QACF,IAAI;YACF;QACF,EAAE,OAAO,GAAG;YACV;QACF,SAAU;YACR;YACA;;QAEF;;IAEF;IAEA,IAAI,IAAI;QACN,wCAAU;YACR;YACA;QACF;;IAEF;IAEA,IAAI,EAAE,KAAK,EAAE;QACX;QACA,OAAO;;IAET;IAEA,IAAI;QACF,OAAQ;YACN,KAAK;gBACH;gBACA;;YAEF,KAAK;gBACH;gBACA;YACF;gBACE;QACJ;QACA;IACF,EAAE;AACJ;AAEA;AAEA;AAEA"}}, - {"offset": {"line": 149, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, - {"offset": {"line": 154, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/comptime/early-return/input/index.js"],"sourcesContent":["import * as module from \"./module\";\nconsole.log(module);\n"],"names":[],"mappings":";;;;AACA,QAAQ,GAAG,CAAC"}}, - {"offset": {"line": 159, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}] + {"offset": {"line": 5, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/comptime/early-return/input/module.js"],"sourcesContent":["export function a() {\n if (true) {\n a1();\n return;\n }\n a2();\n var a3 = 3;\n function a4() {\n var a5;\n }\n (function a6() {\n var a7;\n });\n const a8 = () => {\n var a9;\n };\n class a10 {}\n let a11 = 11;\n}\n\nexport function b() {\n if (true) {\n b1();\n return;\n } else {\n b2();\n }\n b3();\n}\n\nexport function c() {\n if (true) {\n return;\n }\n c1();\n}\n\nexport function d() {\n if (true) {\n return;\n } else {\n d1();\n }\n d2();\n}\n\nexport function e() {\n if (false) {\n e1();\n } else {\n return;\n }\n e2();\n}\n\nexport function f() {\n if (false) {\n } else {\n return;\n }\n f1();\n}\n\nexport function g() {\n if (false) {\n g1();\n } else {\n g2();\n return;\n }\n g3();\n}\n\nexport function h() {\n if (false) {\n } else {\n h1();\n return;\n }\n h2();\n}\n\nexport function i(j) {\n if (j < 1) return i1();\n return i2();\n}\n\nexport function j(j) {\n if (j < 1) {\n return i1();\n }\n return i2();\n}\n\nclass K {\n constructor() {\n try {\n k1();\n } catch (e) {\n k2();\n return;\n k3();\n } finally {\n k4();\n }\n k5();\n }\n\n l() {\n try {\n l1();\n } catch (e) {\n l2();\n } finally {\n l3();\n return;\n l4();\n }\n l5();\n }\n\n get m() {\n if (true) {\n m1();\n return;\n }\n m2();\n }\n\n set m(value) {\n m1();\n return m2();\n m3();\n }\n\n n = () => {\n switch (42) {\n case 1:\n n1();\n return;\n n2();\n case 2:\n n3();\n break;\n default:\n n4();\n }\n n5();\n };\n\n o() {\n if (something) {\n require(\"./module\");\n return;\n } else {\n require(\"./module\");\n return;\n }\n }\n}\n\nz1();\n\nreturn;\n\nz2();\n"],"names":[],"mappings":";;;;;;;;;;;;AAAO,SAAS;IACd,wCAAU;QACR;QACA;IACF;;IAEA,IAAI;IACJ,SAAS;QACP,IAAI;IACN;IAIA,MAAM;IAGA,IAAA;IACN,IAAI;AACN;AAEO,SAAS;IACd,wCAAU;QACR;QACA;IACF,OAAO;;IAEP;;AAEF;AAEO,SAAS;IACd,wCAAU;QACR;IACF;;AAEF;AAEO,SAAS;IACd,wCAAU;QACR;IACF,OAAO;;IAEP;;AAEF;AAEO,SAAS;IACd,uCAAW;;IAEX,OAAO;QACL;IACF;;AAEF;AAEO,SAAS;IACd,uCAAW,CACX,OAAO;QACL;IACF;;AAEF;AAEO,SAAS;IACd,uCAAW;;IAEX,OAAO;QACL;QACA;IACF;;AAEF;AAEO,SAAS;IACd,uCAAW,CACX,OAAO;QACL;QACA;IACF;;AAEF;AAEO,SAAS,EAAE,CAAC;IACjB,IAAI,IAAI,GAAG,OAAO;IAClB,OAAO;AACT;AAEO,SAAS,EAAE,CAAC;IACjB,IAAI,IAAI,GAAG;QACT,OAAO;IACT;IACA,OAAO;AACT;AAEA,MAAM;IACJ,aAAc;QACZ,IAAI;YACF;QACF,EAAE,OAAO,GAAG;YACV;YACA;;QAEF,SAAU;YACR;QACF;QACA;IACF;IAEA,IAAI;QACF,IAAI;YACF;QACF,EAAE,OAAO,GAAG;YACV;QACF,SAAU;YACR;YACA;;QAEF;;IAEF;IAEA,IAAI,IAAI;QACN,wCAAU;YACR;YACA;QACF;;IAEF;IAEA,IAAI,EAAE,KAAK,EAAE;QACX;QACA,OAAO;;IAET;IAEA,IAAI;QACF,OAAQ;YACN,KAAK;gBACH;gBACA;;YAEF,KAAK;gBACH;gBACA;YACF;gBACE;QACJ;QACA;IACF,EAAE;IAEF,IAAI;QACF,IAAI,WAAW;;YAEb;QACF,OAAO;;YAEL;QACF;IACF;AACF;AAEA;AAEA;AAEA"}}, + {"offset": {"line": 158, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}, + {"offset": {"line": 163, "column": 0}, "map": {"version":3,"sources":["turbopack://[project]/crates/turbopack-tests/tests/snapshot/comptime/early-return/input/index.js"],"sourcesContent":["import * as module from \"./module\";\nconsole.log(module);\n"],"names":[],"mappings":";;;;AACA,QAAQ,GAAG,CAAC"}}, + {"offset": {"line": 168, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}] } \ No newline at end of file