diff --git a/naga/src/error.rs b/naga/src/error.rs index c5845e5fd6..856e56eaa1 100644 --- a/naga/src/error.rs +++ b/naga/src/error.rs @@ -16,6 +16,7 @@ impl fmt::Display for ShaderError { write!(f, "\nShader '{label}' parsing {string}") } } + #[cfg(feature = "glsl-in")] impl fmt::Display for ShaderError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -24,6 +25,7 @@ impl fmt::Display for ShaderError { write!(f, "\nShader '{label}' parsing {string}") } } + #[cfg(feature = "spv-in")] impl fmt::Display for ShaderError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -32,6 +34,7 @@ impl fmt::Display for ShaderError { write!(f, "\nShader '{label}' parsing {string}") } } + impl fmt::Display for ShaderError> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { use codespan_reporting::{ @@ -45,7 +48,24 @@ impl fmt::Display for ShaderError 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)| { @@ -63,6 +83,7 @@ impl fmt::Display for ShaderError ) } } + impl Error for ShaderError where ShaderError: fmt::Display,