From 05def0462594f2533a5d41a6d9c6f870a0281793 Mon Sep 17 00:00:00 2001 From: PgBiel <9021226+PgBiel@users.noreply.github.com> Date: Tue, 4 Jun 2024 20:09:57 -0300 Subject: [PATCH] initial diagnostic identifiers --- crates/typst/src/diag.rs | 36 ++++++++++++++++++++++++++++++++- crates/typst/src/eval/import.rs | 2 ++ crates/typst/src/eval/markup.rs | 4 ++-- crates/typst/src/lib.rs | 2 +- crates/typst/src/text/mod.rs | 1 + 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/crates/typst/src/diag.rs b/crates/typst/src/diag.rs index ad3f3e4a9..f043c550a 100644 --- a/crates/typst/src/diag.rs +++ b/crates/typst/src/diag.rs @@ -115,12 +115,14 @@ macro_rules! __error { macro_rules! __warning { ( $span:expr, + $id:ident, $fmt:literal $(, $arg:expr)* $(; hint: $hint:literal $(, $hint_arg:expr)*)* $(,)? ) => { $crate::diag::SourceDiagnostic::warning( $span, + std::option::Option::Some($crate::diag::WarnIdentifier::$id), $crate::diag::eco_format!($fmt, $($arg),*), ) $(.with_hint($crate::diag::eco_format!($hint, $($hint_arg),*)))* }; @@ -157,6 +159,8 @@ pub struct SourceDiagnostic { pub severity: Severity, /// The span of the relevant node in the source code. pub span: Span, + /// The identifier for this diagnostic. + pub identifier: Option, /// A diagnostic message describing the problem. pub message: EcoString, /// The trace of function calls leading to the problem. @@ -181,6 +185,7 @@ impl SourceDiagnostic { Self { severity: Severity::Error, span, + identifier: None, trace: eco_vec![], message: message.into(), hints: eco_vec![], @@ -188,10 +193,15 @@ impl SourceDiagnostic { } /// Create a new, bare warning. - pub fn warning(span: Span, message: impl Into) -> Self { + pub fn warning( + span: Span, + identifier: Option, + message: impl Into, + ) -> Self { Self { severity: Severity::Warning, span, + identifier: identifier.map(Identifier::Warn), trace: eco_vec![], message: message.into(), hints: eco_vec![], @@ -220,6 +230,7 @@ impl From for SourceDiagnostic { fn from(error: SyntaxError) -> Self { Self { severity: Severity::Error, + identifier: None, span: error.span, message: error.message, trace: eco_vec![], @@ -228,6 +239,29 @@ impl From for SourceDiagnostic { } } +/// Any possible identifier for a diagnostic. +#[derive(Debug, Clone, Eq, PartialEq, Hash)] +pub enum Identifier { + /// Identifier for a built-in compiler error. + Error(ErrorIdentifier), + /// Identifier for a built-in compiler warning. + Warn(WarnIdentifier), + /// Identifier for a warning raised by a package. + User(EcoString), +} + +#[derive(Debug, Clone, Eq, PartialEq, Hash)] +pub enum ErrorIdentifier {} + +#[derive(Debug, Clone, Eq, PartialEq, Hash)] +pub enum WarnIdentifier { + UnnecessaryImportRenaming, + UnnecessaryStars, + UnnecessaryUnderscores, + NonConvergingLayout, + UnknownFontFamilies, +} + /// A part of a diagnostic's [trace](SourceDiagnostic::trace). #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] pub enum Tracepoint { diff --git a/crates/typst/src/eval/import.rs b/crates/typst/src/eval/import.rs index da07e6664..2375fcadb 100644 --- a/crates/typst/src/eval/import.rs +++ b/crates/typst/src/eval/import.rs @@ -37,6 +37,7 @@ impl Eval for ast::ModuleImport<'_> { // Warn on `import x as x` vm.engine.sink.warn(warning!( new_name.span(), + UnnecessaryImportRenaming, "unnecessary import rename to same name", )); } @@ -112,6 +113,7 @@ impl Eval for ast::ModuleImport<'_> { { vm.engine.sink.warn(warning!( renamed_item.new_name().span(), + UnnecessaryImportRenaming, "unnecessary import rename to same name", )); } diff --git a/crates/typst/src/eval/markup.rs b/crates/typst/src/eval/markup.rs index b44c97897..68a41408f 100644 --- a/crates/typst/src/eval/markup.rs +++ b/crates/typst/src/eval/markup.rs @@ -136,7 +136,7 @@ impl Eval for ast::Strong<'_> { vm.engine .sink .warn(warning!( - self.span(), "no text within stars"; + self.span(), UnnecessaryStars, "no text within stars"; hint: "using multiple consecutive stars (e.g. **) has no additional effect", )); } @@ -154,7 +154,7 @@ impl Eval for ast::Emph<'_> { vm.engine .sink .warn(warning!( - self.span(), "no text within underscores"; + self.span(), UnnecessaryUnderscores, "no text within underscores"; hint: "using multiple consecutive underscores (e.g. __) has no additional effect" )); } diff --git a/crates/typst/src/lib.rs b/crates/typst/src/lib.rs index 50575d129..e934ea37e 100644 --- a/crates/typst/src/lib.rs +++ b/crates/typst/src/lib.rs @@ -152,7 +152,7 @@ fn compile_inner( if iter >= 5 { sink.warn(warning!( - Span::detached(), "layout did not converge within 5 attempts"; + Span::detached(), NonConvergingLayout, "layout did not converge within 5 attempts"; hint: "check if any states or queries are updating themselves" )); break; diff --git a/crates/typst/src/text/mod.rs b/crates/typst/src/text/mod.rs index 7648f08fa..70d514d0c 100644 --- a/crates/typst/src/text/mod.rs +++ b/crates/typst/src/text/mod.rs @@ -134,6 +134,7 @@ pub struct TextElem { if !book.contains_family(family.as_str()) { engine.sink.warn(warning!( font_list.span, + UnknownFontFamilies, "unknown font family: {}", family.as_str(), ));