diff --git a/Source/WebGPU/WGSL/TypeCheck.cpp b/Source/WebGPU/WGSL/TypeCheck.cpp index 4eab76a20471..bd8d1e0aa379 100644 --- a/Source/WebGPU/WGSL/TypeCheck.cpp +++ b/Source/WebGPU/WGSL/TypeCheck.cpp @@ -302,7 +302,7 @@ void TypeChecker::visit(AST::IndexAccessExpression& access) return; } - if (!unify(index, m_types.i32Type()) && !unify(index, m_types.u32Type()) && !unify(index, m_types.abstractIntType())) { + if (!unify(m_types.i32Type(), index) && !unify(m_types.u32Type(), index) && !unify(m_types.abstractIntType(), index)) { typeError(access.span(), "index must be of type 'i32' or 'u32', found: '", *index, "'"); return; } @@ -314,6 +314,13 @@ void TypeChecker::visit(AST::IndexAccessExpression& access) return; } + if (std::holds_alternative(*base)) { + // FIXME: check bounds if index is constant + auto& vector = std::get(*base); + inferred(vector.element); + return; + } + // FIXME: Implement reference and matrix accesses typeError(access.span(), "cannot index type '", *base, "'"); } diff --git a/Source/WebGPU/WGSL/tests/invalid/vector.wgsl b/Source/WebGPU/WGSL/tests/invalid/vector.wgsl new file mode 100644 index 000000000000..8e77966c5766 --- /dev/null +++ b/Source/WebGPU/WGSL/tests/invalid/vector.wgsl @@ -0,0 +1,4 @@ +fn testIndexAccess() { + // CHECK-L: index must be of type 'i32' or 'u32', found: 'f32' + let x1 = vec2(0)[1f]; +}