Skip to content

Commit

Permalink
feat(swc/ast): update ast with bytecheck derive
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Jun 29, 2022
1 parent 1f3eba7 commit 08889d3
Show file tree
Hide file tree
Showing 22 changed files with 142 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions crates/ast_node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ pub fn ast_node(
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "rkyv",
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
)]
#[cfg_attr(
feature = "rkyv",
archive(bound(
Expand Down Expand Up @@ -269,6 +273,10 @@ pub fn ast_node(
feature = "rkyv",
archive(bound(serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace"))
)]
#[cfg_attr(
feature = "rkyv",
archive_attr(repr(C), derive(bytecheck::CheckBytes))
)]
serde_tag
#[serde(rename_all = "camelCase")]
serde_rename
Expand Down
1 change: 1 addition & 0 deletions crates/swc_atoms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ version = "0.2.12"
bench = false

[dependencies]
bytecheck = { version = "0.6.8", optional = true }
once_cell = "1"
rkyv = { version = "0.7.39", optional = true }
rustc-hash = "1.1.0"
Expand Down
1 change: 1 addition & 0 deletions crates/swc_atoms/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ include!(concat!(env!("OUT_DIR"), "/js_word.rs"));
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))]
pub struct Atom(Arc<str>);

impl Atom {
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ perf = ["parking_lot"]
plugin-base = ["anyhow", "rkyv-impl", "diagnostic-serde"]
plugin-mode = ["plugin-base"]
plugin-rt = ["plugin-base"]
rkyv-impl = ["rkyv"]
rkyv-impl = ["rkyv", "bytecheck"]
tty-emitter = ["atty", "termcolor"]

[dependencies]
Expand All @@ -34,7 +34,7 @@ arbitrary = { version = "1", optional = true, features = ["derive"] }
ast_node = { version = "0.7.5", path = "../ast_node" }
atty = { version = "0.2", optional = true }
better_scoped_tls = { version = "0.1.0", path = "../better_scoped_tls" }
bytecheck = "0.6.8"
bytecheck = { version = "0.6.8", optional = true }
cfg-if = "1.0.0"
debug_unreachable = "0.1.1"
either = "1.5"
Expand Down
5 changes: 5 additions & 0 deletions crates/swc_common/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ impl SingleThreadedComments {
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))]
pub struct Comment {
pub kind: CommentKind,
pub span: Span,
Expand All @@ -558,6 +559,10 @@ impl Spanned for Comment {
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "rkyv",
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
)]
pub enum CommentKind {
Line,
Block,
Expand Down
12 changes: 12 additions & 0 deletions crates/swc_common/src/errors/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ use crate::syntax_pos::{MultiSpan, Span};
feature = "plugin-base",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "plugin-base",
archive_attr(repr(C), derive(bytecheck::CheckBytes))
)]
pub struct Diagnostic {
pub level: Level,
pub message: Vec<(String, Style)>,
Expand All @@ -41,6 +45,10 @@ pub struct Diagnostic {
feature = "plugin-base",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "plugin-base",
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
)]
pub enum DiagnosticId {
Error(String),
Lint(String),
Expand All @@ -56,6 +64,10 @@ pub enum DiagnosticId {
feature = "plugin-base",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "plugin-base",
archive_attr(repr(C), derive(bytecheck::CheckBytes))
)]
pub struct SubDiagnostic {
pub level: Level,
pub message: Vec<(String, Style)>,
Expand Down
23 changes: 23 additions & 0 deletions crates/swc_common/src/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// TODO: https://github.com/rkyv/bytecheck/issues/20
#![allow(ambiguous_associated_items)]

use std::{
borrow::Cow,
cell::RefCell,
Expand Down Expand Up @@ -50,6 +53,10 @@ mod styled_buffer;
feature = "plugin-base",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "plugin-base",
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
)]
pub enum Applicability {
MachineApplicable,
HasPlaceholders,
Expand All @@ -66,6 +73,10 @@ pub enum Applicability {
feature = "plugin-base",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "plugin-base",
archive_attr(repr(C), derive(bytecheck::CheckBytes))
)]
pub struct CodeSuggestion {
/// Each substitute can have multiple variants due to multiple
/// applicable suggestions
Expand Down Expand Up @@ -117,6 +128,10 @@ pub struct CodeSuggestion {
feature = "plugin-base",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "plugin-base",
archive_attr(repr(C), derive(bytecheck::CheckBytes))
)]
pub struct Substitution {
pub parts: Vec<SubstitutionPart>,
}
Expand All @@ -130,6 +145,10 @@ pub struct Substitution {
feature = "plugin-base",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "plugin-base",
archive_attr(repr(C), derive(bytecheck::CheckBytes))
)]
pub struct SubstitutionPart {
pub span: Span,
pub snippet: String,
Expand Down Expand Up @@ -877,6 +896,10 @@ impl Handler {
feature = "plugin-base",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "plugin-base",
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
)]
pub enum Level {
Bug,
Fatal,
Expand Down
4 changes: 4 additions & 0 deletions crates/swc_common/src/errors/snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ pub struct StyledString {
feature = "plugin-base",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "plugin-base",
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
)]
pub enum Style {
MainHeaderMsg,
HeaderMsg,
Expand Down
4 changes: 4 additions & 0 deletions crates/swc_common/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ use crate::{syntax_pos::Mark, SyntaxContext};
feature = "plugin-base",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "plugin-base",
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
)]
/// Enum for possible errors while running transform via plugin.
/// This error indicates internal operation failure either in plugin_runner
/// or plugin_macro. Plugin's transform fn itself does not allow to return
Expand Down
26 changes: 26 additions & 0 deletions crates/swc_common/src/syntax_pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub mod hygiene;
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))]
pub struct Span {
#[serde(rename = "start")]
#[cfg_attr(feature = "rkyv", omit_bounds)]
Expand Down Expand Up @@ -106,6 +107,10 @@ better_scoped_tls::scoped_tls!(
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "rkyv",
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
)]
#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Hash)]
pub enum FileName {
Real(#[cfg_attr(feature = "rkyv", with(crate::source_map::EncodePathBuf))] PathBuf),
Expand Down Expand Up @@ -299,6 +304,10 @@ impl FileName {
feature = "plugin-base",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "plugin-base",
archive_attr(repr(C), derive(bytecheck::CheckBytes))
)]
pub struct MultiSpan {
primary_spans: Vec<Span>,
span_labels: Vec<(Span, String)>,
Expand Down Expand Up @@ -710,6 +719,7 @@ pub const NO_EXPANSION: SyntaxContext = SyntaxContext::empty();
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct MultiByteChar {
/// The absolute offset of the character in the SourceMap
Expand All @@ -723,6 +733,10 @@ pub struct MultiByteChar {
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "rkyv",
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum NonNarrowChar {
/// Represents a zero-width character
Expand Down Expand Up @@ -790,6 +804,7 @@ impl Sub<BytePos> for NonNarrowChar {
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))]
#[derive(Clone)]
pub struct SourceFile {
/// The name of the file that the source came from. Source that doesn't
Expand Down Expand Up @@ -990,6 +1005,7 @@ pub trait Pos {
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))]
pub struct BytePos(#[cfg_attr(feature = "rkyv", omit_bounds)] pub u32);

impl BytePos {
Expand All @@ -1015,6 +1031,7 @@ impl BytePos {
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))]
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub struct CharPos(pub usize);

Expand Down Expand Up @@ -1110,6 +1127,7 @@ impl Sub for CharPos {
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))]
#[derive(Debug, Clone)]
pub struct Loc {
/// Information about the original source
Expand Down Expand Up @@ -1144,6 +1162,7 @@ pub struct SourceFileAndLine {
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))]
#[derive(Debug)]
pub struct SourceFileAndBytePos {
pub sf: Lrc<SourceFile>,
Expand All @@ -1155,6 +1174,7 @@ pub struct SourceFileAndBytePos {
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))]
pub struct LineInfo {
/// Index of line, starting from 0.
pub line_index: usize,
Expand All @@ -1177,6 +1197,7 @@ pub struct LineCol {
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))]
pub struct FileLines {
pub file: Lrc<SourceFile>,
pub lines: Vec<LineInfo>,
Expand All @@ -1194,6 +1215,10 @@ pub type FileLinesResult = Result<FileLines, SpanLinesError>;
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "rkyv",
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
)]
pub enum SpanLinesError {
IllFormedSpan(Span),
DistinctSources(DistinctSources),
Expand All @@ -1213,6 +1238,7 @@ pub enum SpanSnippetError {
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))]
pub struct DistinctSources {
pub begin: (FileName, BytePos),
pub end: (FileName, BytePos),
Expand Down
2 changes: 2 additions & 0 deletions crates/swc_common/src/syntax_pos/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use crate::collections::AHashMap;
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))]
pub struct SyntaxContext(#[cfg_attr(feature = "rkyv", omit_bounds)] u32);

#[cfg(feature = "arbitrary")]
Expand Down Expand Up @@ -70,6 +71,7 @@ struct MarkData {
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))]
pub struct MutableMarkContext(pub u32, pub u32, pub u32);

// List of proxy calls injected by the host in the plugin's runtime context.
Expand Down
1 change: 1 addition & 0 deletions crates/swc_common/tests/attr_interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct Tuple(#[span] HasSpan, usize, usize);
feature = "rkyv",
archive(bound(serialize = "__S: rkyv::ser::Serializer + rkyv::ser::ScratchSpace"))
)]
#[cfg_attr(feature = "rkyv", archive_attr(repr(C), derive(bytecheck::CheckBytes)))]
pub struct HasSpan {
#[cfg_attr(feature = "rkyv", omit_bounds)]
pub span: Span,
Expand Down
3 changes: 2 additions & 1 deletion crates/swc_ecma_ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ bench = false
[features]
default = []
fuzzing = ["arbitrary", "swc_common/arbitrary"]
rkyv-impl = ["rkyv", "swc_common/rkyv-impl"]
rkyv-impl = ["rkyv", "bytecheck", "swc_common/rkyv-impl"]

[dependencies]
arbitrary = { version = "1", optional = true, features = ["derive"] }
bitflags = "1"
bytecheck = { version = "0.6.8", optional = true }
is-macro = "0.2.0"
num-bigint = { version = "0.4", features = ["serde"] }
rkyv = { version = "0.7.39", optional = true }
Expand Down
4 changes: 4 additions & 0 deletions crates/swc_ecma_ast/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ pub struct Decorator {
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(
feature = "rkyv",
archive_attr(repr(u32), derive(bytecheck::CheckBytes))
)]
pub enum MethodKind {
#[serde(rename = "method")]
Method,
Expand Down
Loading

0 comments on commit 08889d3

Please sign in to comment.