Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wgsl] Use alias instead of type for type aliases #2299

Merged
merged 6 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/front/wgsl/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ impl<'a> Error<'a> {
ExpectedToken::Assignment => "assignment or increment/decrement".to_string(),
ExpectedToken::SwitchItem => "switch item ('case' or 'default') or a closing curly bracket to signify the end of the switch statement ('}')".to_string(),
ExpectedToken::WorkgroupSizeSeparator => "workgroup size separator (',') or a closing parenthesis".to_string(),
ExpectedToken::GlobalItem => "global item ('struct', 'const', 'var', 'type', ';', 'fn') or the end of the file".to_string(),
ExpectedToken::GlobalItem => "global item ('struct', 'const', 'var', 'alias', ';', 'fn') or the end of the file".to_string(),
ExpectedToken::Type => "type".to_string(),
ExpectedToken::Variable => "variable access".to_string(),
ExpectedToken::Function => "function name".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion src/front/wgsl/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2213,7 +2213,7 @@ impl Parser {
let members = self.struct_body(lexer, ctx)?;
Some(ast::GlobalDeclKind::Struct(ast::Struct { name, members }))
}
(Token::Word("type"), _) => {
(Token::Word("alias"), _) => {
let name = lexer.next_ident()?;

lexer.expect(Token::Operation('='))?;
Expand Down
10 changes: 10 additions & 0 deletions src/front/wgsl/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,3 +471,13 @@ fn parse_storage_buffers() {
)
.unwrap();
}

#[test]
fn parse_alias() {
parse_str(
"
alias Vec4 = vec4<f32>;
",
)
.unwrap();
}
1 change: 1 addition & 0 deletions src/keywords/wgsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub const RESERVED: &[&str] = &[
"vec3",
"vec4",
// Other Keywords
"alias",
"bitcast",
"break",
"case",
Expand Down
2 changes: 1 addition & 1 deletion tests/in/module-scope.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ var Texture: texture_2d<f32>;
@group(0) @binding(1)
var Sampler: sampler;

type Vec2 = vec2<f32>;
alias Vec2 = vec2<f32>;
8 changes: 4 additions & 4 deletions tests/in/type-alias.wgsl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type FVec3 = vec3<f32>;
type IVec3 = vec3i;
type Mat2 = mat2x2<f32>;
type Mat3 = mat3x3f;
alias FVec3 = vec3<f32>;
alias IVec3 = vec3i;
alias Mat2 = mat2x2<f32>;
alias Mat3 = mat3x3f;

fn main() {
let a = FVec3(0.0, 0.0, 0.0);
Expand Down
14 changes: 7 additions & 7 deletions tests/wgsl-errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,20 +912,20 @@ fn validation_error(source: &str) -> Result<naga::valid::ModuleInfo, naga::valid
#[test]
fn invalid_arrays() {
check_validation! {
"type Bad = array<array<f32>, 4>;",
"type Bad = array<sampler, 4>;",
"type Bad = array<texture_2d<f32>, 4>;":
"alias Bad = array<array<f32>, 4>;",
"alias Bad = array<sampler, 4>;",
"alias Bad = array<texture_2d<f32>, 4>;":
Err(naga::valid::ValidationError::Type {
source: naga::valid::TypeError::InvalidArrayBaseType(_),
..
})
}

check_validation! {
"type Bad = array<f32, true>;",
"alias Bad = array<f32, true>;",
r#"
const length: f32 = 2.718;
type Bad = array<f32, length>;
alias Bad = array<f32, length>;
"#:
Err(naga::valid::ValidationError::Type {
source: naga::valid::TypeError::InvalidArraySizeConstant(_),
Expand All @@ -934,8 +934,8 @@ fn invalid_arrays() {
}

check_validation! {
"type Bad = array<f32, 0>;",
"type Bad = array<f32, -1>;":
"alias Bad = array<f32, 0>;",
"alias Bad = array<f32, -1>;":
Err(naga::valid::ValidationError::Type {
source: naga::valid::TypeError::NonPositiveArrayLength(_),
..
Expand Down