From 24d0efc68f5b153efbf64681f65b86ce3dd7fbb6 Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Mon, 20 Nov 2023 16:55:22 -0800 Subject: [PATCH] [naga wgsl-in] Preserve type names in `alias` declarations. Given a WGSL `alias` declaration, create a Naga `Type` with the alias's name, rather than dropping the type name on the floor. --- naga/src/front/wgsl/lower/mod.rs | 40 ++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/naga/src/front/wgsl/lower/mod.rs b/naga/src/front/wgsl/lower/mod.rs index 684ba7ab0be..78417fbd5fa 100644 --- a/naga/src/front/wgsl/lower/mod.rs +++ b/naga/src/front/wgsl/lower/mod.rs @@ -98,10 +98,14 @@ impl<'source> GlobalContext<'source, '_, '_> { } } - fn ensure_type_exists(&mut self, inner: crate::TypeInner) -> Handle { + fn ensure_type_exists( + &mut self, + name: Option, + inner: crate::TypeInner, + ) -> Handle { self.module .types - .insert(crate::Type { inner, name: None }, Span::UNDEFINED) + .insert(crate::Type { inner, name }, Span::UNDEFINED) } } @@ -635,7 +639,7 @@ impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> { } fn ensure_type_exists(&mut self, inner: crate::TypeInner) -> Handle { - self.as_global().ensure_type_exists(inner) + self.as_global().ensure_type_exists(None, inner) } } @@ -936,7 +940,11 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { .insert(s.name.name, LoweredGlobalDecl::Type(handle)); } ast::GlobalDeclKind::Type(ref alias) => { - let ty = self.resolve_ast_type(alias.ty, &mut ctx)?; + let ty = self.resolve_named_ast_type( + alias.ty, + Some(alias.name.name.to_string()), + &mut ctx, + )?; ctx.globals .insert(alias.name.name, LoweredGlobalDecl::Type(ty)); } @@ -2513,10 +2521,19 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { }) } - /// Return a Naga `Handle` representing the front-end type `handle`. - fn resolve_ast_type( + /// Build the Naga equivalent of a named AST type. + /// + /// Return a Naga `Handle` representing the front-end type + /// `handle`, which should be named `name`, if given. + /// + /// If `handle` refers to a type cached in [`SpecialTypes`], + /// `name` may be ignored. + /// + /// [`SpecialTypes`]: crate::SpecialTypes + fn resolve_named_ast_type( &mut self, handle: Handle>, + name: Option, ctx: &mut GlobalContext<'source, '_, '_>, ) -> Result, Error<'source>> { let inner = match ctx.types[handle] { @@ -2577,7 +2594,16 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { } }; - Ok(ctx.ensure_type_exists(inner)) + Ok(ctx.ensure_type_exists(name, inner)) + } + + /// Return a Naga `Handle` representing the front-end type `handle`. + fn resolve_ast_type( + &mut self, + handle: Handle>, + ctx: &mut GlobalContext<'source, '_, '_>, + ) -> Result, Error<'source>> { + self.resolve_named_ast_type(handle, None, ctx) } fn binding(