diff --git a/src/diag.rs b/src/diag.rs index 38f3d0a27..f85583901 100644 --- a/src/diag.rs +++ b/src/diag.rs @@ -46,18 +46,13 @@ impl Display for Level { /// ``` /// # use typstc::error; /// # use typstc::syntax::Span; -/// # use typstc::Feedback; /// # let span = Span::ZERO; -/// # let mut feedback = Feedback::new(); /// # let name = ""; /// // Create formatted error values. /// let error = error!("expected {}", name); /// /// // Create spanned errors. /// let spanned = error!(span, "there is an error here"); -/// -/// // Create an error and directly add it to existing feedback. -/// error!(@feedback, span, "oh no!"); /// ``` /// /// [`Error`]: diagnostic/enum.Level.html#variant.Error @@ -85,10 +80,6 @@ macro_rules! warning { #[macro_export] #[doc(hidden)] macro_rules! __impl_diagnostic { - ($level:expr; @$feedback:expr, $($tts:tt)*) => { - $feedback.diags.push($crate::__impl_diagnostic!($level; $($tts)*)); - }; - ($level:expr; $fmt:literal $($tts:tt)*) => { $crate::diag::Diag::new($level, format!($fmt $($tts)*)) }; diff --git a/src/eval/args.rs b/src/eval/args.rs index b89ee61f6..d11deac60 100644 --- a/src/eval/args.rs +++ b/src/eval/args.rs @@ -129,7 +129,7 @@ impl Args { pub fn done(&self, ctx: &mut LayoutContext) { for entry in self.0.v.values() { let span = entry.key_span.join(entry.value.span); - error!(@ctx.f, span, "unexpected argument"); + ctx.diag(error!(span, "unexpected argument")); } } } diff --git a/src/eval/mod.rs b/src/eval/mod.rs index 4ec29056b..6f882ab85 100644 --- a/src/eval/mod.rs +++ b/src/eval/mod.rs @@ -100,7 +100,7 @@ impl Eval for ExprCall { (func.clone())(args, ctx).await } else { if !name.is_empty() { - error!(@ctx.f, span, "unknown function"); + ctx.diag(error!(span, "unknown function")); ctx.f.decos.push(Deco::Unresolved.span_with(span)); } Value::Dict(dict) @@ -159,7 +159,7 @@ fn neg(ctx: &mut LayoutContext, span: Span, value: Value) -> Value { Relative(v) => Relative(-v), Linear(v) => Linear(-v), v => { - error!(@ctx.f, span, "cannot negate {}", v.ty()); + ctx.diag(error!(span, "cannot negate {}", v.ty())); Value::Error } } @@ -196,7 +196,7 @@ fn add(ctx: &mut LayoutContext, span: Span, lhs: Value, rhs: Value) -> Value { (Commands(a), Commands(b)) => Commands(concat(a, b)), (a, b) => { - error!(@ctx.f, span, "cannot add {} and {}", a.ty(), b.ty()); + ctx.diag(error!(span, "cannot add {} and {}", a.ty(), b.ty())); Value::Error } } @@ -225,7 +225,7 @@ fn sub(ctx: &mut LayoutContext, span: Span, lhs: Value, rhs: Value) -> Value { (Linear(a), Linear(b)) => Linear(a - b), (a, b) => { - error!(@ctx.f, span, "cannot subtract {1} from {0}", a.ty(), b.ty()); + ctx.diag(error!(span, "cannot subtract {1} from {0}", a.ty(), b.ty())); Value::Error } } @@ -260,7 +260,7 @@ fn mul(ctx: &mut LayoutContext, span: Span, lhs: Value, rhs: Value) -> Value { (Str(a), Int(b)) => Str(a.repeat(b.max(0) as usize)), (a, b) => { - error!(@ctx.f, span, "cannot multiply {} with {}", a.ty(), b.ty()); + ctx.diag(error!(span, "cannot multiply {} with {}", a.ty(), b.ty())); Value::Error } } @@ -285,7 +285,7 @@ fn div(ctx: &mut LayoutContext, span: Span, lhs: Value, rhs: Value) -> Value { (Linear(a), Float(b)) => Linear(a / b), (a, b) => { - error!(@ctx.f, span, "cannot divide {} by {}", a.ty(), b.ty()); + ctx.diag(error!(span, "cannot divide {} by {}", a.ty(), b.ty())); Value::Error } } diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 7803c7476..5e4e020ef 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -13,10 +13,11 @@ pub use tree::*; use crate::geom::{Insets, Point, Rect, RectExt, Sides, Size, SizeExt}; +use crate::diag::Diag; use crate::eval::{PageState, State, TextState}; use crate::font::SharedFontLoader; use crate::shaping::Shaped; -use crate::syntax::SynTree; +use crate::syntax::{Deco, Spanned, SynTree}; use crate::{Feedback, Pass}; /// Layout a syntax tree and return the produced layout. @@ -98,6 +99,18 @@ pub struct LayoutContext { pub f: Feedback, } +impl LayoutContext { + /// Add a diagnostic to the feedback. + pub fn diag(&mut self, diag: Spanned) { + self.f.diags.push(diag); + } + + /// Add a decoration to the feedback. + pub fn deco(&mut self, deco: Spanned) { + self.f.decos.push(deco); + } +} + /// The constraints for layouting a single node. #[derive(Debug, Clone)] pub struct LayoutConstraints { diff --git a/src/layout/tree.rs b/src/layout/tree.rs index c5b6609b1..435b7109e 100644 --- a/src/layout/tree.rs +++ b/src/layout/tree.rs @@ -198,10 +198,10 @@ impl<'a> TreeLayouter<'a> { if self.constraints.root { self.layouter.finish_space(true) } else { - error!( - @self.ctx.f, span, + self.ctx.diag(error!( + span, "page break can only be issued from root context", - ); + )); } } @@ -224,10 +224,10 @@ impl<'a> TreeLayouter<'a> { self.constraints.base = space.usable(); self.layouter.set_spaces(vec![space], true); } else { - error!( - @self.ctx.f, span, + self.ctx.diag(error!( + span, "page style can only be changed from root context", - ); + )); } } diff --git a/src/library/align.rs b/src/library/align.rs index eaef4d87d..ba5de18a9 100644 --- a/src/library/align.rs +++ b/src/library/align.rs @@ -53,12 +53,12 @@ fn parse_aligns( // `deferred_center` to true and handle the situation once we know more. if let Some(axis) = axis { if align.v.axis().map_or(false, |a| a != axis) { - error!( - @ctx.f, align.span, + ctx.diag(error!( + align.span, "invalid alignment {} for {} axis", align.v, axis, - ); + )); } else if had[axis as usize] { - error!(@ctx.f, align.span, "duplicate alignment for {} axis", axis); + ctx.diag(error!(align.span, "duplicate alignment for {} axis", axis)); } else { let gen_align = align.v.to_gen(ctx.state.sys); *aligns.get_mut(axis.to_gen(ctx.state.sys)) = gen_align; @@ -66,7 +66,7 @@ fn parse_aligns( } } else { if had == [true, true] { - error!(@ctx.f, align.span, "duplicate alignment"); + ctx.diag(error!(align.span, "duplicate alignment")); } else if deferred_center { // We have two unflushed centers, meaning we know that both axes // are to be centered. diff --git a/src/library/color.rs b/src/library/color.rs index a4958c536..261352bab 100644 --- a/src/library/color.rs +++ b/src/library/color.rs @@ -12,7 +12,7 @@ pub async fn rgb(mut args: Args, ctx: &mut LayoutContext) -> Value { let mut clamp = |component: Option>, default| { component.map_or(default, |c| { if c.v < 0 || c.v > 255 { - error!(@ctx.f, c.span, "should be between 0 and 255") + ctx.diag(error!(c.span, "should be between 0 and 255")); } c.v.max(0).min(255) as u8 })