diff options
| author | Yong He <yonghe@outlook.com> | 2024-05-15 18:07:36 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-15 18:07:36 -0700 |
| commit | 3b0de8b6ea484091146f61e663c63beeac5b4798 (patch) | |
| tree | 8093bc0af8d0801435c29abf5c5f1fdd3e7cc09a /source/slang/slang-check-decl.cpp | |
| parent | cc88530a722cc2ce7a09f2a39dadeeb504e2f221 (diff) | |
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.
Diffstat (limited to 'source/slang/slang-check-decl.cpp')
| -rw-r--r-- | source/slang/slang-check-decl.cpp | 23 |
1 files changed, 18 insertions, 5 deletions
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<AggTypeDecl>(getParentDecl(varDecl))) + TypeTag varTypeTags = getTypeTags(varDecl->getType()); + auto parentDecl = as<AggTypeDecl>(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<ParamDecl>(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)) |
