initial diagnostic identifiers

This commit is contained in:
PgBiel 2024-06-04 20:09:57 -03:00
parent 45366c0112
commit 05def04625
5 changed files with 41 additions and 4 deletions

View File

@ -115,12 +115,14 @@ macro_rules! __error {
macro_rules! __warning { macro_rules! __warning {
( (
$span:expr, $span:expr,
$id:ident,
$fmt:literal $(, $arg:expr)* $fmt:literal $(, $arg:expr)*
$(; hint: $hint:literal $(, $hint_arg:expr)*)* $(; hint: $hint:literal $(, $hint_arg:expr)*)*
$(,)? $(,)?
) => { ) => {
$crate::diag::SourceDiagnostic::warning( $crate::diag::SourceDiagnostic::warning(
$span, $span,
std::option::Option::Some($crate::diag::WarnIdentifier::$id),
$crate::diag::eco_format!($fmt, $($arg),*), $crate::diag::eco_format!($fmt, $($arg),*),
) $(.with_hint($crate::diag::eco_format!($hint, $($hint_arg),*)))* ) $(.with_hint($crate::diag::eco_format!($hint, $($hint_arg),*)))*
}; };
@ -157,6 +159,8 @@ pub struct SourceDiagnostic {
pub severity: Severity, pub severity: Severity,
/// The span of the relevant node in the source code. /// The span of the relevant node in the source code.
pub span: Span, pub span: Span,
/// The identifier for this diagnostic.
pub identifier: Option<Identifier>,
/// 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.
@ -181,6 +185,7 @@ impl SourceDiagnostic {
Self { Self {
severity: Severity::Error, severity: Severity::Error,
span, span,
identifier: None,
trace: eco_vec![], trace: eco_vec![],
message: message.into(), message: message.into(),
hints: eco_vec![], hints: eco_vec![],
@ -188,10 +193,15 @@ impl SourceDiagnostic {
} }
/// Create a new, bare warning. /// Create a new, bare warning.
pub fn warning(span: Span, message: impl Into<EcoString>) -> Self { pub fn warning(
span: Span,
identifier: Option<WarnIdentifier>,
message: impl Into<EcoString>,
) -> Self {
Self { Self {
severity: Severity::Warning, severity: Severity::Warning,
span, span,
identifier: identifier.map(Identifier::Warn),
trace: eco_vec![], trace: eco_vec![],
message: message.into(), message: message.into(),
hints: eco_vec![], hints: eco_vec![],
@ -220,6 +230,7 @@ impl From<SyntaxError> for SourceDiagnostic {
fn from(error: SyntaxError) -> Self { fn from(error: SyntaxError) -> Self {
Self { Self {
severity: Severity::Error, severity: Severity::Error,
identifier: None,
span: error.span, span: error.span,
message: error.message, message: error.message,
trace: eco_vec![], trace: eco_vec![],
@ -228,6 +239,29 @@ impl From<SyntaxError> 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). /// A part of a diagnostic's [trace](SourceDiagnostic::trace).
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum Tracepoint { pub enum Tracepoint {

View File

@ -37,6 +37,7 @@ impl Eval for ast::ModuleImport<'_> {
// Warn on `import x as x` // Warn on `import x as x`
vm.engine.sink.warn(warning!( vm.engine.sink.warn(warning!(
new_name.span(), new_name.span(),
UnnecessaryImportRenaming,
"unnecessary import rename to same name", "unnecessary import rename to same name",
)); ));
} }
@ -112,6 +113,7 @@ impl Eval for ast::ModuleImport<'_> {
{ {
vm.engine.sink.warn(warning!( vm.engine.sink.warn(warning!(
renamed_item.new_name().span(), renamed_item.new_name().span(),
UnnecessaryImportRenaming,
"unnecessary import rename to same name", "unnecessary import rename to same name",
)); ));
} }

View File

@ -136,7 +136,7 @@ impl Eval for ast::Strong<'_> {
vm.engine vm.engine
.sink .sink
.warn(warning!( .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", hint: "using multiple consecutive stars (e.g. **) has no additional effect",
)); ));
} }
@ -154,7 +154,7 @@ impl Eval for ast::Emph<'_> {
vm.engine vm.engine
.sink .sink
.warn(warning!( .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" hint: "using multiple consecutive underscores (e.g. __) has no additional effect"
)); ));
} }

View File

@ -152,7 +152,7 @@ fn compile_inner(
if iter >= 5 { if iter >= 5 {
sink.warn(warning!( 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" hint: "check if any states or queries are updating themselves"
)); ));
break; break;

View File

@ -134,6 +134,7 @@ pub struct TextElem {
if !book.contains_family(family.as_str()) { if !book.contains_family(family.as_str()) {
engine.sink.warn(warning!( engine.sink.warn(warning!(
font_list.span, font_list.span,
UnknownFontFamilies,
"unknown font family: {}", "unknown font family: {}",
family.as_str(), family.as_str(),
)); ));