Skip to content

Commit

Permalink
Include naga diagnostic in the parse error (#1760)
Browse files Browse the repository at this point in the history
Wraps the Naga's `ParseError` with `NagaParseError` type, that uses the
Naga's full error formatting for its `Display` impl, including shader
source.
  • Loading branch information
scoopr authored Aug 3, 2021
1 parent b9d18a1 commit 5d9c276
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
7 changes: 6 additions & 1 deletion wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,12 @@ impl<A: HalApi> Device<A> {
Ok(module) => module,
Err(err) => {
log::error!("Failed to parse WGSL code for {:?}: {}", desc.label, err);
return Err(pipeline::CreateShaderModuleError::Parsing);
return Err(pipeline::CreateShaderModuleError::Parsing(
pipeline::NagaParseError {
shader_source: code.to_string(),
error: err,
},
));
}
}
}
Expand Down
17 changes: 16 additions & 1 deletion wgpu-core/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,25 @@ impl<A: hal::Api> Resource for ShaderModule<A> {
}
}

#[derive(Clone, Debug, Error)]
pub struct NagaParseError {
pub shader_source: String,
pub error: naga::front::wgsl::ParseError,
}
impl std::fmt::Display for NagaParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"\nShader error:\n{}",
self.error.emit_to_string(&self.shader_source)
)
}
}

#[derive(Clone, Debug, Error)]
pub enum CreateShaderModuleError {
#[error("Failed to parse a shader")]
Parsing,
Parsing(#[from] NagaParseError),
#[error("Failed to generate the backend-specific code")]
Generation,
#[error(transparent)]
Expand Down

0 comments on commit 5d9c276

Please sign in to comment.