include sources in shader validation error (#3724)

## Objective

When print shader validation error messages, we didn't print the sources and error message text, which led to some confusing error messages.

```cs
error: 
   ┌─ wgsl:15:11
   │
15 │     return material.color + 1u;
   │           ^^^^^^^^^^^^^^^^^^^^ naga::Expression [11]
```

## Solution

New error message:
```cs
error: Entry point fragment at Vertex is invalid
   ┌─ wgsl:15:11
   │
15 │     return material.color + 1u;
   │           ^^^^^^^^^^^^^^^^^^^^ naga::Expression [11]
   │
   = Expression [11] is invalid
   = Operation Add can't work with [8] and [10]
```
This commit is contained in:
Jakob Hellermann 2022-02-04 03:37:38 +00:00
parent aa7b158893
commit b7dfe1677f

View file

@ -464,15 +464,22 @@ fn log_shader_error(source: &ProcessedShader, error: &AsModuleDescriptorError) {
let config = term::Config::default();
let mut writer = term::termcolor::Ansi::new(Vec::new());
let diagnostic = Diagnostic::error().with_labels(
error
.spans()
.map(|(span, desc)| {
Label::primary((), span.to_range().unwrap())
.with_message(desc.to_owned())
})
.collect(),
);
let diagnostic = Diagnostic::error()
.with_message(error.to_string())
.with_labels(
error
.spans()
.map(|(span, desc)| {
Label::primary((), span.to_range().unwrap())
.with_message(desc.to_owned())
})
.collect(),
)
.with_notes(
ErrorSources::of(error)
.map(|source| source.to_string())
.collect(),
);
term::emit(&mut writer, &config, &files, &diagnostic).expect("cannot write error");
@ -490,3 +497,25 @@ fn log_shader_error(source: &ProcessedShader, error: &AsModuleDescriptorError) {
}
}
}
struct ErrorSources<'a> {
current: Option<&'a (dyn std::error::Error + 'static)>,
}
impl<'a> ErrorSources<'a> {
fn of(error: &'a dyn std::error::Error) -> Self {
Self {
current: error.source(),
}
}
}
impl<'a> Iterator for ErrorSources<'a> {
type Item = &'a (dyn std::error::Error + 'static);
fn next(&mut self) -> Option<Self::Item> {
let current = self.current;
self.current = self.current.and_then(std::error::Error::source);
current
}
}