Skip to content

Commit

Permalink
Use rkyv in strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
djkoloski authored and kwonoj committed Apr 8, 2023
1 parent b2906bb commit 86362d3
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 53 deletions.
14 changes: 9 additions & 5 deletions crates/swc_atoms/Cargo.toml
Expand Up @@ -22,14 +22,18 @@ rkyv-bytecheck-impl = ["__rkyv", "rkyv-latest"]
[dependencies]
bytecheck = { version = "0.6.9", optional = true }
once_cell = "1"
rkyv = { package = "rkyv", version = "=0.7.40", optional = true }
rkyv = { package = "rkyv", version = "=0.7.40", optional = true, features = [
"strict",
] }
# This is to avoid cargo version selection conflict between rkyv=0.7.40 and other versions, as it is strictly pinned
# cannot be merged.
rkyv-latest = { package = "rkyv-test", version = "=0.7.38-test.2", optional = true }
rustc-hash = "1.1.0"
serde = "1"
rkyv-latest = { package = "rkyv-test", version = "=0.7.38-test.2", optional = true, features = [
"strict",
] }
rustc-hash = "1.1.0"
serde = "1"
string_cache = "0.8.7"
triomphe = "0.1.8"
triomphe = "0.1.8"

[build-dependencies]
string_cache_codegen = "0.5.2"
2 changes: 1 addition & 1 deletion crates/swc_common/Cargo.toml
Expand Up @@ -60,7 +60,7 @@ new_debug_unreachable = "1.0.4"
num-bigint = "0.4"
once_cell = "1.10.0"
parking_lot = { version = "0.12.0", optional = true }
rkyv = { version = "=0.7.40", optional = true }
rkyv = { version = "=0.7.40", optional = true, features = ["strict"] }
# This is to avoid cargo version selection conflict between rkyv=0.7.40 and other versions, as it is strictly pinned
# cannot be merged.
rkyv-latest = { package = "rkyv-test", version = "=0.7.38-test.2", optional = true }
Expand Down
49 changes: 30 additions & 19 deletions crates/swc_common/src/errors/diagnostic.rs
Expand Up @@ -16,6 +16,17 @@ use rkyv_latest as rkyv;
use super::{snippet::Style, Applicability, CodeSuggestion, Level, Substitution, SubstitutionPart};
use crate::syntax_pos::{MultiSpan, Span};

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(
feature = "diagnostic-serde",
derive(serde::Serialize, serde::Deserialize)
)]
#[cfg_attr(
any(feature = "rkyv-impl", feature = "rkyv-bytecheck-impl"),
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
pub struct Message(pub String, pub Style);

#[must_use]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(
Expand All @@ -28,7 +39,7 @@ use crate::syntax_pos::{MultiSpan, Span};
)]
pub struct Diagnostic {
pub level: Level,
pub message: Vec<(String, Style)>,
pub message: Vec<Message>,
pub code: Option<DiagnosticId>,
pub span: MultiSpan,
pub children: Vec<SubDiagnostic>,
Expand Down Expand Up @@ -61,7 +72,7 @@ pub enum DiagnosticId {
)]
pub struct SubDiagnostic {
pub level: Level,
pub message: Vec<(String, Style)>,
pub message: Vec<Message>,
pub span: MultiSpan,
pub render_span: Option<MultiSpan>,
}
Expand Down Expand Up @@ -117,7 +128,7 @@ impl Diagnostic {
pub fn new_with_code(level: Level, code: Option<DiagnosticId>, message: &str) -> Self {
Diagnostic {
level,
message: vec![(message.to_owned(), Style::NoStyle)],
message: vec![Message(message.to_owned(), Style::NoStyle)],
code,
span: MultiSpan::new(),
children: vec![],
Expand Down Expand Up @@ -184,18 +195,18 @@ impl Diagnostic {
expected_extra: &dyn fmt::Display,
found_extra: &dyn fmt::Display,
) -> &mut Self {
let mut msg: Vec<_> = vec![(format!("expected {} `", label), Style::NoStyle)];
let mut msg: Vec<_> = vec![Message(format!("expected {} `", label), Style::NoStyle)];
msg.extend(expected.0.iter().map(|x| match *x {
StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
StringPart::Normal(ref s) => Message(s.to_owned(), Style::NoStyle),
StringPart::Highlighted(ref s) => Message(s.to_owned(), Style::Highlight),
}));
msg.push((format!("`{}\n", expected_extra), Style::NoStyle));
msg.push((format!(" found {} `", label), Style::NoStyle));
msg.push(Message(format!("`{}\n", expected_extra), Style::NoStyle));
msg.push(Message(format!(" found {} `", label), Style::NoStyle));
msg.extend(found.0.iter().map(|x| match *x {
StringPart::Normal(ref s) => (s.to_owned(), Style::NoStyle),
StringPart::Highlighted(ref s) => (s.to_owned(), Style::Highlight),
StringPart::Normal(ref s) => Message(s.to_owned(), Style::NoStyle),
StringPart::Highlighted(ref s) => Message(s.to_owned(), Style::Highlight),
}));
msg.push((format!("`{}", found_extra), Style::NoStyle));
msg.push(Message(format!("`{}", found_extra), Style::NoStyle));

// For now, just attach these as notes
self.highlighted_note(msg);
Expand All @@ -204,9 +215,9 @@ impl Diagnostic {

pub fn note_trait_signature(&mut self, name: String, signature: String) -> &mut Self {
self.highlighted_note(vec![
(format!("`{}` from trait: `", name), Style::NoStyle),
(signature, Style::Highlight),
("`".to_string(), Style::NoStyle),
Message(format!("`{}` from trait: `", name), Style::NoStyle),
Message(signature, Style::Highlight),
Message("`".to_string(), Style::NoStyle),
]);
self
}
Expand All @@ -216,7 +227,7 @@ impl Diagnostic {
self
}

pub fn highlighted_note(&mut self, msg: Vec<(String, Style)>) -> &mut Self {
pub fn highlighted_note(&mut self, msg: Vec<Message>) -> &mut Self {
self.sub_with_highlights(Level::Note, msg, MultiSpan::new(), None);
self
}
Expand Down Expand Up @@ -431,7 +442,7 @@ impl Diagnostic {
.collect::<String>()
}

pub fn styled_message(&self) -> &Vec<(String, Style)> {
pub fn styled_message(&self) -> &Vec<Message> {
&self.message
}

Expand All @@ -454,7 +465,7 @@ impl Diagnostic {
) {
let sub = SubDiagnostic {
level,
message: vec![(message.to_owned(), Style::NoStyle)],
message: vec![Message(message.to_owned(), Style::NoStyle)],
span,
render_span,
};
Expand All @@ -466,7 +477,7 @@ impl Diagnostic {
fn sub_with_highlights(
&mut self,
level: Level,
message: Vec<(String, Style)>,
message: Vec<Message>,
span: MultiSpan,
render_span: Option<MultiSpan>,
) {
Expand All @@ -488,7 +499,7 @@ impl SubDiagnostic {
.collect::<String>()
}

pub fn styled_message(&self) -> &Vec<(String, Style)> {
pub fn styled_message(&self) -> &Vec<Message> {
&self.message
}
}
15 changes: 8 additions & 7 deletions crates/swc_common/src/errors/emitter.rs
Expand Up @@ -23,6 +23,7 @@ use unicode_width;

use self::Destination::*;
use super::{
diagnostic::Message,
snippet::{Annotation, AnnotationType, Line, MultilineAnnotation, Style, StyledString},
styled_buffer::StyledBuffer,
CodeSuggestion, DiagnosticBuilder, DiagnosticId, Level, SourceMapperDyn, SubDiagnostic,
Expand Down Expand Up @@ -842,7 +843,7 @@ impl EmitterWriter {
if spans_updated {
children.push(SubDiagnostic {
level: Level::Note,
message: vec![(
message: vec![Message(
"this error originates in a macro outside of the current crate (in Nightly \
builds, run with -Z external-macro-backtrace for more info)"
.to_string(),
Expand All @@ -859,7 +860,7 @@ impl EmitterWriter {
fn msg_to_buffer(
&self,
buffer: &mut StyledBuffer,
msg: &[(String, Style)],
msg: &[Message],
padding: usize,
label: &str,
override_style: Option<Style>,
Expand Down Expand Up @@ -912,7 +913,7 @@ impl EmitterWriter {
// see how it *looks* with
// very *weird* formats
// see?
for (text, style) in msg.iter() {
for &Message(ref text, ref style) in msg.iter() {
let lines = text.split('\n').collect::<Vec<_>>();
if lines.len() > 1 {
for (i, line) in lines.iter().enumerate() {
Expand All @@ -932,7 +933,7 @@ impl EmitterWriter {
fn emit_message_default(
&mut self,
msp: &MultiSpan,
msg: &[(String, Style)],
msg: &[Message],
code: &Option<DiagnosticId>,
level: Level,
max_line_num_len: usize,
Expand Down Expand Up @@ -975,7 +976,7 @@ impl EmitterWriter {
if !level_str.is_empty() {
buffer.append(0, ": ", header_style);
}
for (text, _) in msg.iter() {
for &Message(ref text, _) in msg.iter() {
buffer.append(0, text, header_style);
}
}
Expand Down Expand Up @@ -1212,7 +1213,7 @@ impl EmitterWriter {
}
self.msg_to_buffer(
&mut buffer,
&[(suggestion.msg.to_owned(), Style::NoStyle)],
&[Message(suggestion.msg.to_owned(), Style::NoStyle)],
max_line_num_len,
"suggestion",
Some(Style::HeaderMsg),
Expand Down Expand Up @@ -1332,7 +1333,7 @@ impl EmitterWriter {
fn emit_messages_default(
&mut self,
level: Level,
message: &[(String, Style)],
message: &[Message],
code: &Option<DiagnosticId>,
span: &MultiSpan,
children: &[SubDiagnostic],
Expand Down
8 changes: 4 additions & 4 deletions crates/swc_common/src/source_map.rs
Expand Up @@ -488,8 +488,8 @@ impl SourceMap {

if lo.file.start_pos != hi.file.start_pos {
return Err(Box::new(SpanLinesError::DistinctSources(DistinctSources {
begin: (lo.file.name.clone(), lo.file.start_pos),
end: (hi.file.name.clone(), hi.file.start_pos),
begin: FilePos(lo.file.name.clone(), lo.file.start_pos),
end: FilePos(hi.file.name.clone(), hi.file.start_pos),
})));
}
assert!(hi.line >= lo.line);
Expand Down Expand Up @@ -565,8 +565,8 @@ impl SourceMap {
if local_begin.sf.start_pos != local_end.sf.start_pos {
Err(Box::new(SpanSnippetError::DistinctSources(
DistinctSources {
begin: (local_begin.sf.name.clone(), local_begin.sf.start_pos),
end: (local_end.sf.name.clone(), local_end.sf.start_pos),
begin: FilePos(local_begin.sf.name.clone(), local_begin.sf.start_pos),
end: FilePos(local_end.sf.name.clone(), local_end.sf.start_pos),
},
)))
} else {
Expand Down
28 changes: 23 additions & 5 deletions crates/swc_common/src/syntax_pos.rs
Expand Up @@ -312,6 +312,17 @@ impl FileName {
}
}

#[derive(Clone, Debug, Default, Hash, PartialEq, Eq)]
#[cfg_attr(
feature = "diagnostic-serde",
derive(serde::Serialize, serde::Deserialize)
)]
#[cfg_attr(
any(feature = "rkyv-impl", feature = "rkyv-bytecheck-impl"),
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
pub struct PrimarySpanLabel(pub Span, pub String);

/// A collection of spans. Spans have two orthogonal attributes:
///
/// - they can be *primary spans*. In this case they are the locus of the error,
Expand All @@ -329,7 +340,7 @@ impl FileName {
)]
pub struct MultiSpan {
primary_spans: Vec<Span>,
span_labels: Vec<(Span, String)>,
span_labels: Vec<PrimarySpanLabel>,
}

extern "C" {
Expand Down Expand Up @@ -633,7 +644,7 @@ impl MultiSpan {
}

pub fn push_span_label(&mut self, span: Span, label: String) {
self.span_labels.push((span, label));
self.span_labels.push(PrimarySpanLabel(span, label));
}

/// Selects the first primary span (if any)
Expand Down Expand Up @@ -689,7 +700,7 @@ impl MultiSpan {
let mut span_labels = self
.span_labels
.iter()
.map(|&(span, ref label)| SpanLabel {
.map(|&PrimarySpanLabel(span, ref label)| SpanLabel {
span,
is_primary: is_primary(span),
label: Some(label.clone()),
Expand Down Expand Up @@ -1296,14 +1307,21 @@ pub enum SpanSnippetError {
SourceNotAvailable { filename: FileName },
}

#[derive(Clone, PartialEq, Eq, Debug)]
#[cfg_attr(
any(feature = "rkyv-impl", feature = "rkyv-bytecheck-impl"),
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
pub struct FilePos(pub FileName, pub BytePos);

#[derive(Clone, PartialEq, Eq, Debug)]
#[cfg_attr(
any(feature = "rkyv-impl", feature = "rkyv-bytecheck-impl"),
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
pub struct DistinctSources {
pub begin: (FileName, BytePos),
pub end: (FileName, BytePos),
pub begin: FilePos,
pub end: FilePos,
}

#[derive(Clone, PartialEq, Eq, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_css_ast/Cargo.toml
Expand Up @@ -21,7 +21,7 @@ serde-impl = ["serde"]
[dependencies]
bytecheck = { version = "0.6.9", optional = true }
is-macro = "0.2.0"
rkyv = { version = "=0.7.40", optional = true }
rkyv = { version = "=0.7.40", optional = true, features = ["strict"] }
serde = { version = "1.0.127", features = ["derive"], optional = true }

string_enum = { version = "0.4.0", path = "../string_enum/" }
Expand Down
22 changes: 13 additions & 9 deletions crates/swc_ecma_ast/Cargo.toml
Expand Up @@ -31,18 +31,22 @@ rkyv-bytecheck-impl = [
serde-impl = ["serde"]

[dependencies]
arbitrary = { version = "1", optional = true, features = ["derive"] }
bitflags = "1"
bytecheck = { version = "0.6.9", optional = true }
is-macro = "0.2.1"
arbitrary = { version = "1", optional = true, features = ["derive"] }
bitflags = "1"
bytecheck = { version = "0.6.9", optional = true }
is-macro = "0.2.1"
num-bigint = { version = "0.4", features = ["serde"] }
rkyv = { package = "rkyv", version = "=0.7.40", optional = true }
rkyv = { package = "rkyv", version = "=0.7.40", optional = true, features = [
"strict",
] }
# This is to avoid cargo version selection conflict between rkyv=0.7.40 and other versions, as it is strictly pinned
# cannot be merged.
rkyv-latest = { package = "rkyv-test", version = "=0.7.38-test.2", optional = true }
scoped-tls = "1.0.0"
serde = { version = "1.0.133", features = ["derive"], optional = true }
unicode-id = "0.3"
rkyv-latest = { package = "rkyv-test", version = "=0.7.38-test.2", optional = true, features = [
"strict",
] }
scoped-tls = "1.0.0"
serde = { version = "1.0.133", features = ["derive"], optional = true }
unicode-id = "0.3"

string_enum = { version = "0.4.0", path = "../string_enum" }
swc_atoms = { version = "0.4.43", path = "../swc_atoms" }
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_html_ast/Cargo.toml
Expand Up @@ -23,7 +23,7 @@ serde-impl = ["serde"]
[dependencies]
bytecheck = { version = "0.6.9", optional = true }
is-macro = "0.2.0"
rkyv = { version = "=0.7.40", optional = true }
rkyv = { version = "=0.7.40", optional = true, features = ["strict"] }
serde = { version = "1.0.127", features = ["derive"], optional = true }

string_enum = { version = "0.4.0", path = "../string_enum/" }
Expand Down
4 changes: 3 additions & 1 deletion crates/swc_plugin_proxy/Cargo.toml
Expand Up @@ -37,7 +37,9 @@ plugin-mode = ["__plugin_mode", "swc_common/plugin-base", "rkyv-impl"]

[dependencies]

rkyv = { package = "rkyv", version = "=0.7.40", optional = true }
rkyv = { package = "rkyv", version = "=0.7.40", optional = true, features = [
"strict",
] }
# This is to avoid cargo version selection conflict between rkyv=0.7.40 and other versions, as it is strictly pinned
# cannot be merged.
rkyv-latest = { package = "rkyv-test", version = "=0.7.38-test.2", optional = true }
Expand Down

0 comments on commit 86362d3

Please sign in to comment.