summaryrefslogtreecommitdiff
path: root/tests/language-feature
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2024-09-10 08:13:52 -0700
committerGitHub <noreply@github.com>2024-09-10 08:13:52 -0700
commit1c6e2413957007475e5145a1d837b05d88df06c2 (patch)
tree95a53229e22afbf79f65b194aa23086c5dd411b7 /tests/language-feature
parent936c22a9a938744eb43c310dd82c9c6944f79e87 (diff)
Disambiguate subscript decls by preferring the one with more accessors. (#5046)
Diffstat (limited to 'tests/language-feature')
-rw-r--r--tests/language-feature/overloaded-subscript.slang48
1 files changed, 48 insertions, 0 deletions
diff --git a/tests/language-feature/overloaded-subscript.slang b/tests/language-feature/overloaded-subscript.slang
new file mode 100644
index 000000000..f396f4f66
--- /dev/null
+++ b/tests/language-feature/overloaded-subscript.slang
@@ -0,0 +1,48 @@
+//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-slang -compute -shaderobj -output-using-type
+//TEST(compute, vulkan):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-vk -compute -shaderobj -output-using-type
+
+// Test that we can disambiguiate subscript decls by prefering the candidate that contains a super set of
+// accessors than the other candidates.
+interface IBuf<T>
+{
+ T read(int x);
+}
+interface IRWBuf<T> : IBuf<T>
+{
+ [mutating]
+ void write(int x, T v);
+}
+
+extension<T, U : IBuf<T>> U
+{
+ __subscript(int x) -> T { get { return read(x); } }
+}
+
+extension<T, U : IRWBuf<T>> U
+{
+ __subscript(int x)->T { get { return read(x); } set { write(x, newValue); } }
+}
+
+struct MyArray<T> : IRWBuf<T>
+{
+ T data[4];
+ T read(int x) { return data[x]; }
+ [mutating]
+ void write(int x, T v) { data[x] = v; }
+}
+
+
+//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
+RWStructuredBuffer<int> outputBuffer;
+
+[numthreads(1,1,1)]
+void computeMain()
+{
+ MyArray<int> arr = {};
+ arr[0] = 1;
+ arr[1] = 2;
+ // CHECK: 1
+ // CHECK: 2
+ outputBuffer[0] = arr[0];
+ outputBuffer[1] = arr[1];
+}