summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2025-07-15 23:36:18 -0700
committerGitHub <noreply@github.com>2025-07-16 06:36:18 +0000
commit56e91e91e425b77a9d1512cca3d26ce446781935 (patch)
tree78ab9ceaaad5a36c480359e3e7e23c0bef48d863 /tests
parentbcd53322fb7bc3d226cc03a47cc6b91a37850690 (diff)
Diagnose on initExpr of a global `const` decl (#7711)
* Add diagnostic for const/uniform global variables with initializers Global const and uniform variables without the static modifier are treated as uniform parameters in HLSL, which cannot have default values. This change adds error 31224 to catch this case early and provide clear guidance to users. Fixes #7701 🤖 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: Yong He <csyonghe@users.noreply.github.com> * Add test case for global const/uniform variable diagnostic Add test for diagnostic 31224 which catches global const and uniform variables with initializers that aren't declared static. The test verifies both error cases and valid cases to ensure the diagnostic works correctly. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: Yong He <csyonghe@users.noreply.github.com> * Fix failing tests due to new global const diagnostic Add 'static' keyword to global const variables with initializers to resolve diagnostic 31224: "global const variable with initializer must be declared static" Co-authored-by: Jay Kwak <jkwak-work@users.noreply.github.com> * Allow specialization constants with initializers without static - Modified diagnostic logic in slang-check-decl.cpp to allow specialization constants (SpecializationConstantAttribute and VkConstantIdAttribute) to have initializers without requiring the static keyword - Updated 8 SPIRV test files to remove unnecessary static keywords from specialization constant declarations - Enhanced diagnostic test case to include specialization constant examples - All tests passing with the new behavior 🤖 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: Yong He <csyonghe@users.noreply.github.com> * format code (#7765) Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> * Remove static keyword from specialization constants and add comprehensive test cases - Remove static from specialization constants in test files as they are semantically meaningless - Update tests to use existing diagnostic 31219 for static specialization constant errors - Add comprehensive test cases that verify the error occurs when static is used with specialization constants - Fixes suggested in PR review to make static specialization constants an error Co-authored-by: ArielG-NV <ArielG-NV@users.noreply.github.com> * Add error for static specialization constants Static specialization constants are semantically meaningless and should produce an error. This change adds logic to trigger diagnostic 31219 when both 'static' and specialization constant attributes are present. The existing diagnostic 31219 'push or specialization constants cannot be static' is now correctly triggered for: - [SpecializationConstant] static const variables - [vk::constant_id] static const variables Co-authored-by: ArielG-NV <ArielG-NV@users.noreply.github.com> * Update source/slang/slang-check-decl.cpp * Update tests/glsl/compute-shader-layout-id.slang * Update tests/glsl/compute-shader-layout-id.slang * Fix test case Claude broke * Update global-uniform.slang * Update global-uniform.slang.expected --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Yong He <csyonghe@users.noreply.github.com> Co-authored-by: ArielG-NV <159081215+ArielG-NV@users.noreply.github.com> Co-authored-by: Jay Kwak <jkwak-work@users.noreply.github.com> Co-authored-by: slangbot <ellieh+slangbot@nvidia.com> Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> Co-authored-by: ArielG-NV <ArielG-NV@users.noreply.github.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/diagnostics/global-const-uniform-with-init.slang49
-rw-r--r--tests/diagnostics/global-uniform.slang2
-rw-r--r--tests/diagnostics/global-uniform.slang.expected3
3 files changed, 50 insertions, 4 deletions
diff --git a/tests/diagnostics/global-const-uniform-with-init.slang b/tests/diagnostics/global-const-uniform-with-init.slang
new file mode 100644
index 000000000..352c309e3
--- /dev/null
+++ b/tests/diagnostics/global-const-uniform-with-init.slang
@@ -0,0 +1,49 @@
+//TEST:SIMPLE(filecheck=CHK): -target hlsl -entry main
+
+// Test for diagnostic 31224: global const/uniform variables with initializers must be static
+// Exception: specialization constants are allowed to have initializers without static
+// Test for diagnostic 31219: specialization constants cannot be declared static
+
+// These should trigger error 31224
+//CHK-DAG: ([[# @LINE + 1]]): error 31224: global const variable with initializer must be declared static: 'globalConstWithInit'
+const float globalConstWithInit = 1.0f;
+
+//CHK-DAG: ([[# @LINE + 1]]): error 31224: global const variable with initializer must be declared static: 'uniformWithInit'
+uniform float uniformWithInit = 2.0f;
+
+// These are valid and should not produce errors
+static const float staticConstWithInit = 3.0f; // OK - static const with init
+const float globalConstNoInit; // OK - const without init
+uniform float uniformNoInit; // OK - uniform without init
+
+// Specialization constants with initializers are allowed without static
+[SpecializationConstant]
+const int specConstInt = 42; // OK - specialization constant with init
+
+[vk::constant_id(5)]
+const float specConstFloat = 3.14f; // OK - vk constant id with init
+
+// These should trigger error 31219: static specialization constants are not allowed
+//CHK-DAG: ([[# @LINE + 2]]): error 31219: push or specialization constants cannot be 'static'.
+[SpecializationConstant]
+static const int staticSpecConstInt = 100;
+
+//CHK-DAG: ([[# @LINE + 2]]): error 31219: push or specialization constants cannot be 'static'.
+[vk::constant_id(7)]
+static const float staticSpecConstFloat = 2.71f;
+
+void functionScope()
+{
+ const float localConstWithInit = 4.0f; // OK - local const with init
+}
+
+struct MyStruct
+{
+ static const float memberStaticConst = 5.0f; // OK - member static const
+};
+
+[shader("vertex")]
+float4 main() : SV_Position
+{
+ return float4(staticConstWithInit, 0, 0, 1);
+} \ No newline at end of file
diff --git a/tests/diagnostics/global-uniform.slang b/tests/diagnostics/global-uniform.slang
index de4537115..1223f624c 100644
--- a/tests/diagnostics/global-uniform.slang
+++ b/tests/diagnostics/global-uniform.slang
@@ -7,7 +7,7 @@
uniform float a;
-const uint4 b = uint4(0,1,2,3);
+static const uint4 b = uint4(0,1,2,3);
struct C { float x; int y; };
C c;
diff --git a/tests/diagnostics/global-uniform.slang.expected b/tests/diagnostics/global-uniform.slang.expected
index 70877f4bd..3c9f3ba2a 100644
--- a/tests/diagnostics/global-uniform.slang.expected
+++ b/tests/diagnostics/global-uniform.slang.expected
@@ -1,8 +1,5 @@
result code = 0
standard error = {
-tests/diagnostics/global-uniform.slang(10): warning 39019: 'b' is implicitly a global shader parameter, not a global variable. If a global variable is intended, add the 'static' modifier. If a uniform shader parameter is intended, add the 'uniform' modifier to silence this warning.
-const uint4 b = uint4(0,1,2,3);
- ^
tests/diagnostics/global-uniform.slang(13): warning 39019: 'c' is implicitly a global shader parameter, not a global variable. If a global variable is intended, add the 'static' modifier. If a uniform shader parameter is intended, add the 'uniform' modifier to silence this warning.
C c;
^