From dcd269782a16e55c284bbd8679c4cb9624656465 Mon Sep 17 00:00:00 2001 From: Systemcluster Date: Sun, 14 Feb 2021 00:29:08 +0100 Subject: [PATCH] [wgsl] restrict type casts to matching container types --- src/front/wgsl/mod.rs | 20 +++++++++++++++----- tests/out/shadow.msl.snap | 10 ++++------ tests/out/skybox.msl.snap | 4 ++-- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/front/wgsl/mod.rs b/src/front/wgsl/mod.rs index e4febf8a08..8275f38ea0 100644 --- a/src/front/wgsl/mod.rs +++ b/src/front/wgsl/mod.rs @@ -844,7 +844,6 @@ impl Parser { ctx.constants, )?; let kind = inner.scalar_kind(); - let ty = ctx.types.fetch_or_append(crate::Type { name: None, inner }); lexer.expect(Token::Paren('('))?; let mut components = Vec::new(); @@ -856,13 +855,24 @@ impl Parser { } lexer.expect(Token::Paren(')'))?; if components.is_empty() { - crate::Expression::As { - expr: last_component, - kind: kind.ok_or(Error::BadTypeCast(word))?, - convert: true, + let last_component_inner = ctx.resolve_type(last_component)?; + match (&inner, last_component_inner) { + (crate::TypeInner::Scalar { .. }, crate::TypeInner::Scalar { .. }) + | (crate::TypeInner::Matrix { .. }, crate::TypeInner::Matrix { .. }) + | (crate::TypeInner::Vector { .. }, crate::TypeInner::Vector { .. }) => { + crate::Expression::As { + expr: last_component, + kind: kind.ok_or(Error::BadTypeCast(word))?, + convert: true, + } + } + _ => { + return Err(Error::BadTypeCast(word)); + } } } else { components.push(last_component); + let ty = ctx.types.fetch_or_append(crate::Type { name: None, inner }); crate::Expression::Compose { ty, components } } } diff --git a/tests/out/shadow.msl.snap b/tests/out/shadow.msl.snap index ccb2a01ba8..400381564b 100644 --- a/tests/out/shadow.msl.snap +++ b/tests/out/shadow.msl.snap @@ -37,11 +37,9 @@ typedef float type7; typedef metal::float2 type8; -typedef int type9; +typedef metal::float3 type9; -typedef metal::float3 type10; - -typedef bool type11; +typedef bool type10; type7 fetch_shadow( type6 light_id, @@ -57,7 +55,7 @@ type7 fetch_shadow( } struct fs_mainInput { - type10 in_normal_fs [[user(loc0)]]; + type9 in_normal_fs [[user(loc0)]]; type2 in_position_fs [[user(loc1)]]; }; @@ -73,7 +71,7 @@ fragment fs_mainOutput fs_main( type5 sampler_shadow [[sampler(0)]] ) { fs_mainOutput output; - type10 color1 = type10(0.05, 0.05, 0.05); + type9 color1 = type9(0.05, 0.05, 0.05); type6 i = 0u; bool loop_init = true; while(true) { diff --git a/tests/out/skybox.msl.snap b/tests/out/skybox.msl.snap index c05cba30ce..bb036f6ac2 100644 --- a/tests/out/skybox.msl.snap +++ b/tests/out/skybox.msl.snap @@ -20,9 +20,9 @@ struct Data { typedef int type4; -typedef float type5; +typedef metal::float3x3 type5; -typedef metal::float3x3 type6; +typedef float type6; typedef metal::texturecube type7;