summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--source/slang/slang-check-decl.cpp25
-rw-r--r--tests/language-feature/interfaces/non-static-method-implemented-by-static.slang45
-rw-r--r--tests/language-feature/interfaces/non-static-method-implemented-by-static.slang.expected.txt6
-rw-r--r--tests/language-feature/interfaces/static-method-not-implemented.slang27
4 files changed, 103 insertions, 0 deletions
diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp
index 33319ca8f..d0c3e8d96 100644
--- a/source/slang/slang-check-decl.cpp
+++ b/source/slang/slang-check-decl.cpp
@@ -4621,6 +4621,25 @@ static bool matchParamDirection(ParameterDirection implDir, ParameterDirection r
return false;
}
+static void removeNonStaticLookupItems(LookupResult& lookupResult)
+{
+ List<LookupResultItem> newItems;
+ for (auto item : lookupResult)
+ {
+ if (item.declRef.getDecl()->hasModifier<HLSLStaticModifier>())
+ {
+ newItems.add(item);
+ }
+ }
+
+ lookupResult.items = newItems;
+ lookupResult.item = LookupResultItem();
+ if (lookupResult.items.getCount() > 0)
+ {
+ lookupResult.item = lookupResult.items[0];
+ }
+}
+
bool SemanticsVisitor::trySynthesizeMethodRequirementWitness(
ConformanceCheckingContext* context,
LookupResult const& lookupResult,
@@ -4724,6 +4743,12 @@ bool SemanticsVisitor::trySynthesizeMethodRequirementWitness(
baseOverloadedExpr->lookupResult2 = lookupResult;
}
+ // Non-static methods cannot implement static methods, remove them.
+ if (requiredMemberDeclRef.getDecl()->hasModifier<HLSLStaticModifier>())
+ {
+ removeNonStaticLookupItems(baseOverloadedExpr->lookupResult2);
+ }
+
// If `synThis` is non-null, then we will use it as the base of
// the overloaded expression, so that we have an overloaded
// member reference, and not just an overloaded reference to some
diff --git a/tests/language-feature/interfaces/non-static-method-implemented-by-static.slang b/tests/language-feature/interfaces/non-static-method-implemented-by-static.slang
new file mode 100644
index 000000000..9c243af94
--- /dev/null
+++ b/tests/language-feature/interfaces/non-static-method-implemented-by-static.slang
@@ -0,0 +1,45 @@
+//TEST(compute):COMPARE_COMPUTE_EX:-vk -compute -output-using-type
+
+//TEST_INPUT: set outputBuffer = out ubuffer(data=[0 0 0 0 0], stride=4);
+RWStructuredBuffer<int> outputBuffer;
+
+interface Base
+{
+ int getValue();
+ int getValue(float a);
+}
+
+struct Impl1 : Base
+{
+ // This is static and allowed to implement interface's non-static method.
+ static int getValue() { return 5; }
+ int getValue(float a) { return int(a) + 5; }
+}
+
+struct Impl2 : Base
+{
+ // This is static with one default parameter and allowed to implement interface's non-static method.
+ static int getValue(int a = 3) { return a + 5; }
+ int getValue(float a) { return int(a) + 5; }
+}
+
+int callGet<T : Base>(T t)
+{
+ return t.getValue();
+}
+
+[numthreads(1, 1, 1)]
+void computeMain()
+{
+ Impl1 impl1;
+ Impl2 impl2;
+
+ uint index = 0;
+
+ outputBuffer[index++] = Impl1::getValue();
+ outputBuffer[index++] = callGet(impl1);
+
+ outputBuffer[index++] = Impl2::getValue();
+ outputBuffer[index++] = callGet(impl2);
+ outputBuffer[index++] = impl2.getValue(5);
+}
diff --git a/tests/language-feature/interfaces/non-static-method-implemented-by-static.slang.expected.txt b/tests/language-feature/interfaces/non-static-method-implemented-by-static.slang.expected.txt
new file mode 100644
index 000000000..42d1e8278
--- /dev/null
+++ b/tests/language-feature/interfaces/non-static-method-implemented-by-static.slang.expected.txt
@@ -0,0 +1,6 @@
+type: int32_t
+5
+5
+8
+8
+10
diff --git a/tests/language-feature/interfaces/static-method-not-implemented.slang b/tests/language-feature/interfaces/static-method-not-implemented.slang
new file mode 100644
index 000000000..1d46c1be6
--- /dev/null
+++ b/tests/language-feature/interfaces/static-method-not-implemented.slang
@@ -0,0 +1,27 @@
+//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK): -target spirv
+
+interface Base
+{
+ static int getValue();
+}
+
+struct Impl : Base
+{
+ // This is not static and not allowed to implement the interface's static method.
+ // CHECK: error 38105: member 'getValue' does not match interface requirement
+ int getValue() { return 5; }
+}
+
+int callGet<Foo : Base>()
+{
+ return Foo::getValue();
+}
+
+RWStructuredBuffer<int> result;
+
+[numthreads(1, 1, 1)]
+void computeMain()
+{
+ result[0] = callGet<Impl>();
+}
+