From 515b0f1cc854ae40cb435e0bebd87151ee47b9e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Mon, 15 Jul 2024 19:20:10 +0900 Subject: [PATCH 01/46] Dep on allocator --- crates/swc_ecma_ast/Cargo.toml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/swc_ecma_ast/Cargo.toml b/crates/swc_ecma_ast/Cargo.toml index 2a871818c148..f7b19d8cb207 100644 --- a/crates/swc_ecma_ast/Cargo.toml +++ b/crates/swc_ecma_ast/Cargo.toml @@ -23,10 +23,11 @@ rkyv-impl = [ "__rkyv", "rkyv", "bytecheck", + "swc_allocator/rkyv", "swc_atoms/rkyv-impl", "swc_common/rkyv-impl", ] -serde-impl = ["serde"] +serde-impl = ["serde", "swc_allocator/serde"] [dependencies] arbitrary = { workspace = true, features = ["derive"], optional = true } @@ -43,6 +44,7 @@ rkyv = { workspace = true, features = [ scoped-tls = { workspace = true } serde = { workspace = true, features = ["derive"], optional = true } string_enum = { version = "0.4.4", path = "../string_enum" } +swc_allocator = { version = "0.1.4", path = "../swc_allocator" } swc_atoms = { version = "0.6.5", path = "../swc_atoms" } swc_common = { version = "0.35.0", path = "../swc_common" } unicode-id-start = { workspace = true } From 28eb1e600a80fd179929d8fe9f5c6deecd4cda36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Mon, 15 Jul 2024 19:20:15 +0900 Subject: [PATCH 02/46] cargo lockfile --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index dcc366fbd301..5e041eba630c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4132,6 +4132,7 @@ dependencies = [ "serde", "serde_json", "string_enum", + "swc_allocator", "swc_atoms", "swc_common", "unicode-id-start", From a3c78d0c3833711c568aeb9549d71d35aa324077 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Mon, 15 Jul 2024 19:21:20 +0900 Subject: [PATCH 03/46] Use box & vec --- crates/swc_ecma_ast/src/class.rs | 1 + crates/swc_ecma_ast/src/decl.rs | 1 + crates/swc_ecma_ast/src/expr.rs | 1 + crates/swc_ecma_ast/src/function.rs | 1 + crates/swc_ecma_ast/src/ident.rs | 1 + crates/swc_ecma_ast/src/jsx.rs | 1 + crates/swc_ecma_ast/src/lit.rs | 1 + crates/swc_ecma_ast/src/module.rs | 1 + crates/swc_ecma_ast/src/module_decl.rs | 1 + crates/swc_ecma_ast/src/operators.rs | 1 + 10 files changed, 10 insertions(+) diff --git a/crates/swc_ecma_ast/src/class.rs b/crates/swc_ecma_ast/src/class.rs index 4574c0edb5bb..ab07be5089f8 100644 --- a/crates/swc_ecma_ast/src/class.rs +++ b/crates/swc_ecma_ast/src/class.rs @@ -1,4 +1,5 @@ use is_macro::Is; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, SyntaxContext, DUMMY_SP}; use crate::{ diff --git a/crates/swc_ecma_ast/src/decl.rs b/crates/swc_ecma_ast/src/decl.rs index af6d89e7ab84..d82bdac3150b 100644 --- a/crates/swc_ecma_ast/src/decl.rs +++ b/crates/swc_ecma_ast/src/decl.rs @@ -1,5 +1,6 @@ use is_macro::Is; use string_enum::StringEnum; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, SyntaxContext, DUMMY_SP}; use crate::{ diff --git a/crates/swc_ecma_ast/src/expr.rs b/crates/swc_ecma_ast/src/expr.rs index 489daa80759a..9e6fc290cd2f 100644 --- a/crates/swc_ecma_ast/src/expr.rs +++ b/crates/swc_ecma_ast/src/expr.rs @@ -3,6 +3,7 @@ use std::{borrow::Cow, mem::transmute}; use is_macro::Is; use string_enum::StringEnum; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_atoms::Atom; use swc_common::{ ast_node, util::take::Take, BytePos, EqIgnoreSpan, Span, Spanned, SyntaxContext, DUMMY_SP, diff --git a/crates/swc_ecma_ast/src/function.rs b/crates/swc_ecma_ast/src/function.rs index 1d34eeca9772..510b3a8205d8 100644 --- a/crates/swc_ecma_ast/src/function.rs +++ b/crates/swc_ecma_ast/src/function.rs @@ -1,4 +1,5 @@ use is_macro::Is; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, SyntaxContext, DUMMY_SP}; use crate::{ diff --git a/crates/swc_ecma_ast/src/ident.rs b/crates/swc_ecma_ast/src/ident.rs index fa67207b645a..e2057232ceb9 100644 --- a/crates/swc_ecma_ast/src/ident.rs +++ b/crates/swc_ecma_ast/src/ident.rs @@ -5,6 +5,7 @@ use std::{ }; use phf::phf_set; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_atoms::{js_word, Atom}; use swc_common::{ ast_node, util::take::Take, BytePos, EqIgnoreSpan, Mark, Span, Spanned, SyntaxContext, DUMMY_SP, diff --git a/crates/swc_ecma_ast/src/jsx.rs b/crates/swc_ecma_ast/src/jsx.rs index 41055f03b864..0bf698918079 100644 --- a/crates/swc_ecma_ast/src/jsx.rs +++ b/crates/swc_ecma_ast/src/jsx.rs @@ -1,4 +1,5 @@ use is_macro::Is; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_atoms::Atom; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP}; diff --git a/crates/swc_ecma_ast/src/lit.rs b/crates/swc_ecma_ast/src/lit.rs index bb7555c59a4b..95f2d103931a 100644 --- a/crates/swc_ecma_ast/src/lit.rs +++ b/crates/swc_ecma_ast/src/lit.rs @@ -5,6 +5,7 @@ use std::{ }; use num_bigint::BigInt as BigIntValue; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_atoms::{js_word, Atom}; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP}; diff --git a/crates/swc_ecma_ast/src/module.rs b/crates/swc_ecma_ast/src/module.rs index fe9456d323e1..72c2b5b13a3b 100644 --- a/crates/swc_ecma_ast/src/module.rs +++ b/crates/swc_ecma_ast/src/module.rs @@ -1,4 +1,5 @@ use is_macro::Is; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_atoms::Atom; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP}; diff --git a/crates/swc_ecma_ast/src/module_decl.rs b/crates/swc_ecma_ast/src/module_decl.rs index df23cd837afd..0c872d75f71d 100644 --- a/crates/swc_ecma_ast/src/module_decl.rs +++ b/crates/swc_ecma_ast/src/module_decl.rs @@ -1,4 +1,5 @@ use is_macro::Is; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_atoms::Atom; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP}; diff --git a/crates/swc_ecma_ast/src/operators.rs b/crates/swc_ecma_ast/src/operators.rs index f64efc54a3af..fd4eff7fdb50 100644 --- a/crates/swc_ecma_ast/src/operators.rs +++ b/crates/swc_ecma_ast/src/operators.rs @@ -1,4 +1,5 @@ use string_enum::StringEnum; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_common::EqIgnoreSpan; #[derive(StringEnum, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash, EqIgnoreSpan, Default)] From 1f22ed48a3aa568e0e8d3fdac3314e6bed0017f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 05:14:36 +0900 Subject: [PATCH 04/46] fix --- crates/swc_ecma_ast/src/expr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/swc_ecma_ast/src/expr.rs b/crates/swc_ecma_ast/src/expr.rs index 9e6fc290cd2f..662cdf65bdac 100644 --- a/crates/swc_ecma_ast/src/expr.rs +++ b/crates/swc_ecma_ast/src/expr.rs @@ -529,7 +529,7 @@ impl ObjectLit { /// /// Returns [None] if this is not a valid for `with` of [crate::ImportDecl]. pub fn as_import_with(&self) -> Option { - let mut values = vec![]; + let mut values = Vec::new(); for prop in &self.props { match prop { PropOrSpread::Spread(..) => return None, From bc1acf264a8fd68f9f0283df0b01810872da4ed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 05:25:44 +0900 Subject: [PATCH 05/46] fix more --- crates/swc_ecma_ast/src/prop.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/swc_ecma_ast/src/prop.rs b/crates/swc_ecma_ast/src/prop.rs index 40318dcffd0a..c00535310591 100644 --- a/crates/swc_ecma_ast/src/prop.rs +++ b/crates/swc_ecma_ast/src/prop.rs @@ -1,4 +1,5 @@ use is_macro::Is; +use swc_allocator::boxed::Box; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP}; use crate::{ From c773b67283fbecf916f45b75356ab5c91823847c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 05:26:09 +0900 Subject: [PATCH 06/46] lint --- crates/swc_ecma_ast/src/ident.rs | 2 +- crates/swc_ecma_ast/src/lit.rs | 2 +- crates/swc_ecma_ast/src/module.rs | 2 +- crates/swc_ecma_ast/src/operators.rs | 1 - 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/swc_ecma_ast/src/ident.rs b/crates/swc_ecma_ast/src/ident.rs index e2057232ceb9..cc286fb10cfc 100644 --- a/crates/swc_ecma_ast/src/ident.rs +++ b/crates/swc_ecma_ast/src/ident.rs @@ -5,7 +5,7 @@ use std::{ }; use phf::phf_set; -use swc_allocator::{boxed::Box, vec::Vec}; +use swc_allocator::boxed::Box; use swc_atoms::{js_word, Atom}; use swc_common::{ ast_node, util::take::Take, BytePos, EqIgnoreSpan, Mark, Span, Spanned, SyntaxContext, DUMMY_SP, diff --git a/crates/swc_ecma_ast/src/lit.rs b/crates/swc_ecma_ast/src/lit.rs index 95f2d103931a..5a8389cc267f 100644 --- a/crates/swc_ecma_ast/src/lit.rs +++ b/crates/swc_ecma_ast/src/lit.rs @@ -5,7 +5,7 @@ use std::{ }; use num_bigint::BigInt as BigIntValue; -use swc_allocator::{boxed::Box, vec::Vec}; +use swc_allocator::boxed::Box; use swc_atoms::{js_word, Atom}; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP}; diff --git a/crates/swc_ecma_ast/src/module.rs b/crates/swc_ecma_ast/src/module.rs index 72c2b5b13a3b..811fc2ecab80 100644 --- a/crates/swc_ecma_ast/src/module.rs +++ b/crates/swc_ecma_ast/src/module.rs @@ -1,5 +1,5 @@ use is_macro::Is; -use swc_allocator::{boxed::Box, vec::Vec}; +use swc_allocator::vec::Vec; use swc_atoms::Atom; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP}; diff --git a/crates/swc_ecma_ast/src/operators.rs b/crates/swc_ecma_ast/src/operators.rs index fd4eff7fdb50..f64efc54a3af 100644 --- a/crates/swc_ecma_ast/src/operators.rs +++ b/crates/swc_ecma_ast/src/operators.rs @@ -1,5 +1,4 @@ use string_enum::StringEnum; -use swc_allocator::{boxed::Box, vec::Vec}; use swc_common::EqIgnoreSpan; #[derive(StringEnum, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash, EqIgnoreSpan, Default)] From a3351e2a50fab0558fd2c5cd230e1ebf05cad004 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 05:27:53 +0900 Subject: [PATCH 07/46] More box --- crates/swc_ecma_ast/src/expr.rs | 6 +++--- crates/swc_ecma_ast/src/pat.rs | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/swc_ecma_ast/src/expr.rs b/crates/swc_ecma_ast/src/expr.rs index 662cdf65bdac..7c26ca7215a1 100644 --- a/crates/swc_ecma_ast/src/expr.rs +++ b/crates/swc_ecma_ast/src/expr.rs @@ -1402,7 +1402,7 @@ impl TryFrom> for AssignTarget { type Error = Box; fn try_from(p: Box) -> Result { - (*p).try_into().map_err(Box::new) + p.unbox().try_into().map_err(Box::new) } } @@ -1503,7 +1503,7 @@ impl TryFrom> for SimpleAssignTarget { type Error = Box; fn try_from(e: Box) -> Result { - Ok(match *e { + Ok(match e.unbox() { Expr::Ident(i) => SimpleAssignTarget::Ident(i.into()), Expr::Member(m) => SimpleAssignTarget::Member(m), Expr::SuperProp(s) => SimpleAssignTarget::SuperProp(s), @@ -1514,7 +1514,7 @@ impl TryFrom> for SimpleAssignTarget { Expr::TsNonNull(n) => SimpleAssignTarget::TsNonNull(n), Expr::TsTypeAssertion(a) => SimpleAssignTarget::TsTypeAssertion(a), Expr::TsInstantiation(a) => SimpleAssignTarget::TsInstantiation(a), - _ => return Err(e), + e => return Err(Box::new(e)), }) } } diff --git a/crates/swc_ecma_ast/src/pat.rs b/crates/swc_ecma_ast/src/pat.rs index ab42072eb61a..505702fda7e2 100644 --- a/crates/swc_ecma_ast/src/pat.rs +++ b/crates/swc_ecma_ast/src/pat.rs @@ -1,4 +1,5 @@ use is_macro::Is; +use swc_allocator::boxed::Box; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP}; use crate::{ From c672cc41dc93aa7de6ae488801279c80b77a931d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 05:42:48 +0900 Subject: [PATCH 08/46] `#[fundamental]` --- crates/swc_allocator/src/boxed/mod.rs | 1 + crates/swc_allocator/src/lib.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/crates/swc_allocator/src/boxed/mod.rs b/crates/swc_allocator/src/boxed/mod.rs index 63b3ce8b681a..7573d2eb9de4 100644 --- a/crates/swc_allocator/src/boxed/mod.rs +++ b/crates/swc_allocator/src/boxed/mod.rs @@ -19,6 +19,7 @@ mod serde; /// Faster alterantive for [`std::boxed::Box`]. #[repr(transparent)] #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[fundamental] pub struct Box(pub(crate) allocator_api2::boxed::Box); impl From for Box { diff --git a/crates/swc_allocator/src/lib.rs b/crates/swc_allocator/src/lib.rs index 5fd3f8e57faf..655493b7d10a 100644 --- a/crates/swc_allocator/src/lib.rs +++ b/crates/swc_allocator/src/lib.rs @@ -29,6 +29,7 @@ #![cfg_attr(docsrs, feature(doc_cfg))] #![deny(missing_docs)] #![allow(clippy::derivable_impls)] +#![feature(fundamental, fn_ptr_trait, with_negative_coherence)] pub use crate::alloc::Allocator; From f756595f1e6bc2bbcf6c457ae9cad56c251f51df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 05:49:19 +0900 Subject: [PATCH 09/46] fix --- crates/swc_ecma_ast/src/stmt.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/swc_ecma_ast/src/stmt.rs b/crates/swc_ecma_ast/src/stmt.rs index 32665b359a73..b532eafd8229 100644 --- a/crates/swc_ecma_ast/src/stmt.rs +++ b/crates/swc_ecma_ast/src/stmt.rs @@ -1,4 +1,5 @@ use is_macro::Is; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, SyntaxContext, DUMMY_SP}; use crate::{ From 37c165a80b53f5acaf0bc597f767db95b6b08b61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 05:49:29 +0900 Subject: [PATCH 10/46] Fix AST --- crates/swc_ecma_ast/src/stmt.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/crates/swc_ecma_ast/src/stmt.rs b/crates/swc_ecma_ast/src/stmt.rs index b532eafd8229..22ba3d6f23d4 100644 --- a/crates/swc_ecma_ast/src/stmt.rs +++ b/crates/swc_ecma_ast/src/stmt.rs @@ -24,11 +24,7 @@ pub struct BlockStmt { impl Take for BlockStmt { fn dummy() -> Self { - BlockStmt { - span: DUMMY_SP, - stmts: vec![], - ctxt: Default::default(), - } + Default::default() } } From 4589e5e3d76f831fbd09cd5380546a1c03264cce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 05:51:37 +0900 Subject: [PATCH 11/46] Dep --- crates/swc_ecma_parser/Cargo.toml | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/crates/swc_ecma_parser/Cargo.toml b/crates/swc_ecma_parser/Cargo.toml index 7ec2d0665345..d3df5618d650 100644 --- a/crates/swc_ecma_parser/Cargo.toml +++ b/crates/swc_ecma_parser/Cargo.toml @@ -25,21 +25,22 @@ typescript = [] verify = ["swc_ecma_visit"] [dependencies] -either = { workspace = true } -num-bigint = { workspace = true } -num-traits = { workspace = true } -serde = { workspace = true, features = ["derive"] } -smallvec = { workspace = true } -smartstring = { workspace = true } -tracing = { workspace = true } -typed-arena = { workspace = true } - +either = { workspace = true } new_debug_unreachable = { workspace = true } +num-bigint = { workspace = true } +num-traits = { workspace = true } phf = { workspace = true, features = ["macros"] } -swc_atoms = { version = "0.6.5", path = "../swc_atoms" } -swc_common = { version = "0.35.0", path = "../swc_common" } -swc_ecma_ast = { version = "0.116.0", path = "../swc_ecma_ast" } -swc_ecma_visit = { version = "0.102.0", path = "../swc_ecma_visit", optional = true } +serde = { workspace = true, features = ["derive"] } +smallvec = { workspace = true } +smartstring = { workspace = true } +tracing = { workspace = true } +typed-arena = { workspace = true } + +swc_allocator = { version = "0.1.4", path = "../swc_allocator", default-features = false } +swc_atoms = { version = "0.6.5", path = "../swc_atoms" } +swc_common = { version = "0.35.0", path = "../swc_common" } +swc_ecma_ast = { version = "0.116.0", path = "../swc_ecma_ast" } +swc_ecma_visit = { version = "0.102.0", path = "../swc_ecma_visit", optional = true } [target.'cfg(not(any(target_arch = "wasm32", target_arch = "arm")))'.dependencies] stacker = { version = "0.1.15", optional = true } From 8f4e3819210e38760d5dbe911b50608c325df1a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 05:51:47 +0900 Subject: [PATCH 12/46] cargo lockfile --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index 5e041eba630c..a5233ea334ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4476,6 +4476,7 @@ dependencies = [ "smallvec", "smartstring", "stacker", + "swc_allocator", "swc_atoms", "swc_common", "swc_ecma_ast", From c2ff010be97bab28b0e7f87f740f2bb7e0ec6b25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 05:52:39 +0900 Subject: [PATCH 13/46] Dep --- crates/swc_ecma_visit/Cargo.toml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/swc_ecma_visit/Cargo.toml b/crates/swc_ecma_visit/Cargo.toml index e4b27abdae26..cf75f4f947cb 100644 --- a/crates/swc_ecma_visit/Cargo.toml +++ b/crates/swc_ecma_visit/Cargo.toml @@ -26,7 +26,8 @@ num-bigint = { workspace = true, features = ["serde"] } serde = { workspace = true, optional = true } tracing = { workspace = true } -swc_atoms = { version = "0.6.5", path = "../swc_atoms" } -swc_common = { version = "0.35.0", path = "../swc_common" } -swc_ecma_ast = { version = "0.116.0", path = "../swc_ecma_ast" } -swc_visit = { version = "0.5.14", path = "../swc_visit" } +swc_allocator = { version = "0.1.4", path = "../swc_allocator", default-features = false } +swc_atoms = { version = "0.6.5", path = "../swc_atoms" } +swc_common = { version = "0.35.0", path = "../swc_common" } +swc_ecma_ast = { version = "0.116.0", path = "../swc_ecma_ast" } +swc_visit = { version = "0.5.14", path = "../swc_visit" } From e7f92b6f66b55f185fdfb42a554805469ad34aa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 05:52:43 +0900 Subject: [PATCH 14/46] cargo lockfile --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index a5233ea334ed..86f0aff5e83a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4870,6 +4870,7 @@ dependencies = [ "new_debug_unreachable", "num-bigint", "serde", + "swc_allocator", "swc_atoms", "swc_common", "swc_ecma_ast", From d282b2e62650447cc910d3465722279b1ca04b02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 05:53:47 +0900 Subject: [PATCH 15/46] Import for vistor --- crates/swc_ecma_visit/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/swc_ecma_visit/src/lib.rs b/crates/swc_ecma_visit/src/lib.rs index 754d01cd0a5d..b432d9e8a896 100644 --- a/crates/swc_ecma_visit/src/lib.rs +++ b/crates/swc_ecma_visit/src/lib.rs @@ -9,6 +9,7 @@ pub extern crate swc_ecma_ast; use std::{borrow::Cow, fmt::Debug}; use num_bigint::BigInt as BigIntValue; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_atoms::Atom; use swc_common::{pass::CompilerPass, Span, SyntaxContext, DUMMY_SP}; use swc_ecma_ast::*; From 755db3475cf98c8e4ef6b594325fb28392b95cd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 06:34:23 +0900 Subject: [PATCH 16/46] Fix parser --- crates/swc_ecma_parser/src/lexer/mod.rs | 4 +-- crates/swc_ecma_parser/src/lexer/tests.rs | 26 +++++++++---------- .../src/parser/class_and_fn.rs | 21 ++++++++------- crates/swc_ecma_parser/src/parser/expr.rs | 7 ++--- crates/swc_ecma_parser/src/parser/expr/ops.rs | 1 + .../swc_ecma_parser/src/parser/expr/tests.rs | 14 +++++----- .../src/parser/expr/verifier.rs | 2 +- crates/swc_ecma_parser/src/parser/jsx.rs | 5 ++-- .../swc_ecma_parser/src/parser/jsx/tests.rs | 10 +++---- crates/swc_ecma_parser/src/parser/object.rs | 12 ++++----- crates/swc_ecma_parser/src/parser/pat.rs | 11 ++++---- crates/swc_ecma_parser/src/parser/stmt.rs | 17 ++++++------ .../src/parser/stmt/module_item.rs | 8 +++--- crates/swc_ecma_parser/src/parser/tests.rs | 8 +++--- .../swc_ecma_parser/src/parser/typescript.rs | 11 ++++---- 15 files changed, 83 insertions(+), 74 deletions(-) diff --git a/crates/swc_ecma_parser/src/lexer/mod.rs b/crates/swc_ecma_parser/src/lexer/mod.rs index b28c7c5f05af..d303e51e79f1 100644 --- a/crates/swc_ecma_parser/src/lexer/mod.rs +++ b/crates/swc_ecma_parser/src/lexer/mod.rs @@ -70,7 +70,7 @@ impl IntoIterator for Char { CharIter(match char::from_u32(self.0) { Some(c) => smallvec![c], None => { - let mut buf = smallvec![]; + let mut buf = smallVec::new(); let high = self.0 & 0xffff0000 >> 16; @@ -913,7 +913,7 @@ impl<'a> Lexer<'a> { fn read_unicode_escape(&mut self) -> LexResult> { debug_assert_eq!(self.cur(), Some('u')); - let mut chars = vec![]; + let mut chars = Vec::new(); let mut is_curly = false; self.bump(); // 'u' diff --git a/crates/swc_ecma_parser/src/lexer/tests.rs b/crates/swc_ecma_parser/src/lexer/tests.rs index 3ea373071980..613cfccf3b60 100644 --- a/crates/swc_ecma_parser/src/lexer/tests.rs +++ b/crates/swc_ecma_parser/src/lexer/tests.rs @@ -1091,7 +1091,7 @@ fn shebang() { #[test] fn empty() { - assert_eq!(lex_tokens(crate::Syntax::default(), "",), vec![]); + assert_eq!(lex_tokens(crate::Syntax::default(), "",), Vec::new()); } #[test] @@ -1844,14 +1844,14 @@ fn lex_semicolons(b: &mut Bencher) { fn issue_1272_1_ts() { let (tokens, errors) = lex_errors(crate::Syntax::Typescript(Default::default()), "\\u{16}"); assert_eq!(tokens.len(), 1); - assert_ne!(errors, vec![]); + assert_ne!(errors, Vec::new()); } #[test] fn issue_1272_1_js() { let (tokens, errors) = lex_errors(crate::Syntax::Es(Default::default()), "\\u{16}"); assert_eq!(tokens.len(), 1); - assert_ne!(errors, vec![]); + assert_ne!(errors, Vec::new()); } #[test] @@ -1859,7 +1859,7 @@ fn issue_1272_2_ts() { // Not recoverable yet let (tokens, errors) = lex_errors(crate::Syntax::Typescript(Default::default()), "\u{16}"); assert_eq!(tokens.len(), 1); - assert_eq!(errors, vec![]); + assert_eq!(errors, Vec::new()); } #[test] @@ -1867,14 +1867,14 @@ fn issue_1272_2_js() { // Not recoverable yet let (tokens, errors) = lex_errors(crate::Syntax::Es(Default::default()), "\u{16}"); assert_eq!(tokens.len(), 1); - assert_eq!(errors, vec![]); + assert_eq!(errors, Vec::new()); } #[test] fn issue_2853_1_js() { let (tokens, errors) = lex_errors(crate::Syntax::Es(Default::default()), "const a = \"\\0a\""); - assert_eq!(errors, vec![]); + assert_eq!(errors, Vec::new()); assert_eq!( tokens, vec![ @@ -1896,7 +1896,7 @@ fn issue_2853_2_ts() { "const a = \"\\0a\"", ); - assert_eq!(errors, vec![]); + assert_eq!(errors, Vec::new()); assert_eq!( tokens, vec![ @@ -1918,7 +1918,7 @@ fn issue_2853_3_js() { "const a = \"\u{0000}a\"", ); - assert_eq!(errors, vec![]); + assert_eq!(errors, Vec::new()); assert_eq!( tokens, vec![ @@ -1940,7 +1940,7 @@ fn issue_2853_4_ts() { "const a = \"\u{0000}a\"", ); - assert_eq!(errors, vec![]); + assert_eq!(errors, Vec::new()); assert_eq!( tokens, vec![ @@ -1965,7 +1965,7 @@ fn issue_2853_5_jsx() { "const a = \"\\0a\"", ); - assert_eq!(errors, vec![]); + assert_eq!(errors, Vec::new()); assert_eq!( tokens, vec![ @@ -1990,7 +1990,7 @@ fn issue_2853_6_tsx() { "const a = \"\\0a\"", ); - assert_eq!(errors, vec![]); + assert_eq!(errors, Vec::new()); assert_eq!( tokens, vec![ @@ -2015,7 +2015,7 @@ fn issue_2853_7_jsx() { "const a = \"\u{0000}a\"", ); - assert_eq!(errors, vec![]); + assert_eq!(errors, Vec::new()); assert_eq!( tokens, vec![ @@ -2040,7 +2040,7 @@ fn issue_2853_8_tsx() { "const a = \"\u{0000}a\"", ); - assert_eq!(errors, vec![]); + assert_eq!(errors, Vec::new()); assert_eq!( tokens, vec![ diff --git a/crates/swc_ecma_parser/src/parser/class_and_fn.rs b/crates/swc_ecma_parser/src/parser/class_and_fn.rs index 16bb7a6bc752..ba44d5913d5f 100644 --- a/crates/swc_ecma_parser/src/parser/class_and_fn.rs +++ b/crates/swc_ecma_parser/src/parser/class_and_fn.rs @@ -1,3 +1,4 @@ +use swc_allocator::{boxed::Box, vec::Vec}; use swc_common::Spanned; use super::*; @@ -8,12 +9,12 @@ impl Parser { pub(super) fn parse_async_fn_expr(&mut self) -> PResult> { let start = cur_pos!(self); expect!(self, "async"); - self.parse_fn(None, Some(start), vec![]) + self.parse_fn(None, Some(start), Vec::new()) } /// Parse function expression pub(super) fn parse_fn_expr(&mut self) -> PResult> { - self.parse_fn(None, None, vec![]) + self.parse_fn(None, None, Vec::new()) } pub(super) fn parse_async_fn_decl(&mut self, decorators: Vec) -> PResult { @@ -124,7 +125,7 @@ impl Parser { let implements = if p.input.syntax().typescript() && eat!(p, "implements") { p.parse_ts_heritage_clause()? } else { - vec![] + Vec::new() }; { @@ -251,11 +252,11 @@ impl Parser { pub(super) fn parse_decorators(&mut self, allow_export: bool) -> PResult> { if !self.syntax().decorators() { - return Ok(vec![]); + return Ok(Vec::new()); } trace_cur!(self, parse_decorators); - let mut decorators = vec![]; + let mut decorators = Vec::new(); let start = cur_pos!(self); while is!(self, '@') { @@ -333,7 +334,7 @@ impl Parser { } fn parse_class_body(&mut self) -> PResult> { - let mut elems = vec![]; + let mut elems = Vec::new(); let mut has_constructor_with_body = false; while !eof!(self) && !is!(self, '}') { if eat_exact!(self, ';') { @@ -1413,7 +1414,7 @@ impl Parser { ..self.ctx() }; let state = State { - labels: vec![], + labels: Vec::new(), ..Default::default() }; self.with_ctx(ctx) @@ -1792,11 +1793,11 @@ mod tests { expr: Box::new(Expr::Class(ClassExpr { ident: None, class: Box::new(Class { - decorators: vec![], + decorators: Vec::new(), span, - body: vec![], + body: Vec::new(), super_class: Some(expr("a")), - implements: vec![], + implements: Vec::new(), is_abstract: false, ..Default::default() }), diff --git a/crates/swc_ecma_parser/src/parser/expr.rs b/crates/swc_ecma_parser/src/parser/expr.rs index 918105619a66..a17bcbb03863 100644 --- a/crates/swc_ecma_parser/src/parser/expr.rs +++ b/crates/swc_ecma_parser/src/parser/expr.rs @@ -1,4 +1,5 @@ use either::Either; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_common::{ast_node, util::take::Take, Spanned}; use super::{pat::PatType, util::ExprExt, *}; @@ -500,7 +501,7 @@ impl Parser { let start = cur_pos!(self); assert_and_bump!(self, '['); - let mut elems = vec![]; + let mut elems = Vec::new(); while !eof!(self) && !is!(self, ']') { if is!(self, ',') { @@ -1013,7 +1014,7 @@ impl Parser { ) -> PResult<(Vec>, Vec)> { trace_cur!(self, parse_tpl_elements); - let mut exprs = vec![]; + let mut exprs = Vec::new(); let cur_elem = self.parse_tpl_element(is_tagged_tpl)?; let mut is_tail = cur_elem.tail; @@ -1731,7 +1732,7 @@ impl Parser { expect!(self, '('); - let mut items = vec![]; + let mut items = Vec::new(); let mut trailing_comma = None; // TODO(kdy1): optimize (once we parsed a pattern, we can parse everything else diff --git a/crates/swc_ecma_parser/src/parser/expr/ops.rs b/crates/swc_ecma_parser/src/parser/expr/ops.rs index ecedf9ee735c..ee1f5bcbd480 100644 --- a/crates/swc_ecma_parser/src/parser/expr/ops.rs +++ b/crates/swc_ecma_parser/src/parser/expr/ops.rs @@ -1,4 +1,5 @@ //! Parser for unary operations and binary operations. +use swc_allocator::{boxed::Box, vec::Vec}; use tracing::trace; use super::*; diff --git a/crates/swc_ecma_parser/src/parser/expr/tests.rs b/crates/swc_ecma_parser/src/parser/expr/tests.rs index 5f211dcc82f6..b27d8e9dc6b1 100644 --- a/crates/swc_ecma_parser/src/parser/expr/tests.rs +++ b/crates/swc_ecma_parser/src/parser/expr/tests.rs @@ -98,7 +98,7 @@ fn async_call() { Box::new(Expr::Call(CallExpr { span, callee: Callee::Expr(expr("async")), - args: vec![], + args: Vec::new(), ..Default::default() })) ); @@ -112,7 +112,7 @@ fn async_arrow() { span, is_async: true, is_generator: false, - params: vec![], + params: Vec::new(), body: Box::new(BlockStmtOrExpr::Expr(expr("foo"))), ..Default::default() })) @@ -188,7 +188,7 @@ fn lhs_expr_as_new_expr_prod() { Box::new(Expr::New(NewExpr { span, callee: lhs("Date.toString"), - args: Some(vec![]), + args: Some(Vec::new()), ..Default::default() })) ); @@ -201,7 +201,7 @@ fn lhs_expr_as_call() { Box::new(Expr::Call(CallExpr { span, callee: Callee::Expr(lhs("new Date.toString()")), - args: vec![], + args: Vec::new(), ..Default::default() })) ) @@ -215,7 +215,7 @@ fn arrow_fn_no_args() { span, is_async: false, is_generator: false, - params: vec![], + params: Vec::new(), body: Box::new(BlockStmtOrExpr::Expr(expr("1"))), ..Default::default() })) @@ -342,7 +342,7 @@ fn iife() { Box::new(Expr::Call(CallExpr { span, callee: Callee::Expr(expr("(function(){})")), - args: vec![], + args: Vec::new(), ..Default::default() })) ) @@ -515,7 +515,7 @@ fn issue_5947() { fn issue_6781() { let cm = SourceMap::default(); let fm = cm.new_source_file(FileName::Anon.into(), "import.meta.env".to_string()); - let mut errors = vec![]; + let mut errors = Vec::new(); let expr = parse_file_as_expr( &fm, Default::default(), diff --git a/crates/swc_ecma_parser/src/parser/expr/verifier.rs b/crates/swc_ecma_parser/src/parser/expr/verifier.rs index c38f41aee6fd..a99d352ca2f7 100644 --- a/crates/swc_ecma_parser/src/parser/expr/verifier.rs +++ b/crates/swc_ecma_parser/src/parser/expr/verifier.rs @@ -6,7 +6,7 @@ use super::*; impl Parser { #[cfg(feature = "verify")] pub(in crate::parser) fn verify_expr(&mut self, expr: Box) -> PResult> { - let mut v = Verifier { errors: vec![] }; + let mut v = Verifier { errors: Vec::new() }; v.visit_expr(&expr); diff --git a/crates/swc_ecma_parser/src/parser/jsx.rs b/crates/swc_ecma_parser/src/parser/jsx.rs index 55c2da134bd7..baad0a8105c5 100644 --- a/crates/swc_ecma_parser/src/parser/jsx.rs +++ b/crates/swc_ecma_parser/src/parser/jsx.rs @@ -1,4 +1,5 @@ use either::Either; +use swc_allocator::boxed::Box; use swc_common::Spanned; use super::*; @@ -232,7 +233,7 @@ impl Parser { None }; - let mut attrs = vec![]; + let mut attrs = Vec::new(); while cur!(self, false).is_ok() { trace_cur!(self, parse_jsx_opening__attrs_loop); @@ -307,7 +308,7 @@ impl Parser { trace_cur!(p, parse_jsx_element__after_opening_element); - let mut children = vec![]; + let mut children = Vec::new(); let mut closing_element = None; let self_closing = match opening_element { diff --git a/crates/swc_ecma_parser/src/parser/jsx/tests.rs b/crates/swc_ecma_parser/src/parser/jsx/tests.rs index 8db4229470d8..ef9c9bdc0165 100644 --- a/crates/swc_ecma_parser/src/parser/jsx/tests.rs +++ b/crates/swc_ecma_parser/src/parser/jsx/tests.rs @@ -24,10 +24,10 @@ fn self_closing_01() { span, name: JSXElementName::Ident(Ident::new_no_ctxt("a".into(), span)), self_closing: true, - attrs: vec![], + attrs: Vec::new(), type_args: None, }, - children: vec![], + children: Vec::new(), closing: None, }))) ); @@ -43,7 +43,7 @@ fn normal_01() { span, name: JSXElementName::Ident(Ident::new_no_ctxt("a".into(), span)), self_closing: false, - attrs: vec![], + attrs: Vec::new(), type_args: None, }, children: vec![JSXElementChild::JSXText(JSXText { @@ -80,7 +80,7 @@ fn escape_in_attr() { self_closing: true, type_args: None, }, - children: vec![], + children: Vec::new(), closing: None }))) ); @@ -110,7 +110,7 @@ fn issue_584() { self_closing: true, type_args: None, }, - children: vec![], + children: Vec::new(), closing: None }))) ); diff --git a/crates/swc_ecma_parser/src/parser/object.rs b/crates/swc_ecma_parser/src/parser/object.rs index 5ccb22ed9f02..c3da76d2f8fe 100644 --- a/crates/swc_ecma_parser/src/parser/object.rs +++ b/crates/swc_ecma_parser/src/parser/object.rs @@ -22,7 +22,7 @@ impl Parser { let mut trailing_comma = None; assert_and_bump!(p, '{'); - let mut props = vec![]; + let mut props = Vec::new(); while !eat!(p, '}') { props.push(p.parse_object_prop()?); @@ -163,7 +163,7 @@ impl ParseObject> for Parser { }) .parse_fn_args_body( // no decorator in an object literal - vec![], + Vec::new(), start, |p| p.parse_unique_formal_params(), false, @@ -221,7 +221,7 @@ impl ParseObject> for Parser { }) .parse_fn_args_body( // no decorator in an object literal - vec![], + Vec::new(), start, |p| p.parse_unique_formal_params(), false, @@ -287,7 +287,7 @@ impl ParseObject> for Parser { "get" => parser .parse_fn_args_body( // no decorator in an object literal - vec![], + Vec::new(), start, |p| { let params = p.parse_formal_params()?; @@ -324,7 +324,7 @@ impl ParseObject> for Parser { parser .parse_fn_args_body( // no decorator in an object literal - vec![], + Vec::new(), start, |p| { let params = p.parse_formal_params()?; @@ -392,7 +392,7 @@ impl ParseObject> for Parser { "async" => parser .parse_fn_args_body( // no decorator in an object literal - vec![], + Vec::new(), start, |p| p.parse_unique_formal_params(), true, diff --git a/crates/swc_ecma_parser/src/parser/pat.rs b/crates/swc_ecma_parser/src/parser/pat.rs index 90b411a48c44..6bf56159b740 100644 --- a/crates/swc_ecma_parser/src/parser/pat.rs +++ b/crates/swc_ecma_parser/src/parser/pat.rs @@ -1,6 +1,7 @@ //! 13.3.3 Destructuring Binding Patterns use std::iter; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_common::Spanned; use super::{util::ExprExt, *}; @@ -92,7 +93,7 @@ impl Parser { assert_and_bump!(self, '['); - let mut elems = vec![]; + let mut elems = Vec::new(); let mut comma = 0; let mut rest_span = Span::default(); @@ -282,7 +283,7 @@ impl Parser { } pub(super) fn parse_constructor_params(&mut self) -> PResult> { - let mut params = vec![]; + let mut params = Vec::new(); let mut rest_span = Span::default(); while !eof!(self) && !is!(self, ')') { @@ -397,7 +398,7 @@ impl Parser { } pub(super) fn parse_formal_params(&mut self) -> PResult> { - let mut params = vec![]; + let mut params = Vec::new(); let mut rest_span = Span::default(); while !eof!(self) && !is!(self, ')') { @@ -707,7 +708,7 @@ impl Parser { if exprs.is_empty() { return Ok(ArrayPat { span, - elems: vec![], + elems: Vec::new(), optional: false, type_ann: None, } @@ -819,7 +820,7 @@ impl Parser { let len = exprs.len(); if len == 0 { - return Ok(vec![]); + return Ok(Vec::new()); } let mut params = Vec::with_capacity(len); diff --git a/crates/swc_ecma_parser/src/parser/stmt.rs b/crates/swc_ecma_parser/src/parser/stmt.rs index 63ab239de0df..18c352bd68c7 100644 --- a/crates/swc_ecma_parser/src/parser/stmt.rs +++ b/crates/swc_ecma_parser/src/parser/stmt.rs @@ -1,3 +1,4 @@ +use swc_allocator::{boxed::Box, vec::Vec}; use swc_common::Spanned; use typed_arena::Arena; @@ -631,7 +632,7 @@ impl<'a, I: Tokens> Parser { let discriminant = self.include_in_expr(true).parse_expr()?; expect!(self, ')'); - let mut cases = vec![]; + let mut cases = Vec::new(); let mut span_of_previous_default = None; expect!(self, '{'); @@ -642,7 +643,7 @@ impl<'a, I: Tokens> Parser { self.with_ctx(ctx).parse_with(|p| { while is_one_of!(p, "case", "default") { - let mut cons = vec![]; + let mut cons = Vec::new(); let is_case = is!(p, "case"); let case_start = cur_pos!(p); bump!(p); @@ -808,7 +809,7 @@ impl<'a, I: Tokens> Parser { assert_and_bump!(self, "using"); - let mut decls = vec![]; + let mut decls = Vec::new(); let mut first = true; while first || eat!(self, ',') { if first { @@ -894,7 +895,7 @@ impl<'a, I: Tokens> Parser { span: span!(self, start), kind, declare: false, - decls: vec![], + decls: Vec::new(), ..Default::default() })); } @@ -903,7 +904,7 @@ impl<'a, I: Tokens> Parser { } } - let mut decls = vec![]; + let mut decls = Vec::new(); let mut first = true; while first || eat!(self, ',') { if first { @@ -1138,7 +1139,7 @@ impl<'a, I: Tokens> Parser { self.with_ctx(ctx).parse_with(|p| { let start = l.span.lo(); - let mut errors = vec![]; + let mut errors = Vec::new(); for lb in &p.state.labels { if l.sym == *lb { errors.push(Error::new( @@ -1150,7 +1151,7 @@ impl<'a, I: Tokens> Parser { p.state.labels.push(l.sym.clone()); let body = Box::new(if is!(p, "function") { - let f = p.parse_fn_decl(vec![])?; + let f = p.parse_fn_decl(Vec::new())?; if let Decl::Fn(FnDecl { function, .. }) = &f { if p.ctx().strict { p.emit_err(function.span, SyntaxError::LabelledFunctionInStrict) @@ -1667,7 +1668,7 @@ mod tests { } ], super_class: None, - body: vec![], + body: Vec::new(), is_abstract: false, ..Default::default() }), diff --git a/crates/swc_ecma_parser/src/parser/stmt/module_item.rs b/crates/swc_ecma_parser/src/parser/stmt/module_item.rs index 056b55eb8de6..b4d9a9d65a15 100644 --- a/crates/swc_ecma_parser/src/parser/stmt/module_item.rs +++ b/crates/swc_ecma_parser/src/parser/stmt/module_item.rs @@ -1,3 +1,5 @@ +use swc_allocator::{boxed::Box, vec::Vec}; + use super::*; impl Parser { @@ -69,7 +71,7 @@ impl Parser { return Ok(ImportDecl { span: span!(self, start), src, - specifiers: vec![], + specifiers: Vec::new(), type_only: false, with, phase: Default::default(), @@ -79,7 +81,7 @@ impl Parser { let mut type_only = false; let mut phase = ImportPhase::Evaluation; - let mut specifiers = vec![]; + let mut specifiers = Vec::new(); 'import_maybe_ident: { if is!(self, BindingIdent) { @@ -611,7 +613,7 @@ impl Parser { .into()); } - let mut specifiers = vec![]; + let mut specifiers = Vec::new(); let mut has_default = false; let mut has_ns = false; diff --git a/crates/swc_ecma_parser/src/parser/tests.rs b/crates/swc_ecma_parser/src/parser/tests.rs index 99de14b1a79d..c5fa4d73c8b4 100644 --- a/crates/swc_ecma_parser/src/parser/tests.rs +++ b/crates/swc_ecma_parser/src/parser/tests.rs @@ -24,7 +24,7 @@ fn assert_module_error(src: &'static str) -> Module { let program = p.parse_program()?; let errors = p.take_errors(); - assert_ne!(errors, vec![]); + assert_ne!(errors, Vec::new()); let module = program.expect_module(); @@ -274,8 +274,8 @@ fn issue_2853_1() { let program = p.parse_program()?; let errors = p.take_errors(); - assert_eq!(errors, vec![]); - assert_eq!(errors, vec![]); + assert_eq!(errors, Vec::new()); + assert_eq!(errors, Vec::new()); Ok(program) }); @@ -287,7 +287,7 @@ fn issue_2853_2() { let program = p.parse_program()?; let errors = p.take_errors(); - assert_eq!(errors, vec![]); + assert_eq!(errors, Vec::new()); Ok(program) }); diff --git a/crates/swc_ecma_parser/src/parser/typescript.rs b/crates/swc_ecma_parser/src/parser/typescript.rs index 714ed00d1044..cf72f1d1ada4 100644 --- a/crates/swc_ecma_parser/src/parser/typescript.rs +++ b/crates/swc_ecma_parser/src/parser/typescript.rs @@ -1,6 +1,7 @@ use std::fmt::Write; use either::Either; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_atoms::js_word; use swc_common::Spanned; @@ -80,7 +81,7 @@ impl Parser { { debug_assert!(self.input.syntax().typescript()); - let mut buf = vec![]; + let mut buf = Vec::new(); while !self.is_ts_list_terminator(kind)? { // Skipping "parseListElement" from the TS source since that's just for error // handling. @@ -116,7 +117,7 @@ impl Parser { { debug_assert!(self.input.syntax().typescript()); - let mut buf = vec![]; + let mut buf = Vec::new(); loop { trace_cur!(self, parse_ts_delimited_list_inner__element); @@ -1079,7 +1080,7 @@ impl Parser { let extends = if eat!(self, "extends") { self.parse_ts_heritage_clause()? } else { - vec![] + Vec::new() }; // Recover from @@ -1902,7 +1903,7 @@ impl Parser { trace_cur!(self, parse_tpl_elements); - let mut types = vec![]; + let mut types = Vec::new(); let cur_elem = self.parse_tpl_element(false)?; let mut is_tail = cur_elem.tail; @@ -1931,7 +1932,7 @@ impl Parser { debug_assert!(self.input.syntax().typescript()); let params = self.parse_formal_params()?; - let mut list = vec![]; + let mut list = Vec::new(); for param in params { let item = match param.pat { From 05f9996f057d15931403a35472ac8aae978d1ca6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 06:46:54 +0900 Subject: [PATCH 17/46] Fix more --- crates/swc_ecma_ast/src/typescript.rs | 1 + crates/swc_ecma_parser/src/parser/mod.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/crates/swc_ecma_ast/src/typescript.rs b/crates/swc_ecma_ast/src/typescript.rs index 7380b417547a..7373d76cb869 100644 --- a/crates/swc_ecma_ast/src/typescript.rs +++ b/crates/swc_ecma_ast/src/typescript.rs @@ -11,6 +11,7 @@ use serde::{ Deserialize, Deserializer, Serialize, }; use string_enum::StringEnum; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_atoms::Atom; use swc_common::{ast_node, EqIgnoreSpan, Span}; diff --git a/crates/swc_ecma_parser/src/parser/mod.rs b/crates/swc_ecma_parser/src/parser/mod.rs index e699b4c3a58d..a6c4f3bbb189 100644 --- a/crates/swc_ecma_parser/src/parser/mod.rs +++ b/crates/swc_ecma_parser/src/parser/mod.rs @@ -3,6 +3,7 @@ use std::ops::{Deref, DerefMut}; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_atoms::{Atom, JsWord}; use swc_common::{collections::AHashMap, comments::Comments, input::StringInput, BytePos, Span}; use swc_ecma_ast::*; From b74d02c2a3141b44d63eee15c095c4f3485cf3c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 06:50:10 +0900 Subject: [PATCH 18/46] Fix more --- crates/swc_ecma_parser/src/lexer/mod.rs | 2 +- crates/swc_ecma_parser/src/lib.rs | 1 + crates/swc_ecma_parser/src/parser/class_and_fn.rs | 10 +++++----- crates/swc_ecma_parser/src/parser/expr/ops.rs | 2 +- crates/swc_ecma_parser/src/parser/typescript.rs | 4 ++-- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/crates/swc_ecma_parser/src/lexer/mod.rs b/crates/swc_ecma_parser/src/lexer/mod.rs index d303e51e79f1..7a37b549ddee 100644 --- a/crates/swc_ecma_parser/src/lexer/mod.rs +++ b/crates/swc_ecma_parser/src/lexer/mod.rs @@ -70,7 +70,7 @@ impl IntoIterator for Char { CharIter(match char::from_u32(self.0) { Some(c) => smallvec![c], None => { - let mut buf = smallVec::new(); + let mut buf = smallvec![]; let high = self.0 & 0xffff0000 >> 16; diff --git a/crates/swc_ecma_parser/src/lib.rs b/crates/swc_ecma_parser/src/lib.rs index e2ed67dbff1f..95082a0f1488 100644 --- a/crates/swc_ecma_parser/src/lib.rs +++ b/crates/swc_ecma_parser/src/lib.rs @@ -127,6 +127,7 @@ use error::Error; use lexer::Lexer; use serde::{Deserialize, Serialize}; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_common::{comments::Comments, input::SourceFileInput, SourceFile}; use swc_ecma_ast::*; diff --git a/crates/swc_ecma_parser/src/parser/class_and_fn.rs b/crates/swc_ecma_parser/src/parser/class_and_fn.rs index ba44d5913d5f..d3a6d1ea7faa 100644 --- a/crates/swc_ecma_parser/src/parser/class_and_fn.rs +++ b/crates/swc_ecma_parser/src/parser/class_and_fn.rs @@ -231,8 +231,8 @@ impl Parser { } fn parse_super_class(&mut self) -> PResult<(Box, Option>)> { - let super_class = self.parse_lhs_expr()?; - match *super_class { + let super_class = self.parse_lhs_expr()?.unbox(); + match super_class { Expr::TsInstantiation(TsInstantiation { expr, type_args, .. }) => Ok((expr, Some(type_args))), @@ -242,9 +242,9 @@ impl Parser { // may not include `TsExprWithTypeArgs` // but it's a super class with type params, for example, in JSX. if self.syntax().typescript() && is!(self, '<') { - Ok((super_class, self.parse_ts_type_args().map(Some)?)) + Ok((super_class.into(), self.parse_ts_type_args().map(Some)?)) } else { - Ok((super_class, None)) + Ok((super_class.into(), None)) } } } @@ -797,7 +797,7 @@ impl Parser { let type_params = self.try_parse_ts_type_params(false, true)?; if let Some(type_params) = type_params { - for param in type_params.params { + for param in &type_params.params { self.emit_err(param.span(), SyntaxError::TS1092); } } diff --git a/crates/swc_ecma_parser/src/parser/expr/ops.rs b/crates/swc_ecma_parser/src/parser/expr/ops.rs index ee1f5bcbd480..f8a5c971df05 100644 --- a/crates/swc_ecma_parser/src/parser/expr/ops.rs +++ b/crates/swc_ecma_parser/src/parser/expr/ops.rs @@ -1,5 +1,5 @@ //! Parser for unary operations and binary operations. -use swc_allocator::{boxed::Box, vec::Vec}; +use swc_allocator::boxed::Box; use tracing::trace; use super::*; diff --git a/crates/swc_ecma_parser/src/parser/typescript.rs b/crates/swc_ecma_parser/src/parser/typescript.rs index cf72f1d1ada4..d34a728400e6 100644 --- a/crates/swc_ecma_parser/src/parser/typescript.rs +++ b/crates/swc_ecma_parser/src/parser/typescript.rs @@ -844,8 +844,8 @@ impl Parser { let inner = self.parse_ts_module_or_ns_decl(inner_start)?; let inner = TsNamespaceDecl { span: inner.span, - id: match inner.id { - TsModuleName::Ident(i) => i, + id: match &inner.id { + TsModuleName::Ident(i) => i.clone(), _ => unreachable!(), }, body: Box::new(inner.body.unwrap()), From 6d08d0e4db5ca52988871df5f100fccdf26d9f4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 06:51:42 +0900 Subject: [PATCH 19/46] vec![] --- crates/swc_allocator/src/lib.rs | 3 +++ crates/swc_allocator/src/vec/mod.rs | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/crates/swc_allocator/src/lib.rs b/crates/swc_allocator/src/lib.rs index 655493b7d10a..3c634c0b2c9b 100644 --- a/crates/swc_allocator/src/lib.rs +++ b/crates/swc_allocator/src/lib.rs @@ -31,6 +31,9 @@ #![allow(clippy::derivable_impls)] #![feature(fundamental, fn_ptr_trait, with_negative_coherence)] +/// NOT A PUBLIC API. +#[doc(hidden)] +pub extern crate allocator_api2; pub use crate::alloc::Allocator; mod alloc; diff --git a/crates/swc_allocator/src/vec/mod.rs b/crates/swc_allocator/src/vec/mod.rs index 7569af262f9a..303c2e7e0ce7 100644 --- a/crates/swc_allocator/src/vec/mod.rs +++ b/crates/swc_allocator/src/vec/mod.rs @@ -10,6 +10,14 @@ mod rkyv; use crate::{boxed::Box, FastAlloc}; +/// Creates a `Vec` from a list of elements. +#[macro_export] +macro_rules! vec { + ($($tt:tt)*) => {{ + $crate::vec::Vec::from($crate::allocator_api2::vec![$($tt)*]) + }}; +} + /// Faster version of [`std::vec::Vec`]. #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[repr(transparent)] From f499a1a1a79a6f076cbb4a0a97f66b78ff057e6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 06:55:06 +0900 Subject: [PATCH 20/46] More work --- crates/swc_ecma_parser/src/error.rs | 11 ++++++----- crates/swc_ecma_parser/src/parser/expr.rs | 2 +- crates/swc_ecma_parser/src/parser/typescript.rs | 8 ++++---- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/crates/swc_ecma_parser/src/error.rs b/crates/swc_ecma_parser/src/error.rs index 37ebffe5b5ea..04b28a036b85 100644 --- a/crates/swc_ecma_parser/src/error.rs +++ b/crates/swc_ecma_parser/src/error.rs @@ -2,6 +2,7 @@ use std::{borrow::Cow, fmt::Debug}; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_atoms::JsWord; use swc_common::{ errors::{DiagnosticBuilder, Handler}, @@ -35,7 +36,7 @@ impl Error { } pub fn into_kind(self) -> SyntaxError { - self.error.1 + self.error.unbox().1 } } @@ -765,7 +766,7 @@ impl SyntaxError { impl Error { #[cold] #[inline(never)] - pub fn into_diagnostic(self, handler: &Handler) -> DiagnosticBuilder { + pub fn into_diagnostic<'a>(&self, handler: &'a Handler) -> DiagnosticBuilder<'a> { if let SyntaxError::WithLabel { inner, note, span } = self.error.1 { let mut db = inner.into_diagnostic(handler); db.span_label(span, note); @@ -774,7 +775,7 @@ impl Error { let span = self.span(); - let kind = self.into_kind(); + let kind = self.kind(); let msg = kind.msg(); let mut db = handler.struct_span_err(span, &msg); @@ -782,12 +783,12 @@ impl Error { match kind { SyntaxError::ExpectedSemiForExprStmt { expr } => { db.span_label( - expr, + *expr, "This is the expression part of an expression statement", ); } SyntaxError::MultipleDefault { previous } => { - db.span_label(previous, "previous default case is declared at here"); + db.span_label(*previous, "previous default case is declared at here"); } _ => {} } diff --git a/crates/swc_ecma_parser/src/parser/expr.rs b/crates/swc_ecma_parser/src/parser/expr.rs index a17bcbb03863..f3a92b599541 100644 --- a/crates/swc_ecma_parser/src/parser/expr.rs +++ b/crates/swc_ecma_parser/src/parser/expr.rs @@ -1,5 +1,5 @@ use either::Either; -use swc_allocator::{boxed::Box, vec::Vec}; +use swc_allocator::{boxed::Box, vec, vec::Vec}; use swc_common::{ast_node, util::take::Take, Spanned}; use super::{pat::PatType, util::ExprExt, *}; diff --git a/crates/swc_ecma_parser/src/parser/typescript.rs b/crates/swc_ecma_parser/src/parser/typescript.rs index d34a728400e6..5dbb7baf5ea5 100644 --- a/crates/swc_ecma_parser/src/parser/typescript.rs +++ b/crates/swc_ecma_parser/src/parser/typescript.rs @@ -1,7 +1,7 @@ use std::fmt::Write; use either::Either; -use swc_allocator::{boxed::Box, vec::Vec}; +use swc_allocator::{boxed::Box, vec, vec::Vec}; use swc_atoms::js_word; use swc_common::Spanned; @@ -841,11 +841,11 @@ impl Parser { let id = self.parse_ident_name()?; let body: TsNamespaceBody = if eat!(self, '.') { let inner_start = cur_pos!(self); - let inner = self.parse_ts_module_or_ns_decl(inner_start)?; + let inner = self.parse_ts_module_or_ns_decl(inner_start)?.unbox(); let inner = TsNamespaceDecl { span: inner.span, - id: match &inner.id { - TsModuleName::Ident(i) => i.clone(), + id: match inner.id { + TsModuleName::Ident(i) => i, _ => unreachable!(), }, body: Box::new(inner.body.unwrap()), From 212a87d397b83c12775005116f53ab77e2740a99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 06:56:18 +0900 Subject: [PATCH 21/46] More work --- crates/swc_ecma_parser/src/lexer/jsx.rs | 1 + crates/swc_ecma_parser/src/parser/pat.rs | 2 +- crates/swc_ecma_parser/src/parser/stmt.rs | 8 ++++---- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/crates/swc_ecma_parser/src/lexer/jsx.rs b/crates/swc_ecma_parser/src/lexer/jsx.rs index 117297fa09bb..b5c3dd37db39 100644 --- a/crates/swc_ecma_parser/src/lexer/jsx.rs +++ b/crates/swc_ecma_parser/src/lexer/jsx.rs @@ -1,5 +1,6 @@ use either::Either; use smartstring::{LazyCompact, SmartString}; +use swc_allocator::vec; use super::*; diff --git a/crates/swc_ecma_parser/src/parser/pat.rs b/crates/swc_ecma_parser/src/parser/pat.rs index 6bf56159b740..f4f963d2f75f 100644 --- a/crates/swc_ecma_parser/src/parser/pat.rs +++ b/crates/swc_ecma_parser/src/parser/pat.rs @@ -1,7 +1,7 @@ //! 13.3.3 Destructuring Binding Patterns use std::iter; -use swc_allocator::{boxed::Box, vec::Vec}; +use swc_allocator::{boxed::Box, vec, vec::Vec}; use swc_common::Spanned; use super::{util::ExprExt, *}; diff --git a/crates/swc_ecma_parser/src/parser/stmt.rs b/crates/swc_ecma_parser/src/parser/stmt.rs index 18c352bd68c7..f21919f15f2d 100644 --- a/crates/swc_ecma_parser/src/parser/stmt.rs +++ b/crates/swc_ecma_parser/src/parser/stmt.rs @@ -1,4 +1,4 @@ -use swc_allocator::{boxed::Box, vec::Vec}; +use swc_allocator::{boxed::Box, vec, vec::Vec}; use swc_common::Spanned; use typed_arena::Arena; @@ -26,7 +26,7 @@ impl<'a, I: Tokens> Parser { let old_ctx = self.ctx(); - let stmts = Arena::new(); + let stmts = vec![]; while { if self.input.cur().is_none() && end.is_some() { let eof_text = self.input.dump_cur(); @@ -60,7 +60,7 @@ impl<'a, I: Tokens> Parser { } } - stmts.alloc(stmt); + stmts.push(stmt); } if self.input.cur().is_some() && end.is_some() { @@ -69,7 +69,7 @@ impl<'a, I: Tokens> Parser { self.set_ctx(old_ctx); - Ok(stmts.into_vec()) + Ok(stmts) } /// Parse a statement but not a declaration. From a0449c39a0aa6380f3e0b28c7740a315e01eb817 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 06:57:48 +0900 Subject: [PATCH 22/46] More work --- crates/swc_ecma_parser/src/error.rs | 4 ++-- crates/swc_ecma_parser/src/parser/expr.rs | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/crates/swc_ecma_parser/src/error.rs b/crates/swc_ecma_parser/src/error.rs index 04b28a036b85..5f412d9f93aa 100644 --- a/crates/swc_ecma_parser/src/error.rs +++ b/crates/swc_ecma_parser/src/error.rs @@ -767,9 +767,9 @@ impl Error { #[cold] #[inline(never)] pub fn into_diagnostic<'a>(&self, handler: &'a Handler) -> DiagnosticBuilder<'a> { - if let SyntaxError::WithLabel { inner, note, span } = self.error.1 { + if let SyntaxError::WithLabel { inner, note, span } = &self.error.1 { let mut db = inner.into_diagnostic(handler); - db.span_label(span, note); + db.span_label(*span, *note); return db; } diff --git a/crates/swc_ecma_parser/src/parser/expr.rs b/crates/swc_ecma_parser/src/parser/expr.rs index f3a92b599541..3bdecfc84f66 100644 --- a/crates/swc_ecma_parser/src/parser/expr.rs +++ b/crates/swc_ecma_parser/src/parser/expr.rs @@ -1643,7 +1643,9 @@ impl Parser { None }; - if let Expr::New(ne @ NewExpr { args: None, .. }) = *callee { + let callee = callee.unbox(); + + if let Expr::New(ne @ NewExpr { args: None, .. }) = callee { // If this is parsed using 'NewExpression' rule, just return it. // Because it's not left-recursive. if type_args.is_some() { @@ -1671,7 +1673,7 @@ impl Parser { }), true, ), - _ => (Callee::Expr(callee), false), + _ => (Callee::Expr(callee.into()), false), }; let args = self.parse_args(is_import)?; @@ -1708,7 +1710,7 @@ impl Parser { // This is parsed using production 'NewExpression', which contains // 'MemberExpression' - Ok(callee) + Ok(callee.into()) } pub(super) fn parse_for_head_prefix(&mut self) -> PResult> { From 3b3041ee5fbe64a0d62625429906c4a1945ae668 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 06:58:42 +0900 Subject: [PATCH 23/46] More work --- crates/swc_ecma_parser/src/lexer/mod.rs | 1 + crates/swc_ecma_parser/src/lexer/number.rs | 1 + crates/swc_ecma_parser/src/lexer/state.rs | 1 + crates/swc_ecma_parser/src/parser/stmt.rs | 1 - crates/swc_ecma_parser/src/token.rs | 1 + 5 files changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/swc_ecma_parser/src/lexer/mod.rs b/crates/swc_ecma_parser/src/lexer/mod.rs index 7a37b549ddee..c0e80c2e3552 100644 --- a/crates/swc_ecma_parser/src/lexer/mod.rs +++ b/crates/swc_ecma_parser/src/lexer/mod.rs @@ -4,6 +4,7 @@ use std::{cell::RefCell, char, iter::FusedIterator, mem::transmute, rc::Rc}; use either::Either::{Left, Right}; use smallvec::{smallvec, SmallVec}; +use swc_allocator::{vec, vec::Vec}; use swc_atoms::{Atom, AtomStoreCell}; use swc_common::{comments::Comments, input::StringInput, BytePos, Span}; use swc_ecma_ast::{op, AssignOp, EsVersion, Ident}; diff --git a/crates/swc_ecma_parser/src/lexer/number.rs b/crates/swc_ecma_parser/src/lexer/number.rs index c35a837ebfbb..6a105a1d5f9b 100644 --- a/crates/swc_ecma_parser/src/lexer/number.rs +++ b/crates/swc_ecma_parser/src/lexer/number.rs @@ -7,6 +7,7 @@ use std::borrow::Cow; use either::Either; use num_bigint::BigInt as BigIntValue; use num_traits::{Num as NumTrait, ToPrimitive}; +use swc_allocator::boxed::Box; use tracing::trace; use super::*; diff --git a/crates/swc_ecma_parser/src/lexer/state.rs b/crates/swc_ecma_parser/src/lexer/state.rs index bb07f699bcbb..9dcf248c79a3 100644 --- a/crates/swc_ecma_parser/src/lexer/state.rs +++ b/crates/swc_ecma_parser/src/lexer/state.rs @@ -1,6 +1,7 @@ use std::mem::take; use smallvec::{smallvec, SmallVec}; +use swc_allocator::{boxed::Box, vec, vec::Vec}; use swc_common::{BytePos, Span}; use tracing::trace; diff --git a/crates/swc_ecma_parser/src/parser/stmt.rs b/crates/swc_ecma_parser/src/parser/stmt.rs index f21919f15f2d..1edf3010c312 100644 --- a/crates/swc_ecma_parser/src/parser/stmt.rs +++ b/crates/swc_ecma_parser/src/parser/stmt.rs @@ -1,6 +1,5 @@ use swc_allocator::{boxed::Box, vec, vec::Vec}; use swc_common::Spanned; -use typed_arena::Arena; use super::{pat::PatType, *}; use crate::error::SyntaxError; diff --git a/crates/swc_ecma_parser/src/token.rs b/crates/swc_ecma_parser/src/token.rs index 87426de9e4c9..0a199ca064b3 100644 --- a/crates/swc_ecma_parser/src/token.rs +++ b/crates/swc_ecma_parser/src/token.rs @@ -7,6 +7,7 @@ use std::{ }; use num_bigint::BigInt as BigIntValue; +use swc_allocator::boxed::Box; use swc_atoms::{atom, Atom, AtomStore}; use swc_common::{Span, Spanned}; use swc_ecma_ast::{AssignOp, BinaryOp}; From fdf2f1e4d25727cb3f542b7c8ddf3048ff3d449f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 07:00:52 +0900 Subject: [PATCH 24/46] More work --- crates/swc_ecma_parser/src/lexer/state.rs | 2 +- crates/swc_ecma_parser/src/parser/input.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/swc_ecma_parser/src/lexer/state.rs b/crates/swc_ecma_parser/src/lexer/state.rs index 9dcf248c79a3..4821ab2630c1 100644 --- a/crates/swc_ecma_parser/src/lexer/state.rs +++ b/crates/swc_ecma_parser/src/lexer/state.rs @@ -1,7 +1,7 @@ use std::mem::take; use smallvec::{smallvec, SmallVec}; -use swc_allocator::{boxed::Box, vec, vec::Vec}; +use swc_allocator::vec::Vec; use swc_common::{BytePos, Span}; use tracing::trace; diff --git a/crates/swc_ecma_parser/src/parser/input.rs b/crates/swc_ecma_parser/src/parser/input.rs index ffc96372ef42..8d5d46c16b34 100644 --- a/crates/swc_ecma_parser/src/parser/input.rs +++ b/crates/swc_ecma_parser/src/parser/input.rs @@ -2,6 +2,7 @@ use std::{cell::RefCell, mem, mem::take, rc::Rc}; use debug_unreachable::debug_unreachable; use lexer::TokenContexts; +use swc_allocator::vec::Vec; use swc_common::{BytePos, Span}; use super::Parser; From 6ee8b2649d7ae9ca804b084a8b0d0886e0ef1d87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 07:02:18 +0900 Subject: [PATCH 25/46] More work --- crates/swc_ecma_parser/src/parser/typescript.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/crates/swc_ecma_parser/src/parser/typescript.rs b/crates/swc_ecma_parser/src/parser/typescript.rs index 5dbb7baf5ea5..567fb1502acb 100644 --- a/crates/swc_ecma_parser/src/parser/typescript.rs +++ b/crates/swc_ecma_parser/src/parser/typescript.rs @@ -1036,8 +1036,9 @@ impl Parser { ) { self.emit_err(span!(self, start), SyntaxError::TS2499); } + let expr = expr.unbox(); - match *expr { + match expr { Expr::TsInstantiation(v) => Ok(TsExprWithTypeArgs { span: v.span, expr: v.expr, @@ -1052,7 +1053,7 @@ impl Parser { Ok(TsExprWithTypeArgs { span: span!(self, start), - expr, + expr: Box::new(expr), type_args, }) } @@ -2135,7 +2136,7 @@ impl Parser { TsLit::BigInt(BigInt { span, - value: Box::new(-*value), + value: Box::new(-value.unbox()), raw: Some(new_raw.into()), }) } @@ -2417,7 +2418,7 @@ impl Parser { lo: declare_start, ..c.class.span }, - ..*c.class + ..c.class.unbox() }), ..c } @@ -2440,7 +2441,7 @@ impl Parser { lo: declare_start, ..decl.span }, - ..*decl + ..decl.unbox() }) .map(Box::new) .map(From::from) @@ -2455,7 +2456,7 @@ impl Parser { lo: declare_start, ..decl.span }, - ..*decl + ..decl.unbox() }) .map(Box::new) .map(From::from) @@ -2824,6 +2825,7 @@ fn make_decl_declare(mut decl: Decl) -> Decl { #[cfg(test)] mod tests { + use swc_allocator::{boxed::Box, vec, vec::Vec}; use swc_common::DUMMY_SP; use swc_ecma_ast::*; use swc_ecma_visit::assert_eq_ignore_span; From ad831af9c64942a9fe45dcb40640395eea7b44a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 07:05:40 +0900 Subject: [PATCH 26/46] More work --- crates/swc_ecma_ast/src/pat.rs | 2 +- crates/swc_ecma_parser/src/parser/object.rs | 5 +++-- crates/swc_ecma_parser/src/parser/pat.rs | 2 +- crates/swc_ecma_parser/src/parser/stmt.rs | 6 +++--- crates/swc_ecma_parser/src/parser/stmt/module_item.rs | 4 ++-- crates/swc_ecma_parser/src/parser/typescript.rs | 2 +- 6 files changed, 11 insertions(+), 10 deletions(-) diff --git a/crates/swc_ecma_ast/src/pat.rs b/crates/swc_ecma_ast/src/pat.rs index 505702fda7e2..0fcf2d74c188 100644 --- a/crates/swc_ecma_ast/src/pat.rs +++ b/crates/swc_ecma_ast/src/pat.rs @@ -1,5 +1,5 @@ use is_macro::Is; -use swc_allocator::boxed::Box; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP}; use crate::{ diff --git a/crates/swc_ecma_parser/src/parser/object.rs b/crates/swc_ecma_parser/src/parser/object.rs index c3da76d2f8fe..5e817739b0f3 100644 --- a/crates/swc_ecma_parser/src/parser/object.rs +++ b/crates/swc_ecma_parser/src/parser/object.rs @@ -1,5 +1,6 @@ //! Parser for object literal. +use swc_allocator::{boxed::Box, vec, vec::Vec}; use swc_common::{Spanned, DUMMY_SP}; use super::*; @@ -301,7 +302,7 @@ impl ParseObject> for Parser { false, false, ) - .map(|v| *v) + .map(|v| v.unbox()) .map( |Function { body, return_type, .. @@ -353,7 +354,7 @@ impl ParseObject> for Parser { false, false, ) - .map(|v| *v) + .map(|v| v.unbox()) .map( |Function { mut params, body, .. diff --git a/crates/swc_ecma_parser/src/parser/pat.rs b/crates/swc_ecma_parser/src/parser/pat.rs index f4f963d2f75f..6bf56159b740 100644 --- a/crates/swc_ecma_parser/src/parser/pat.rs +++ b/crates/swc_ecma_parser/src/parser/pat.rs @@ -1,7 +1,7 @@ //! 13.3.3 Destructuring Binding Patterns use std::iter; -use swc_allocator::{boxed::Box, vec, vec::Vec}; +use swc_allocator::{boxed::Box, vec::Vec}; use swc_common::Spanned; use super::{util::ExprExt, *}; diff --git a/crates/swc_ecma_parser/src/parser/stmt.rs b/crates/swc_ecma_parser/src/parser/stmt.rs index 1edf3010c312..42f36d0e05ff 100644 --- a/crates/swc_ecma_parser/src/parser/stmt.rs +++ b/crates/swc_ecma_parser/src/parser/stmt.rs @@ -390,16 +390,16 @@ impl<'a, I: Tokens> Parser { // simply start parsing an expression, and afterwards, if the // next token is a colon and the expression was a simple // Identifier node, we switch to interpreting it as a label. - let expr = self.include_in_expr(true).parse_expr()?; + let expr = self.include_in_expr(true).parse_expr()?.unbox(); - let expr = match *expr { + let expr = match expr { Expr::Ident(ident) => { if eat!(self, ':') { return self.parse_labelled_stmt(ident); } ident.into() } - _ => self.verify_expr(expr)?, + _ => self.verify_expr(expr.into())?, }; if let Expr::Ident(ref ident) = *expr { if &*ident.sym == "interface" && self.input.had_line_break_before_cur() { diff --git a/crates/swc_ecma_parser/src/parser/stmt/module_item.rs b/crates/swc_ecma_parser/src/parser/stmt/module_item.rs index b4d9a9d65a15..503c55f05503 100644 --- a/crates/swc_ecma_parser/src/parser/stmt/module_item.rs +++ b/crates/swc_ecma_parser/src/parser/stmt/module_item.rs @@ -60,7 +60,7 @@ impl Parser { && !self.input.had_line_break_before_cur() && (eat!(self, "assert") || eat!(self, "with")) { - match *self.parse_object::>()? { + match self.parse_object::>()?.unbox() { Expr::Object(v) => Some(Box::new(v)), _ => unreachable!(), } @@ -185,7 +185,7 @@ impl Parser { && !self.input.had_line_break_before_cur() && (eat!(self, "assert") || eat!(self, "with")) { - match *self.parse_object::>()? { + match self.parse_object::>()?.unbox() { Expr::Object(v) => Some(Box::new(v)), _ => unreachable!(), } diff --git a/crates/swc_ecma_parser/src/parser/typescript.rs b/crates/swc_ecma_parser/src/parser/typescript.rs index 567fb1502acb..c8432332bb33 100644 --- a/crates/swc_ecma_parser/src/parser/typescript.rs +++ b/crates/swc_ecma_parser/src/parser/typescript.rs @@ -2397,7 +2397,7 @@ impl Parser { lo: declare_start, ..f.function.span }, - ..*f.function + ..f.function.unbox() }), ..f } From ca893808ec002849c6b2bca655305ea36d0392df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 07:07:57 +0900 Subject: [PATCH 27/46] More work --- crates/swc_ecma_parser/src/parser/pat.rs | 14 ++++++++------ .../swc_ecma_parser/src/parser/stmt/module_item.rs | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/crates/swc_ecma_parser/src/parser/pat.rs b/crates/swc_ecma_parser/src/parser/pat.rs index 6bf56159b740..8e758e98e0f7 100644 --- a/crates/swc_ecma_parser/src/parser/pat.rs +++ b/crates/swc_ecma_parser/src/parser/pat.rs @@ -536,7 +536,7 @@ impl Parser { // be reparsed as an AssignmentPattern. } - _ => match *expr { + _ => match &*expr { // It is a Syntax Error if the LeftHandSideExpression is // CoverParenthesizedExpressionAndArrowParameterList:(Expression) and // Expression derives a phrase that would produce a Syntax Error according @@ -545,7 +545,7 @@ impl Parser { Expr::Paren(..) => { return Ok(expr.into()); } - Expr::Ident(i) => return Ok(i.into()), + Expr::Ident(i) => return Ok(i.clone().into()), _ => { return Ok(expr.into()); } @@ -576,8 +576,9 @@ impl Parser { if !expr.is_valid_simple_assignment_target(self.ctx().strict) { self.emit_err(span, SyntaxError::NotSimpleAssign) } - match *expr { - Expr::Ident(i) => return Ok(i.into()), + + match &*expr { + Expr::Ident(i) => return Ok(i.clone().into()), _ => { return Ok(expr.into()); } @@ -591,7 +592,8 @@ impl Parser { } } - match *expr { + let expr = expr.unbox(); + match expr { Expr::Paren(..) => { self.emit_err(span, SyntaxError::InvalidPat); Ok(Invalid { span }.into()) @@ -631,7 +633,7 @@ impl Parser { .map(|(idx, prop)| { let span = prop.span(); match prop { - PropOrSpread::Prop(prop) => match *prop { + PropOrSpread::Prop(prop) => match prop.unbox() { Prop::Shorthand(id) => { Ok(ObjectPatProp::Assign(AssignPatProp { span: id.span(), diff --git a/crates/swc_ecma_parser/src/parser/stmt/module_item.rs b/crates/swc_ecma_parser/src/parser/stmt/module_item.rs index 503c55f05503..59d6e33f1314 100644 --- a/crates/swc_ecma_parser/src/parser/stmt/module_item.rs +++ b/crates/swc_ecma_parser/src/parser/stmt/module_item.rs @@ -860,7 +860,7 @@ impl Parser { && !self.input.had_line_break_before_cur() && (eat!(self, "assert") || eat!(self, "with")) { - match *self.parse_object::>()? { + match self.parse_object::>()?.unbox() { Expr::Object(v) => Some(Box::new(v)), _ => unreachable!(), } From 43c7e0fd85c3d739d121889f1c6f786fe9c1b5fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 07:11:51 +0900 Subject: [PATCH 28/46] fix `vec!` --- crates/swc_allocator/src/vec/mod.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/swc_allocator/src/vec/mod.rs b/crates/swc_allocator/src/vec/mod.rs index 303c2e7e0ce7..094a8cfc7918 100644 --- a/crates/swc_allocator/src/vec/mod.rs +++ b/crates/swc_allocator/src/vec/mod.rs @@ -14,7 +14,7 @@ use crate::{boxed::Box, FastAlloc}; #[macro_export] macro_rules! vec { ($($tt:tt)*) => {{ - $crate::vec::Vec::from($crate::allocator_api2::vec![$($tt)*]) + $crate::vec::Vec::from($crate::allocator_api2::vec![in $crate::FastAlloc::default(), $($tt)*]) }}; } @@ -27,6 +27,12 @@ macro_rules! vec { )] pub struct Vec(allocator_api2::vec::Vec); +impl From> for Vec { + fn from(v: allocator_api2::vec::Vec) -> Self { + Self(v) + } +} + impl Vec { /// Constructs a new, empty `Vec`. /// From 9331e1b735d1869e1d052263765ca6b1cbcc45eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 07:12:58 +0900 Subject: [PATCH 29/46] fix `vec!` --- crates/swc_allocator/src/vec/mod.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/swc_allocator/src/vec/mod.rs b/crates/swc_allocator/src/vec/mod.rs index 094a8cfc7918..499bcfe7cb83 100644 --- a/crates/swc_allocator/src/vec/mod.rs +++ b/crates/swc_allocator/src/vec/mod.rs @@ -13,8 +13,12 @@ use crate::{boxed::Box, FastAlloc}; /// Creates a `Vec` from a list of elements. #[macro_export] macro_rules! vec { + () => {{ + $crate::vec::Vec::new() + }}; + ($($tt:tt)*) => {{ - $crate::vec::Vec::from($crate::allocator_api2::vec![in $crate::FastAlloc::default(), $($tt)*]) + $crate::vec::Vec::from($crate::allocator_api2::vec![in $crate::FastAlloc::default(); $($tt)*]) }}; } From 01046c48592c43e0a2bcea046c74c78ae96d5231 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 07:18:17 +0900 Subject: [PATCH 30/46] Fix macro --- crates/swc_allocator/src/lib.rs | 12 ++++++++++++ crates/swc_allocator/src/vec/mod.rs | 12 ------------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/crates/swc_allocator/src/lib.rs b/crates/swc_allocator/src/lib.rs index 3c634c0b2c9b..cd24343622c8 100644 --- a/crates/swc_allocator/src/lib.rs +++ b/crates/swc_allocator/src/lib.rs @@ -76,4 +76,16 @@ impl FastAlloc { alloc: None, } } +/// Creates a `Vec` from a list of elements. +#[macro_export] +macro_rules! vec { + () => {{ + $crate::vec::Vec::new() + }}; + + ($($tt:tt)*) => {{ + let alloc= $crate::FastAlloc::default(); + let v: $crate::allocator_api2::vec::Vec<_, $crate::FastAlloc> = $crate::allocator_api2::vec![in alloc; $($tt)*]; + $crate::vec::Vec::from(v) + }}; } diff --git a/crates/swc_allocator/src/vec/mod.rs b/crates/swc_allocator/src/vec/mod.rs index 499bcfe7cb83..08247e92fbbe 100644 --- a/crates/swc_allocator/src/vec/mod.rs +++ b/crates/swc_allocator/src/vec/mod.rs @@ -10,18 +10,6 @@ mod rkyv; use crate::{boxed::Box, FastAlloc}; -/// Creates a `Vec` from a list of elements. -#[macro_export] -macro_rules! vec { - () => {{ - $crate::vec::Vec::new() - }}; - - ($($tt:tt)*) => {{ - $crate::vec::Vec::from($crate::allocator_api2::vec![in $crate::FastAlloc::default(); $($tt)*]) - }}; -} - /// Faster version of [`std::vec::Vec`]. #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[repr(transparent)] From 555ddcef933954e4629df9f7b0ecc597ba04fe92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 07:26:04 +0900 Subject: [PATCH 31/46] Fix allocator --- crates/swc_allocator/src/boxed/mod.rs | 9 ++++++++- crates/swc_allocator/src/lib.rs | 22 ++++++++++++++-------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/crates/swc_allocator/src/boxed/mod.rs b/crates/swc_allocator/src/boxed/mod.rs index 7573d2eb9de4..aae51da5baae 100644 --- a/crates/swc_allocator/src/boxed/mod.rs +++ b/crates/swc_allocator/src/boxed/mod.rs @@ -9,7 +9,7 @@ use std::{ pin::Pin, }; -use crate::FastAlloc; +use crate::{vec::Vec, FastAlloc}; #[cfg(feature = "rkyv")] mod rkyv; @@ -649,3 +649,10 @@ where self.0.len() } } + +impl Box<[T]> { + /// Converts a boxed slice into a `Vec`. + pub fn into_vec(self) -> Vec { + allocator_api2::boxed::Box::<[T], FastAlloc>::into_vec(self.0).into() + } +} diff --git a/crates/swc_allocator/src/lib.rs b/crates/swc_allocator/src/lib.rs index cd24343622c8..ea4c5f3ba643 100644 --- a/crates/swc_allocator/src/lib.rs +++ b/crates/swc_allocator/src/lib.rs @@ -77,15 +77,21 @@ impl FastAlloc { } } /// Creates a `Vec` from a list of elements. +/// Creates a new instance of [crate::vec::Vec] #[macro_export] macro_rules! vec { - () => {{ + () => ( $crate::vec::Vec::new() - }}; - - ($($tt:tt)*) => {{ - let alloc= $crate::FastAlloc::default(); - let v: $crate::allocator_api2::vec::Vec<_, $crate::FastAlloc> = $crate::allocator_api2::vec![in alloc; $($tt)*]; - $crate::vec::Vec::from(v) - }}; + ); + ($elem:expr; $n:expr) => ( + $crate::vec::from_elem_in($elem, $n, $crate::FastAlloc::default()) + ); + ($($x:expr),+ $(,)?) => ( + $crate::boxed::Box::<[_]>::into_vec( + $crate::boxed::Box::slice( + $crate::boxed::Box::new([$($x),+]) + ), + $crate::FastAlloc::default() + ) + ); } From fc63819a3756643568a5c0f029d169e0709fa5dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 07:31:06 +0900 Subject: [PATCH 32/46] Fix allocator --- crates/swc_allocator/src/boxed/mod.rs | 63 +++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/crates/swc_allocator/src/boxed/mod.rs b/crates/swc_allocator/src/boxed/mod.rs index aae51da5baae..d2554adb2021 100644 --- a/crates/swc_allocator/src/boxed/mod.rs +++ b/crates/swc_allocator/src/boxed/mod.rs @@ -135,6 +135,56 @@ impl Box { )) } + /// Constructs a box from a raw pointer in the given allocator. + /// + /// After calling this function, the raw pointer is owned by the + /// resulting `Box`. Specifically, the `Box` destructor will call + /// the destructor of `T` and free the allocated memory. For this + /// to be safe, the memory must have been allocated in accordance + /// with the [memory layout] used by `Box` . + /// + /// # Safety + /// + /// This function is unsafe because improper use may lead to + /// memory problems. For example, a double-free may occur if the + /// function is called twice on the same raw pointer. + /// + /// + /// # Examples + /// + /// Recreate a `Box` which was previously converted to a raw pointer + /// using [`Box::into_raw_with_allocator`]: + /// ``` + /// use std::alloc::System; + /// # use allocator_api2::boxed::Box; + /// + /// let x = Box::new_in(5, System); + /// let (ptr, alloc) = Box::into_raw_with_allocator(x); + /// let x = unsafe { Box::from_raw_in(ptr, alloc) }; + /// ``` + /// Manually create a `Box` from scratch by using the system allocator: + /// ``` + /// use allocator_api2::alloc::{Allocator, Layout, System}; + /// # use allocator_api2::boxed::Box; + /// + /// unsafe { + /// let ptr = System.allocate(Layout::new::())?.as_ptr().cast::(); + /// // In general .write is required to avoid attempting to destruct + /// // the (uninitialized) previous contents of `ptr`, though for this + /// // simple example `*ptr = 5` would have worked as well. + /// ptr.write(5); + /// let x = Box::from_raw_in(ptr, System); + /// } + /// # Ok::<(), allocator_api2::alloc::AllocError>(()) + /// ``` + /// + /// [memory layout]: self#memory-layout + /// [`Layout`]: crate::Layout + #[inline(always)] + pub const unsafe fn from_raw_in(raw: *mut T, alloc: FastAlloc) -> Self { + Box(allocator_api2::boxed::Box::from_raw_in(raw, alloc)) + } + /// Consumes the `Box`, returning a wrapped raw pointer. /// /// The pointer will be properly aligned and non-null. @@ -656,3 +706,16 @@ impl Box<[T]> { allocator_api2::boxed::Box::<[T], FastAlloc>::into_vec(self.0).into() } } + +impl Box<[T; N]> { + /// Converts a boxed slice into a `Vec`. + #[inline(always)] + pub fn slice(b: Self) -> Box<[T]> { + Box(allocator_api2::boxed::Box::slice(b.0)) + } + + /// Converts a boxed slice into a `Vec`. + pub fn into_vec(self) -> Vec { + allocator_api2::boxed::Box::<[T; N], FastAlloc>::into_vec(self.0).into() + } +} From 4651c43817a4037ffa7a2e1aae3a83b60d4e0924 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 07:31:38 +0900 Subject: [PATCH 33/46] fixup --- crates/swc_allocator/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/swc_allocator/src/lib.rs b/crates/swc_allocator/src/lib.rs index ea4c5f3ba643..c6ab5a24d3f1 100644 --- a/crates/swc_allocator/src/lib.rs +++ b/crates/swc_allocator/src/lib.rs @@ -90,8 +90,7 @@ macro_rules! vec { $crate::boxed::Box::<[_]>::into_vec( $crate::boxed::Box::slice( $crate::boxed::Box::new([$($x),+]) - ), - $crate::FastAlloc::default() + ) ) ); } From 4b6cc0d0d0b1cf2ce8ad7d49493fa2a41a8992af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 07:34:01 +0900 Subject: [PATCH 34/46] Fix parser --- crates/swc_ecma_parser/src/lexer/jsx.rs | 2 +- crates/swc_ecma_parser/src/lexer/number.rs | 2 +- crates/swc_ecma_parser/src/lexer/tests.rs | 1 + crates/swc_ecma_parser/src/parser/pat.rs | 60 ++++++++++++---------- crates/swc_ecma_parser/src/parser/stmt.rs | 2 +- crates/swc_ecma_parser/src/parser/tests.rs | 1 + 6 files changed, 39 insertions(+), 29 deletions(-) diff --git a/crates/swc_ecma_parser/src/lexer/jsx.rs b/crates/swc_ecma_parser/src/lexer/jsx.rs index b5c3dd37db39..b7322df16450 100644 --- a/crates/swc_ecma_parser/src/lexer/jsx.rs +++ b/crates/swc_ecma_parser/src/lexer/jsx.rs @@ -73,7 +73,7 @@ impl<'a> Lexer<'a> { self.emit_error( cur_pos, SyntaxError::UnexpectedTokenWithSuggestions { - candidate_list: vec!["`{'>'}`", "`>`"], + candidate_list: swc_allocator::vec!["`{'>'}`", "`>`"], }, ); unsafe { diff --git a/crates/swc_ecma_parser/src/lexer/number.rs b/crates/swc_ecma_parser/src/lexer/number.rs index 6a105a1d5f9b..85d557310043 100644 --- a/crates/swc_ecma_parser/src/lexer/number.rs +++ b/crates/swc_ecma_parser/src/lexer/number.rs @@ -808,7 +808,7 @@ mod tests { assert_eq!(expected, value); } else if let Ok(vec) = vec { assert_ne!( - vec![Token::Num { + swc_allocator::vec![Token::Num { value: expected, raw: expected.to_string().into() }], diff --git a/crates/swc_ecma_parser/src/lexer/tests.rs b/crates/swc_ecma_parser/src/lexer/tests.rs index 613cfccf3b60..4701f77dfcaa 100644 --- a/crates/swc_ecma_parser/src/lexer/tests.rs +++ b/crates/swc_ecma_parser/src/lexer/tests.rs @@ -2,6 +2,7 @@ extern crate test; use std::{ops::Range, str}; +use swc_allocator::boxed::Box; use swc_atoms::Atom; use swc_common::{BytePos, Span}; use swc_ecma_ast::{AssignOp, AssignOp::*}; diff --git a/crates/swc_ecma_parser/src/parser/pat.rs b/crates/swc_ecma_parser/src/parser/pat.rs index 8e758e98e0f7..fb80d566f286 100644 --- a/crates/swc_ecma_parser/src/parser/pat.rs +++ b/crates/swc_ecma_parser/src/parser/pat.rs @@ -633,32 +633,40 @@ impl Parser { .map(|(idx, prop)| { let span = prop.span(); match prop { - PropOrSpread::Prop(prop) => match prop.unbox() { - Prop::Shorthand(id) => { - Ok(ObjectPatProp::Assign(AssignPatProp { - span: id.span(), - key: id.into(), - value: None, - })) - } - Prop::KeyValue(kv_prop) => { - Ok(ObjectPatProp::KeyValue(KeyValuePatProp { - key: kv_prop.key, - value: Box::new(self.reparse_expr_as_pat( - pat_ty.element(), - kv_prop.value, - )?), - })) - } - Prop::Assign(assign_prop) => { - Ok(ObjectPatProp::Assign(AssignPatProp { - span, - key: assign_prop.key.into(), - value: Some(assign_prop.value), - })) + PropOrSpread::Prop(prop) => { + let prop = prop.unbox(); + + match prop { + Prop::Shorthand(id) => { + Ok(ObjectPatProp::Assign(AssignPatProp { + span: id.span(), + key: id.into(), + value: None, + })) + } + Prop::KeyValue(kv_prop) => { + Ok(ObjectPatProp::KeyValue(KeyValuePatProp { + key: kv_prop.key, + value: Box::new(self.reparse_expr_as_pat( + pat_ty.element(), + kv_prop.value, + )?), + })) + } + Prop::Assign(assign_prop) => { + Ok(ObjectPatProp::Assign(AssignPatProp { + span, + key: assign_prop.key.into(), + value: Some(assign_prop.value), + })) + } + _ => syntax_error!( + self, + prop.span(), + SyntaxError::InvalidPat + ), } - _ => syntax_error!(self, prop.span(), SyntaxError::InvalidPat), - }, + } PropOrSpread::Spread(SpreadElement { dot3_token, expr }) => { if idx != len - 1 { @@ -674,7 +682,7 @@ impl Parser { let element_pat_ty = pat_ty.element(); let pat = if let PatType::BindingElement = element_pat_ty { - if let Expr::Ident(i) = *expr { + if let Expr::Ident(i) = expr.unbox() { i.into() } else { self.emit_err(span, SyntaxError::DotsWithoutIdentifier); diff --git a/crates/swc_ecma_parser/src/parser/stmt.rs b/crates/swc_ecma_parser/src/parser/stmt.rs index 42f36d0e05ff..cca28b7f0755 100644 --- a/crates/swc_ecma_parser/src/parser/stmt.rs +++ b/crates/swc_ecma_parser/src/parser/stmt.rs @@ -25,7 +25,7 @@ impl<'a, I: Tokens> Parser { let old_ctx = self.ctx(); - let stmts = vec![]; + let mut stmts = vec![]; while { if self.input.cur().is_none() && end.is_some() { let eof_text = self.input.dump_cur(); diff --git a/crates/swc_ecma_parser/src/parser/tests.rs b/crates/swc_ecma_parser/src/parser/tests.rs index c5fa4d73c8b4..4dd68db2dc3f 100644 --- a/crates/swc_ecma_parser/src/parser/tests.rs +++ b/crates/swc_ecma_parser/src/parser/tests.rs @@ -1,3 +1,4 @@ +use swc_allocator::{vec, vec::Vec}; use swc_common::comments::SingleThreadedComments; use super::*; From 2bc6ace2478c016075d8be49d7955d89ffd30ed5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 07:36:17 +0900 Subject: [PATCH 35/46] Fix for rebase --- crates/swc_allocator/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/swc_allocator/src/lib.rs b/crates/swc_allocator/src/lib.rs index c6ab5a24d3f1..1c94b5cfc6f3 100644 --- a/crates/swc_allocator/src/lib.rs +++ b/crates/swc_allocator/src/lib.rs @@ -76,8 +76,9 @@ impl FastAlloc { alloc: None, } } +} + /// Creates a `Vec` from a list of elements. -/// Creates a new instance of [crate::vec::Vec] #[macro_export] macro_rules! vec { () => ( From f9361fcb908b15497e9511f67fdb5e58b13c9994 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 07:36:52 +0900 Subject: [PATCH 36/46] allocator --- crates/swc_ecma_parser/benches/parser.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/swc_ecma_parser/benches/parser.rs b/crates/swc_ecma_parser/benches/parser.rs index d6c8bbe17e48..25f0ac531173 100644 --- a/crates/swc_ecma_parser/benches/parser.rs +++ b/crates/swc_ecma_parser/benches/parser.rs @@ -1,6 +1,7 @@ extern crate swc_malloc; use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Bencher, Criterion}; +use swc_allocator::Allocator; use swc_common::{comments::SingleThreadedComments, FileName}; use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax, TsSyntax}; @@ -10,6 +11,9 @@ fn bench_module(b: &mut Bencher, syntax: Syntax, src: &'static str) { let fm = cm.new_source_file(FileName::Anon.into(), src.into()); b.iter(|| { + let allocator = Allocator::default(); + let _guard = unsafe { allocator.guard() }; + let _ = black_box({ let lexer = Lexer::new( syntax, From f113358dd714803c00a4f7b434a1689fe0766a19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 07:38:08 +0900 Subject: [PATCH 37/46] Use scoped --- crates/swc_ecma_parser/benches/lexer.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/swc_ecma_parser/benches/lexer.rs b/crates/swc_ecma_parser/benches/lexer.rs index 5f3a9e8f8b48..c3d2e9ad3c03 100644 --- a/crates/swc_ecma_parser/benches/lexer.rs +++ b/crates/swc_ecma_parser/benches/lexer.rs @@ -1,6 +1,7 @@ extern crate swc_malloc; use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Bencher, Criterion}; +use swc_allocator::Allocator; use swc_common::FileName; use swc_ecma_parser::{lexer::Lexer, StringInput, Syntax, TsSyntax}; @@ -9,6 +10,9 @@ fn bench_module(b: &mut Bencher, syntax: Syntax, src: &'static str) { let fm = cm.new_source_file(FileName::Anon.into(), src.into()); b.iter(|| { + let allocator = Allocator::default(); + let _guard = unsafe { allocator.guard() }; + let lexer = Lexer::new(syntax, Default::default(), StringInput::from(&*fm), None); for t in lexer { black_box(t); From ed943de749341bb2d5b917dd5d6c174b9a32961c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 08:36:25 +0900 Subject: [PATCH 38/46] dep --- crates/swc_ecma_parser/Cargo.toml | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/crates/swc_ecma_parser/Cargo.toml b/crates/swc_ecma_parser/Cargo.toml index d3df5618d650..103104327937 100644 --- a/crates/swc_ecma_parser/Cargo.toml +++ b/crates/swc_ecma_parser/Cargo.toml @@ -18,11 +18,13 @@ bench = false [features] # Used for debugging -debug = ["tracing-spans"] -default = ["typescript", "stacker"] +debug = ["tracing-spans"] +default = ["typescript", "stacker"] tracing-spans = [] -typescript = [] -verify = ["swc_ecma_visit"] +typescript = [] +verify = [ + #"swc_ecma_visit" +] [dependencies] either = { workspace = true } @@ -36,11 +38,11 @@ smartstring = { workspace = true } tracing = { workspace = true } typed-arena = { workspace = true } -swc_allocator = { version = "0.1.4", path = "../swc_allocator", default-features = false } -swc_atoms = { version = "0.6.5", path = "../swc_atoms" } -swc_common = { version = "0.35.0", path = "../swc_common" } -swc_ecma_ast = { version = "0.116.0", path = "../swc_ecma_ast" } -swc_ecma_visit = { version = "0.102.0", path = "../swc_ecma_visit", optional = true } +swc_allocator = { version = "0.1.4", path = "../swc_allocator", default-features = false } +swc_atoms = { version = "0.6.5", path = "../swc_atoms" } +swc_common = { version = "0.35.0", path = "../swc_common" } +swc_ecma_ast = { version = "0.116.0", path = "../swc_ecma_ast" } +# swc_ecma_visit = { version = "0.102.0", path = "../swc_ecma_visit", optional = true } [target.'cfg(not(any(target_arch = "wasm32", target_arch = "arm")))'.dependencies] stacker = { version = "0.1.15", optional = true } @@ -55,9 +57,9 @@ codspeed-criterion-compat = { workspace = true } swc_ecma_ast = { version = "0.116.0", path = "../swc_ecma_ast", features = [ "serde-impl", ] } -swc_ecma_visit = { version = "0.102.0", path = "../swc_ecma_visit" } +# swc_ecma_visit = { version = "0.102.0", path = "../swc_ecma_visit" } swc_malloc = { version = "0.5.10", path = "../swc_malloc" } -testing = { version = "0.37.0", path = "../testing" } +testing = { version = "0.37.0", path = "../testing" } [[example]] name = "lexer" From c10b4c2a97b25aaa7ac0e34d1fd0689356900797 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 08:36:31 +0900 Subject: [PATCH 39/46] cargo lockifle --- Cargo.lock | 1 - 1 file changed, 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 86f0aff5e83a..df52681de618 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4480,7 +4480,6 @@ dependencies = [ "swc_atoms", "swc_common", "swc_ecma_ast", - "swc_ecma_visit", "swc_malloc", "testing", "tracing", From 3a64b917ff078d6b93198304b17f0ecf15728ae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 08:38:22 +0900 Subject: [PATCH 40/46] Lifetime to guard --- crates/swc_allocator/src/alloc.rs | 8 +++++--- crates/swc_allocator/src/lib.rs | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/swc_allocator/src/alloc.rs b/crates/swc_allocator/src/alloc.rs index 94197fcc5fcd..1cbfdf43e9b4 100644 --- a/crates/swc_allocator/src/alloc.rs +++ b/crates/swc_allocator/src/alloc.rs @@ -21,11 +21,13 @@ pub struct Allocator { alloc: Bump, } -pub struct AllocGuard { +pub struct AllocGuard<'a> { orig: Option<&'static Allocator>, + + _ref: &'a Allocator, } -impl Drop for AllocGuard { +impl Drop for AllocGuard<'_> { fn drop(&mut self) { ALLOC.set(self.orig.take()); } @@ -47,7 +49,7 @@ impl Allocator { }; ALLOC.set(Some(s)); - AllocGuard { orig } + AllocGuard { orig, _ref: self } } } diff --git a/crates/swc_allocator/src/lib.rs b/crates/swc_allocator/src/lib.rs index 1c94b5cfc6f3..1e392f135b5c 100644 --- a/crates/swc_allocator/src/lib.rs +++ b/crates/swc_allocator/src/lib.rs @@ -34,6 +34,7 @@ /// NOT A PUBLIC API. #[doc(hidden)] pub extern crate allocator_api2; + pub use crate::alloc::Allocator; mod alloc; From 4ac82d639cc94f805e72c9ce19c7a7b2b6712ee6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 09:04:30 +0900 Subject: [PATCH 41/46] cleanup --- crates/swc_allocator/src/alloc.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/crates/swc_allocator/src/alloc.rs b/crates/swc_allocator/src/alloc.rs index 1cbfdf43e9b4..86694f820b8a 100644 --- a/crates/swc_allocator/src/alloc.rs +++ b/crates/swc_allocator/src/alloc.rs @@ -57,11 +57,7 @@ impl Default for FastAlloc { fn default() -> Self { Self { #[cfg(feature = "scoped")] - alloc: if let Some(v) = ALLOC.get() { - Some(v) - } else { - None - }, + alloc: ALLOC.get(), } } } From 0454242174043a44638a8ddd66f6f0eb133308f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 09:06:46 +0900 Subject: [PATCH 42/46] comment --- crates/swc_allocator/src/alloc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/swc_allocator/src/alloc.rs b/crates/swc_allocator/src/alloc.rs index 86694f820b8a..9f3c71caa6df 100644 --- a/crates/swc_allocator/src/alloc.rs +++ b/crates/swc_allocator/src/alloc.rs @@ -44,7 +44,7 @@ impl Allocator { let orig = ALLOC.get(); let s = unsafe { - // Safery: We are using a scoped API + // Safery: Guard holds &self, so it cannot be moved nor dropped. transmute::<&Allocator, &'static Allocator>(self) }; From 18cd36193112592b0baf502524e81d69eb6f2e22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 09:09:48 +0900 Subject: [PATCH 43/46] fix drop impl --- crates/swc_allocator/src/alloc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/swc_allocator/src/alloc.rs b/crates/swc_allocator/src/alloc.rs index 9f3c71caa6df..551916c4fb70 100644 --- a/crates/swc_allocator/src/alloc.rs +++ b/crates/swc_allocator/src/alloc.rs @@ -29,7 +29,7 @@ pub struct AllocGuard<'a> { impl Drop for AllocGuard<'_> { fn drop(&mut self) { - ALLOC.set(self.orig.take()); + ALLOC.set(self.orig); } } From c850fd27075a84f07bb06e1772cd03af02f160be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 09:15:22 +0900 Subject: [PATCH 44/46] Try pin --- crates/swc_allocator/src/alloc.rs | 10 +++++++--- crates/swc_allocator/src/lib.rs | 3 ++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/crates/swc_allocator/src/alloc.rs b/crates/swc_allocator/src/alloc.rs index 551916c4fb70..1de019b5fb0f 100644 --- a/crates/swc_allocator/src/alloc.rs +++ b/crates/swc_allocator/src/alloc.rs @@ -3,6 +3,7 @@ use std::{ cell::Cell, mem::transmute, ops::{Deref, DerefMut}, + pin::Pin, ptr::NonNull, }; @@ -11,8 +12,10 @@ use bumpalo::Bump; use crate::FastAlloc; +pub(crate) type Alloc = Pin<&'static Allocator>; + thread_local! { - static ALLOC: Cell> = const { Cell::new(None) }; + static ALLOC: Cell> = const { Cell::new(None) }; } /// The actual storage for [FastAlloc]. @@ -22,7 +25,7 @@ pub struct Allocator { } pub struct AllocGuard<'a> { - orig: Option<&'static Allocator>, + orig: Option, _ref: &'a Allocator, } @@ -43,9 +46,10 @@ impl Allocator { pub unsafe fn guard(&self) -> AllocGuard { let orig = ALLOC.get(); + let pin: Pin<&Allocator> = Pin::new(self); let s = unsafe { // Safery: Guard holds &self, so it cannot be moved nor dropped. - transmute::<&Allocator, &'static Allocator>(self) + transmute::, Pin<&'static Allocator>>(pin) }; ALLOC.set(Some(s)); diff --git a/crates/swc_allocator/src/lib.rs b/crates/swc_allocator/src/lib.rs index 1e392f135b5c..10cfdbb3dfad 100644 --- a/crates/swc_allocator/src/lib.rs +++ b/crates/swc_allocator/src/lib.rs @@ -35,6 +35,7 @@ #[doc(hidden)] pub extern crate allocator_api2; +use crate::alloc::Alloc; pub use crate::alloc::Allocator; mod alloc; @@ -64,7 +65,7 @@ pub mod vec; #[derive(Clone, Copy)] pub struct FastAlloc { #[cfg(feature = "scoped")] - alloc: Option<&'static Allocator>, + alloc: Option, } impl FastAlloc { From ff91e269e1db8aa0570de84e979aac1a2450e0c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 09:27:07 +0900 Subject: [PATCH 45/46] Add segfault test --- crates/swc_ecma_parser/tests/segfault.rs | 33 ++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 crates/swc_ecma_parser/tests/segfault.rs diff --git a/crates/swc_ecma_parser/tests/segfault.rs b/crates/swc_ecma_parser/tests/segfault.rs new file mode 100644 index 000000000000..1527219d10bc --- /dev/null +++ b/crates/swc_ecma_parser/tests/segfault.rs @@ -0,0 +1,33 @@ +extern crate swc_malloc; + +use codspeed_criterion_compat::black_box; +use swc_allocator::Allocator; +use swc_common::{comments::SingleThreadedComments, FileName}; +use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax}; + +fn bench_module(syntax: Syntax, src: &'static str) { + let _ = ::testing::run_test(false, |cm, _| { + let comments = SingleThreadedComments::default(); + let fm = cm.new_source_file(FileName::Anon.into(), src.into()); + + let allocator = Allocator::default(); + + let _ = black_box({ + let lexer = Lexer::new( + syntax, + Default::default(), + StringInput::from(&*fm), + Some(&comments), + ); + let mut parser = Parser::new_from(lexer); + parser.parse_module() + }); + + Ok(()) + }); +} + +#[test] +fn test_1() { + bench_module(Default::default(), include_str!("../colors.js")); +} From f18b948121b70acbd662e7d38bfa77b98d1e4230 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 16 Jul 2024 09:41:08 +0900 Subject: [PATCH 46/46] fix unbox() --- crates/swc_allocator/src/boxed/mod.rs | 5 ++++- crates/swc_ecma_parser/tests/segfault.rs | 3 --- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/swc_allocator/src/boxed/mod.rs b/crates/swc_allocator/src/boxed/mod.rs index d2554adb2021..6cca09f8629a 100644 --- a/crates/swc_allocator/src/boxed/mod.rs +++ b/crates/swc_allocator/src/boxed/mod.rs @@ -7,6 +7,7 @@ use std::{ iter::FusedIterator, ops::{Deref, DerefMut}, pin::Pin, + ptr, }; use crate::{vec::Vec, FastAlloc}; @@ -82,7 +83,9 @@ impl Box { /// Moves the value out of the box. pub fn unbox(self) -> T { - allocator_api2::boxed::Box::into_inner(self.0) + let raw = allocator_api2::boxed::Box::into_raw(self.0); + + unsafe { ptr::read(raw) } } } diff --git a/crates/swc_ecma_parser/tests/segfault.rs b/crates/swc_ecma_parser/tests/segfault.rs index 1527219d10bc..9d0c589baafb 100644 --- a/crates/swc_ecma_parser/tests/segfault.rs +++ b/crates/swc_ecma_parser/tests/segfault.rs @@ -1,7 +1,6 @@ extern crate swc_malloc; use codspeed_criterion_compat::black_box; -use swc_allocator::Allocator; use swc_common::{comments::SingleThreadedComments, FileName}; use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax}; @@ -10,8 +9,6 @@ fn bench_module(syntax: Syntax, src: &'static str) { let comments = SingleThreadedComments::default(); let fm = cm.new_source_file(FileName::Anon.into(), src.into()); - let allocator = Allocator::default(); - let _ = black_box({ let lexer = Lexer::new( syntax,