summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2024-09-04 13:25:37 -0700
committerGitHub <noreply@github.com>2024-09-04 13:25:37 -0700
commitddd29057e48a5b309726750e3daf78bfd073038e (patch)
treea054b99acb87d61ef4818dce5fa837ccfd050288 /tests
parent56a3c028a6725e13a2ae3a724eaee05ad9f4802a (diff)
Fix extension override behavior, and disallow extension on interface types. (#4977)
* Add a test to ensure extension does not override existing conformance. * Fix doc. * Update documentation. * Fix doc. * Add diagnostic test.
Diffstat (limited to 'tests')
-rw-r--r--tests/diagnostics/interfaces/interface-extension.slang10
-rw-r--r--tests/language-feature/extensions/extension-override.slang65
2 files changed, 75 insertions, 0 deletions
diff --git a/tests/diagnostics/interfaces/interface-extension.slang b/tests/diagnostics/interfaces/interface-extension.slang
new file mode 100644
index 000000000..b63b454ab
--- /dev/null
+++ b/tests/diagnostics/interfaces/interface-extension.slang
@@ -0,0 +1,10 @@
+//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK):-target cpp -stage compute -entry main -disable-specialization
+
+interface IFoo{}
+
+
+// CHECK: ([[# @LINE+1]]): error 30852
+extension IFoo
+{
+ int f() { return 0; }
+} \ No newline at end of file
diff --git a/tests/language-feature/extensions/extension-override.slang b/tests/language-feature/extensions/extension-override.slang
new file mode 100644
index 000000000..30fa64965
--- /dev/null
+++ b/tests/language-feature/extensions/extension-override.slang
@@ -0,0 +1,65 @@
+// Test that the override behavior around extensions and generic extensions works as expected.
+
+// When there are multiple ways for a type to conform to an interface, then the expected behavior
+// is that:
+// 1. If the type directly implements an interface, use that conformance.
+// 2. Otherwise, if there is a direct extension on the type that makes it conform to the interface, use that
+// extension.
+// 3. Otherwise, if there is a generic extension that makes the type conform to the interface, use that.
+
+//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -shaderobj
+interface IFoo
+{
+ int getVal();
+}
+
+interface IBar
+{
+ int getValPlusOne();
+}
+
+interface IBaz
+{
+ int getValPlusTwo();
+}
+
+struct MyInt
+{
+ int v;
+}
+
+extension MyInt : IFoo
+{
+ int getVal() { return v; }
+}
+
+extension MyInt : IBar
+{
+ int getValPlusOne() { return this.getVal() + 2; }
+}
+
+extension<T: IFoo> T : IBar
+{
+ int getValPlusOne() { return this.getVal() + 1; }
+}
+
+int helper1<T:IBar>(T v){ return v.getValPlusOne();}
+int helper2<T:IFoo>(T v){ return v.getValPlusOne();}
+
+//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
+RWStructuredBuffer<int> outputBuffer;
+
+[numthreads(1,1,1)]
+void computeMain()
+{
+ MyInt v = {1};
+
+ // CHECK: 3
+ outputBuffer[0] = v.getValPlusOne(); // should call MyInt::ext::getValPlusOne();
+
+ // CHECK: 3
+ outputBuffer[1] = helper1(v); // should call MyInt::ext::getValPlusOne();
+
+ // CHECK: 2
+ outputBuffer[2] = helper2(v); // should call T::ext::getValPlusOne();
+} \ No newline at end of file