summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorJulius Ikkala <julius.ikkala@gmail.com>2025-05-23 22:27:37 +0300
committerGitHub <noreply@github.com>2025-05-23 12:27:37 -0700
commit57c3f938221c427b78da7087f8a832ba4a271a7c (patch)
treee9a6d26278dc1ad75b222ac4fc9b7a1d8449e576 /tests
parentd108bfa677c70808b32bd77e93637ed34c19c75d (diff)
Implement throw & catch statements (#6916)
* Implement throw statement It already existed in the IR, so only parsing, checking and lowering was missing. * Initial catch implementation Likely very broken. * Error out when catch() isn't last in scope * Prevent accessing variables from scope preceding catch As those may actually not be available at that point. * Add IError and use it in Result type lowering * Add diagnostic tests * Allow caught throws in non-throw functions * Fix catch propagating between functions & SPIR-V merge issue * Add test for non-trivial error types * Fix MSVC build * Fix invalid value type from Result lowering * Also lower error handling in templates * Lower result types only after specialization * Attempt to disambiguate error enums by witness table * Revert matching by witness, types should be distinct too * Don't assert valueField when getting Result's error value It may not exist if the function returns void, but getting the error value is still legitimate. * Update tests for new error numbers & get rid of expected.txt * Change catch lowering to resemble breaking a loop ... To make SPIR-V happy. * Fix dead catch blocks and invalid cached dominator tree * More SPIR-V adjustment * Lower catch as two nested loops * Add defer interaction test and revert broken defer changes * Fix enum type when throwing literals * Cleanup and bikeshedding * Document error handling mechanism * Fix table of contents * Use boolean tag in Result<T, E> * Use anyValue storage for Result<T,E> * Remove IError * Fix formatting * Eradicate success values from docs and tests * Use parseModernParamDecl for catch parameter * Implement do-catch syntax * Implement catch-all * Fix formatting * Fix marshalling native calls that throw --------- Co-authored-by: Yong He <yonghe@outlook.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/language-feature/error-handling/basic.slang91
-rw-r--r--tests/language-feature/error-handling/catch-all.slang57
-rw-r--r--tests/language-feature/error-handling/defer-interaction.slang58
-rw-r--r--tests/language-feature/error-handling/generics.slang47
-rw-r--r--tests/language-feature/error-handling/non-trivial-error-type.slang40
-rw-r--r--tests/language-feature/error-handling/throw-in-defer.slang17
-rw-r--r--tests/language-feature/error-handling/throw-type-mismatch.slang17
-rw-r--r--tests/language-feature/error-handling/throw-without-throws.slang19
-rw-r--r--tests/language-feature/error-handling/try-in-defer.slang19
9 files changed, 365 insertions, 0 deletions
diff --git a/tests/language-feature/error-handling/basic.slang b/tests/language-feature/error-handling/basic.slang
new file mode 100644
index 000000000..4cb270cb8
--- /dev/null
+++ b/tests/language-feature/error-handling/basic.slang
@@ -0,0 +1,91 @@
+//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -shaderobj
+//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -vk -shaderobj
+//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -cpu -shaderobj
+
+// CHECK: 2
+// CHECK-NEXT: 0
+// CHECK-NEXT: 11
+// CHECK-NEXT: 12
+// CHECK-NEXT: 6
+// CHECK-NEXT: 1
+
+//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0], stride=4):out,name=outputBuffer
+RWStructuredBuffer<int> outputBuffer;
+
+enum MyError1
+{
+ Fail
+};
+
+enum MyError2
+{
+ Fail1,
+ Fail2 = 0x12
+};
+
+void throwingFunc() throws MyError1
+{
+ throw MyError1.Fail;
+}
+
+int maybeBadFunc1(int n) throws MyError1
+{
+ if (n == 1) throw MyError1.Fail;
+ return n;
+}
+
+int maybeBadFunc2(int n) throws MyError2
+{
+ if (n == 2) throw MyError2.Fail2;
+ return n;
+}
+
+int multiCatchFunc(int n)
+{
+ do
+ {
+ let a = try maybeBadFunc1(n);
+ let b = try maybeBadFunc2(n);
+ return a+b;
+ }
+ catch(err: MyError1)
+ {
+ return 0x11;
+ }
+ catch(err: MyError2)
+ {
+ return reinterpret<int>(err);
+ }
+}
+
+int containedThrow()
+{
+ do
+ {
+ throw MyError1.Fail;
+ }
+ catch(err: MyError1)
+ {
+ return 1;
+ }
+}
+
+[numthreads(1, 1, 1)]
+void computeMain(int3 dispatchThreadID: SV_DispatchThreadID)
+{
+ do
+ {
+ try throwingFunc();
+ outputBuffer[0] = 1;
+ }
+ catch(err: MyError1)
+ {
+ outputBuffer[0] = 2;
+ }
+
+ outputBuffer[1] = multiCatchFunc(0);
+ outputBuffer[2] = multiCatchFunc(1);
+ outputBuffer[3] = multiCatchFunc(2);
+ outputBuffer[4] = multiCatchFunc(3);
+ outputBuffer[5] = containedThrow();
+}
diff --git a/tests/language-feature/error-handling/catch-all.slang b/tests/language-feature/error-handling/catch-all.slang
new file mode 100644
index 000000000..afa0693e4
--- /dev/null
+++ b/tests/language-feature/error-handling/catch-all.slang
@@ -0,0 +1,57 @@
+//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -shaderobj
+//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -vk -shaderobj
+//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -cpu -shaderobj
+
+//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
+RWStructuredBuffer<int> outputBuffer;
+
+enum MyError1
+{
+ Fail = 0x10
+};
+
+enum MyError2
+{
+ Fail = 0x20
+};
+
+int f(int n) throws MyError1
+{
+ if (n == 1) throw MyError1.Fail;
+ return n;
+}
+
+int g(int n) throws MyError2
+{
+ if (n == 2) throw MyError2.Fail;
+ return n;
+}
+
+void handlerFunc(int i, int n)
+{
+ do
+ {
+ int a = try f(n);
+ int b = try g(n);
+ int c = a+b+1;
+ outputBuffer[i] = c;
+ }
+ catch(err: MyError1)
+ {
+ outputBuffer[i] = reinterpret<int>(err);
+ }
+ catch
+ {
+ outputBuffer[i] = 0x30;
+ }
+}
+
+[numthreads(1, 1, 1)]
+void computeMain(int3 dispatchThreadID: SV_DispatchThreadID)
+{
+ int i = 0;
+ handlerFunc(0, 0); // CHECK: 1
+ handlerFunc(1, 1); // CHECK-NEXT: 10
+ handlerFunc(2, 2); // CHECK-NEXT: 30
+ handlerFunc(3, 3); // CHECK-NEXT 7
+}
diff --git a/tests/language-feature/error-handling/defer-interaction.slang b/tests/language-feature/error-handling/defer-interaction.slang
new file mode 100644
index 000000000..0b3f9f829
--- /dev/null
+++ b/tests/language-feature/error-handling/defer-interaction.slang
@@ -0,0 +1,58 @@
+//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -shaderobj
+//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -vk -shaderobj
+//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -cpu -shaderobj
+
+//TEST_INPUT:ubuffer(data=[0 0 0 0 0], stride=4):out,name=outputBuffer
+RWStructuredBuffer<int> outputBuffer;
+
+enum MyError
+{
+ Fail
+};
+
+int maybeThrowingFunc(int n) throws MyError
+{
+ if (n == 3)
+ throw MyError.Fail;
+ return n;
+}
+
+void testFunc(int n, inout int i)
+{
+ int value = n;
+ defer
+ {
+ outputBuffer[i++] = value;
+ }
+
+ defer
+ {
+ do
+ {
+ let m = try maybeThrowingFunc(n);
+ value += m;
+ }
+ catch(err: MyError)
+ {
+ defer
+ {
+ outputBuffer[i++] = 0x80;
+ }
+ outputBuffer[i++] = 0xFF;
+ }
+ }
+}
+
+[numthreads(1, 1, 1)]
+void computeMain(int3 dispatchThreadID: SV_DispatchThreadID)
+{
+ int i = 0;
+ // CHECK: 2
+ testFunc(1, i);
+ // CHECK-NEXT: 4
+ testFunc(2, i);
+ // CHECK-NEXT: FF
+ // CHECK-NEXT: 80
+ // CHECK-NEXT: 3
+ testFunc(3, i);
+}
diff --git a/tests/language-feature/error-handling/generics.slang b/tests/language-feature/error-handling/generics.slang
new file mode 100644
index 000000000..377ef622d
--- /dev/null
+++ b/tests/language-feature/error-handling/generics.slang
@@ -0,0 +1,47 @@
+//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -shaderobj
+//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -vk -shaderobj
+//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -cpu -shaderobj
+
+// CHECK: 5
+// CHECK-NEXT: 1
+// CHECK-NEXT: 0
+
+//TEST_INPUT:ubuffer(data=[0 0 0], stride=4):out,name=outputBuffer
+RWStructuredBuffer<int> outputBuffer;
+
+enum MyError
+{
+ Fail = 1
+};
+
+T func<T: __BuiltinFloatingPointType>(T val) throws MyError
+{
+ do
+ {
+ if (val >= T(3))
+ throw MyError.Fail;
+ return val * T(2);
+ }
+ catch(err: MyError)
+ {
+ // Just rethrow to test catching inside a generic as well.
+ throw err;
+ }
+}
+
+[numthreads(1, 1, 1)]
+void computeMain(int3 dispatchThreadID: SV_DispatchThreadID)
+{
+ int i = 0;
+ do
+ {
+ outputBuffer[i] = int(try func(2.5f));
+ i+=1;
+ outputBuffer[i] = int(try func(3.5f));
+ i+=1;
+ }
+ catch(err: MyError)
+ {
+ outputBuffer[i] = reinterpret<int>(err);
+ }
+}
diff --git a/tests/language-feature/error-handling/non-trivial-error-type.slang b/tests/language-feature/error-handling/non-trivial-error-type.slang
new file mode 100644
index 000000000..9e03536d3
--- /dev/null
+++ b/tests/language-feature/error-handling/non-trivial-error-type.slang
@@ -0,0 +1,40 @@
+//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -shaderobj
+//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -vk -shaderobj
+//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -cpu -shaderobj
+
+// CHECK: 2
+// CHECK-NEXT: 13
+// CHECK-NEXT: 0
+
+//TEST_INPUT:ubuffer(data=[0 0 0], stride=4):out,name=outputBuffer
+RWStructuredBuffer<int> outputBuffer;
+
+struct MyError
+{
+ int code = 0;
+ int param = 0;
+};
+
+int func(int val) throws MyError
+{
+ if (val >= 3)
+ throw MyError(1, val);
+ return val * 2;
+}
+
+[numthreads(1, 1, 1)]
+void computeMain(int3 dispatchThreadID: SV_DispatchThreadID)
+{
+ int i = 0;
+ do
+ {
+ outputBuffer[i] = try func(1);
+ i+=1;
+ outputBuffer[i] = try func(3);
+ i+=1;
+ }
+ catch(err: MyError)
+ {
+ outputBuffer[i] = err.code * 0x10 + err.param;
+ }
+}
diff --git a/tests/language-feature/error-handling/throw-in-defer.slang b/tests/language-feature/error-handling/throw-in-defer.slang
new file mode 100644
index 000000000..d19bf227e
--- /dev/null
+++ b/tests/language-feature/error-handling/throw-in-defer.slang
@@ -0,0 +1,17 @@
+//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK):
+enum MyError
+{
+ Fail
+}
+
+void f() throws MyError
+{
+ defer {
+ // Throw isn't allowed to escape defer for the same reason as 'return',
+ // it'd prevent other defer statements from running. This is legal if
+ // you catch it, though.
+ throw MyError.Fail;
+ }
+}
+
+// CHECK: error 30113
diff --git a/tests/language-feature/error-handling/throw-type-mismatch.slang b/tests/language-feature/error-handling/throw-type-mismatch.slang
new file mode 100644
index 000000000..7e5278962
--- /dev/null
+++ b/tests/language-feature/error-handling/throw-type-mismatch.slang
@@ -0,0 +1,17 @@
+//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK):
+enum MyError1
+{
+ Fail
+}
+
+enum MyError2
+{
+ Fail
+}
+
+int g() throws MyError1
+{
+ throw MyError2.Fail;
+}
+
+// CHECK: error 30116
diff --git a/tests/language-feature/error-handling/throw-without-throws.slang b/tests/language-feature/error-handling/throw-without-throws.slang
new file mode 100644
index 000000000..e37198b82
--- /dev/null
+++ b/tests/language-feature/error-handling/throw-without-throws.slang
@@ -0,0 +1,19 @@
+//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK):
+enum MyError
+{
+ Fail
+}
+
+int g() throws MyError
+{
+ throw MyError.Fail;
+}
+
+void f()
+{
+ let n = try g();
+ throw MyError.Fail;
+}
+
+// CHECK: error 30093
+// CHECK: error 30115
diff --git a/tests/language-feature/error-handling/try-in-defer.slang b/tests/language-feature/error-handling/try-in-defer.slang
new file mode 100644
index 000000000..54cd18e12
--- /dev/null
+++ b/tests/language-feature/error-handling/try-in-defer.slang
@@ -0,0 +1,19 @@
+//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK):
+enum MyError
+{
+ Fail
+}
+
+int g() throws MyError
+{
+ throw MyError.Fail;
+}
+
+void f() throws MyError
+{
+ defer {
+ let n = try g();
+ }
+}
+
+// CHECK: error 30114