From 3b0de8b6ea484091146f61e663c63beeac5b4798 Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 15 May 2024 18:07:36 -0700 Subject: Add diagnostic to prevent defining unsized variables. (#4168) * Add diagnostic to prevent defining unsized static variables. * Fix tests. * Add more tests. * Fix to allow defining variables of link-time size. * update diagnostic message. * Fix tests. * Simplify code. --- source/slang/slang-check-decl.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'source/slang/slang-check-decl.cpp') diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index 875fddf2f..9a4ee9d71 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -2040,11 +2040,14 @@ namespace Slang } } - if (auto parentDecl = as(getParentDecl(varDecl))) + TypeTag varTypeTags = getTypeTags(varDecl->getType()); + auto parentDecl = as(getParentDecl(varDecl)); + if (parentDecl) { - auto typeTags = getTypeTags(varDecl->getType()); - parentDecl->addTag(typeTags); - if ((int)typeTags & (int)TypeTag::Unsized) + parentDecl->addTag(varTypeTags); + auto unsizedMask = (int)TypeTag::Unsized; + bool isUnknownSize = (((int)varTypeTags & unsizedMask) != 0); + if (isUnknownSize) { // Unsized decl must appear as the last member of the struct. for (auto memberIdx = parentDecl->members.getCount() - 1; memberIdx >= 0; memberIdx--) @@ -2064,7 +2067,17 @@ namespace Slang } } } - + bool isGlobalOrLocalVar = !isGlobalShaderParameter(varDecl) && !as(varDecl) && + (!parentDecl || isEffectivelyStatic(varDecl)); + if (isGlobalOrLocalVar) + { + bool isUnsized = (((int)varTypeTags & (int)TypeTag::Unsized) != 0); + if (isUnsized) + { + getSink()->diagnose(varDecl, Diagnostics::varCannotBeUnsized); + } + } + if (auto elementType = getConstantBufferElementType(varDecl->getType())) { if (doesTypeHaveTag(elementType, TypeTag::Incomplete)) -- cgit v1.2.3