Skip to content

Commit a5b8afe

Browse files
committed
Define TypeInner::is_constructible; use it in front::wgsl.
1 parent 6357bd7 commit a5b8afe

2 files changed

Lines changed: 32 additions & 8 deletions

File tree

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,9 @@ impl<'source> Lowerer<'source, '_> {
152152

153153
let expr;
154154
match (components, constructor) {
155-
// Zero-value constructor with explicit type. Not everything this
156-
// matches is constructible, but the non-constructible types not
157-
// excluded by `!is_dynamically_sized` are rejected by the
158-
// parser.
155+
// Zero-value constructor with explicit type.
159156
(Components::None, Constructor::Type((result_ty, inner)))
160-
if !inner.is_dynamically_sized(&ctx.module.types) =>
157+
if inner.is_constructible(&ctx.module.types) =>
161158
{
162159
expr = crate::Expression::ZeroValue(result_ty);
163160
}
@@ -535,11 +532,11 @@ impl<'source> Lowerer<'source, '_> {
535532
expr = crate::Expression::Compose { ty, components };
536533
}
537534

538-
// Array constructor, explicit type. Override- and runtime-sized arrays are not constructible.
535+
// Array constructor, explicit type.
539536
(
540537
components,
541538
Constructor::Type((ty, inner @ &crate::TypeInner::Array { base, .. })),
542-
) if !inner.is_dynamically_sized(&ctx.module.types) => {
539+
) if inner.is_constructible(&ctx.module.types) => {
543540
let mut components = components.into_components_vec();
544541
ctx.try_automatic_conversions_slice(&mut components, &Tr::Handle(base), ty_span)?;
545542
expr = crate::Expression::Compose { ty, components };
@@ -549,7 +546,7 @@ impl<'source> Lowerer<'source, '_> {
549546
(
550547
components,
551548
Constructor::Type((ty, inner @ &crate::TypeInner::Struct { ref members, .. })),
552-
) if !inner.is_dynamically_sized(&ctx.module.types) => {
549+
) if inner.is_constructible(&ctx.module.types) => {
553550
let mut components = components.into_components_vec();
554551
let struct_ty_span = ctx.module.types.get_span(ty);
555552

naga/src/proc/type_methods.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,33 @@ impl crate::TypeInner {
364364
}
365365
}
366366

367+
/// Returns true if `self` is a constructible type.
368+
pub fn is_constructible(&self, types: &crate::UniqueArena<crate::Type>) -> bool {
369+
use crate::TypeInner as Ti;
370+
match *self {
371+
Ti::Array { size, .. } => match size {
372+
ir::ArraySize::Constant(_) => true,
373+
ir::ArraySize::Pending(_) | ir::ArraySize::Dynamic => false,
374+
},
375+
Ti::Struct { ref members, .. } => members
376+
.last()
377+
.map(|last| !types[last.ty].inner.is_dynamically_sized(types))
378+
.unwrap_or(true),
379+
Ti::Atomic(_)
380+
| Ti::Pointer { .. }
381+
| Ti::ValuePointer { .. }
382+
| Ti::Image { .. }
383+
| Ti::Sampler { .. }
384+
| Ti::AccelerationStructure { .. }
385+
| Ti::BindingArray { .. } => false,
386+
Ti::Scalar(_)
387+
| Ti::Vector { .. }
388+
| Ti::Matrix { .. }
389+
| Ti::RayQuery { .. }
390+
| Ti::CooperativeMatrix { .. } => true,
391+
}
392+
}
393+
367394
pub const fn components(&self) -> Option<u32> {
368395
Some(match *self {
369396
Self::Vector { size, .. } => size as u32,

0 commit comments

Comments
 (0)