diff --git a/src/sema/sema.zig b/src/sema/sema.zig index e7d5cbb..e990a0e 100644 --- a/src/sema/sema.zig +++ b/src/sema/sema.zig @@ -2946,3 +2946,81 @@ test "valid code has no diagnostics" { \\} , .{}, &.{}); } + +test "operator checks follow CheckBinOpForHLSL" { + try expectDiags( + \\struct S { float a; }; + \\Texture2D t; + \\void f(S x, S y, float a[2], float b[2], bool bb, min16int m, double d, int e, float2x4 mat, float2 v, float4x4 m44, float4x1 m41) { + \\ x + y; + \\ x == y; + \\ t == t; + \\ a == b; + \\ bb += true; + \\ bb |= true; + \\ m / m; + \\ d % d; + \\ ~1.5; + \\ e << 1.5; + \\ v = mat; + \\ m44 += m41; + \\ v + mat; + \\ x[0]; + \\} + , .{}, &.{ + "4: error: scalar, vector, or matrix expected", + "5: error: operator cannot be used with user-defined type 'S'", + "6: error: operator cannot be used with built-in type 'Texture2D >'", + "7: error: equality operators cannot be used with array types", + "8: error: operator cannot be used with a bool lvalue", + "10: error: signed integer division is not supported on minimum-precision types, cast to int to use 32-bit division", + "11: error: modulo cannot be used with doubles, cast to float first", + "12: error: int or unsigned int type required", + "13: error: int or unsigned int type required", + "14: error: cannot convert from 'matrix' to 'vector'", + "15: error: cannot convert from 'matrix' to 'matrix'", + "16: error: cannot convert from 'matrix' to 'vector'", + "17: error: type 'S' does not provide a subscript operator", + }); +} + +test "registers, resource indexing and type attributes" { + try expectDiags( + \\Texture2D t : register(u0); + \\RWBuffer rw : register(t1); + \\float4 c : register(x0); + \\SamplerState s0 : register(s0) : register(s1); + \\Buffer b; + \\Buffer bm; + \\typedef column_major float4x4 cm; + \\typedef row_major cm rcm; + \\matrix sn; + \\float4 f() { + \\ float4 r = b[int2(1, 2)]; + \\ r += t.mips[0][1]; + \\ float2 v; v[2] = 1; + \\ return r + t.Bogus; + \\} + , .{}, &.{ + "1: error: invalid register specification, expected 't' or 's' binding", + "2: error: invalid register specification, expected 'u' binding", + "3: error: register type is unsupported - available types are 'b', 'c', 'i', 's', 't', 'u'", + "4: error: conflicting register semantics", + "6: error: elements of typed buffers and textures must be scalars or vectors", + "8: error: 'row_major' and 'column_major' attributes are not compatible", + "9: error: 'unorm' and 'snorm' attributes are not compatible", + "11: error: no viable overloaded operator[] for type 'Buffer >'", + "12: error: no viable overloaded operator[] for type 'Texture2D >::mips_slice_type'", + "13: error: vector element index '2' is out of bounds", + "14: error: no member named 'Bogus' in 'Texture2D >'", + }); +} + +test "recursion is reported from exported functions" { + try expectDiags( + \\void recurse(inout float4 f) { recurse(f); } + \\export void entry(inout float4 f) { recurse(f); } + , .{}, &.{ + "1: error: recursive functions are not allowed: function 'entry' calls recursive function 'recurse'", + }); +}