From 384b7bb027a01a91aa403ee55804fb3165aecb4e Mon Sep 17 00:00:00 2001 From: Connor Fitzgerald Date: Mon, 16 Mar 2026 17:52:58 -0400 Subject: [PATCH] Update codespan-reporting to 0.13 (#9243) --- CHANGELOG.md | 1 + Cargo.lock | 4 +-- Cargo.toml | 2 +- naga/src/error.rs | 66 +++++++++++++++++++++++++----------- naga/src/front/glsl/error.rs | 55 ++++++++++++++++++++++++++---- naga/src/front/spv/error.rs | 28 ++++++++++----- naga/src/front/wgsl/error.rs | 10 +++--- naga/src/span.rs | 10 +++--- 8 files changed, 131 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d6b340be..63795190d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -195,6 +195,7 @@ By @kpreid in [#9042](https://github.com/gfx-rs/wgpu/pull/9042). - Prevent UB from incorrectly using ray queries on HLSL. By @Vecvec in [#8763](https://github.com/gfx-rs/wgpu/pull/8763). - Added support for dual-source blending in SPIR-V shaders. By @andyleiserson in [#8865](https://github.com/gfx-rs/wgpu/pull/8865). - Added `supported_capabilities` to all backends. By @inner-daemons in [#9068](https://github.com/gfx-rs/wgpu/pull/9068). +- Updated codespan-reporting to 0.13. By @cwfitzgerald in [#9243](https://github.com/gfx-rs/wgpu/pull/9243). #### Metal diff --git a/Cargo.lock b/Cargo.lock index 909ac93b9..9acfa7988 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -717,9 +717,9 @@ checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9" [[package]] name = "codespan-reporting" -version = "0.12.0" +version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe6d2e5af09e8c8ad56c969f2157a3d4238cebc7c55f0a517728c38f7b200f81" +checksum = "af491d569909a7e4dee0ad7db7f5341fef5c614d5b8ec8cf765732aba3cff681" dependencies = [ "serde", "termcolor", diff --git a/Cargo.toml b/Cargo.toml index d24b4c888..7c8e42077 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -118,7 +118,7 @@ bytemuck = { version = "1.22", features = [ cargo_metadata = "0.23" cfg_aliases = "0.2.1" cfg-if = "1" -codespan-reporting = { version = "0.12", default-features = false } +codespan-reporting = { version = "0.13", default-features = false } ctor = "0.6" diff = "0.1" document-features = "0.2.10" diff --git a/naga/src/error.rs b/naga/src/error.rs index a5e29f469..43e0b0b50 100644 --- a/naga/src/error.rs +++ b/naga/src/error.rs @@ -44,17 +44,11 @@ impl fmt::Display for ShaderError let files = SimpleFile::new(label, replace_control_chars(&self.source)); let config = term::Config::default(); - let writer = { - let mut writer = DiagnosticBuffer::new(); - term::emit( - writer.inner_mut(), - &config, - &files, - &self.inner.diagnostic(), - ) + let mut writer = DiagnosticBuffer::new(); + writer + .emit_to_self(&config, &files, &self.inner.diagnostic()) .expect("cannot write error"); - writer.into_string() - }; + let writer = writer.into_string(); write!(f, "\nShader validation {writer}") } @@ -63,25 +57,46 @@ impl fmt::Display for ShaderError cfg_if::cfg_if! { if #[cfg(feature = "termcolor")] { type DiagnosticBufferInner = codespan_reporting::term::termcolor::NoColor>; - pub(crate) use codespan_reporting::term::termcolor::WriteColor as _ErrorWrite; } else if #[cfg(feature = "stderr")] { type DiagnosticBufferInner = alloc::vec::Vec; - pub(crate) use std::io::Write as _ErrorWrite; } else { type DiagnosticBufferInner = String; - pub(crate) use core::fmt::Write as _ErrorWrite; } } -// Using this indirect export to avoid duplicating the expect(...) for all three cases above. +cfg_if::cfg_if! { + if #[cfg(all(feature = "stderr", feature = "termcolor"))] { + pub(crate) use codespan_reporting::term::termcolor::WriteColor as _ErrorWrite; + } else if #[cfg(feature = "stderr")] { + pub(crate) use std::io::Write as _ErrorWrite; + } +} + +#[cfg(feature = "stderr")] +pub(crate) use _ErrorWrite as ErrorWrite; + +#[cfg(feature = "stderr")] #[cfg_attr( not(any(feature = "spv-in", feature = "glsl-in")), expect( - unused_imports, - reason = "only need `ErrorWrite` with an appropriate front-end." + dead_code, + reason = "only need `emit_to_writer` with an appropriate front-end." ) )] -pub(crate) use _ErrorWrite as ErrorWrite; +pub(crate) fn emit_to_writer<'files, F: codespan_reporting::files::Files<'files> + ?Sized>( + writer: &mut impl ErrorWrite, + config: &codespan_reporting::term::Config, + files: &'files F, + diagnostic: &codespan_reporting::diagnostic::Diagnostic, +) -> Result<(), codespan_reporting::files::Error> { + cfg_if::cfg_if! { + if #[cfg(feature = "termcolor")] { + codespan_reporting::term::emit_to_write_style(writer, config, files, diagnostic) + } else { + codespan_reporting::term::emit_to_io_write(writer, config, files, diagnostic) + } + } +} pub(crate) struct DiagnosticBuffer { inner: DiagnosticBufferInner, @@ -109,8 +124,21 @@ impl DiagnosticBuffer { Self { inner } } - pub const fn inner_mut(&mut self) -> &mut DiagnosticBufferInner { - &mut self.inner + pub fn emit_to_self<'files, F: codespan_reporting::files::Files<'files> + ?Sized>( + &mut self, + config: &codespan_reporting::term::Config, + files: &'files F, + diagnostic: &codespan_reporting::diagnostic::Diagnostic, + ) -> Result<(), codespan_reporting::files::Error> { + cfg_if::cfg_if! { + if #[cfg(feature = "termcolor")] { + codespan_reporting::term::emit_to_write_style(&mut self.inner, config, files, diagnostic) + } else if #[cfg(feature = "stderr")] { + codespan_reporting::term::emit_to_io_write(&mut self.inner, config, files, diagnostic) + } else { + codespan_reporting::term::emit_to_string(&mut self.inner, config, files, diagnostic) + } + } } pub fn into_string(self) -> String { diff --git a/naga/src/front/glsl/error.rs b/naga/src/front/glsl/error.rs index 9c9e58549..8f5d57308 100644 --- a/naga/src/front/glsl/error.rs +++ b/naga/src/front/glsl/error.rs @@ -12,8 +12,10 @@ use pp_rs::token::PreprocessorError; use thiserror::Error; use super::token::TokenValue; +#[cfg(feature = "stderr")] +use crate::error::ErrorWrite; use crate::{error::replace_control_chars, SourceLocation}; -use crate::{error::ErrorWrite, proc::ConstantEvaluatorError, Span}; +use crate::{proc::ConstantEvaluatorError, Span}; fn join_with_comma(list: &[ExpectedToken]) -> String { let mut string = "".to_string(); @@ -165,31 +167,70 @@ pub struct ParseErrors { } impl ParseErrors { + #[cfg(feature = "stderr")] pub fn emit_to_writer(&self, writer: &mut impl ErrorWrite, source: &str) { self.emit_to_writer_with_path(writer, source, "glsl"); } + #[cfg(feature = "stderr")] pub fn emit_to_writer_with_path(&self, writer: &mut impl ErrorWrite, source: &str, path: &str) { let path = path.to_string(); let files = SimpleFile::new(path, replace_control_chars(source)); let config = term::Config::default(); for err in &self.errors { - let mut diagnostic = Diagnostic::error().with_message(err.kind.to_string()); + let diagnostic = Self::make_diagnostic(err); + crate::error::emit_to_writer(writer, &config, &files, &diagnostic) + .expect("cannot write error"); + } + } - if let Some(range) = err.meta.to_range() { - diagnostic = diagnostic.with_labels(vec![Label::primary((), range)]); + /// Emits a summary of the errors to standard error stream. + #[cfg(feature = "stderr")] + pub fn emit_to_stderr(&self, source: &str) { + self.emit_to_stderr_with_path(source, "glsl") + } + + /// Emits a summary of the errors to standard error stream. + #[cfg(feature = "stderr")] + pub fn emit_to_stderr_with_path(&self, source: &str, path: &str) { + cfg_if::cfg_if! { + if #[cfg(feature = "termcolor")] { + let writer = term::termcolor::StandardStream::stderr(term::termcolor::ColorChoice::Auto); + self.emit_to_writer_with_path(&mut writer.lock(), source, path); + } else { + let writer = std::io::stderr(); + self.emit_to_writer_with_path(&mut writer.lock(), source, path); } - - term::emit(writer, &config, &files, &diagnostic).expect("cannot write error"); } } pub fn emit_to_string(&self, source: &str) -> String { + self.emit_to_string_with_path(source, "glsl") + } + + pub fn emit_to_string_with_path(&self, source: &str, path: &str) -> String { + let path = path.to_string(); + let files = SimpleFile::new(path, replace_control_chars(source)); + let config = term::Config::default(); + let mut writer = crate::error::DiagnosticBuffer::new(); - self.emit_to_writer(writer.inner_mut(), source); + for err in &self.errors { + let diagnostic = Self::make_diagnostic(err); + writer + .emit_to_self(&config, &files, &diagnostic) + .expect("cannot write error"); + } writer.into_string() } + + fn make_diagnostic(err: &Error) -> Diagnostic<()> { + let mut diagnostic = Diagnostic::error().with_message(err.kind.to_string()); + if let Some(range) = err.meta.to_range() { + diagnostic = diagnostic.with_labels(vec![Label::primary((), range)]); + } + diagnostic + } } impl core::fmt::Display for ParseErrors { diff --git a/naga/src/front/spv/error.rs b/naga/src/front/spv/error.rs index 76b84c59b..c0c525d36 100644 --- a/naga/src/front/spv/error.rs +++ b/naga/src/front/spv/error.rs @@ -8,11 +8,9 @@ use codespan_reporting::files::SimpleFile; use codespan_reporting::term; use super::ModuleState; -use crate::{ - arena::Handle, - error::{replace_control_chars, ErrorWrite}, - front::atomic_upgrade, -}; +#[cfg(feature = "stderr")] +use crate::error::ErrorWrite; +use crate::{arena::Handle, error::replace_control_chars, front::atomic_upgrade}; #[derive(Clone, Debug, thiserror::Error)] pub enum Error { @@ -159,22 +157,36 @@ pub enum Error { } impl Error { + #[cfg(feature = "stderr")] pub fn emit_to_writer(&self, writer: &mut impl ErrorWrite, source: &str) { - self.emit_to_writer_with_path(writer, source, "glsl"); + self.emit_to_writer_with_path(writer, source, "spv"); } + #[cfg(feature = "stderr")] pub fn emit_to_writer_with_path(&self, writer: &mut impl ErrorWrite, source: &str, path: &str) { let path = path.to_string(); let files = SimpleFile::new(path, replace_control_chars(source)); let config = term::Config::default(); let diagnostic = Diagnostic::error().with_message(format!("{self:?}")); - term::emit(writer, &config, &files, &diagnostic).expect("cannot write error"); + crate::error::emit_to_writer(writer, &config, &files, &diagnostic) + .expect("cannot write error"); } pub fn emit_to_string(&self, source: &str) -> String { + self.emit_to_string_with_path(source, "spv") + } + + pub fn emit_to_string_with_path(&self, source: &str, path: &str) -> String { + let path = path.to_string(); + let files = SimpleFile::new(path, replace_control_chars(source)); + let config = term::Config::default(); + let diagnostic = Diagnostic::error().with_message(format!("{self:?}")); + let mut writer = crate::error::DiagnosticBuffer::new(); - self.emit_to_writer(writer.inner_mut(), source); + writer + .emit_to_self(&config, &files, &diagnostic) + .expect("cannot write error"); writer.into_string() } } diff --git a/naga/src/front/wgsl/error.rs b/naga/src/front/wgsl/error.rs index 0df208278..ec7de5409 100644 --- a/naga/src/front/wgsl/error.rs +++ b/naga/src/front/wgsl/error.rs @@ -86,13 +86,14 @@ impl ParseError { cfg_if::cfg_if! { if #[cfg(feature = "termcolor")] { let writer = term::termcolor::StandardStream::stderr(term::termcolor::ColorChoice::Auto); + term::emit_to_write_style(&mut writer.lock(), &config, &files, &self.diagnostic()) + .expect("cannot write error"); } else { let writer = std::io::stderr(); + term::emit_to_io_write(&mut writer.lock(), &config, &files, &self.diagnostic()) + .expect("cannot write error"); } } - - term::emit(&mut writer.lock(), &config, &files, &self.diagnostic()) - .expect("cannot write error"); } /// Emits a summary of the error to a string. @@ -110,7 +111,8 @@ impl ParseError { let config = term::Config::default(); let mut writer = crate::error::DiagnosticBuffer::new(); - term::emit(writer.inner_mut(), &config, &files, &self.diagnostic()) + writer + .emit_to_self(&config, &files, &self.diagnostic()) .expect("cannot write error"); writer.into_string() } diff --git a/naga/src/span.rs b/naga/src/span.rs index 9562f3fc2..7a4d4737a 100644 --- a/naga/src/span.rs +++ b/naga/src/span.rs @@ -294,13 +294,14 @@ impl WithSpan { cfg_if::cfg_if! { if #[cfg(feature = "termcolor")] { let writer = term::termcolor::StandardStream::stderr(term::termcolor::ColorChoice::Auto); + term::emit_to_write_style(&mut writer.lock(), &config, &files, &self.diagnostic()) + .expect("cannot write error"); } else { let writer = std::io::stderr(); + term::emit_to_io_write(&mut writer.lock(), &config, &files, &self.diagnostic()) + .expect("cannot write error"); } } - - term::emit(&mut writer.lock(), &config, &files, &self.diagnostic()) - .expect("cannot write error"); } /// Emits a summary of the error to a string. @@ -322,7 +323,8 @@ impl WithSpan { let config = term::Config::default(); let mut writer = crate::error::DiagnosticBuffer::new(); - term::emit(writer.inner_mut(), &config, &files, &self.diagnostic()) + writer + .emit_to_self(&config, &files, &self.diagnostic()) .expect("cannot write error"); writer.into_string() }