From adaea0e993fd8db351b5dad92802e47ed6d0ec77 Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 17 Aug 2022 23:08:34 -0700 Subject: 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 --- source/slang/slang-check-decl.cpp | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 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 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 + 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(type); + if (!basicType) + return false; + + switch (basicType->baseType) + { + case BaseType::UInt8: + return (value >= 0 && value <= std::numeric_limits::max()) || (value == -1); + case BaseType::UInt16: + return (value >= 0 && value <= std::numeric_limits::max()) || (value == -1); + case BaseType::UInt: + return (value >= 0 && value <= std::numeric_limits::max()) || (value == -1); + case BaseType::UInt64: + return true; + case BaseType::Int8: + return value >= std::numeric_limits::min() && value <= std::numeric_limits::max(); + case BaseType::Int16: + return value >= std::numeric_limits::min() && value <= std::numeric_limits::max(); + case BaseType::Int: + return value >= std::numeric_limits::min() && value <= std::numeric_limits::max(); + case BaseType::Int64: + return value >= std::numeric_limits::min() && value <= std::numeric_limits::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; } -- cgit v1.2.3