Skip to content

Commit aad187f

Browse files
jimblandyErichDonGubler
authored andcommitted
[naga wgsl] New TypeContext method write_unnamed_struct.
When asked to generate WGSL for `TypeInner::Struct`, rather than unconditionally calling `unreachable!`, defer to a new `TypeContext` method, `write_unnamed_struct`. Provide appropriate `write_unnamed_struct` implementations: - In the WGSL backend, implement this as `unreachable!`, since the WGSL backend should always know the proper name to use for a struct. - For diagnostic messages, generate something human-readable that indicates that some struct type was encountered. - For logging and debugging, defer to `TypeInner`'s `Debug` implementation.
1 parent 25f0396 commit aad187f

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

naga/src/back/wgsl/writer.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,6 +1764,10 @@ impl TypeContext for WriterTypeContext<'_> {
17641764
self.names[&NameKey::Type(handle)].as_str()
17651765
}
17661766

1767+
fn write_unnamed_struct<W: Write>(&self, _: &TypeInner, _: &mut W) -> core::fmt::Result {
1768+
unreachable!("the WGSL back end should always provide type handles");
1769+
}
1770+
17671771
fn write_override<W: Write>(&self, _: Handle<crate::Override>, _: &mut W) -> core::fmt::Result {
17681772
unreachable!("overrides should be validated out");
17691773
}

naga/src/common/wgsl/types.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ pub trait TypeContext {
3939
out: &mut W,
4040
) -> core::fmt::Result;
4141

42+
/// Write a [`TypeInner::Struct`] for which we are unable to find a name.
43+
///
44+
/// The names of struct types are only available if we have `Handle<Type>`,
45+
/// not from [`TypeInner`]. For logging and debugging, it's fine to just
46+
/// write something helpful to the developer, but for generating WGSL,
47+
/// this should be unreachable.
48+
fn write_unnamed_struct<W: Write>(&self, inner: &TypeInner, out: &mut W) -> core::fmt::Result;
49+
4250
/// Write a [`TypeInner`] that has no representation as WGSL source,
4351
/// even including Naga extensions.
4452
///
@@ -363,7 +371,7 @@ where
363371
write!(out, "acceleration_structure{}", caps)?
364372
}
365373
TypeInner::Struct { .. } => {
366-
unreachable!("structs can only be referenced by name in WGSL");
374+
ctx.write_unnamed_struct(inner, out)?;
367375
}
368376
TypeInner::RayQuery { vertex_return } => {
369377
let caps = if vertex_return { "<vertex_return>" } else { "" };
@@ -414,6 +422,10 @@ impl TypeContext for crate::proc::GlobalCtx<'_> {
414422
.unwrap_or("{anonymous type}")
415423
}
416424

425+
fn write_unnamed_struct<W: Write>(&self, _: &TypeInner, out: &mut W) -> core::fmt::Result {
426+
write!(out, "{{unnamed struct}}")
427+
}
428+
417429
fn write_override<W: Write>(
418430
&self,
419431
handle: Handle<crate::Override>,
@@ -446,6 +458,10 @@ impl TypeContext for crate::UniqueArena<crate::Type> {
446458
self[handle].name.as_deref().unwrap_or("{anonymous type}")
447459
}
448460

461+
fn write_unnamed_struct<W: Write>(&self, inner: &TypeInner, out: &mut W) -> core::fmt::Result {
462+
write!(out, "{{unnamed struct {inner:?}}}")
463+
}
464+
449465
fn write_override<W: Write>(
450466
&self,
451467
handle: Handle<crate::Override>,

naga/src/front/wgsl/lower/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,14 @@ impl TypeContext for ExpressionContext<'_, '_, '_> {
412412
None => write!(out, "{{anonymous override {handle:?}}}"),
413413
}
414414
}
415+
416+
fn write_unnamed_struct<W: core::fmt::Write>(
417+
&self,
418+
_: &crate::TypeInner,
419+
_: &mut W,
420+
) -> core::fmt::Result {
421+
unreachable!("the WGSL front end should always know the type name");
422+
}
415423
}
416424

417425
impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> {

0 commit comments

Comments
 (0)