mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Replace Vec
with EcoVec
, removed Box
(#2420)
This commit is contained in:
parent
77b84675e5
commit
37a988af83
@ -3,7 +3,7 @@ use std::ops::{Deref, Range};
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
use ecow::EcoString;
|
||||
use ecow::{eco_vec, EcoString, EcoVec};
|
||||
|
||||
use super::ast::AstNode;
|
||||
use super::{FileId, Span, SyntaxKind};
|
||||
@ -616,7 +616,7 @@ impl ErrorNode {
|
||||
error: SyntaxError {
|
||||
span: Span::detached(),
|
||||
message: message.into(),
|
||||
hints: vec![],
|
||||
hints: eco_vec![],
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -652,7 +652,7 @@ pub struct SyntaxError {
|
||||
pub message: EcoString,
|
||||
/// Additonal hints to the user, indicating how this error could be avoided
|
||||
/// or worked around.
|
||||
pub hints: Vec<EcoString>,
|
||||
pub hints: EcoVec<EcoString>,
|
||||
}
|
||||
|
||||
impl SyntaxError {
|
||||
|
@ -7,6 +7,7 @@ use std::str::Utf8Error;
|
||||
use std::string::FromUtf8Error;
|
||||
|
||||
use comemo::Tracked;
|
||||
use ecow::{eco_vec, EcoVec};
|
||||
|
||||
use crate::syntax::{PackageSpec, Span, Spanned, SyntaxError};
|
||||
use crate::{World, WorldExt};
|
||||
@ -29,14 +30,14 @@ macro_rules! __bail {
|
||||
};
|
||||
|
||||
($error:expr) => {
|
||||
return Err(Box::new(vec![$error]))
|
||||
return Err(::ecow::eco_vec![$error])
|
||||
};
|
||||
|
||||
($span:expr, $fmt:literal $(, $arg:expr)* $(,)?) => {
|
||||
return Err(Box::new(vec![$crate::diag::SourceDiagnostic::error(
|
||||
return Err(::ecow::eco_vec![$crate::diag::SourceDiagnostic::error(
|
||||
$span,
|
||||
$crate::diag::eco_format!($fmt, $($arg),*),
|
||||
)]))
|
||||
)])
|
||||
};
|
||||
}
|
||||
|
||||
@ -75,7 +76,7 @@ macro_rules! __warning {
|
||||
}
|
||||
|
||||
/// A result that can carry multiple source errors.
|
||||
pub type SourceResult<T> = Result<T, Box<Vec<SourceDiagnostic>>>;
|
||||
pub type SourceResult<T> = Result<T, EcoVec<SourceDiagnostic>>;
|
||||
|
||||
/// An error or warning in a source file.
|
||||
///
|
||||
@ -90,10 +91,10 @@ pub struct SourceDiagnostic {
|
||||
/// A diagnostic message describing the problem.
|
||||
pub message: EcoString,
|
||||
/// The trace of function calls leading to the problem.
|
||||
pub trace: Vec<Spanned<Tracepoint>>,
|
||||
pub trace: EcoVec<Spanned<Tracepoint>>,
|
||||
/// Additonal hints to the user, indicating how this problem could be avoided
|
||||
/// or worked around.
|
||||
pub hints: Vec<EcoString>,
|
||||
pub hints: EcoVec<EcoString>,
|
||||
}
|
||||
|
||||
/// The severity of a [`SourceDiagnostic`].
|
||||
@ -111,9 +112,9 @@ impl SourceDiagnostic {
|
||||
Self {
|
||||
severity: Severity::Error,
|
||||
span,
|
||||
trace: vec![],
|
||||
trace: eco_vec![],
|
||||
message: message.into(),
|
||||
hints: vec![],
|
||||
hints: eco_vec![],
|
||||
}
|
||||
}
|
||||
|
||||
@ -122,9 +123,9 @@ impl SourceDiagnostic {
|
||||
Self {
|
||||
severity: Severity::Warning,
|
||||
span,
|
||||
trace: vec![],
|
||||
trace: eco_vec![],
|
||||
message: message.into(),
|
||||
hints: vec![],
|
||||
hints: eco_vec![],
|
||||
}
|
||||
}
|
||||
|
||||
@ -152,7 +153,7 @@ impl From<SyntaxError> for SourceDiagnostic {
|
||||
severity: Severity::Error,
|
||||
span: error.span,
|
||||
message: error.message,
|
||||
trace: vec![],
|
||||
trace: eco_vec![],
|
||||
hints: error.hints,
|
||||
}
|
||||
}
|
||||
@ -203,7 +204,7 @@ impl<T> Trace<T> for SourceResult<T> {
|
||||
{
|
||||
self.map_err(|mut errors| {
|
||||
let Some(trace_range) = world.range(span) else { return errors };
|
||||
for error in errors.iter_mut() {
|
||||
for error in errors.make_mut().iter_mut() {
|
||||
// Skip traces that surround the error.
|
||||
if let Some(error_range) = world.range(error.span) {
|
||||
if error.span.id() == span.id()
|
||||
@ -242,7 +243,7 @@ where
|
||||
diagnostic
|
||||
.hint("you can adjust the project root with the --root argument");
|
||||
}
|
||||
Box::new(vec![diagnostic])
|
||||
eco_vec![diagnostic]
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -263,9 +264,7 @@ pub struct HintedString {
|
||||
impl<T> At<T> for Result<T, HintedString> {
|
||||
fn at(self, span: Span) -> SourceResult<T> {
|
||||
self.map_err(|diags| {
|
||||
Box::new(vec![
|
||||
SourceDiagnostic::error(span, diags.message).with_hints(diags.hints)
|
||||
])
|
||||
eco_vec![SourceDiagnostic::error(span, diags.message).with_hints(diags.hints)]
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use std::fmt::{self, Debug, Formatter};
|
||||
|
||||
use ecow::{eco_format, EcoString, EcoVec};
|
||||
use ecow::{eco_format, eco_vec, EcoString, EcoVec};
|
||||
|
||||
use super::{func, scope, ty, Array, Dict, FromValue, IntoValue, Repr, Str, Value};
|
||||
use crate::diag::{bail, At, SourceDiagnostic, SourceResult};
|
||||
@ -155,7 +155,7 @@ impl Args {
|
||||
T: FromValue<Spanned<Value>>,
|
||||
{
|
||||
let mut list = vec![];
|
||||
let mut errors = vec![];
|
||||
let mut errors = eco_vec![];
|
||||
self.items.retain(|item| {
|
||||
if item.name.is_some() {
|
||||
return true;
|
||||
@ -169,7 +169,7 @@ impl Args {
|
||||
false
|
||||
});
|
||||
if !errors.is_empty() {
|
||||
return Err(Box::new(errors));
|
||||
return Err(errors);
|
||||
}
|
||||
Ok(list)
|
||||
}
|
||||
|
@ -134,7 +134,7 @@ pub fn eval(
|
||||
let root = source.root();
|
||||
let errors = root.errors();
|
||||
if !errors.is_empty() && vm.inspected.is_none() {
|
||||
return Err(Box::new(errors.into_iter().map(Into::into).collect()));
|
||||
return Err(errors.into_iter().map(Into::into).collect());
|
||||
}
|
||||
|
||||
// Evaluate the module.
|
||||
@ -178,7 +178,7 @@ pub fn eval_string(
|
||||
|
||||
let errors = root.errors();
|
||||
if !errors.is_empty() {
|
||||
return Err(Box::new(errors.into_iter().map(Into::into).collect()));
|
||||
return Err(errors.into_iter().map(Into::into).collect());
|
||||
}
|
||||
|
||||
// Prepare VT.
|
||||
@ -1786,7 +1786,7 @@ impl Eval for ast::ModuleImport<'_> {
|
||||
}
|
||||
}
|
||||
Some(ast::Imports::Items(items)) => {
|
||||
let mut errors = vec![];
|
||||
let mut errors = eco_vec![];
|
||||
for item in items.iter() {
|
||||
let original_ident = item.original_name();
|
||||
if let Some(value) = scope.get(&original_ident) {
|
||||
@ -1808,7 +1808,7 @@ impl Eval for ast::ModuleImport<'_> {
|
||||
}
|
||||
}
|
||||
if !errors.is_empty() {
|
||||
return Err(Box::new(errors));
|
||||
return Err(errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ mod realize;
|
||||
mod selector;
|
||||
mod styles;
|
||||
|
||||
use ecow::EcoVec;
|
||||
#[doc(inline)]
|
||||
pub use typst_macros::elem;
|
||||
|
||||
@ -89,7 +90,7 @@ pub fn typeset(
|
||||
|
||||
// Promote delayed errors.
|
||||
if !delayed.0.is_empty() {
|
||||
return Err(Box::new(delayed.0));
|
||||
return Err(delayed.0);
|
||||
}
|
||||
|
||||
Ok(document)
|
||||
@ -123,7 +124,7 @@ impl Vt<'_> {
|
||||
match f(self) {
|
||||
Ok(value) => value,
|
||||
Err(errors) => {
|
||||
for error in *errors {
|
||||
for error in errors {
|
||||
self.delayed.push(error);
|
||||
}
|
||||
T::default()
|
||||
@ -134,7 +135,7 @@ impl Vt<'_> {
|
||||
|
||||
/// Holds delayed errors.
|
||||
#[derive(Default, Clone)]
|
||||
pub struct DelayedErrors(Vec<SourceDiagnostic>);
|
||||
pub struct DelayedErrors(EcoVec<SourceDiagnostic>);
|
||||
|
||||
impl DelayedErrors {
|
||||
/// Create an empty list of delayed errors.
|
||||
|
@ -545,7 +545,7 @@ fn test_part(
|
||||
Ok(document) => (document.pages, tracer.warnings()),
|
||||
Err(errors) => {
|
||||
let mut warnings = tracer.warnings();
|
||||
warnings.extend(*errors);
|
||||
warnings.extend(errors);
|
||||
(vec![], warnings)
|
||||
}
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user