diff options
| author | Yong He <yonghe@outlook.com> | 2022-08-17 23:08:34 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-17 23:08:34 -0700 |
| commit | adaea0e993fd8db351b5dad92802e47ed6d0ec77 (patch) | |
| tree | dfad5201677b0202b0b890cbae066b5b2f3f033b /source/slang/slang-check-decl.cpp | |
| parent | d65c6183c0d8b365aa182c3d9026ba85522531f2 (diff) | |
Warning on lossy implicit casts. (#2367)
* Warning on bool to float conversion.
* Fix test cases.
* Improve.
* LanguageServer: don't show constant value for non constant variables.
* Fix tests.
* Fix warnings in tests.
Co-authored-by: Yong He <yhe@nvidia.com>
Diffstat (limited to 'source/slang/slang-check-decl.cpp')
| -rw-r--r-- | source/slang/slang-check-decl.cpp | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index f2a339643..abe7642fb 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -14,6 +14,8 @@ #include "slang-syntax.h" +#include <limits> + namespace Slang { /// Visitor to transition declarations to `DeclCheckState::CheckedModifiers` @@ -3487,7 +3489,36 @@ namespace Slang if(!basicType) return false; - return isIntegerBaseType(basicType->baseType); + return isIntegerBaseType(basicType->baseType) || basicType->baseType == BaseType::Bool; + } + + bool SemanticsVisitor::isIntValueInRangeOfType(IntegerLiteralValue value, Type* type) + { + auto basicType = as<BasicExpressionType>(type); + if (!basicType) + return false; + + switch (basicType->baseType) + { + case BaseType::UInt8: + return (value >= 0 && value <= std::numeric_limits<uint8_t>::max()) || (value == -1); + case BaseType::UInt16: + return (value >= 0 && value <= std::numeric_limits<uint16_t>::max()) || (value == -1); + case BaseType::UInt: + return (value >= 0 && value <= std::numeric_limits<uint32_t>::max()) || (value == -1); + case BaseType::UInt64: + return true; + case BaseType::Int8: + return value >= std::numeric_limits<int8_t>::min() && value <= std::numeric_limits<int8_t>::max(); + case BaseType::Int16: + return value >= std::numeric_limits<int16_t>::min() && value <= std::numeric_limits<int16_t>::max(); + case BaseType::Int: + return value >= std::numeric_limits<int32_t>::min() && value <= std::numeric_limits<int32_t>::max(); + case BaseType::Int64: + return value >= std::numeric_limits<int64_t>::min() && value <= std::numeric_limits<int64_t>::max(); + default: + return false; + } } void SemanticsVisitor::validateEnumTagType(Type* type, SourceLoc const& loc) @@ -3772,7 +3803,7 @@ namespace Slang // We want to enforce that this is an integer constant // expression, but we don't actually care to retain // the value. - CheckIntegerConstantExpression(initExpr); + CheckIntegerConstantExpression(initExpr, IntegerConstantExpressionCoercionType::AnyInteger, nullptr); decl->tagExpr = initExpr; } |
