Skip to content

Commit 26054a3

Browse files
SparkyPotatodavnotdev
authored andcommitted
[naga] Error on duplicate fields in structs (gfx-rs#7088)
* error in wgsl-in * changelog * add test
1 parent bd11262 commit 26054a3

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ By @brodycj in [#6924](https://github.com/gfx-rs/wgpu/pull/6924).
8383

8484
- 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).
8585
- Allow abstract expressions to be used in WGSL function return statements. By @jamienicol in [#7035](https://github.com/gfx-rs/wgpu/pull/7035).
86+
- Error if structs have two fields with the same name. By @SparkyPotato in [#7088](https://github.com/gfx-rs/wgpu/pull/7088).
8687

8788
#### General
8889

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::front::wgsl::parse::lexer::{Lexer, Token};
1212
use crate::front::wgsl::parse::number::Number;
1313
use crate::front::wgsl::Scalar;
1414
use crate::front::SymbolTable;
15-
use crate::{Arena, FastIndexSet, Handle, ShaderStage, Span};
15+
use crate::{Arena, FastHashSet, FastIndexSet, Handle, ShaderStage, Span};
1616

1717
pub mod ast;
1818
pub mod conv;
@@ -1171,6 +1171,7 @@ impl Parser {
11711171
ctx: &mut ExpressionContext<'a, '_, '_>,
11721172
) -> Result<Vec<ast::StructMember<'a>>, Error<'a>> {
11731173
let mut members = Vec::new();
1174+
let mut member_names = FastHashSet::default();
11741175

11751176
lexer.expect(Token::Paren('{'))?;
11761177
let mut ready = true;
@@ -1217,6 +1218,17 @@ impl Parser {
12171218
size: size.value,
12181219
align: align.value,
12191220
});
1221+
1222+
if !member_names.insert(name.name) {
1223+
return Err(Error::Redefinition {
1224+
previous: members
1225+
.iter()
1226+
.find(|x| x.name.name == name.name)
1227+
.map(|x| x.name.span)
1228+
.unwrap(),
1229+
current: name.span,
1230+
});
1231+
}
12201232
}
12211233

12221234
Ok(members)

naga/tests/wgsl_errors.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,6 +2092,27 @@ fn function_param_redefinition_as_local() {
20922092
)
20932093
}
20942094

2095+
#[test]
2096+
fn struct_member_redefinition() {
2097+
check(
2098+
"
2099+
struct A {
2100+
a: f32,
2101+
a: f32,
2102+
}
2103+
",
2104+
r###"error: redefinition of `a`
2105+
┌─ wgsl:3:13
2106+
2107+
3 │ a: f32,
2108+
│ ^ previous definition of `a`
2109+
4 │ a: f32,
2110+
│ ^ redefinition of `a`
2111+
2112+
"###,
2113+
)
2114+
}
2115+
20952116
#[test]
20962117
fn function_must_return_value() {
20972118
check_validation!(

0 commit comments

Comments
 (0)