fix(graphical): Fix wrong severity of related errors

This commit is contained in:
dalance 2023-02-07 11:45:32 +09:00
parent ebc61b5cf8
commit c9767170d4
4 changed files with 131 additions and 5 deletions

View File

@ -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: ")?,

View File

@ -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 {

View File

@ -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))?,
))
}
}

View File

@ -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<MyRelated>,
}
#[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)]