From 185d6f55b35f4f8323035c1199cddb9dd547f254 Mon Sep 17 00:00:00 2001 From: jinrui Date: Fri, 20 Sep 2024 15:46:05 +0800 Subject: [PATCH] fix(ast): Add `archive(check_bytes)` to all relevant AST types (#9574) **Description:** 1. all struct that support rkyv add `archive(check_bytes)` 2. all recursive struct that support rkyv add check_bytes bound this PR will fix the error reported in https://github.com/swc-project/swc/pull/9562 --- .changeset/tough-cooks-stare.md | 8 ++++++++ crates/swc_common/tests/attr_interop.rs | 6 ++++++ crates/swc_ecma_ast/src/expr.rs | 7 +++++++ crates/swc_ecma_ast/src/ident.rs | 7 +++++++ crates/swc_html_ast/src/base.rs | 2 ++ crates/swc_html_ast/src/token.rs | 2 ++ 6 files changed, 32 insertions(+) create mode 100644 .changeset/tough-cooks-stare.md diff --git a/.changeset/tough-cooks-stare.md b/.changeset/tough-cooks-stare.md new file mode 100644 index 00000000000..99fddc16df5 --- /dev/null +++ b/.changeset/tough-cooks-stare.md @@ -0,0 +1,8 @@ +--- +swc_core: patch +swc_common: patch +swc_ecma_ast: patch +swc_html_ast: patch +--- + +fix: all struct that support rkyv add `archive(check_bytes)` diff --git a/crates/swc_common/tests/attr_interop.rs b/crates/swc_common/tests/attr_interop.rs index 3be53899e45..79bda1819ec 100644 --- a/crates/swc_common/tests/attr_interop.rs +++ b/crates/swc_common/tests/attr_interop.rs @@ -23,9 +23,15 @@ pub struct Tuple(#[span] HasSpan, usize, usize); archive(bound(serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace")) )] #[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)))] pub struct HasSpan { #[cfg_attr(feature = "__rkyv", omit_bounds)] + #[cfg_attr(feature = "__rkyv", archive_attr(omit_bounds))] pub span: Span, } diff --git a/crates/swc_ecma_ast/src/expr.rs b/crates/swc_ecma_ast/src/expr.rs index aef159ae015..c57e414a1c5 100644 --- a/crates/swc_ecma_ast/src/expr.rs +++ b/crates/swc_ecma_ast/src/expr.rs @@ -1277,15 +1277,22 @@ impl Take for Import { archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer")) )] #[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 = "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))] 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))] pub expr: Box, } diff --git a/crates/swc_ecma_ast/src/ident.rs b/crates/swc_ecma_ast/src/ident.rs index d21de99f5b1..432b91895a3 100644 --- a/crates/swc_ecma_ast/src/ident.rs +++ b/crates/swc_ecma_ast/src/ident.rs @@ -24,15 +24,22 @@ use crate::{typescript::TsTypeAnn, Expr}; archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer")) )] #[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 = "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))] 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))] pub type_ann: Option>, } diff --git a/crates/swc_html_ast/src/base.rs b/crates/swc_html_ast/src/base.rs index 537372bc727..624b02c9632 100644 --- a/crates/swc_html_ast/src/base.rs +++ b/crates/swc_html_ast/src/base.rs @@ -23,6 +23,7 @@ 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")) @@ -75,6 +76,7 @@ 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")) diff --git a/crates/swc_html_ast/src/token.rs b/crates/swc_html_ast/src/token.rs index 4cc0e504da0..fae47a43f7d 100644 --- a/crates/swc_html_ast/src/token.rs +++ b/crates/swc_html_ast/src/token.rs @@ -29,6 +29,7 @@ pub struct AttributeToken { feature = "rkyv", archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer")) )] +#[cfg_attr(feature = "rkyv", archive(check_bytes))] #[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))] pub enum Raw { Same, @@ -44,6 +45,7 @@ pub enum Raw { feature = "rkyv", archive(bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer")) )] +#[cfg_attr(feature = "rkyv", archive(check_bytes))] #[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))] pub enum Token { Doctype {