From c9767170d4c9c58ee49d55c4d0ef538e980680d4 Mon Sep 17 00:00:00 2001 From: dalance Date: Tue, 7 Feb 2023 11:45:32 +0900 Subject: [PATCH] fix(graphical): Fix wrong severity of related errors --- src/handlers/graphical.rs | 2 +- src/handlers/narratable.rs | 6 +- src/protocol.rs | 2 +- tests/graphical.rs | 126 +++++++++++++++++++++++++++++++++++++ 4 files changed, 131 insertions(+), 5 deletions(-) diff --git a/src/handlers/graphical.rs b/src/handlers/graphical.rs index 15d2980..7c8685d 100644 --- a/src/handlers/graphical.rs +++ b/src/handlers/graphical.rs @@ -281,7 +281,7 @@ impl GraphicalReportHandler { if let Some(related) = diagnostic.related() { writeln!(f)?; for rel in related { - match diagnostic.severity() { + match rel.severity() { Some(Severity::Error) | None => write!(f, "Error: ")?, Some(Severity::Warning) => write!(f, "Warning: ")?, Some(Severity::Advice) => write!(f, "Advice: ")?, diff --git a/src/handlers/narratable.rs b/src/handlers/narratable.rs index 5855752..d90298a 100644 --- a/src/handlers/narratable.rs +++ b/src/handlers/narratable.rs @@ -132,7 +132,7 @@ impl NarratableReportHandler { if let Some(related) = diagnostic.related() { writeln!(f)?; for rel in related { - match diagnostic.severity() { + match rel.severity() { Some(Severity::Error) | None => write!(f, "Error: ")?, Some(Severity::Warning) => write!(f, "Warning: ")?, Some(Severity::Advice) => write!(f, "Advice: ")?, @@ -218,10 +218,10 @@ impl NarratableReportHandler { Ok(()) } - fn render_context<'a>( + fn render_context( &self, f: &mut impl fmt::Write, - source: &'a dyn SourceCode, + source: &dyn SourceCode, context: &LabeledSpan, labels: &[LabeledSpan], ) -> fmt::Result { diff --git a/src/protocol.rs b/src/protocol.rs index d94011a..5b657b4 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -488,7 +488,7 @@ impl SourceOffset { Ok(( loc.file().into(), fs::read_to_string(loc.file()) - .map(|txt| Self::from_location(&txt, loc.line() as usize, loc.column() as usize))?, + .map(|txt| Self::from_location(txt, loc.line() as usize, loc.column() as usize))?, )) } } diff --git a/tests/graphical.rs b/tests/graphical.rs index 69f5bff..34dc433 100644 --- a/tests/graphical.rs +++ b/tests/graphical.rs @@ -1057,6 +1057,132 @@ Error: oops::my::bad Ok(()) } +#[test] +fn related_severity() -> Result<(), MietteError> { + #[derive(Debug, Diagnostic, Error)] + #[error("oops!")] + #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] + struct MyBad { + #[source_code] + src: NamedSource, + #[label("this bit here")] + highlight: SourceSpan, + #[related] + related: Vec, + } + + #[derive(Debug, Diagnostic, Error)] + enum MyRelated { + #[error("oops!")] + #[diagnostic( + severity(Error), + code(oops::my::related::error), + help("try doing it better next time?") + )] + Error { + #[source_code] + src: NamedSource, + #[label("this bit here")] + highlight: SourceSpan, + }, + + #[error("oops!")] + #[diagnostic( + severity(Warning), + code(oops::my::related::warning), + help("try doing it better next time?") + )] + Warning { + #[source_code] + src: NamedSource, + #[label("this bit here")] + highlight: SourceSpan, + }, + + #[error("oops!")] + #[diagnostic( + severity(Advice), + code(oops::my::related::advice), + help("try doing it better next time?") + )] + Advice { + #[source_code] + src: NamedSource, + #[label("this bit here")] + highlight: SourceSpan, + }, + } + + let src = "source\n text\n here".to_string(); + let err = MyBad { + src: NamedSource::new("bad_file.rs", src.clone()), + highlight: (9, 4).into(), + related: vec![ + MyRelated::Error { + src: NamedSource::new("bad_file.rs", src.clone()), + highlight: (0, 6).into(), + }, + MyRelated::Warning { + src: NamedSource::new("bad_file.rs", src.clone()), + highlight: (0, 6).into(), + }, + MyRelated::Advice { + src: NamedSource::new("bad_file.rs", src), + highlight: (0, 6).into(), + }, + ], + }; + let out = fmt_report(err.into()); + println!("Error: {}", out); + let expected = r#"oops::my::bad + + × oops! + ╭─[bad_file.rs:1:1] + 1 │ source + 2 │ text + · ──┬─ + · ╰── this bit here + 3 │ here + ╰──── + help: try doing it better next time? + +Error: oops::my::related::error + + × oops! + ╭─[bad_file.rs:1:1] + 1 │ source + · ───┬── + · ╰── this bit here + 2 │ text + ╰──── + help: try doing it better next time? +Warning: oops::my::related::warning + + ⚠ oops! + ╭─[bad_file.rs:1:1] + 1 │ source + · ───┬── + · ╰── this bit here + 2 │ text + ╰──── + help: try doing it better next time? +Advice: oops::my::related::advice + + ☞ oops! + ╭─[bad_file.rs:1:1] + 1 │ source + · ───┬── + · ╰── this bit here + 2 │ text + ╰──── + help: try doing it better next time? +"# + .trim_start() + .to_string(); + assert_eq!(expected, out); + Ok(()) +} + #[test] fn zero_length_eol_span() { #[derive(Error, Debug, Diagnostic)]