From 5d9c276c827e3c85da36251f1b6904733f31c6c8 Mon Sep 17 00:00:00 2001 From: Mikko Lehtonen Date: Wed, 4 Aug 2021 00:27:34 +0300 Subject: [PATCH] Include naga diagnostic in the parse error (#1760) Wraps the Naga's `ParseError` with `NagaParseError` type, that uses the Naga's full error formatting for its `Display` impl, including shader source. --- wgpu-core/src/device/mod.rs | 7 ++++++- wgpu-core/src/pipeline.rs | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 9f8433db9b..6f5e999399 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -895,7 +895,12 @@ impl Device { 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, + }, + )); } } } diff --git a/wgpu-core/src/pipeline.rs b/wgpu-core/src/pipeline.rs index f090bffd8e..81d90ed711 100644 --- a/wgpu-core/src/pipeline.rs +++ b/wgpu-core/src/pipeline.rs @@ -44,10 +44,25 @@ impl Resource for ShaderModule { } } +#[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)]