Skip to content

Commit 12d09b5

Browse files
mkeshavaNVslangbotexpipiplus1
authored
Fix zero size array handling in slangc (#6399)
* Fix zero size array handling in slangc Fixes #2890 1. Fix zero size array handling in slangc 2. Add new zero size array diagnostic test. * format code * fix review comments --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> Co-authored-by: Ellie Hermaszewska <ellieh@nvidia.com>
1 parent 7315b33 commit 12d09b5

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

source/slang/slang-check-expr.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -2378,6 +2378,18 @@ Expr* SemanticsExprVisitor::visitIndexExpr(IndexExpr* subscriptExpr)
23782378
IntegerConstantExpressionCoercionType::AnyInteger,
23792379
nullptr,
23802380
ConstantFoldingKind::LinkTime);
2381+
2382+
// Validate that array size is greater than zero
2383+
if (auto constElementCount = as<ConstantIntVal>(elementCount))
2384+
{
2385+
if (constElementCount->getValue() <= 0)
2386+
{
2387+
getSink()->diagnose(
2388+
subscriptExpr->indexExprs[0],
2389+
Diagnostics::invalidArraySize);
2390+
return CreateErrorExpr(subscriptExpr);
2391+
}
2392+
}
23812393
}
23822394
else if (subscriptExpr->indexExprs.getCount() != 0)
23832395
{

tests/diagnostics/array-zero-size.slang

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
// Test that array size cannot be zero
44

5-
//TEST:SIMPLE:
5+
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK):
66

77
[numthreads(4, 1, 1)]
88
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
99
{
1010
bar();
1111
}
12-
13-
func bar() -> int[0]; // expected-error 30025 "array size must be larger than zero."
12+
//CHECK: ([[# @LINE+1]]): error 30025
13+
func bar() -> int[0];

0 commit comments

Comments
 (0)