From 81ac77e3ab399446a0962db1e92d8066b73b3b31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Mon, 11 Nov 2024 12:58:51 +0900 Subject: [PATCH] feat(plugin): Bump `rkyv` to `v0.8.8` (#9730) **Related issue:** - Closes https://github.com/swc-project/swc/pull/9716 --------- Co-authored-by: OJ Kwon <1210596+kwonoj@users.noreply.github.com> --- .changeset/eleven-ducks-perform.md | 8 + Cargo.lock | 178 +++++++++++++++--- Cargo.toml | 7 +- crates/ast_node/src/lib.rs | 54 ++++-- crates/swc_allocator/Cargo.toml | 5 +- crates/swc_allocator/src/boxed/rkyv.rs | 55 ++++-- crates/swc_allocator/src/vec/rkyv.rs | 70 +++++-- crates/swc_atoms/Cargo.toml | 5 +- crates/swc_atoms/src/lib.rs | 15 +- crates/swc_common/Cargo.toml | 40 ++-- crates/swc_common/src/comments.rs | 7 +- crates/swc_common/src/errors/diagnostic.rs | 16 +- crates/swc_common/src/errors/mod.rs | 20 +- crates/swc_common/src/errors/snippet.rs | 4 +- crates/swc_common/src/plugin/diagnostics.rs | 4 +- crates/swc_common/src/plugin/serialized.rs | 65 ++++++- crates/swc_common/src/syntax_pos.rs | 158 +++++++++------- crates/swc_common/src/syntax_pos/hygiene.rs | 10 +- crates/swc_common/tests/attr_interop.rs | 21 ++- crates/swc_css_ast/Cargo.toml | 17 +- crates/swc_css_ast/src/at_rule.rs | 26 +-- crates/swc_css_ast/src/selector.rs | 20 +- crates/swc_css_ast/src/token.rs | 22 ++- crates/swc_css_ast/src/value.rs | 21 ++- crates/swc_ecma_ast/Cargo.toml | 9 +- crates/swc_ecma_ast/src/class.rs | 4 +- crates/swc_ecma_ast/src/decl.rs | 4 +- crates/swc_ecma_ast/src/expr.rs | 26 +-- crates/swc_ecma_ast/src/ident.rs | 30 +-- crates/swc_ecma_ast/src/lit.rs | 16 +- crates/swc_ecma_ast/src/module_decl.rs | 4 +- crates/swc_ecma_ast/src/operators.rs | 16 +- crates/swc_ecma_ast/src/typescript.rs | 16 +- crates/swc_html_ast/Cargo.toml | 17 +- crates/swc_html_ast/src/base.rs | 24 ++- crates/swc_html_ast/src/token.rs | 12 +- crates/swc_plugin_proxy/Cargo.toml | 8 +- .../src/comments/plugin_comments_proxy.rs | 11 +- .../read_returned_result_from_host.rs | 15 +- 39 files changed, 697 insertions(+), 363 deletions(-) create mode 100644 .changeset/eleven-ducks-perform.md diff --git a/.changeset/eleven-ducks-perform.md b/.changeset/eleven-ducks-perform.md new file mode 100644 index 000000000000..2a1a293805f1 --- /dev/null +++ b/.changeset/eleven-ducks-perform.md @@ -0,0 +1,8 @@ +--- +ast_node: major +swc_atoms: major +swc_allocator: major +swc_common: major +--- + +feat(plugin): Bump `rkyv` to `v0.8.8` diff --git a/Cargo.lock b/Cargo.lock index b87cc48dbd4a..fc974184c048 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -428,8 +428,20 @@ version = "0.6.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23cdc57ce23ac53c931e88a43d06d070a6fd142f2617be5855eb75efc9beb1c2" dependencies = [ - "bytecheck_derive", - "ptr_meta", + "bytecheck_derive 0.6.12", + "ptr_meta 0.1.4", + "simdutf8", +] + +[[package]] +name = "bytecheck" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50c8f430744b23b54ad15161fcbc22d82a29b73eacbe425fea23ec822600bc6f" +dependencies = [ + "bytecheck_derive 0.8.0", + "ptr_meta 0.3.0", + "rancor", "simdutf8", ] @@ -444,6 +456,17 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "bytecheck_derive" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "523363cbe1df49b68215efdf500b103ac3b0fb4836aed6d15689a076eadb8fff" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.77", +] + [[package]] name = "byteorder" version = "1.5.0" @@ -2425,6 +2448,26 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7843ec2de400bcbc6a6328c958dc38e5359da6e93e72e37bc5246bf1ae776389" +[[package]] +name = "munge" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64142d38c84badf60abf06ff9bd80ad2174306a5b11bd4706535090a30a419df" +dependencies = [ + "munge_macro", +] + +[[package]] +name = "munge_macro" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bb5c1d8184f13f7d0ccbeeca0def2f9a181bce2624302793005f5ca8aa62e5e" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.77", +] + [[package]] name = "napi" version = "2.16.13" @@ -3045,7 +3088,16 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0738ccf7ea06b608c10564b31debd4f5bc5e197fc8bfe088f68ae5ce81e7a4f1" dependencies = [ - "ptr_meta_derive", + "ptr_meta_derive 0.1.4", +] + +[[package]] +name = "ptr_meta" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe9e76f66d3f9606f44e45598d155cb13ecf09f4a28199e48daf8c8fc937ea90" +dependencies = [ + "ptr_meta_derive 0.3.0", ] [[package]] @@ -3059,6 +3111,17 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "ptr_meta_derive" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca414edb151b4c8d125c12566ab0d74dc9cdba36fb80eb7b848c15f495fd32d1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.77", +] + [[package]] name = "pulldown-cmark" version = "0.8.0" @@ -3091,6 +3154,15 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce082a9940a7ace2ad4a8b7d0b1eac6aa378895f18be598230c5f2284ac05426" +[[package]] +name = "rancor" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caf5f7161924b9d1cea0e4cabc97c372cea92b5f927fc13c6bca67157a0ad947" +dependencies = [ + "ptr_meta 0.3.0", +] + [[package]] name = "rand" version = "0.8.5" @@ -3230,7 +3302,16 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71fe3824f5629716b1589be05dacd749f6aa084c87e00e016714a8cdfccc997c" dependencies = [ - "bytecheck", + "bytecheck 0.6.12", +] + +[[package]] +name = "rend" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a35e8a6bf28cd121053a66aa2e6a2e3eaffad4a60012179f0e864aa5ffeff215" +dependencies = [ + "bytecheck 0.8.0", ] [[package]] @@ -3286,18 +3367,37 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9008cd6385b9e161d8229e1f6549dd23c3d022f132a2ea37ac3a10ac4935779b" dependencies = [ "bitvec", - "bytecheck", + "bytecheck 0.6.12", "bytes", "hashbrown 0.12.3", "indexmap 1.9.3", - "ptr_meta", - "rend", - "rkyv_derive", + "ptr_meta 0.1.4", + "rend 0.4.2", + "rkyv_derive 0.7.45", "seahash", "tinyvec", "uuid", ] +[[package]] +name = "rkyv" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "395027076c569819ea6035ee62e664f5e03d74e281744f55261dd1afd939212b" +dependencies = [ + "bytecheck 0.8.0", + "bytes", + "hashbrown 0.14.5", + "indexmap 2.5.0", + "munge", + "ptr_meta 0.3.0", + "rancor", + "rend 0.5.2", + "rkyv_derive 0.8.8", + "tinyvec", + "uuid", +] + [[package]] name = "rkyv_derive" version = "0.7.45" @@ -3309,6 +3409,17 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "rkyv_derive" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09cb82b74b4810f07e460852c32f522e979787691b0b7b7439fe473e49d49b2f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.77", +] + [[package]] name = "rustc-demangle" version = "0.1.24" @@ -3805,7 +3916,7 @@ dependencies = [ "cfg-if", "libc", "psm", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -3929,8 +4040,9 @@ dependencies = [ "codspeed-criterion-compat", "criterion", "hashbrown 0.14.5", - "ptr_meta", - "rkyv", + "ptr_meta 0.3.0", + "rancor", + "rkyv 0.8.8", "rustc-hash", "serde", "swc_malloc", @@ -3942,10 +4054,11 @@ name = "swc_atoms" version = "2.0.0" dependencies = [ "arbitrary", - "bytecheck", + "bytecheck 0.8.0", "hstr", "once_cell", - "rkyv", + "rancor", + "rkyv 0.8.8", "rustc-hash", "serde", ] @@ -4037,7 +4150,7 @@ dependencies = [ "arbitrary", "ast_node", "better_scoped_tls", - "bytecheck", + "bytecheck 0.8.0", "cfg-if", "codspeed-criterion-compat", "criterion", @@ -4047,8 +4160,9 @@ dependencies = [ "num-bigint", "once_cell", "parking_lot", + "rancor", "rayon", - "rkyv", + "rkyv 0.8.8", "rustc-hash", "serde", "serde_json", @@ -4187,8 +4301,10 @@ dependencies = [ name = "swc_css_ast" version = "4.0.1" dependencies = [ + "bytecheck 0.8.0", "is-macro", - "rkyv", + "rancor", + "rkyv 0.8.8", "serde", "string_enum", "swc_atoms", @@ -4361,11 +4477,12 @@ version = "4.0.1" dependencies = [ "arbitrary", "bitflags 2.6.0", - "bytecheck", + "bytecheck 0.8.0", "is-macro", "num-bigint", "phf", - "rkyv", + "rancor", + "rkyv 0.8.8", "scoped-tls", "serde", "serde_json", @@ -5247,8 +5364,9 @@ dependencies = [ name = "swc_html_ast" version = "4.0.1" dependencies = [ + "bytecheck 0.8.0", "is-macro", - "rkyv", + "rkyv 0.8.8", "serde", "string_enum", "swc_atoms", @@ -5440,7 +5558,9 @@ name = "swc_plugin_proxy" version = "4.0.0" dependencies = [ "better_scoped_tls", - "rkyv", + "bytecheck 0.8.0", + "rancor", + "rkyv 0.8.8", "swc_common", "swc_ecma_ast", "swc_trace_macro", @@ -6339,12 +6459,12 @@ dependencies = [ "async-trait", "base64 0.21.7", "bincode", - "bytecheck", + "bytecheck 0.6.12", "bytes", "derivative", "futures-util", "pin-project-lite", - "rkyv", + "rkyv 0.7.45", "serde", "smoltcp", "thiserror", @@ -6594,7 +6714,7 @@ dependencies = [ "memmap2 0.5.10", "more-asserts", "region", - "rkyv", + "rkyv 0.7.45", "self_cell", "shared-buffer", "smallvec", @@ -6669,12 +6789,12 @@ dependencies = [ "async-trait", "base64 0.21.7", "bincode", - "bytecheck", + "bytecheck 0.6.12", "bytes", "derivative", "lz4_flex", "num_enum", - "rkyv", + "rkyv 0.7.45", "serde", "serde_json", "thiserror", @@ -6691,14 +6811,14 @@ version = "4.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d22a00f1a90e9e66d5427853f41e76d8ab89e03eb3034debd11933607fef56a" dependencies = [ - "bytecheck", + "bytecheck 0.6.12", "enum-iterator", "enumset", "getrandom", "hex", "indexmap 1.9.3", "more-asserts", - "rkyv", + "rkyv 0.7.45", "serde", "sha2", "target-lexicon", @@ -6746,7 +6866,7 @@ dependencies = [ "base64 0.21.7", "bincode", "blake3", - "bytecheck", + "bytecheck 0.6.12", "bytes", "cfg-if", "chrono", @@ -6769,7 +6889,7 @@ dependencies = [ "pin-project", "pin-utils", "rand", - "rkyv", + "rkyv 0.7.45", "rusty_pool", "semver 1.0.23", "serde", diff --git a/Cargo.toml b/Cargo.toml index 55e61801ead9..2e806fe6fb9b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,8 +38,9 @@ resolver = "2" [workspace.dependencies] # bytecheck version should be in sync with rkyv version. Do not bump individually. - bytecheck = "0.6.10" - rkyv = "=0.7.45" + bytecheck = "0.8.0" + rancor = "0.1.0" + rkyv = "0.8.8" Inflector = "0.11.4" @@ -103,7 +104,7 @@ resolver = "2" phf = "0.11.2" pretty_assertions = "1.3" proc-macro2 = "1.0.24" - ptr_meta = "0.1.4" + ptr_meta = "0.3.0" quote = "1.0.7" radix_fmt = "1.0.0" rayon = "1.7.0" diff --git a/crates/ast_node/src/lib.rs b/crates/ast_node/src/lib.rs index 5f6662a098e3..bc58300dc4ea 100644 --- a/crates/ast_node/src/lib.rs +++ b/crates/ast_node/src/lib.rs @@ -137,9 +137,7 @@ struct AddAttr; impl VisitMut for AddAttr { fn visit_field_mut(&mut self, f: &mut Field) { f.attrs - .push(parse_quote!(#[cfg_attr(feature = "__rkyv", omit_bounds)])); - f.attrs - .push(parse_quote!(#[cfg_attr(feature = "__rkyv", archive_attr(omit_bounds))])); + .push(parse_quote!(#[cfg_attr(feature = "__rkyv", rkyv(omit_bounds))])); } } @@ -204,14 +202,23 @@ pub fn ast_node( feature = "rkyv-impl", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] - #[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] - #[cfg_attr(feature = "rkyv-impl", archive_attr(check_bytes( - bound = "__C: rkyv::validation::ArchiveContext, <__C as rkyv::Fallible>::Error: std::error::Error" - )))] - #[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))] - #[cfg_attr(feature = "rkyv-impl", archive( - bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer") - ))] + #[cfg_attr( + feature = "rkyv-impl", + rkyv(deserialize_bounds(__D::Error: rkyv::rancor::Source)) + )] + #[cfg_attr(feature = "rkyv-impl", repr(u32))] + #[cfg_attr( + feature = "rkyv-impl", + rkyv(serialize_bounds(__S: rkyv::ser::Writer + rkyv::ser::Allocator, + __S::Error: rkyv::rancor::Source)) + )] + #[cfg_attr( + feature = "rkyv-impl", + rkyv(bytecheck(bounds( + __C: rkyv::validation::ArchiveContext, + __C::Error: rkyv::rancor::Source + ))) + )] #[cfg_attr( feature = "serde-impl", serde(untagged) @@ -267,14 +274,23 @@ pub fn ast_node( feature = "rkyv-impl", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] - #[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] - #[cfg_attr(feature = "rkyv-impl", archive_attr(check_bytes( - bound = "__C: rkyv::validation::ArchiveContext, <__C as rkyv::Fallible>::Error: std::error::Error" - )))] - #[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] - #[cfg_attr(feature = "rkyv-impl", archive( - bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer") - ))] + #[cfg_attr( + feature = "rkyv-impl", + rkyv(deserialize_bounds(__D::Error: rkyv::rancor::Source)) + )] + #[cfg_attr( + feature = "rkyv-impl", + rkyv(bytecheck(bounds( + __C: rkyv::validation::ArchiveContext, + __C::Error: rkyv::rancor::Source + ))) + )] + #[cfg_attr(feature = "rkyv-impl", repr(C))] + #[cfg_attr( + feature = "rkyv-impl", + rkyv(serialize_bounds(__S: rkyv::ser::Writer + rkyv::ser::Allocator, + __S::Error: rkyv::rancor::Source)) + )] #serde_tag #[cfg_attr( feature = "serde-impl", diff --git a/crates/swc_allocator/Cargo.toml b/crates/swc_allocator/Cargo.toml index 109cd21f5a96..9b1a97ec3719 100644 --- a/crates/swc_allocator/Cargo.toml +++ b/crates/swc_allocator/Cargo.toml @@ -14,9 +14,9 @@ version = "1.0.0" [features] -default = ["scoped"] +default = ["scoped", "rkyv"] nightly = ["bumpalo/allocator_api", "hashbrown/nightly"] -rkyv = ["dep:rkyv"] +rkyv = ["dep:rkyv", "dep:rancor"] scoped = ["nightly"] serde = ["dep:serde"] @@ -28,6 +28,7 @@ bumpalo = { workspace = true, features = [ ] } hashbrown = { workspace = true } ptr_meta = { workspace = true } +rancor = { workspace = true, optional = true } rkyv = { workspace = true, optional = true } rustc-hash = { workspace = true } serde = { workspace = true, optional = true } diff --git a/crates/swc_allocator/src/boxed/rkyv.rs b/crates/swc_allocator/src/boxed/rkyv.rs index 2c55abf28d37..cc999802e8f4 100644 --- a/crates/swc_allocator/src/boxed/rkyv.rs +++ b/crates/swc_allocator/src/boxed/rkyv.rs @@ -1,46 +1,55 @@ use std::{alloc, cmp}; +use rancor::{Fallible, ResultExt, Source}; use rkyv::{ boxed::{ArchivedBox, BoxResolver}, - Archive, ArchivePointee, ArchiveUnsized, Deserialize, DeserializeUnsized, Fallible, Serialize, - SerializeUnsized, + traits::{ArchivePointee, LayoutRaw}, + Archive, ArchiveUnsized, Deserialize, DeserializeUnsized, Place, Serialize, SerializeUnsized, }; use super::Box; impl Archive for Box { type Archived = ArchivedBox; - type Resolver = BoxResolver; + type Resolver = BoxResolver; #[inline] - unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { - ArchivedBox::resolve_from_ref(self.as_ref(), pos, resolver, out); + fn resolve(&self, resolver: Self::Resolver, out: Place) { + ArchivedBox::resolve_from_ref(self.as_ref(), resolver, out); } } impl + ?Sized, S: Fallible + ?Sized> Serialize for Box { - #[inline] - fn serialize(&self, serializer: &mut S) -> Result { + fn serialize( + &self, + serializer: &mut S, + ) -> Result::Error> { ArchivedBox::serialize_from_ref(self.as_ref(), serializer) } } impl Deserialize, D> for ArchivedBox where - T: ArchiveUnsized + ?Sized, + T: ArchiveUnsized + LayoutRaw + ?Sized, T::Archived: DeserializeUnsized, D: Fallible + ?Sized, + D::Error: Source, { - #[inline] fn deserialize(&self, deserializer: &mut D) -> Result, D::Error> { + let metadata = self.get().deserialize_metadata(); + let layout = T::layout_raw(metadata).into_error()?; + let data_address = if layout.size() > 0 { + unsafe { alloc::alloc(layout) } + } else { + polyfill::dangling(&layout).as_ptr() + }; + + let out = ptr_meta::from_raw_parts_mut(data_address.cast(), metadata); + unsafe { - let data_address = self - .get() - .deserialize_unsized(deserializer, |layout| alloc::alloc(layout))?; - let metadata = self.get().deserialize_metadata(deserializer)?; - let ptr = ptr_meta::from_raw_parts_mut(data_address, metadata); - Ok(Box::from_raw(ptr)) + self.get().deserialize_unsized(deserializer, out)?; } + unsafe { Ok(Box::from_raw(out)) } } } @@ -57,3 +66,19 @@ impl + ?Sized, U: ?Sized> PartialOrd> f self.get().partial_cmp(other.as_ref()) } } + +mod polyfill { + + use core::{alloc::Layout, ptr::NonNull}; + + pub fn dangling(layout: &Layout) -> NonNull { + #[cfg(miri)] + { + layout.dangling() + } + #[cfg(not(miri))] + unsafe { + NonNull::new_unchecked(layout.align() as *mut u8) + } + } +} diff --git a/crates/swc_allocator/src/vec/rkyv.rs b/crates/swc_allocator/src/vec/rkyv.rs index edad89668fda..20f2761abfcd 100644 --- a/crates/swc_allocator/src/vec/rkyv.rs +++ b/crates/swc_allocator/src/vec/rkyv.rs @@ -1,42 +1,72 @@ -use rkyv::{vec::ArchivedVec, DeserializeUnsized}; +use std::alloc; + +use rancor::{Fallible, ResultExt, Source}; +use rkyv::{ + ser::{Allocator, Writer}, + traits::LayoutRaw, + vec::ArchivedVec, + Archive, Deserialize, DeserializeUnsized, +}; use super::Vec; use crate::boxed::Box; -impl rkyv::Archive for Vec +impl Archive for Vec where - T: rkyv::Archive, + T: Archive, { - type Archived = rkyv::vec::ArchivedVec; + type Archived = ArchivedVec; type Resolver = rkyv::vec::VecResolver; - unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { - rkyv::vec::ArchivedVec::resolve_from_slice(self, pos, resolver, out); + fn resolve(&self, resolver: Self::Resolver, out: rkyv::Place) { + ArchivedVec::resolve_from_slice(self, resolver, out); } } -impl, S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer + ?Sized> - rkyv::Serialize for Vec +impl, S: Fallible + Allocator + Writer + ?Sized> rkyv::Serialize + for Vec { - #[inline] - fn serialize(&self, serializer: &mut S) -> Result { + fn serialize( + &self, + serializer: &mut S, + ) -> Result::Error> { ArchivedVec::::serialize_from_slice(self, serializer) } } -impl rkyv::Deserialize, D> - for ArchivedVec +impl Deserialize, D> for ArchivedVec where - [T::Archived]: rkyv::DeserializeUnsized<[T], D>, + [T::Archived]: DeserializeUnsized<[T], D>, + D::Error: Source, { - #[inline] - fn deserialize(&self, deserializer: &mut D) -> Result, D::Error> { + fn deserialize(&self, deserializer: &mut D) -> Result, ::Error> { + let metadata = self.as_slice().deserialize_metadata(); + let layout = <[T] as LayoutRaw>::layout_raw(metadata).into_error()?; + let data_address = if layout.size() > 0 { + unsafe { alloc::alloc(layout) } + } else { + polyfill::dangling(&layout).as_ptr() + }; + let out = ptr_meta::from_raw_parts_mut(data_address.cast(), metadata); + unsafe { + self.as_slice().deserialize_unsized(deserializer, out)?; + } + unsafe { Ok(Box::<[T]>::from_raw(out).into()) } + } +} + +mod polyfill { + + use core::{alloc::Layout, ptr::NonNull}; + + pub fn dangling(layout: &Layout) -> NonNull { + #[cfg(miri)] + { + layout.dangling() + } + #[cfg(not(miri))] unsafe { - let data_address = - (**self).deserialize_unsized(deserializer, |layout| std::alloc::alloc(layout))?; - let metadata = self.as_slice().deserialize_metadata(deserializer)?; - let ptr = ptr_meta::from_raw_parts_mut(data_address, metadata); - Ok(Box::<[T]>::from_raw(ptr).into()) + NonNull::new_unchecked(layout.align() as *mut u8) } } } diff --git a/crates/swc_atoms/Cargo.toml b/crates/swc_atoms/Cargo.toml index e36340e4612a..314d97512f1e 100644 --- a/crates/swc_atoms/Cargo.toml +++ b/crates/swc_atoms/Cargo.toml @@ -13,7 +13,7 @@ bench = false [features] __rkyv = [] -rkyv-impl = ["__rkyv", "rkyv", "bytecheck"] +rkyv-impl = ["__rkyv", "rkyv", "bytecheck", "rancor"] [dependencies] # bytecheck version should be in sync with rkyv version. Do not bump individually. @@ -21,6 +21,7 @@ arbitrary = { workspace = true, optional = true } bytecheck = { workspace = true, optional = true } hstr = { workspace = true } once_cell = { workspace = true } +rancor = { workspace = true, optional = true } +rkyv = { workspace = true, optional = true } rustc-hash = { workspace = true } serde = { workspace = true } -rkyv = { workspace = true, features = ["validation"], optional = true } diff --git a/crates/swc_atoms/src/lib.rs b/crates/swc_atoms/src/lib.rs index 82f688bac6a2..1ed9365a6ca1 100644 --- a/crates/swc_atoms/src/lib.rs +++ b/crates/swc_atoms/src/lib.rs @@ -28,7 +28,7 @@ pub use self::{atom as js_word, Atom as JsWord}; /// /// See [tendril] for more details. #[derive(Clone, Default, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "rkyv-impl", derive(rkyv::bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct Atom(hstr::Atom); @@ -199,14 +199,17 @@ impl rkyv::Archive for Atom { type Resolver = rkyv::string::StringResolver; #[allow(clippy::unit_arg)] - unsafe fn resolve(&self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived) { - rkyv::string::ArchivedString::resolve_from_str(self, pos, resolver, out) + fn resolve(&self, resolver: Self::Resolver, out: rkyv::Place) { + rkyv::string::ArchivedString::resolve_from_str(self, resolver, out) } } /// NOT A PUBLIC API #[cfg(feature = "rkyv-impl")] -impl rkyv::Serialize for Atom { +impl rkyv::Serialize for Atom +where + ::Error: rancor::Source, +{ fn serialize(&self, serializer: &mut S) -> Result { String::serialize(&self.to_string(), serializer) } @@ -216,9 +219,9 @@ impl rkyv::Serialize for Atom { #[cfg(feature = "rkyv-impl")] impl rkyv::Deserialize for rkyv::string::ArchivedString where - D: ?Sized + rkyv::Fallible, + D: ?Sized + rancor::Fallible, { - fn deserialize(&self, deserializer: &mut D) -> Result::Error> { + fn deserialize(&self, deserializer: &mut D) -> Result::Error> { let s: String = self.deserialize(deserializer)?; Ok(Atom::new(s)) diff --git a/crates/swc_common/Cargo.toml b/crates/swc_common/Cargo.toml index d132edbce218..ad3c360f733e 100644 --- a/crates/swc_common/Cargo.toml +++ b/crates/swc_common/Cargo.toml @@ -34,29 +34,29 @@ plugin_transform_schema_vtest = [] tty-emitter = ["termcolor"] __rkyv = [] -rkyv-impl = ["__rkyv", "rkyv", "swc_atoms/rkyv-impl", "bytecheck"] +rkyv-impl = ["__rkyv", "rkyv", "swc_atoms/rkyv-impl", "bytecheck", "rancor"] [dependencies] -ahash = { workspace = true, optional = true } -anyhow = { workspace = true, optional = true } -arbitrary = { workspace = true, features = ["derive"], optional = true } -# bytecheck version should be in sync with rkyv version. Do not bump individually. -bytecheck = { workspace = true, optional = true } -cfg-if = { workspace = true } -either = { workspace = true } +ahash = { workspace = true, optional = true } +anyhow = { workspace = true, optional = true } +arbitrary = { workspace = true, features = ["derive"], optional = true } +bytecheck = { workspace = true, optional = true } +cfg-if = { workspace = true } +either = { workspace = true } new_debug_unreachable = { workspace = true } -num-bigint = { workspace = true } -once_cell = { workspace = true } -parking_lot = { workspace = true, optional = true } -rkyv = { workspace = true, features = ["validation"], optional = true } -rustc-hash = { workspace = true } -serde = { workspace = true, features = ["derive"] } -siphasher = { workspace = true } -sourcemap = { workspace = true, optional = true } -termcolor = { workspace = true, optional = true } -tracing = { workspace = true } -unicode-width = { workspace = true } -url = { workspace = true } +num-bigint = { workspace = true } +once_cell = { workspace = true } +parking_lot = { workspace = true, optional = true } +rancor = { workspace = true, optional = true } +rkyv = { workspace = true, optional = true } +rustc-hash = { workspace = true } +serde = { workspace = true, features = ["derive"] } +siphasher = { workspace = true } +sourcemap = { workspace = true, optional = true } +termcolor = { workspace = true, optional = true } +tracing = { workspace = true } +unicode-width = { workspace = true } +url = { workspace = true } ast_node = { version = "2.0.0", path = "../ast_node" } better_scoped_tls = { version = "1.0.0", path = "../better_scoped_tls" } diff --git a/crates/swc_common/src/comments.rs b/crates/swc_common/src/comments.rs index 4364381904f9..762bfa633c01 100644 --- a/crates/swc_common/src/comments.rs +++ b/crates/swc_common/src/comments.rs @@ -634,8 +634,8 @@ impl SingleThreadedComments { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct Comment { pub kind: CommentKind, pub span: Span, @@ -654,7 +654,8 @@ impl Spanned for Comment { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(u32))] pub enum CommentKind { Line = 0, Block = 1, diff --git a/crates/swc_common/src/errors/diagnostic.rs b/crates/swc_common/src/errors/diagnostic.rs index 5bed0f239e0e..7f5a262ed187 100644 --- a/crates/swc_common/src/errors/diagnostic.rs +++ b/crates/swc_common/src/errors/diagnostic.rs @@ -22,8 +22,8 @@ use crate::syntax_pos::{MultiSpan, Span}; any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct Message(pub String, pub Style); #[must_use] @@ -36,8 +36,8 @@ pub struct Message(pub String, pub Style); any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct Diagnostic { pub level: Level, pub message: Vec, @@ -56,8 +56,8 @@ pub struct Diagnostic { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(u32))] pub enum DiagnosticId { Error(String), Lint(String), @@ -73,8 +73,8 @@ pub enum DiagnosticId { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct SubDiagnostic { pub level: Level, pub message: Vec, diff --git a/crates/swc_common/src/errors/mod.rs b/crates/swc_common/src/errors/mod.rs index 43a6e979aad0..13f40dd23fa4 100644 --- a/crates/swc_common/src/errors/mod.rs +++ b/crates/swc_common/src/errors/mod.rs @@ -50,8 +50,8 @@ mod styled_buffer; any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(u32))] pub enum Applicability { MachineApplicable, HasPlaceholders, @@ -68,8 +68,8 @@ pub enum Applicability { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct CodeSuggestion { /// Each substitute can have multiple variants due to multiple /// applicable suggestions @@ -121,8 +121,8 @@ pub struct CodeSuggestion { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct Substitution { pub parts: Vec, } @@ -136,8 +136,8 @@ pub struct Substitution { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct SubstitutionPart { pub span: Span, pub snippet: String, @@ -885,8 +885,8 @@ impl Handler { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(u32))] pub enum Level { Bug, Fatal, diff --git a/crates/swc_common/src/errors/snippet.rs b/crates/swc_common/src/errors/snippet.rs index 7eb6e85f22ae..706bb9a9f487 100644 --- a/crates/swc_common/src/errors/snippet.rs +++ b/crates/swc_common/src/errors/snippet.rs @@ -184,8 +184,8 @@ pub struct StyledString { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(u32))] pub enum Style { MainHeaderMsg, HeaderMsg, diff --git a/crates/swc_common/src/plugin/diagnostics.rs b/crates/swc_common/src/plugin/diagnostics.rs index 5e72903e9f54..e64c9ae094ef 100644 --- a/crates/swc_common/src/plugin/diagnostics.rs +++ b/crates/swc_common/src/plugin/diagnostics.rs @@ -7,8 +7,8 @@ any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct PluginCorePkgDiagnostics { pub pkg_version: String, pub git_sha: String, diff --git a/crates/swc_common/src/plugin/serialized.rs b/crates/swc_common/src/plugin/serialized.rs index 6f2b677edf27..8361b6f75e0a 100644 --- a/crates/swc_common/src/plugin/serialized.rs +++ b/crates/swc_common/src/plugin/serialized.rs @@ -1,8 +1,6 @@ use std::any::type_name; use anyhow::Error; -#[cfg(feature = "__rkyv")] -use rkyv::Deserialize; #[derive(Debug, Clone, PartialEq, Eq)] #[non_exhaustive] @@ -10,8 +8,8 @@ use rkyv::Deserialize; feature = "__plugin", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "__plugin", archive(check_bytes))] -#[cfg_attr(feature = "__plugin", archive_attr(repr(u32)))] +#[cfg_attr(feature = "__plugin", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "__plugin", repr(u32))] /// Enum for possible errors while running transform via plugin. /// /// This error indicates internal operation failure either in plugin_runner @@ -39,7 +37,7 @@ pub enum PluginError { /// format struct contains: it is strict implementation detail which can /// change anytime. pub struct PluginSerializedBytes { - pub(crate) field: rkyv::AlignedVec, + pub(crate) field: rkyv::util::AlignedVec, } #[cfg(feature = "__plugin")] @@ -50,7 +48,7 @@ impl PluginSerializedBytes { */ #[tracing::instrument(level = "info", skip_all)] pub fn from_slice(bytes: &[u8]) -> PluginSerializedBytes { - let mut field = rkyv::AlignedVec::new(); + let mut field = rkyv::util::AlignedVec::new(); field.extend_from_slice(bytes); PluginSerializedBytes { field } } @@ -61,6 +59,7 @@ impl PluginSerializedBytes { * This is sort of mimic TryFrom behavior, since we can't use generic * to implement TryFrom trait */ + /* #[tracing::instrument(level = "info", skip_all)] pub fn try_serialize(t: &VersionedSerializable) -> Result where @@ -78,6 +77,26 @@ impl PluginSerializedBytes { } }) } + */ + + #[tracing::instrument(level = "info", skip_all)] + pub fn try_serialize(t: &VersionedSerializable) -> Result + where + W: for<'a> rkyv::Serialize< + rancor::Strategy< + rkyv::ser::Serializer< + rkyv::util::AlignedVec, + rkyv::ser::allocator::ArenaHandle<'a>, + rkyv::ser::sharing::Share, + >, + rancor::Error, + >, + >, + { + rkyv::to_bytes::(t) + .map(|field| PluginSerializedBytes { field }) + .map_err(|err| err.into()) + } /* * Internal fn to constructs an instance from raw bytes ptr. @@ -102,6 +121,35 @@ impl PluginSerializedBytes { (self.field.as_ptr(), self.field.len()) } + #[tracing::instrument(level = "info", skip_all)] + pub fn deserialize(&self) -> Result, Error> + where + W: rkyv::Archive, + W::Archived: rkyv::Deserialize>, + for<'a> W::Archived: bytecheck::CheckBytes< + rancor::Strategy< + rkyv::validation::Validator< + rkyv::validation::archive::ArchiveValidator<'a>, + rkyv::validation::shared::SharedValidator, + >, + rancor::Error, + >, + >, + { + use anyhow::Context; + + let archived = + rkyv::access::(&self.field[..]).map_err(|err| { + anyhow::format_err!("wasm plugin bytecheck failed {:?}", err.to_string()) + })?; + + let deserialized = rkyv::deserialize(archived) + .with_context(|| format!("failed to deserialize `{}`", type_name::()))?; + + Ok(VersionedSerializable(deserialized)) + } + + /* #[tracing::instrument(level = "info", skip_all)] pub fn deserialize(&self) -> Result, Error> where @@ -119,7 +167,7 @@ impl PluginSerializedBytes { archived .deserialize(&mut rkyv::de::deserializers::SharedDeserializeMap::new()) .with_context(|| format!("failed to deserialize `{}`", type_name::())) - } + } */ } /// A wrapper type for the structures to be passed into plugins @@ -134,8 +182,7 @@ impl PluginSerializedBytes { derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] #[repr(transparent)] -#[cfg_attr(feature = "__plugin", archive(check_bytes))] -#[cfg_attr(feature = "__plugin", archive_attr(repr(transparent)))] +#[cfg_attr(feature = "__plugin", derive(bytecheck::CheckBytes))] #[derive(Debug)] pub struct VersionedSerializable( // [NOTE]: https://github.com/rkyv/rkyv/issues/373#issuecomment-1546360897 diff --git a/crates/swc_common/src/syntax_pos.rs b/crates/swc_common/src/syntax_pos.rs index 4ab1870cb6d2..54ec1f5f2698 100644 --- a/crates/swc_common/src/syntax_pos.rs +++ b/crates/swc_common/src/syntax_pos.rs @@ -36,14 +36,14 @@ pub mod hygiene; any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct Span { #[serde(rename = "start")] - #[cfg_attr(feature = "__rkyv", omit_bounds)] + #[cfg_attr(feature = "__rkyv", rkyv(omit_bounds))] pub lo: BytePos, #[serde(rename = "end")] - #[cfg_attr(feature = "__rkyv", omit_bounds)] + #[cfg_attr(feature = "__rkyv", rkyv(omit_bounds))] pub hi: BytePos, } @@ -141,11 +141,17 @@ better_scoped_tls::scoped_tls!( any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(u32))] #[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Hash)] pub enum FileName { - Real(#[cfg_attr(any(feature = "rkyv-impl"), with(crate::source_map::EncodePathBuf))] PathBuf), + Real( + #[cfg_attr( + any(feature = "rkyv-impl"), + rkyv(with = crate::source_map::EncodePathBuf) + )] + PathBuf, + ), /// A macro. This includes the full name of the macro, so that there are no /// clashes. Macros(String), @@ -156,7 +162,7 @@ pub enum FileName { /// Hack in src/libsyntax/parse.rs MacroExpansion, ProcMacroSourceCode, - Url(#[cfg_attr(any(feature = "rkyv-impl"), with(crate::source_map::EncodeUrl))] Url), + Url(#[cfg_attr(any(feature = "rkyv-impl"), rkyv(with = crate::source_map::EncodeUrl))] Url), Internal(String), /// Custom sources for explicit parser calls from plugins and drivers Custom(String), @@ -173,7 +179,7 @@ pub enum FileName { /// version accepts errors #[cfg(feature = "rkyv-impl")] #[derive(Debug, Clone, Copy)] -#[cfg_attr(feature = "rkyv-impl", derive(rkyv::bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct EncodePathBuf; @@ -183,22 +189,18 @@ impl rkyv::with::ArchiveWith for EncodePathBuf { type Resolver = rkyv::string::StringResolver; #[inline] - unsafe fn resolve_with( - field: &PathBuf, - pos: usize, - resolver: Self::Resolver, - out: *mut Self::Archived, - ) { + fn resolve_with(field: &PathBuf, resolver: Self::Resolver, out: rkyv::Place) { // It's safe to unwrap here because if the OsString wasn't valid UTF-8 it would // have failed to serialize - rkyv::string::ArchivedString::resolve_from_str(field.to_str().unwrap(), pos, resolver, out); + rkyv::string::ArchivedString::resolve_from_str(field.to_str().unwrap(), resolver, out); } } #[cfg(feature = "rkyv-impl")] impl rkyv::with::SerializeWith for EncodePathBuf where - S: ?Sized + rkyv::ser::Serializer, + S: ?Sized + rancor::Fallible + rkyv::ser::Writer, + S::Error: rancor::Source, { #[inline] fn serialize_with(field: &PathBuf, serializer: &mut S) -> Result { @@ -210,7 +212,7 @@ where #[cfg(feature = "rkyv-impl")] impl rkyv::with::DeserializeWith for EncodePathBuf where - D: ?Sized + rkyv::Fallible, + D: ?Sized + rancor::Fallible, { #[inline] fn deserialize_with( @@ -224,7 +226,7 @@ where /// A wrapper that attempts to convert a Url to and from String. #[cfg(feature = "rkyv-impl")] #[derive(Debug, Clone, Copy)] -#[cfg_attr(feature = "rkyv-impl", derive(rkyv::bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct EncodeUrl; @@ -234,20 +236,16 @@ impl rkyv::with::ArchiveWith for EncodeUrl { type Resolver = rkyv::string::StringResolver; #[inline] - unsafe fn resolve_with( - field: &Url, - pos: usize, - resolver: Self::Resolver, - out: *mut Self::Archived, - ) { - rkyv::string::ArchivedString::resolve_from_str(field.as_str(), pos, resolver, out); + fn resolve_with(field: &Url, resolver: Self::Resolver, out: rkyv::Place) { + rkyv::string::ArchivedString::resolve_from_str(field.as_str(), resolver, out); } } #[cfg(feature = "rkyv-impl")] impl rkyv::with::SerializeWith for EncodeUrl where - S: ?Sized + rkyv::ser::Serializer, + S: ?Sized + rancor::Fallible + rkyv::ser::Writer, + S::Error: rancor::Source, { #[inline] fn serialize_with(field: &Url, serializer: &mut S) -> Result { @@ -259,7 +257,7 @@ where #[cfg(feature = "rkyv-impl")] impl rkyv::with::DeserializeWith, Url, D> for EncodeUrl where - D: ?Sized + rkyv::Fallible, + D: ?Sized + rancor::Fallible, { #[inline] fn deserialize_with(field: &rkyv::string::ArchivedString, _: &mut D) -> Result { @@ -337,8 +335,8 @@ impl FileName { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct PrimarySpanLabel(pub Span, pub String); /// A collection of spans. Spans have two orthogonal attributes: @@ -356,8 +354,8 @@ pub struct PrimarySpanLabel(pub Span, pub String); any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct MultiSpan { primary_spans: Vec, span_labels: Vec, @@ -652,8 +650,8 @@ pub const NO_EXPANSION: SyntaxContext = SyntaxContext::empty(); any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] #[derive(Copy, Clone, Eq, PartialEq, Debug)] pub struct MultiByteChar { /// The absolute offset of the character in the SourceMap @@ -682,8 +680,8 @@ impl MultiByteChar { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(u32))] #[derive(Copy, Clone, Eq, PartialEq, Debug)] pub enum NonNarrowChar { /// Represents a zero-width character @@ -750,7 +748,7 @@ impl Sub for NonNarrowChar { #[doc(hidden)] #[cfg(feature = "rkyv-impl")] #[derive(Debug, Clone, Copy)] -#[cfg_attr(feature = "rkyv-impl", derive(rkyv::bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct EncodeArcString; @@ -759,21 +757,21 @@ impl rkyv::with::ArchiveWith> for EncodeArcString { type Archived = rkyv::Archived; type Resolver = rkyv::Resolver; - unsafe fn resolve_with( + fn resolve_with( field: &Lrc, - pos: usize, resolver: Self::Resolver, - out: *mut Self::Archived, + out: rkyv::Place, ) { let s = field.to_string(); - rkyv::Archive::resolve(&s, pos, resolver, out); + rkyv::Archive::resolve(&s, resolver, out); } } #[cfg(feature = "rkyv-impl")] impl rkyv::with::SerializeWith, S> for EncodeArcString where - S: ?Sized + rkyv::ser::Serializer, + S: ?Sized + rancor::Fallible + rkyv::ser::Writer, + S::Error: rancor::Source, { fn serialize_with(field: &Lrc, serializer: &mut S) -> Result { rkyv::string::ArchivedString::serialize_from_str(field, serializer) @@ -783,7 +781,7 @@ where #[cfg(feature = "rkyv-impl")] impl rkyv::with::DeserializeWith, Lrc, D> for EncodeArcString where - D: ?Sized + rkyv::Fallible, + D: ?Sized + rancor::Fallible, { fn deserialize_with( field: &rkyv::Archived, @@ -802,8 +800,8 @@ where any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] #[derive(Clone)] pub struct SourceFile { /// The name of the file that the source came from. Source that doesn't @@ -819,7 +817,7 @@ pub struct SourceFile { /// Indicates which crate this `SourceFile` was imported from. pub crate_of_origin: u32, /// The complete source code - #[cfg_attr(any(feature = "rkyv-impl"), with(EncodeArcString))] + #[cfg_attr(any(feature = "rkyv-impl"), rkyv(with = EncodeArcString))] pub src: Lrc, /// The source code's hash pub src_hash: u128, @@ -837,6 +835,24 @@ pub struct SourceFile { pub name_hash: u128, } +#[cfg_attr( + any(feature = "rkyv-impl"), + derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) +)] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] +#[derive(Clone)] +pub struct SourceFileAnalysis { + /// Locations of lines beginnings in the source code + pub lines: Vec, + /// Locations of multi-byte characters in the source code + pub multibyte_chars: Vec, + /// Width of characters that are not narrow in the source code + pub non_narrow_chars: Vec, + /// A hash of the filename, used for speeding up the incr. comp. hashing. + pub name_hash: u128, +} + impl fmt::Debug for SourceFile { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { write!(fmt, "SourceFile({})", self.name) @@ -1024,9 +1040,9 @@ pub trait SmallPos { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] -pub struct BytePos(#[cfg_attr(feature = "__rkyv", omit_bounds)] pub u32); +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] +pub struct BytePos(#[cfg_attr(feature = "__rkyv", rkyv(omit_bounds))] pub u32); impl BytePos { /// Dummy position. This is reserved for synthesized spans. @@ -1064,8 +1080,8 @@ impl BytePos { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)] pub struct CharPos(pub usize); @@ -1180,8 +1196,8 @@ pub struct Loc { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Debug, Clone) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct PartialLoc { pub source_file: Option>, pub line: usize, @@ -1211,8 +1227,8 @@ pub struct SourceFileAndLine { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] #[derive(Debug)] pub struct SourceFileAndBytePos { pub sf: Lrc, @@ -1224,8 +1240,8 @@ pub struct SourceFileAndBytePos { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct LineInfo { /// Index of line, starting from 0. pub line_index: usize, @@ -1264,8 +1280,8 @@ pub struct FileLines { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Debug, Clone) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct PartialFileLines { pub file: Option>, pub lines: Vec, @@ -1285,8 +1301,8 @@ pub type PartialFileLinesResult = Result>; any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(u32))] pub enum SpanLinesError { IllFormedSpan(Span), DistinctSources(DistinctSources), @@ -1297,8 +1313,8 @@ pub enum SpanLinesError { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(u32))] pub enum SpanSnippetError { DummyBytePos, IllFormedSpan(Span), @@ -1317,8 +1333,8 @@ pub enum SpanSnippetError { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(u32))] pub enum SourceMapLookupError { NoFileFor(BytePos), } @@ -1328,8 +1344,8 @@ pub enum SourceMapLookupError { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct FilePos(pub Lrc, pub BytePos); #[derive(Clone, PartialEq, Eq, Debug)] @@ -1337,8 +1353,8 @@ pub struct FilePos(pub Lrc, pub BytePos); any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct DistinctSources { pub begin: FilePos, pub end: FilePos, @@ -1349,8 +1365,8 @@ pub struct DistinctSources { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct MalformedSourceMapPositions { pub name: Lrc, pub source_len: usize, diff --git a/crates/swc_common/src/syntax_pos/hygiene.rs b/crates/swc_common/src/syntax_pos/hygiene.rs index 59f6218b84e8..5fb3bba233fd 100644 --- a/crates/swc_common/src/syntax_pos/hygiene.rs +++ b/crates/swc_common/src/syntax_pos/hygiene.rs @@ -34,9 +34,9 @@ use crate::{collections::AHashMap, EqIgnoreSpan}; any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] -pub struct SyntaxContext(#[cfg_attr(feature = "__rkyv", omit_bounds)] u32); +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] +pub struct SyntaxContext(#[cfg_attr(feature = "__rkyv", rkyv(omit_bounds))] u32); #[cfg(feature = "arbitrary")] #[cfg_attr(docsrs, doc(cfg(feature = "arbitrary")))] @@ -86,8 +86,8 @@ pub(crate) struct MarkData { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct MutableMarkContext(pub u32, pub u32, pub u32); // List of proxy calls injected by the host in the plugin's runtime context. diff --git a/crates/swc_common/tests/attr_interop.rs b/crates/swc_common/tests/attr_interop.rs index 79bda1819ec1..84be81e5b082 100644 --- a/crates/swc_common/tests/attr_interop.rs +++ b/crates/swc_common/tests/attr_interop.rs @@ -19,19 +19,24 @@ pub struct Tuple(#[span] HasSpan, usize, usize); derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] #[cfg_attr( - any(feature = "rkyv-impl"), - archive(bound(serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace")) + feature = "rkyv-impl", + rkyv(serialize_bounds(__S: rkyv::ser::Writer + rkyv::ser::Allocator, + __S::Error: rkyv::rancor::Source)) +)] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr( + feature = "rkyv-impl", + rkyv(deserialize_bounds(__D::Error: rkyv::rancor::Source)) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] #[cfg_attr( feature = "rkyv-impl", - archive_attr(check_bytes(bound = "__C: rkyv::validation::ArchiveContext, <__C as \ - rkyv::Fallible>::Error: std::error::Error")) + bytecheck(bounds( + __C: rkyv::validation::ArchiveContext + )) )] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct HasSpan { - #[cfg_attr(feature = "__rkyv", omit_bounds)] - #[cfg_attr(feature = "__rkyv", archive_attr(omit_bounds))] + #[cfg_attr(feature = "__rkyv", rkyv(omit_bounds))] pub span: Span, } diff --git a/crates/swc_css_ast/Cargo.toml b/crates/swc_css_ast/Cargo.toml index 9e221d7fb0b1..059c0224cde6 100644 --- a/crates/swc_css_ast/Cargo.toml +++ b/crates/swc_css_ast/Cargo.toml @@ -12,17 +12,26 @@ version = "4.0.1" bench = false [features] -__rkyv = [] -default = [] -rkyv-impl = ["__rkyv", "rkyv", "swc_atoms/rkyv-impl", "swc_common/rkyv-impl"] +__rkyv = [] +default = [] +rkyv-impl = [ + "__rkyv", + "rkyv", + "swc_atoms/rkyv-impl", + "swc_common/rkyv-impl", + "bytecheck", + "rancor", +] serde-impl = ["serde"] [dependencies] +bytecheck = { workspace = true, optional = true } is-macro = { workspace = true } +rancor = { workspace = true, optional = true } serde = { workspace = true, features = ["derive"], optional = true } string_enum = { version = "1.0.0", path = "../string_enum/" } swc_atoms = { version = "2.0.0", path = "../swc_atoms" } swc_common = { version = "4.0.0", path = "../swc_common" } -rkyv = { workspace = true, features = ["validation"], optional = true } +rkyv = { workspace = true, optional = true } diff --git a/crates/swc_css_ast/src/at_rule.rs b/crates/swc_css_ast/src/at_rule.rs index 8d5125d0f251..062c200b41b0 100644 --- a/crates/swc_css_ast/src/at_rule.rs +++ b/crates/swc_css_ast/src/at_rule.rs @@ -449,12 +449,13 @@ pub struct MediaFeatureBoolean { feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv", archive(check_bytes))] -#[cfg_attr(feature = "rkyv", archive_attr(repr(u32)))] -#[cfg_attr( - feature = "rkyv", - archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer")) -)] +#[cfg_attr(feature = "rkyv", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv", repr(u32))] +//#[cfg_attr( +// feature = "rkyv", +// archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + +// rkyv::ser::Serializer")) +//)] pub enum MediaFeatureRangeComparison { /// `<` Lt, @@ -775,12 +776,13 @@ pub struct SizeFeatureBoolean { feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv", archive(check_bytes))] -#[cfg_attr(feature = "rkyv", archive_attr(repr(u32)))] -#[cfg_attr( - feature = "rkyv", - archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer")) -)] +#[cfg_attr(feature = "rkyv", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv", repr(u32))] +//#[cfg_attr( +// feature = "rkyv", +// archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + +// rkyv::ser::Serializer")) +//)] pub enum SizeFeatureRangeComparison { /// `<` Lt, diff --git a/crates/swc_css_ast/src/selector.rs b/crates/swc_css_ast/src/selector.rs index 6217fdb8d90f..4854ac390c29 100644 --- a/crates/swc_css_ast/src/selector.rs +++ b/crates/swc_css_ast/src/selector.rs @@ -125,10 +125,11 @@ pub struct Combinator { )] #[cfg_attr( feature = "rkyv", - archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer")) + rkyv(serialize_bounds(__S: rkyv::ser::Writer + rkyv::ser::Allocator, + __S::Error: rkyv::rancor::Source)) )] -#[cfg_attr(feature = "rkyv", archive(check_bytes))] -#[cfg_attr(feature = "rkyv", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv", repr(u32))] pub enum CombinatorValue { /// ` ` Descendant, @@ -262,12 +263,13 @@ pub struct AttributeSelector { feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv", archive(check_bytes))] -#[cfg_attr(feature = "rkyv", archive_attr(repr(u32)))] -#[cfg_attr( - feature = "rkyv", - archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer")) -)] +#[cfg_attr(feature = "rkyv", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv", repr(u32))] +//#[cfg_attr( +// feature = "rkyv", +// archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + +// rkyv::ser::Serializer")) +//)] pub enum AttributeSelectorMatcherValue { /// `=` Equals, diff --git a/crates/swc_css_ast/src/token.rs b/crates/swc_css_ast/src/token.rs index 23848b84cac0..b366083d422f 100644 --- a/crates/swc_css_ast/src/token.rs +++ b/crates/swc_css_ast/src/token.rs @@ -31,8 +31,8 @@ impl Take for TokenAndSpan { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct UrlKeyValue(pub Atom, pub Atom); #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Is, EqIgnoreSpan)] @@ -40,11 +40,12 @@ pub struct UrlKeyValue(pub Atom, pub Atom); feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv", archive(check_bytes))] -#[cfg_attr(feature = "rkyv", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv", repr(u32))] #[cfg_attr( feature = "rkyv", - archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer")) + rkyv(serialize_bounds(__S: rkyv::ser::Writer + rkyv::ser::Allocator, + __S::Error: rkyv::rancor::Source)) )] #[cfg_attr(feature = "serde-impl", derive(Serialize, Deserialize))] pub enum NumberType { @@ -59,8 +60,8 @@ pub enum NumberType { feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv", archive(check_bytes))] -#[cfg_attr(feature = "rkyv", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv", repr(C))] #[cfg_attr(feature = "serde-impl", derive(Serialize, Deserialize))] pub struct DimensionToken { pub value: f64, @@ -78,11 +79,12 @@ pub struct DimensionToken { feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv", archive(check_bytes))] -#[cfg_attr(feature = "rkyv", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv", repr(u32))] #[cfg_attr( feature = "rkyv", - archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer")) + rkyv(serialize_bounds(__S: rkyv::ser::Writer + rkyv::ser::Allocator, + __S::Error: rkyv::rancor::Source)) )] #[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))] pub enum Token { diff --git a/crates/swc_css_ast/src/value.rs b/crates/swc_css_ast/src/value.rs index bf2130982df4..97a155478f83 100644 --- a/crates/swc_css_ast/src/value.rs +++ b/crates/swc_css_ast/src/value.rs @@ -121,10 +121,11 @@ impl EqIgnoreSpan for Str { )] #[cfg_attr( feature = "rkyv", - archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer")) + rkyv(serialize_bounds(__S: rkyv::ser::Writer + rkyv::ser::Allocator, + __S::Error: rkyv::rancor::Source)) )] -#[cfg_attr(feature = "rkyv", archive(check_bytes))] -#[cfg_attr(feature = "rkyv", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv", repr(u32))] pub enum DelimiterValue { /// `,` Comma, @@ -397,10 +398,11 @@ pub struct Ratio { )] #[cfg_attr( feature = "rkyv", - archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer")) + rkyv(serialize_bounds(__S: rkyv::ser::Writer + rkyv::ser::Allocator, + __S::Error: rkyv::rancor::Source)) )] -#[cfg_attr(feature = "rkyv", archive(check_bytes))] -#[cfg_attr(feature = "rkyv", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv", repr(u32))] pub enum BinOp { /// `+` Add, @@ -503,10 +505,11 @@ pub struct CalcOperator { )] #[cfg_attr( feature = "rkyv", - archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer")) + rkyv(serialize_bounds(__S: rkyv::ser::Writer + rkyv::ser::Allocator, + __S::Error: rkyv::rancor::Source)) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(u32))] pub enum CalcOperatorType { /// `+` Add, diff --git a/crates/swc_ecma_ast/Cargo.toml b/crates/swc_ecma_ast/Cargo.toml index b71d30758ab4..7c3d4625dd4d 100644 --- a/crates/swc_ecma_ast/Cargo.toml +++ b/crates/swc_ecma_ast/Cargo.toml @@ -23,20 +23,21 @@ rkyv-impl = [ "__rkyv", "rkyv", "bytecheck", + "rancor", "swc_atoms/rkyv-impl", "swc_common/rkyv-impl", ] serde-impl = ["serde"] [dependencies] -arbitrary = { workspace = true, features = ["derive"], optional = true } -bitflags = { workspace = true } -# bytecheck version should be in sync with rkyv version. Do not bump individually. +arbitrary = { workspace = true, features = ["derive"], optional = true } +bitflags = { workspace = true } bytecheck = { workspace = true, optional = true } is-macro = { workspace = true } num-bigint = { workspace = true, features = ["serde"] } phf = { workspace = true, features = ["macros"] } -rkyv = { workspace = true, features = ["validation"], optional = true } +rancor = { workspace = true, optional = true } +rkyv = { workspace = true, optional = true } scoped-tls = { workspace = true } serde = { workspace = true, features = ["derive"], optional = true } string_enum = { version = "1.0.0", path = "../string_enum" } diff --git a/crates/swc_ecma_ast/src/class.rs b/crates/swc_ecma_ast/src/class.rs index cd533be85018..a5f21a5204cb 100644 --- a/crates/swc_ecma_ast/src/class.rs +++ b/crates/swc_ecma_ast/src/class.rs @@ -259,8 +259,8 @@ pub struct Decorator { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(u32))] #[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))] pub enum MethodKind { #[default] diff --git a/crates/swc_ecma_ast/src/decl.rs b/crates/swc_ecma_ast/src/decl.rs index af6d89e7ab84..722ef16887c2 100644 --- a/crates/swc_ecma_ast/src/decl.rs +++ b/crates/swc_ecma_ast/src/decl.rs @@ -172,8 +172,8 @@ impl Take for VarDecl { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(u32))] pub enum VarDeclKind { /// `var` #[default] diff --git a/crates/swc_ecma_ast/src/expr.rs b/crates/swc_ecma_ast/src/expr.rs index 9e1f06a87175..421cee36f080 100644 --- a/crates/swc_ecma_ast/src/expr.rs +++ b/crates/swc_ecma_ast/src/expr.rs @@ -1077,8 +1077,8 @@ pub struct MetaPropExpr { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(u32))] pub enum MetaPropKind { /// `new.target` NewTarget, @@ -1271,25 +1271,29 @@ impl Take for Import { )] #[cfg_attr( feature = "rkyv", - archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer")) + rkyv(serialize_bounds(__S: rkyv::ser::Writer + rkyv::ser::Allocator, + __S::Error: rkyv::rancor::Source)) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] #[cfg_attr( feature = "rkyv-impl", - archive_attr(check_bytes(bound = "__C: rkyv::validation::ArchiveContext, <__C as \ - rkyv::Fallible>::Error: std::error::Error")) + rkyv(deserialize_bounds(__D::Error: rkyv::rancor::Source)) )] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr( + feature = "rkyv-impl", + rkyv(bytecheck(bounds( + __C: rkyv::validation::ArchiveContext, + __C::Error: rkyv::rancor::Source + ))) + )] +#[cfg_attr(feature = "rkyv-impl", repr(C))] #[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))] pub struct ExprOrSpread { #[cfg_attr(feature = "serde-impl", serde(default))] - #[cfg_attr(feature = "__rkyv", omit_bounds)] - #[cfg_attr(feature = "__rkyv", archive_attr(omit_bounds))] + #[cfg_attr(feature = "__rkyv", rkyv(omit_bounds))] pub spread: Option, #[cfg_attr(feature = "serde-impl", serde(rename = "expression"))] - #[cfg_attr(feature = "__rkyv", omit_bounds)] - #[cfg_attr(feature = "__rkyv", archive_attr(omit_bounds))] + #[cfg_attr(feature = "__rkyv", rkyv(omit_bounds))] pub expr: Box, } diff --git a/crates/swc_ecma_ast/src/ident.rs b/crates/swc_ecma_ast/src/ident.rs index 432b91895a31..82f287d8f3fb 100644 --- a/crates/swc_ecma_ast/src/ident.rs +++ b/crates/swc_ecma_ast/src/ident.rs @@ -20,26 +20,30 @@ use crate::{typescript::TsTypeAnn, Expr}; derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] #[cfg_attr( - feature = "rkyv", - archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer")) + feature = "rkyv-impl", + rkyv(serialize_bounds(__S: rkyv::ser::Writer + rkyv::ser::Allocator, + __S::Error: rkyv::rancor::Source)) +)] +#[cfg_attr( + feature = "rkyv-impl", + rkyv(deserialize_bounds(__D::Error: rkyv::rancor::Source)) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] #[cfg_attr( feature = "rkyv-impl", - archive_attr(check_bytes(bound = "__C: rkyv::validation::ArchiveContext, <__C as \ - rkyv::Fallible>::Error: std::error::Error")) + rkyv(bytecheck(bounds( + __C: rkyv::validation::ArchiveContext, + __C::Error: rkyv::rancor::Source + ))) )] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))] +#[cfg_attr(feature = "rkyv-impl", repr(C))] #[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))] pub struct BindingIdent { #[cfg_attr(feature = "serde-impl", serde(flatten))] - #[cfg_attr(feature = "__rkyv", omit_bounds)] - #[cfg_attr(feature = "__rkyv", archive_attr(omit_bounds))] + #[cfg_attr(feature = "__rkyv", rkyv(omit_bounds))] pub id: Ident, #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeAnnotation"))] - #[cfg_attr(feature = "__rkyv", omit_bounds)] - #[cfg_attr(feature = "__rkyv", archive_attr(omit_bounds))] + #[cfg_attr(feature = "__rkyv", rkyv(omit_bounds))] pub type_ann: Option>, } @@ -165,10 +169,10 @@ bridge_from!(BindingIdent, Ident, Id); #[ast_node("Identifier")] #[derive(Eq, Hash, Default)] pub struct Ident { - #[cfg_attr(feature = "__rkyv", omit_bounds)] + #[cfg_attr(feature = "__rkyv", rkyv(omit_bounds))] pub span: Span, - #[cfg_attr(feature = "__rkyv", omit_bounds)] + #[cfg_attr(feature = "__rkyv", rkyv(omit_bounds))] pub ctxt: SyntaxContext, #[cfg_attr(feature = "serde-impl", serde(rename = "value"))] @@ -382,7 +386,7 @@ impl Ident { #[derive(Eq, Hash, Default, EqIgnoreSpan)] #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] pub struct IdentName { - #[cfg_attr(feature = "__rkyv", omit_bounds)] + #[cfg_attr(feature = "__rkyv", rkyv(omit_bounds))] pub span: Span, #[cfg_attr(feature = "serde-impl", serde(rename = "value"))] diff --git a/crates/swc_ecma_ast/src/lit.rs b/crates/swc_ecma_ast/src/lit.rs index 9df499510689..bc2d6181cf81 100644 --- a/crates/swc_ecma_ast/src/lit.rs +++ b/crates/swc_ecma_ast/src/lit.rs @@ -79,7 +79,7 @@ impl Lit { #[derive(Eq, Hash)] pub struct BigInt { pub span: Span, - #[cfg_attr(any(feature = "rkyv-impl"), with(EncodeBigInt))] + #[cfg_attr(any(feature = "rkyv-impl"), rkyv(with = EncodeBigInt))] pub value: Box, /// Use `None` value only for transformations to avoid recalculate @@ -95,7 +95,7 @@ impl EqIgnoreSpan for BigInt { #[cfg(feature = "rkyv-impl")] #[derive(Debug, Clone, Copy)] -#[cfg_attr(feature = "rkyv-impl", derive(rkyv::bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] #[cfg_attr(feature = "rkyv-impl", repr(C))] pub struct EncodeBigInt; @@ -104,23 +104,23 @@ impl rkyv::with::ArchiveWith> for EncodeBigInt { type Archived = rkyv::Archived; type Resolver = rkyv::Resolver; - unsafe fn resolve_with( + fn resolve_with( field: &Box, - pos: usize, resolver: Self::Resolver, - out: *mut Self::Archived, + out: rkyv::Place, ) { use rkyv::Archive; let s = field.to_string(); - s.resolve(pos, resolver, out); + s.resolve(resolver, out); } } #[cfg(feature = "rkyv-impl")] impl rkyv::with::SerializeWith, S> for EncodeBigInt where - S: ?Sized + rkyv::ser::Serializer, + S: ?Sized + rancor::Fallible + rkyv::ser::Writer, + S::Error: rancor::Source, { fn serialize_with( field: &Box, @@ -134,7 +134,7 @@ where #[cfg(feature = "rkyv-impl")] impl rkyv::with::DeserializeWith, Box, D> for EncodeBigInt where - D: ?Sized + rkyv::Fallible, + D: ?Sized + rancor::Fallible, { fn deserialize_with( field: &rkyv::Archived, diff --git a/crates/swc_ecma_ast/src/module_decl.rs b/crates/swc_ecma_ast/src/module_decl.rs index df23cd837afd..ec9443adf4e0 100644 --- a/crates/swc_ecma_ast/src/module_decl.rs +++ b/crates/swc_ecma_ast/src/module_decl.rs @@ -138,8 +138,8 @@ pub struct ImportDecl { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(u32))] #[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))] pub enum ImportPhase { #[default] diff --git a/crates/swc_ecma_ast/src/operators.rs b/crates/swc_ecma_ast/src/operators.rs index f64efc54a3af..16623ea00cae 100644 --- a/crates/swc_ecma_ast/src/operators.rs +++ b/crates/swc_ecma_ast/src/operators.rs @@ -7,8 +7,8 @@ use swc_common::EqIgnoreSpan; any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(u32))] pub enum BinaryOp { /// `==` #[default] @@ -119,8 +119,8 @@ impl BinaryOp { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(u32))] pub enum AssignOp { /// `=` #[default] @@ -195,8 +195,8 @@ impl AssignOp { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(u32))] pub enum UpdateOp { /// `++` #[default] @@ -211,8 +211,8 @@ pub enum UpdateOp { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(u32))] pub enum UnaryOp { /// `-` Minus, diff --git a/crates/swc_ecma_ast/src/typescript.rs b/crates/swc_ecma_ast/src/typescript.rs index 59ec642a7c81..534e5eb860c5 100644 --- a/crates/swc_ecma_ast/src/typescript.rs +++ b/crates/swc_ecma_ast/src/typescript.rs @@ -387,8 +387,8 @@ pub struct TsKeywordType { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(u32))] #[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))] pub enum TsKeywordTypeKind { #[cfg_attr(feature = "serde-impl", serde(rename = "any"))] @@ -671,8 +671,8 @@ pub struct TsTypeOperator { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(u32))] pub enum TsTypeOperatorOp { /// `keyof` KeyOf, @@ -699,8 +699,8 @@ pub struct TsIndexedAccessType { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(u32))] pub enum TruePlusMinus { True, Plus, @@ -1073,8 +1073,8 @@ pub struct TsSatisfiesExpr { any(feature = "rkyv-impl"), derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv-impl", archive(check_bytes))] -#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))] +#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv-impl", repr(u32))] #[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))] pub enum Accessibility { #[cfg_attr(feature = "serde-impl", serde(rename = "public"))] diff --git a/crates/swc_html_ast/Cargo.toml b/crates/swc_html_ast/Cargo.toml index fa875627cf91..57384c5f7621 100644 --- a/crates/swc_html_ast/Cargo.toml +++ b/crates/swc_html_ast/Cargo.toml @@ -15,14 +15,21 @@ version = "4.0.1" bench = false [features] -__rkyv = [] -rkyv-impl = ["__rkyv", "rkyv", "swc_atoms/rkyv-impl", "swc_common/rkyv-impl"] +__rkyv = [] +rkyv-impl = [ + "__rkyv", + "rkyv", + "swc_atoms/rkyv-impl", + "swc_common/rkyv-impl", + "bytecheck", +] serde-impl = ["serde"] [dependencies] -is-macro = { workspace = true } -rkyv = { workspace = true, features = ["validation"], optional = true } -serde = { workspace = true, features = ["derive"], optional = true } +bytecheck = { workspace = true, optional = true } +is-macro = { workspace = true } +rkyv = { workspace = true, optional = true } +serde = { workspace = true, features = ["derive"], optional = true } string_enum = { version = "1.0.0", path = "../string_enum/" } swc_atoms = { version = "2.0.0", path = "../swc_atoms" } diff --git a/crates/swc_html_ast/src/base.rs b/crates/swc_html_ast/src/base.rs index 624b02c96327..7bbb4f7a816a 100644 --- a/crates/swc_html_ast/src/base.rs +++ b/crates/swc_html_ast/src/base.rs @@ -23,11 +23,13 @@ pub struct DocumentFragment { feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv", archive(check_bytes))] -#[cfg_attr( - feature = "rkyv", - archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer")) -)] +#[cfg_attr(feature = "rkyv", derive(bytecheck::CheckBytes))] +//#[cfg_attr( +// feature = "rkyv", +// archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + +// rkyv::ser::Serializer")) +//)] +#[cfg_attr(feature = "rkyv", repr(u32))] pub enum DocumentMode { /// `no-quirks` NoQuirks, @@ -76,11 +78,13 @@ impl EqIgnoreSpan for DocumentType { feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "rkyv", archive(check_bytes))] -#[cfg_attr( - feature = "rkyv", - archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer")) -)] +#[cfg_attr(feature = "rkyv", derive(bytecheck::CheckBytes))] +//#[cfg_attr( +// feature = "rkyv", +// archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + +// rkyv::ser::Serializer")) +//)] +#[cfg_attr(feature = "rkyv", repr(u32))] pub enum Namespace { /// `http://www.w3.org/1999/xhtml` HTML, diff --git a/crates/swc_html_ast/src/token.rs b/crates/swc_html_ast/src/token.rs index fae47a43f7d2..56a08195a0ad 100644 --- a/crates/swc_html_ast/src/token.rs +++ b/crates/swc_html_ast/src/token.rs @@ -27,9 +27,11 @@ pub struct AttributeToken { )] #[cfg_attr( feature = "rkyv", - archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer")) + rkyv(serialize_bounds(__S: rkyv::ser::Writer + rkyv::ser::Allocator, + __S::Error: rkyv::rancor::Source)) )] -#[cfg_attr(feature = "rkyv", archive(check_bytes))] +#[cfg_attr(feature = "rkyv", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv", repr(u32))] #[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))] pub enum Raw { Same, @@ -43,9 +45,11 @@ pub enum Raw { )] #[cfg_attr( feature = "rkyv", - archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer")) + rkyv(serialize_bounds(__S: rkyv::ser::Writer + rkyv::ser::Allocator, + __S::Error: rkyv::rancor::Source)) )] -#[cfg_attr(feature = "rkyv", archive(check_bytes))] +#[cfg_attr(feature = "rkyv", derive(bytecheck::CheckBytes))] +#[cfg_attr(feature = "rkyv", repr(u32))] #[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))] pub enum Token { Doctype { diff --git a/crates/swc_plugin_proxy/Cargo.toml b/crates/swc_plugin_proxy/Cargo.toml index 554bf0e81b11..d28355b2ee7f 100644 --- a/crates/swc_plugin_proxy/Cargo.toml +++ b/crates/swc_plugin_proxy/Cargo.toml @@ -12,7 +12,7 @@ bench = false [features] __rkyv = [] -rkyv-impl = ["__rkyv", "rkyv"] +rkyv-impl = ["__rkyv", "rkyv", "bytecheck", "rancor"] __plugin_mode = [] __plugin_rt = [] @@ -22,8 +22,10 @@ plugin-rt = ["__plugin_rt", "swc_common/plugin-base", "rkyv-impl"] [dependencies] -rkyv = { workspace = true, features = ["validation"], optional = true } -tracing = { workspace = true } +bytecheck = { workspace = true, optional = true } +rancor = { workspace = true, optional = true } +rkyv = { workspace = true, optional = true } +tracing = { workspace = true } better_scoped_tls = { version = "1.0.0", path = "../better_scoped_tls" } swc_common = { version = "4.0.0", path = "../swc_common" } diff --git a/crates/swc_plugin_proxy/src/comments/plugin_comments_proxy.rs b/crates/swc_plugin_proxy/src/comments/plugin_comments_proxy.rs index 353ebe97e0d3..78759222fdcf 100644 --- a/crates/swc_plugin_proxy/src/comments/plugin_comments_proxy.rs +++ b/crates/swc_plugin_proxy/src/comments/plugin_comments_proxy.rs @@ -48,7 +48,16 @@ impl PluginCommentsProxy { #[cfg_attr(not(target_arch = "wasm32"), allow(unused))] fn allocate_comments_buffer_to_host(&self, value: T) where - T: rkyv::Serialize>, + T: for<'a> rkyv::Serialize< + rancor::Strategy< + rkyv::ser::Serializer< + rkyv::util::AlignedVec, + rkyv::ser::allocator::ArenaHandle<'a>, + rkyv::ser::sharing::Share, + >, + rancor::Error, + >, + >, { #[cfg(target_arch = "wasm32")] { diff --git a/crates/swc_plugin_proxy/src/memory_interop/read_returned_result_from_host.rs b/crates/swc_plugin_proxy/src/memory_interop/read_returned_result_from_host.rs index 386b3fd25600..63b003e4f1a6 100644 --- a/crates/swc_plugin_proxy/src/memory_interop/read_returned_result_from_host.rs +++ b/crates/swc_plugin_proxy/src/memory_interop/read_returned_result_from_host.rs @@ -7,8 +7,7 @@ use swc_common::plugin::serialized::PluginSerializedBytes; feature = "__rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -#[cfg_attr(feature = "__rkyv", archive(check_bytes))] -#[cfg_attr(feature = "__rkyv", archive_attr(repr(C)))] +#[cfg_attr(feature = "__rkyv", repr(C))] pub struct AllocatedBytesPtr(pub u32, pub u32); #[cfg(target_arch = "wasm32")] @@ -92,8 +91,16 @@ pub fn read_returned_result_from_host(f: F) -> Option where F: FnOnce(u32) -> u32, R: rkyv::Archive, - R::Archived: rkyv::Deserialize - + for<'a> rkyv::CheckBytes>, + R::Archived: rkyv::Deserialize>, + for<'a> R::Archived: bytecheck::CheckBytes< + rancor::Strategy< + rkyv::validation::Validator< + rkyv::validation::archive::ArchiveValidator<'a>, + rkyv::validation::shared::SharedValidator, + >, + rancor::Error, + >, + >, { let allocated_returned_value_ptr = read_returned_result_from_host_inner(f);