summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorkaizhangNV <149626564+kaizhangNV@users.noreply.github.com>2025-02-12 15:37:05 -0600
committerGitHub <noreply@github.com>2025-02-12 13:37:05 -0800
commit3f102afe1038882f336dc052a9954811150fa700 (patch)
tree9411909bf649eed9724d14226ed07890a0671329 /tests
parent0fee8c16e25521d13b50427ac6bf87d7f3419b9c (diff)
Fix crash when handling uninitialized resource type (#6338)
* Fix crash when handling uninitialized resource type close #6328. When declare a var with struct type, if the struct has a resource type field and it doesn't provide explicit constructor, because slang won't implicit construct such variable at declare site if user doesn't explicitly call the initializer list, we should report the resource type field uninitialized as an error to prevent future possible crash when legalize any use of such variable.
Diffstat (limited to 'tests')
-rw-r--r--tests/diagnostics/uninitialized-resource-type.slang42
-rw-r--r--tests/diagnostics/uninitialized-resource-type.slang.1.expected9
-rw-r--r--tests/diagnostics/uninitialized-resource-type.slang.expected9
3 files changed, 60 insertions, 0 deletions
diff --git a/tests/diagnostics/uninitialized-resource-type.slang b/tests/diagnostics/uninitialized-resource-type.slang
new file mode 100644
index 000000000..4d5b8f5b3
--- /dev/null
+++ b/tests/diagnostics/uninitialized-resource-type.slang
@@ -0,0 +1,42 @@
+//DIAGNOSTIC_TEST:SIMPLE: -target hlsl -DTEST_1
+//DIAGNOSTIC_TEST:SIMPLE: -target hlsl -DTEST_2
+
+SamplerState sampler;
+
+struct Foo
+{
+ bool sample_bar = false;
+#ifdef TEST_1
+ Texture2D<float4> bar = {};
+#elif defined(TEST_2)
+ Texture2D<float4> bar[2];
+#endif
+};
+
+struct Result
+{
+ float4 color = float4(0.0);
+};
+
+Result process(in Foo foo)
+{
+ Result result = {};
+
+ if (foo.sample_bar) {
+#ifdef TEST_1
+ result.color = foo.bar.Sample(sampler, float2(0.0, 0.0));
+#elif defined(TEST_2)
+ result.color = foo.bar[0].Sample(sampler, float2(0.0, 0.0));
+#endif
+ }
+
+ return result;
+}
+
+[shader("compute")]
+[numthreads(8, 8, 1)]
+float4 cs_main(uint3 thread_id : SV_DispatchThreadID) {
+ Foo foo;
+ const let result = process(foo);
+ return result.color;
+}
diff --git a/tests/diagnostics/uninitialized-resource-type.slang.1.expected b/tests/diagnostics/uninitialized-resource-type.slang.1.expected
new file mode 100644
index 000000000..69b4f0ece
--- /dev/null
+++ b/tests/diagnostics/uninitialized-resource-type.slang.1.expected
@@ -0,0 +1,9 @@
+result code = -1
+standard error = {
+tests/diagnostics/uninitialized-resource-type.slang(40): warning 41016: use of uninitialized variable 'foo'
+ const let result = process(foo);
+ ^
+tests/diagnostics/uninitialized-resource-type.slang(40): error 56003: use of uninitialized opaque handle 'array<Texture2D,2>'.
+ const let result = process(foo);
+ ^
+}
diff --git a/tests/diagnostics/uninitialized-resource-type.slang.expected b/tests/diagnostics/uninitialized-resource-type.slang.expected
new file mode 100644
index 000000000..a74d7f9ff
--- /dev/null
+++ b/tests/diagnostics/uninitialized-resource-type.slang.expected
@@ -0,0 +1,9 @@
+result code = -1
+standard error = {
+tests/diagnostics/uninitialized-resource-type.slang(40): warning 41016: use of uninitialized variable 'foo'
+ const let result = process(foo);
+ ^
+tests/diagnostics/uninitialized-resource-type.slang(40): error 56003: use of uninitialized opaque handle 'Texture2D'.
+ const let result = process(foo);
+ ^
+}