Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong severity of related errors #234

Merged
merged 1 commit into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/handlers/graphical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: ")?,
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/narratable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: ")?,
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))?,
))
}
}
Expand Down
126 changes: 126 additions & 0 deletions tests/graphical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down