From 9ef7072397457e8cdb8a63a1a6953d01f46b2904 Mon Sep 17 00:00:00 2001 From: Mukund Keshava Date: Tue, 27 May 2025 20:24:15 +0530 Subject: Add check for subscript operator return type (#7244) Fixes #6987 --- .../subscript-missing-return-type.slang | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 tests/diagnostics/subscript-missing-return-type.slang (limited to 'tests') diff --git a/tests/diagnostics/subscript-missing-return-type.slang b/tests/diagnostics/subscript-missing-return-type.slang new file mode 100644 index 000000000..23c2cda5e --- /dev/null +++ b/tests/diagnostics/subscript-missing-return-type.slang @@ -0,0 +1,32 @@ +// Test that __subscript without return type produces proper error message + +//TEST:SIMPLE(filecheck=CHECK): -target spirv + +struct MyType +{ + int val[12]; + + // CHECK: ([[# @LINE+1]]): error 30901: __subscript declaration must have a return type specified after '->' + __subscript(int x, int y) + { + get { return val[x*3 + y]; } + } +} + +// This should compile fine - subscript with return type +struct MyType2 +{ + int val[12]; + __subscript(int x, int y) -> int + { + get { return val[x*3 + y]; } + } +} + +[shader("compute")] +[numthreads(1,1,1)] +void computeMain() +{ + MyType2 obj; + int v = obj[1, 2]; +} \ No newline at end of file -- cgit v1.2.3