From 62d49918fda06e2e407d663c7d7b8fbae3a52350 Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Sun, 16 Mar 2025 12:38:14 -0700 Subject: [PATCH] [naga wgsl] Provide defaults for `TypeContext` non-WGSL methods. Provide default implementations for the `write_non_wgsl_inner` and `write_non_wgsl_scalar` methods of `naga::common::wgsl::TypeContext`. We will be implementing this trait for various types in the WGSL front end and the validator, and almost all of these applications will need basically identical implementations of these methods. --- naga/src/common/wgsl/types.rs | 38 +++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/naga/src/common/wgsl/types.rs b/naga/src/common/wgsl/types.rs index ae30c635a..8c3e83159 100644 --- a/naga/src/common/wgsl/types.rs +++ b/naga/src/common/wgsl/types.rs @@ -44,20 +44,42 @@ pub trait TypeContext { /// /// A backend might implement this with a call to the [`unreachable!`] /// macro, since backends are allowed to assume that the module has passed - /// validation, whereas something generating type names to appear in error messages - /// might punt to `TypeInner`'s [`core::fmt::Debug`] implementation, since it's - /// probably better to show the user something they can act on. - fn write_non_wgsl_inner(&self, inner: &TypeInner, out: &mut W) -> core::fmt::Result; + /// validation. + /// + /// The default implementation is appropriate for generating type names to + /// appear in error messages. It punts to `TypeInner`'s [`core::fmt::Debug`] + /// implementation, since it's probably best to show the user something they + /// can act on. + fn write_non_wgsl_inner(&self, inner: &TypeInner, out: &mut W) -> core::fmt::Result { + write!(out, "{{non-WGSL Naga type {inner:?}}}") + } /// Write a [`Scalar`] that has no representation as WGSL source, /// even including Naga extensions. /// /// A backend might implement this with a call to the [`unreachable!`] /// macro, since backends are allowed to assume that the module has passed - /// validation, whereas something generating type names to appear in error messages - /// might punt to `Scalar`'s [`core::fmt::Debug`] implementation, since it's - /// probably better to show the user something they can act on. - fn write_non_wgsl_scalar(&self, scalar: Scalar, out: &mut W) -> core::fmt::Result; + /// validation. + /// + /// The default implementation is appropriate for generating type names to + /// appear in error messages. It punts to `Scalar`'s [`core::fmt::Debug`] + /// implementation, since it's probably best to show the user something they + /// can act on. + fn write_non_wgsl_scalar(&self, scalar: Scalar, out: &mut W) -> core::fmt::Result { + match scalar.kind { + crate::ScalarKind::Sint + | crate::ScalarKind::Uint + | crate::ScalarKind::Float + | crate::ScalarKind::Bool => write!(out, "{{non-WGSL Naga scalar {scalar:?}}}"), + + // The abstract types are kind of an odd quasi-WGSL category: + // they are definitely part of the spec, but they are not expressible + // in WGSL itself. So we want to call them out by name in error messages, + // but the WGSL backend should never generate these. + crate::ScalarKind::AbstractInt => out.write_str("{AbstractInt}"), + crate::ScalarKind::AbstractFloat => out.write_str("{AbstractFloat}"), + } + } /// Write the type `ty` as it would appear in a value's declaration. ///