[naga] Error on duplicate fields in structs (#7088)

* error in wgsl-in

* changelog

* add test
This commit is contained in:
Shaye Garg
2025-02-12 03:27:55 +00:00
committed by GitHub
parent 8e235b4d92
commit d7a42933a5
3 changed files with 35 additions and 1 deletions

View File

@@ -83,6 +83,7 @@ By @brodycj in [#6924](https://github.com/gfx-rs/wgpu/pull/6924).
- Fix some instances of functions which have a return type but don't return a value being incorrectly validated. By @jamienicol in [#7013](https://github.com/gfx-rs/wgpu/pull/7013).
- Allow abstract expressions to be used in WGSL function return statements. By @jamienicol in [#7035](https://github.com/gfx-rs/wgpu/pull/7035).
- Error if structs have two fields with the same name. By @SparkyPotato in [#7088](https://github.com/gfx-rs/wgpu/pull/7088).
#### General

View File

@@ -12,7 +12,7 @@ use crate::front::wgsl::parse::lexer::{Lexer, Token};
use crate::front::wgsl::parse::number::Number;
use crate::front::wgsl::Scalar;
use crate::front::SymbolTable;
use crate::{Arena, FastIndexSet, Handle, ShaderStage, Span};
use crate::{Arena, FastHashSet, FastIndexSet, Handle, ShaderStage, Span};
pub mod ast;
pub mod conv;
@@ -1171,6 +1171,7 @@ impl Parser {
ctx: &mut ExpressionContext<'a, '_, '_>,
) -> Result<Vec<ast::StructMember<'a>>, Error<'a>> {
let mut members = Vec::new();
let mut member_names = FastHashSet::default();
lexer.expect(Token::Paren('{'))?;
let mut ready = true;
@@ -1217,6 +1218,17 @@ impl Parser {
size: size.value,
align: align.value,
});
if !member_names.insert(name.name) {
return Err(Error::Redefinition {
previous: members
.iter()
.find(|x| x.name.name == name.name)
.map(|x| x.name.span)
.unwrap(),
current: name.span,
});
}
}
Ok(members)

View File

@@ -2092,6 +2092,27 @@ fn function_param_redefinition_as_local() {
)
}
#[test]
fn struct_member_redefinition() {
check(
"
struct A {
a: f32,
a: f32,
}
",
r###"error: redefinition of `a`
┌─ wgsl:3:13
3 │ a: f32,
│ ^ previous definition of `a`
4 │ a: f32,
│ ^ redefinition of `a`
"###,
)
}
#[test]
fn function_must_return_value() {
check_validation!(