summaryrefslogtreecommitdiffstats
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
parent936c22a9a938744eb43c310dd82c9c6944f79e87 (diff)
Disambiguate subscript decls by preferring the one with more accessors. (#5046)
-rw-r--r--source/slang/slang-check-overload.cpp41
-rw-r--r--tests/language-feature/overloaded-subscript.slang48
2 files changed, 89 insertions, 0 deletions
diff --git a/source/slang/slang-check-overload.cpp b/source/slang/slang-check-overload.cpp
index 9b3d6baff..3b36b270b 100644
--- a/source/slang/slang-check-overload.cpp
+++ b/source/slang/slang-check-overload.cpp
@@ -1199,6 +1199,8 @@ namespace Slang
return parent;
}
+ // Returns -1 if left is preferred, 1 if right is preferred, and 0 if they are equal.
+ //
int SemanticsVisitor::CompareLookupResultItems(
LookupResultItem const& left,
LookupResultItem const& right)
@@ -1283,6 +1285,45 @@ namespace Slang
return -1;
}
+ // If both are subscript decls, prefer the one that provides more
+ // accessors.
+ if (auto leftSubscriptDecl = left.declRef.as<SubscriptDecl>())
+ {
+ if (auto rightSubscriptDecl = right.declRef.as<SubscriptDecl>())
+ {
+ auto leftAccessorCount = leftSubscriptDecl.getDecl()->getMembersOfType<AccessorDecl>().getCount();
+ auto rightAccessorCount = rightSubscriptDecl.getDecl()->getMembersOfType<AccessorDecl>().getCount();
+ auto decl1IsSubsetOfDecl2 = [=](SubscriptDecl* decl1, SubscriptDecl* decl2)
+ {
+ for (auto accessorDecl1 : decl1->getMembersOfType<AccessorDecl>())
+ {
+ bool found = false;
+ for (auto accessorDecl2 : decl2->getMembersOfType<AccessorDecl>())
+ {
+ if (accessorDecl1->astNodeType == accessorDecl2->astNodeType)
+ {
+ found = true;
+ break;
+ }
+ }
+ if (!found)
+ return false;
+ }
+ return true;
+ };
+ if (leftAccessorCount > rightAccessorCount
+ && decl1IsSubsetOfDecl2(rightSubscriptDecl.getDecl(), leftSubscriptDecl.getDecl()))
+ {
+ return -1;
+ }
+ else if (rightAccessorCount > leftAccessorCount
+ && decl1IsSubsetOfDecl2(leftSubscriptDecl.getDecl(), rightSubscriptDecl.getDecl()))
+ {
+ return 1;
+ }
+ }
+ }
+
// TODO: We should generalize above rules such that in a tie a declaration
// A::m is better than B::m when all other factors are equal and
// A inherits from B.
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];
+}