Skip to content

Commit 3da2382

Browse files
committed
[naga wgsl] Move Scalar diagnostic formatting to common code.
Add function `naga::common::wgsl::write_scalar_for_diagnostics` for writing a WGSL representation of a `Scalar` appropriate for diagnostic purposes. Change `naga::front::wgsl::to_wgsl`'s implementation of `TypeContext` to use that new function.
1 parent 7c0c43b commit 3da2382

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

naga/src/common/wgsl/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ mod types;
66

77
pub use diagnostics::DisplayFilterableTriggeringRule;
88
pub use to_wgsl::{address_space_str, ToWgsl, TryToWgsl};
9-
pub use types::TypeContext;
9+
pub use types::{write_scalar_for_diagnostics, TypeContext};

naga/src/common/wgsl/types.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,30 @@ impl From<core::fmt::Error> for WriteTypeError {
299299
Self::Format(err)
300300
}
301301
}
302+
303+
/// Write `scalar` in WGSL as appropriate for diagnostic messages.
304+
///
305+
/// If `scalar` can be expressed in WGSL, write its WGSL form to `out`.
306+
///
307+
/// If `scalar` is an abstract type, write something suggestive and
308+
/// human-readable to `out`.
309+
///
310+
/// Otherwise, write something human-readable that includes the
311+
/// [`Debug`] representation of `scalar`.
312+
///
313+
/// [`Debug`]: core::fmt::Debug
314+
pub fn write_scalar_for_diagnostics<W: Write>(scalar: Scalar, out: &mut W) -> core::fmt::Result {
315+
match scalar.kind {
316+
crate::ScalarKind::Sint
317+
| crate::ScalarKind::Uint
318+
| crate::ScalarKind::Float
319+
| crate::ScalarKind::Bool => write!(out, "{{non-WGSL Naga scalar {scalar:?}}}"),
320+
321+
// The abstract types are kind of an odd pseudo-WGSL category:
322+
// they are definitely part of the spec, but they are not expressible
323+
// in WGSL itself. So we want to call them out by name in error messages,
324+
// but the WGSL backend should never generate these.
325+
crate::ScalarKind::AbstractInt => out.write_str("{AbstractInt}"),
326+
crate::ScalarKind::AbstractFloat => out.write_str("{AbstractFloat}"),
327+
}
328+
}

naga/src/front/wgsl/to_wgsl.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,7 @@ impl<W: fmt::Write> TypeContext<W> for GlobalCtx<'_> {
8686
}
8787

8888
fn write_non_wgsl_scalar(&self, scalar: Scalar, out: &mut W) -> fmt::Result {
89-
match scalar.kind {
90-
crate::ScalarKind::Sint
91-
| crate::ScalarKind::Uint
92-
| crate::ScalarKind::Float
93-
| crate::ScalarKind::Bool => write!(out, "{{non-WGSL Naga scalar {scalar:?}}}"),
94-
95-
// The abstract types are kind of an odd pseudo-WGSL category:
96-
// they are definitely part of the spec, but they are not expressible
97-
// in WGSL itself. So we want to call them out by name in error messages,
98-
// but the WGSL backend should never generate these.
99-
crate::ScalarKind::AbstractInt => out.write_str("{AbstractInt}"),
100-
crate::ScalarKind::AbstractFloat => out.write_str("{AbstractFloat}"),
101-
}
89+
crate::common::wgsl::write_scalar_for_diagnostics(scalar, out)
10290
}
10391
}
10492

0 commit comments

Comments
 (0)