Skip to content

Commit 0568562

Browse files
committed
[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.
1 parent 42058cf commit 0568562

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ Passing an owned value `window` to `Surface` will return a `Surface<'static>`. S
9999

100100
- Preserve the source spans for constants and expressions correctly across module compaction. By @jimblandy in [#4696](https://github.com/gfx-rs/wgpu/pull/4696).
101101

102+
- Record the names of WGSL `alias` declarations in Naga IR `Type`s. By @jimblandy in [#4733](https://github.com/gfx-rs/wgpu/pull/4733).
103+
102104
### Examples
103105

104106
- remove winit dependency from hello-compute example by @psvri in [#4699](https://github.com/gfx-rs/wgpu/pull/4699)
@@ -2353,4 +2355,4 @@ DeviceDescriptor {
23532355
- concept of the storage hub
23542356
- basic recording of passes and command buffers
23552357
- submission-based lifetime tracking and command buffer recycling
2356-
- automatic resource transitions
2358+
- automatic resource transitions

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,14 @@ impl<'source> GlobalContext<'source, '_, '_> {
9898
}
9999
}
100100

101-
fn ensure_type_exists(&mut self, inner: crate::TypeInner) -> Handle<crate::Type> {
101+
fn ensure_type_exists(
102+
&mut self,
103+
name: Option<String>,
104+
inner: crate::TypeInner,
105+
) -> Handle<crate::Type> {
102106
self.module
103107
.types
104-
.insert(crate::Type { inner, name: None }, Span::UNDEFINED)
108+
.insert(crate::Type { inner, name }, Span::UNDEFINED)
105109
}
106110
}
107111

@@ -635,7 +639,7 @@ impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> {
635639
}
636640

637641
fn ensure_type_exists(&mut self, inner: crate::TypeInner) -> Handle<crate::Type> {
638-
self.as_global().ensure_type_exists(inner)
642+
self.as_global().ensure_type_exists(None, inner)
639643
}
640644
}
641645

@@ -936,7 +940,11 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
936940
.insert(s.name.name, LoweredGlobalDecl::Type(handle));
937941
}
938942
ast::GlobalDeclKind::Type(ref alias) => {
939-
let ty = self.resolve_ast_type(alias.ty, &mut ctx)?;
943+
let ty = self.resolve_named_ast_type(
944+
alias.ty,
945+
Some(alias.name.name.to_string()),
946+
&mut ctx,
947+
)?;
940948
ctx.globals
941949
.insert(alias.name.name, LoweredGlobalDecl::Type(ty));
942950
}
@@ -2513,10 +2521,19 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
25132521
})
25142522
}
25152523

2516-
/// Return a Naga `Handle<Type>` representing the front-end type `handle`.
2517-
fn resolve_ast_type(
2524+
/// Build the Naga equivalent of a named AST type.
2525+
///
2526+
/// Return a Naga `Handle<Type>` representing the front-end type
2527+
/// `handle`, which should be named `name`, if given.
2528+
///
2529+
/// If `handle` refers to a type cached in [`SpecialTypes`],
2530+
/// `name` may be ignored.
2531+
///
2532+
/// [`SpecialTypes`]: crate::SpecialTypes
2533+
fn resolve_named_ast_type(
25182534
&mut self,
25192535
handle: Handle<ast::Type<'source>>,
2536+
name: Option<String>,
25202537
ctx: &mut GlobalContext<'source, '_, '_>,
25212538
) -> Result<Handle<crate::Type>, Error<'source>> {
25222539
let inner = match ctx.types[handle] {
@@ -2577,7 +2594,16 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
25772594
}
25782595
};
25792596

2580-
Ok(ctx.ensure_type_exists(inner))
2597+
Ok(ctx.ensure_type_exists(name, inner))
2598+
}
2599+
2600+
/// Return a Naga `Handle<Type>` representing the front-end type `handle`.
2601+
fn resolve_ast_type(
2602+
&mut self,
2603+
handle: Handle<ast::Type<'source>>,
2604+
ctx: &mut GlobalContext<'source, '_, '_>,
2605+
) -> Result<Handle<crate::Type>, Error<'source>> {
2606+
self.resolve_named_ast_type(handle, None, ctx)
25812607
}
25822608

25832609
fn binding(

0 commit comments

Comments
 (0)