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

Fold Error source chain Display output into shader errors #3731

Closed
Closed
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
23 changes: 22 additions & 1 deletion naga/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ impl fmt::Display for ShaderError<crate::front::wgsl::ParseError> {
write!(f, "\nShader '{label}' parsing {string}")
}
}

#[cfg(feature = "glsl-in")]
impl fmt::Display for ShaderError<crate::front::glsl::ParseErrors> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -24,6 +25,7 @@ impl fmt::Display for ShaderError<crate::front::glsl::ParseErrors> {
write!(f, "\nShader '{label}' parsing {string}")
}
}

#[cfg(feature = "spv-in")]
impl fmt::Display for ShaderError<crate::front::spv::Error> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -32,6 +34,7 @@ impl fmt::Display for ShaderError<crate::front::spv::Error> {
write!(f, "\nShader '{label}' parsing {string}")
}
}

impl fmt::Display for ShaderError<crate::WithSpan<crate::valid::ValidationError>> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use codespan_reporting::{
Expand All @@ -45,7 +48,24 @@ impl fmt::Display for ShaderError<crate::WithSpan<crate::valid::ValidationError>
let config = term::Config::default();
let mut writer = termcolor::NoColor::new(Vec::new());

let diagnostic = Diagnostic::error().with_labels(
let err_chain = {
use std::fmt::Write;

let mut msg_buf = String::new();
write!(msg_buf, "{}", self.inner).unwrap();

let mut source = self.inner.source();
while let Some(next) = source {
// NOTE: The spacing here matters for presentation as a `Diagnostic`. Formula used:
//
// * 7 spaces to offset `error: `
// * 2 more spaces for "compact" indentation of the original error
writeln!(msg_buf, " {next}").unwrap();
source = next.source();
}
msg_buf
};
let diagnostic = Diagnostic::error().with_message(err_chain).with_labels(
self.inner
.spans()
.map(|&(span, ref desc)| {
Expand All @@ -63,6 +83,7 @@ impl fmt::Display for ShaderError<crate::WithSpan<crate::valid::ValidationError>
)
}
}

impl<E> Error for ShaderError<E>
where
ShaderError<E>: fmt::Display,
Expand Down
Loading