From ff93928902970eb02bff46da93578ee838ce9720 Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Wed, 26 Apr 2023 16:19:22 -0400 Subject: [PATCH] refactor(core)!: fold `Error` source chain `Display` output into shader errors --- wgpu-core/src/pipeline.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/wgpu-core/src/pipeline.rs b/wgpu-core/src/pipeline.rs index b58d5a427f0..9a7602a7dcc 100644 --- a/wgpu-core/src/pipeline.rs +++ b/wgpu-core/src/pipeline.rs @@ -7,7 +7,13 @@ use crate::{ validation, Label, LifeGuard, Stored, }; use arrayvec::ArrayVec; -use std::{borrow::Cow, error::Error, fmt, marker::PhantomData, num::NonZeroU32}; +use std::{ + borrow::Cow, + error::Error, + fmt::{self, Write}, + marker::PhantomData, + num::NonZeroU32, +}; use thiserror::Error; /// Information about buffer bindings, which @@ -92,7 +98,22 @@ impl fmt::Display for ShaderError> let config = term::Config::default(); let mut writer = term::termcolor::NoColor::new(Vec::new()); - let diagnostic = Diagnostic::error().with_labels( + let err_chain = { + 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)| {