From 12d09b5a6cb9e921aace1bec484c76244e9afa94 Mon Sep 17 00:00:00 2001 From: Mukund Keshava Date: Wed, 19 Feb 2025 18:19:10 +0530 Subject: 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 --- source/slang/slang-check-expr.cpp | 12 ++++++++++++ tests/diagnostics/array-zero-size.slang | 6 +++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/source/slang/slang-check-expr.cpp b/source/slang/slang-check-expr.cpp index 7d4fbdf4c..1fb2b336f 100644 --- a/source/slang/slang-check-expr.cpp +++ b/source/slang/slang-check-expr.cpp @@ -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(elementCount)) + { + if (constElementCount->getValue() <= 0) + { + getSink()->diagnose( + subscriptExpr->indexExprs[0], + Diagnostics::invalidArraySize); + return CreateErrorExpr(subscriptExpr); + } + } } else if (subscriptExpr->indexExprs.getCount() != 0) { diff --git a/tests/diagnostics/array-zero-size.slang b/tests/diagnostics/array-zero-size.slang index 5c62610ba..f67f163fd 100644 --- a/tests/diagnostics/array-zero-size.slang +++ b/tests/diagnostics/array-zero-size.slang @@ -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." \ No newline at end of file +//CHECK: ([[# @LINE+1]]): error 30025 +func bar() -> int[0]; -- cgit v1.2.3