Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix zero size array handling in slangc #6399

Merged
merged 6 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions source/slang/slang-check-expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2378,6 +2378,18 @@ Expr* SemanticsExprVisitor::visitIndexExpr(IndexExpr* subscriptExpr)
IntegerConstantExpressionCoercionType::AnyInteger,
nullptr,
ConstantFoldingKind::LinkTime);

// Validate that array size is greater than zero
if (auto constElementCount = as<ConstantIntVal>(elementCount))
{
if (constElementCount->getValue() <= 0)
{
getSink()->diagnose(
subscriptExpr->indexExprs[0],
Diagnostics::invalidArraySize);
return CreateErrorExpr(subscriptExpr);
}
}
}
else if (subscriptExpr->indexExprs.getCount() != 0)
{
Expand Down
6 changes: 3 additions & 3 deletions tests/diagnostics/array-zero-size.slang
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

// Test that array size cannot be zero

//TEST:SIMPLE:
//DIAGNOSTIC_TEST:SIMPLE(filecheck=CHECK):

[numthreads(4, 1, 1)]
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
bar();
}

func bar() -> int[0]; // expected-error 30025 "array size must be larger than zero."
//CHECK: ([[# @LINE+1]]): error 30025
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this line, you can write:

//CHECK: ([[# @LINE+1]]): error 30025

To match for (13): error 30025 in the output.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, updated.

func bar() -> int[0];
Loading