Replace Vec with EcoVec, removed Box (#2420)

This commit is contained in:
Sébastien d'Herbais de Thun 2023-10-17 20:50:36 +02:00 committed by GitHub
parent 77b84675e5
commit 37a988af83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 30 deletions

View File

@ -3,7 +3,7 @@ use std::ops::{Deref, Range};
use std::rc::Rc; use std::rc::Rc;
use std::sync::Arc; use std::sync::Arc;
use ecow::EcoString; use ecow::{eco_vec, EcoString, EcoVec};
use super::ast::AstNode; use super::ast::AstNode;
use super::{FileId, Span, SyntaxKind}; use super::{FileId, Span, SyntaxKind};
@ -616,7 +616,7 @@ impl ErrorNode {
error: SyntaxError { error: SyntaxError {
span: Span::detached(), span: Span::detached(),
message: message.into(), message: message.into(),
hints: vec![], hints: eco_vec![],
}, },
} }
} }
@ -652,7 +652,7 @@ pub struct SyntaxError {
pub message: EcoString, pub message: EcoString,
/// Additonal hints to the user, indicating how this error could be avoided /// Additonal hints to the user, indicating how this error could be avoided
/// or worked around. /// or worked around.
pub hints: Vec<EcoString>, pub hints: EcoVec<EcoString>,
} }
impl SyntaxError { impl SyntaxError {

View File

@ -7,6 +7,7 @@ use std::str::Utf8Error;
use std::string::FromUtf8Error; use std::string::FromUtf8Error;
use comemo::Tracked; use comemo::Tracked;
use ecow::{eco_vec, EcoVec};
use crate::syntax::{PackageSpec, Span, Spanned, SyntaxError}; use crate::syntax::{PackageSpec, Span, Spanned, SyntaxError};
use crate::{World, WorldExt}; use crate::{World, WorldExt};
@ -29,14 +30,14 @@ macro_rules! __bail {
}; };
($error:expr) => { ($error:expr) => {
return Err(Box::new(vec![$error])) return Err(::ecow::eco_vec![$error])
}; };
($span:expr, $fmt:literal $(, $arg:expr)* $(,)?) => { ($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, $span,
$crate::diag::eco_format!($fmt, $($arg),*), $crate::diag::eco_format!($fmt, $($arg),*),
)])) )])
}; };
} }
@ -75,7 +76,7 @@ macro_rules! __warning {
} }
/// A result that can carry multiple source errors. /// 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. /// An error or warning in a source file.
/// ///
@ -90,10 +91,10 @@ pub struct SourceDiagnostic {
/// A diagnostic message describing the problem. /// A diagnostic message describing the problem.
pub message: EcoString, pub message: EcoString,
/// The trace of function calls leading to the problem. /// 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 /// Additonal hints to the user, indicating how this problem could be avoided
/// or worked around. /// or worked around.
pub hints: Vec<EcoString>, pub hints: EcoVec<EcoString>,
} }
/// The severity of a [`SourceDiagnostic`]. /// The severity of a [`SourceDiagnostic`].
@ -111,9 +112,9 @@ impl SourceDiagnostic {
Self { Self {
severity: Severity::Error, severity: Severity::Error,
span, span,
trace: vec![], trace: eco_vec![],
message: message.into(), message: message.into(),
hints: vec![], hints: eco_vec![],
} }
} }
@ -122,9 +123,9 @@ impl SourceDiagnostic {
Self { Self {
severity: Severity::Warning, severity: Severity::Warning,
span, span,
trace: vec![], trace: eco_vec![],
message: message.into(), message: message.into(),
hints: vec![], hints: eco_vec![],
} }
} }
@ -152,7 +153,7 @@ impl From<SyntaxError> for SourceDiagnostic {
severity: Severity::Error, severity: Severity::Error,
span: error.span, span: error.span,
message: error.message, message: error.message,
trace: vec![], trace: eco_vec![],
hints: error.hints, hints: error.hints,
} }
} }
@ -203,7 +204,7 @@ impl<T> Trace<T> for SourceResult<T> {
{ {
self.map_err(|mut errors| { self.map_err(|mut errors| {
let Some(trace_range) = world.range(span) else { return 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. // Skip traces that surround the error.
if let Some(error_range) = world.range(error.span) { if let Some(error_range) = world.range(error.span) {
if error.span.id() == span.id() if error.span.id() == span.id()
@ -242,7 +243,7 @@ where
diagnostic diagnostic
.hint("you can adjust the project root with the --root argument"); .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> { impl<T> At<T> for Result<T, HintedString> {
fn at(self, span: Span) -> SourceResult<T> { fn at(self, span: Span) -> SourceResult<T> {
self.map_err(|diags| { self.map_err(|diags| {
Box::new(vec![ eco_vec![SourceDiagnostic::error(span, diags.message).with_hints(diags.hints)]
SourceDiagnostic::error(span, diags.message).with_hints(diags.hints)
])
}) })
} }
} }

View File

@ -1,6 +1,6 @@
use std::fmt::{self, Debug, Formatter}; 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 super::{func, scope, ty, Array, Dict, FromValue, IntoValue, Repr, Str, Value};
use crate::diag::{bail, At, SourceDiagnostic, SourceResult}; use crate::diag::{bail, At, SourceDiagnostic, SourceResult};
@ -155,7 +155,7 @@ impl Args {
T: FromValue<Spanned<Value>>, T: FromValue<Spanned<Value>>,
{ {
let mut list = vec![]; let mut list = vec![];
let mut errors = vec![]; let mut errors = eco_vec![];
self.items.retain(|item| { self.items.retain(|item| {
if item.name.is_some() { if item.name.is_some() {
return true; return true;
@ -169,7 +169,7 @@ impl Args {
false false
}); });
if !errors.is_empty() { if !errors.is_empty() {
return Err(Box::new(errors)); return Err(errors);
} }
Ok(list) Ok(list)
} }

View File

@ -134,7 +134,7 @@ pub fn eval(
let root = source.root(); let root = source.root();
let errors = root.errors(); let errors = root.errors();
if !errors.is_empty() && vm.inspected.is_none() { 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. // Evaluate the module.
@ -178,7 +178,7 @@ pub fn eval_string(
let errors = root.errors(); let errors = root.errors();
if !errors.is_empty() { 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. // Prepare VT.
@ -1786,7 +1786,7 @@ impl Eval for ast::ModuleImport<'_> {
} }
} }
Some(ast::Imports::Items(items)) => { Some(ast::Imports::Items(items)) => {
let mut errors = vec![]; let mut errors = eco_vec![];
for item in items.iter() { for item in items.iter() {
let original_ident = item.original_name(); let original_ident = item.original_name();
if let Some(value) = scope.get(&original_ident) { if let Some(value) = scope.get(&original_ident) {
@ -1808,7 +1808,7 @@ impl Eval for ast::ModuleImport<'_> {
} }
} }
if !errors.is_empty() { if !errors.is_empty() {
return Err(Box::new(errors)); return Err(errors);
} }
} }
} }

View File

@ -8,6 +8,7 @@ mod realize;
mod selector; mod selector;
mod styles; mod styles;
use ecow::EcoVec;
#[doc(inline)] #[doc(inline)]
pub use typst_macros::elem; pub use typst_macros::elem;
@ -89,7 +90,7 @@ pub fn typeset(
// Promote delayed errors. // Promote delayed errors.
if !delayed.0.is_empty() { if !delayed.0.is_empty() {
return Err(Box::new(delayed.0)); return Err(delayed.0);
} }
Ok(document) Ok(document)
@ -123,7 +124,7 @@ impl Vt<'_> {
match f(self) { match f(self) {
Ok(value) => value, Ok(value) => value,
Err(errors) => { Err(errors) => {
for error in *errors { for error in errors {
self.delayed.push(error); self.delayed.push(error);
} }
T::default() T::default()
@ -134,7 +135,7 @@ impl Vt<'_> {
/// Holds delayed errors. /// Holds delayed errors.
#[derive(Default, Clone)] #[derive(Default, Clone)]
pub struct DelayedErrors(Vec<SourceDiagnostic>); pub struct DelayedErrors(EcoVec<SourceDiagnostic>);
impl DelayedErrors { impl DelayedErrors {
/// Create an empty list of delayed errors. /// Create an empty list of delayed errors.

View File

@ -545,7 +545,7 @@ fn test_part(
Ok(document) => (document.pages, tracer.warnings()), Ok(document) => (document.pages, tracer.warnings()),
Err(errors) => { Err(errors) => {
let mut warnings = tracer.warnings(); let mut warnings = tracer.warnings();
warnings.extend(*errors); warnings.extend(errors);
(vec![], warnings) (vec![], warnings)
} }
}; };