From 81a1e8d3cf0574988dffafcfdf4a92085875d6a5 Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Fri, 23 Jan 2026 17:24:11 -0800 Subject: [PATCH] [naga wgsl-in] Don't permit template parameters after enumerants. --- naga/src/front/wgsl/lower/mod.rs | 47 +++++++++++++++++++++----------- naga/tests/naga/wgsl_errors.rs | 14 ++++++++++ 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/naga/src/front/wgsl/lower/mod.rs b/naga/src/front/wgsl/lower/mod.rs index 8a4df430f..2b8241593 100644 --- a/naga/src/front/wgsl/lower/mod.rs +++ b/naga/src/front/wgsl/lower/mod.rs @@ -913,6 +913,16 @@ impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> { self.as_global().ensure_type_exists(None, inner) } + /// Check that `expr` is an identifier resolving to a predeclared enumerant. + /// + /// The identifier must not have any template parameters. + /// + /// Return the name of the identifier, together with its span. + /// + /// Actually, this only checks that the identifier refers to some + /// predeclared object, not necessarily an enumerant. This should be good + /// enough, since the caller is going to compare the name against some list + /// of permitted enumerants anyway. fn enumerant( &self, expr: Handle>, @@ -920,23 +930,28 @@ impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> { let span = self.ast_expressions.get_span(expr); let expr = &self.ast_expressions[expr]; - match *expr { - ast::Expression::Ident(ast::TemplateElaboratedIdent { - ident: ast::IdentExpr::Local(_), - .. - }) => Err(Box::new(Error::UnexpectedIdentForEnumerant(span))), - ast::Expression::Ident(ast::TemplateElaboratedIdent { - ident: ast::IdentExpr::Unresolved(name), - .. - }) => { - if self.globals.get(name).is_some() { - Err(Box::new(Error::UnexpectedIdentForEnumerant(span))) - } else { - Ok((name, span)) - } - } - _ => Err(Box::new(Error::UnexpectedExprForEnumerant(span))), + let ast::Expression::Ident(ref ident) = *expr else { + return Err(Box::new(Error::UnexpectedExprForEnumerant(span))); + }; + + let ast::TemplateElaboratedIdent { + ident: ast::IdentExpr::Unresolved(name), + ref template_list, + .. + } = *ident + else { + return Err(Box::new(Error::UnexpectedIdentForEnumerant(span))); + }; + + if self.globals.get(name).is_some() { + return Err(Box::new(Error::UnexpectedIdentForEnumerant(span))); } + + if !template_list.is_empty() { + return Err(Box::new(Error::UnexpectedTemplate(span))); + } + + Ok((name, span)) } fn var_address_space( diff --git a/naga/tests/naga/wgsl_errors.rs b/naga/tests/naga/wgsl_errors.rs index 8e159b416..6438a3b7d 100644 --- a/naga/tests/naga/wgsl_errors.rs +++ b/naga/tests/naga/wgsl_errors.rs @@ -4344,6 +4344,20 @@ fn source_with_control_char() { ); } +#[test] +fn enumerant_with_template_parameters() { + check( + r#"var> x: u32;"#, + "error: unexpected template + ┌─ wgsl:1:5 + │ +1 │ var> x: u32; + │ ^^^^^^^^^^^^^^^^^^^ expected identifier + +", + ); +} + #[test] fn ray_query_enable_extension() { check_extension_validation!(