Skip to content

Commit 621d7ae

Browse files
csyonghegithub-actions[bot]ArielG-NVjkwak-workslangbot
committed
Diagnose on initExpr of a global const decl (shader-slang#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 shader-slang#7701 🤖 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: Yong He <[email protected]> * 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 <[email protected]> * 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 <[email protected]> * 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 <[email protected]> * format code (shader-slang#7765) Co-authored-by: slangbot <[email protected]> * 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 <[email protected]> * 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 <[email protected]> * 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 <[email protected]> Co-authored-by: ArielG-NV <[email protected]> Co-authored-by: Jay Kwak <[email protected]> Co-authored-by: slangbot <[email protected]> Co-authored-by: slangbot <[email protected]> Co-authored-by: ArielG-NV <[email protected]>
1 parent 69864ef commit 621d7ae

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

source/slang/slang-check-decl.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,11 @@ struct SemanticsDeclModifiersVisitor : public SemanticsDeclVisitorBase,
213213
// causing errors.
214214
bool hasConst = false;
215215
bool hasUniform = false;
216+
bool hasUniform = false;
216217
bool hasExportOrExtern = false;
217218
bool hasStatic = false;
218219
bool hasSpecializationConstant = false;
220+
bool hasSpecializationConstant = false;
219221
for (auto m : decl->modifiers)
220222
{
221223
if (as<ExternModifier>(m) || as<HLSLExportModifier>(m))
@@ -224,10 +226,14 @@ struct SemanticsDeclModifiersVisitor : public SemanticsDeclVisitorBase,
224226
hasConst = true;
225227
else if (as<HLSLUniformModifier>(m))
226228
hasUniform = true;
229+
else if (as<HLSLUniformModifier>(m))
230+
hasUniform = true;
227231
else if (as<HLSLStaticModifier>(m))
228232
hasStatic = true;
229233
else if (as<SpecializationConstantAttribute>(m) || as<VkConstantIdAttribute>(m))
230234
hasSpecializationConstant = true;
235+
else if (as<SpecializationConstantAttribute>(m) || as<VkConstantIdAttribute>(m))
236+
hasSpecializationConstant = true;
231237
}
232238
if (hasExportOrExtern && hasConst != hasStatic)
233239
getSink()->diagnose(
@@ -240,19 +246,13 @@ struct SemanticsDeclModifiersVisitor : public SemanticsDeclVisitorBase,
240246
// In HLSL, const global variables without static are uniform parameters
241247
// that cannot have default values
242248
// Exception: specialization constants are allowed to have initializers
243-
// Exception: In GLSL mode, global const variables are real constants, not uniform
244-
// parameters
245249
if (isGlobalDecl(decl) && (hasConst || hasUniform) && !hasStatic &&
246250
!hasSpecializationConstant && decl->initExpr)
247251
{
248-
auto moduleDecl = getModuleDecl(decl);
249-
if (!moduleDecl || !moduleDecl->hasModifier<GLSLModuleModifier>())
250-
{
251-
getSink()->diagnose(
252-
decl,
253-
Diagnostics::constGlobalVarWithInitRequiresStatic,
254-
decl->getName());
255-
}
252+
getSink()->diagnose(
253+
decl,
254+
Diagnostics::constGlobalVarWithInitRequiresStatic,
255+
decl->getName());
256256
}
257257
}
258258

0 commit comments

Comments
 (0)