diff options
| author | Yong He <yonghe@outlook.com> | 2023-04-13 16:40:36 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-13 16:40:36 -0700 |
| commit | 3bbac5f16e9dd47acd2132c0bb2a43393831c450 (patch) | |
| tree | bddf137c252c164f7cd08f1f58b559b96ee49419 /source/slang/slang-check-conversion.cpp | |
| parent | 3b4a50d74059a26af2ed8c37fb2042f33ba7cf2c (diff) | |
Warn on float-to-double coercion for arguments. (#2802)
* Warn on float-to-double coercion for arguments.
* Fix test.
* Rename.
* Fixup.
---------
Co-authored-by: Yong He <yhe@nvidia.com>
Diffstat (limited to 'source/slang/slang-check-conversion.cpp')
| -rw-r--r-- | source/slang/slang-check-conversion.cpp | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/source/slang/slang-check-conversion.cpp b/source/slang/slang-check-conversion.cpp index abfe7afa0..7c4560b5b 100644 --- a/source/slang/slang-check-conversion.cpp +++ b/source/slang/slang-check-conversion.cpp @@ -23,6 +23,17 @@ namespace Slang return kConversionCost_Explicit; } + BuiltinConversionKind SemanticsVisitor::getImplicitConversionBuiltinKind( + Decl* decl) + { + if (auto modifier = decl->findModifier<ImplicitConversionModifier>()) + { + return modifier->builtinConversionKind; + } + + return kBuiltinConversion_Unknown; + } + bool SemanticsVisitor::isEffectivelyScalarForInitializerLists( Type* type) { @@ -121,6 +132,7 @@ namespace Slang { ioInitArgIndex++; return _coerce( + CoercionSite::Initializer, toType, outToExpr, firstInitExpr->type, @@ -211,6 +223,7 @@ namespace Slang { auto arg = fromInitializerListExpr->args[ioArgIndex++]; return _coerce( + CoercionSite::Initializer, toType, outToExpr, arg->type, @@ -616,6 +629,7 @@ namespace Slang } bool SemanticsVisitor::_coerce( + CoercionSite site, Type* toType, Expr** outToExpr, Type* fromType, @@ -852,6 +866,7 @@ namespace Slang } if(!_coerce( + site, toType, outToExpr, fromElementType, @@ -908,6 +923,7 @@ namespace Slang } if (!_coerce( + site, toType, outToExpr, fromValueType, @@ -1012,7 +1028,7 @@ namespace Slang // cost associated with the initializer we are invoking. // ConversionCost cost = getImplicitConversionCost( - overloadContext.bestCandidate->item.declRef.getDecl());; + overloadContext.bestCandidate->item.declRef.getDecl()); // If the cost is too high to be usable as an // implicit conversion, then we will report the @@ -1053,6 +1069,17 @@ namespace Slang getSink()->diagnose(fromExpr, Diagnostics::unrecommendedImplicitConversion, fromType, toType); } } + + if (site == CoercionSite::Argument) + { + auto builtinConversionKind = getImplicitConversionBuiltinKind( + overloadContext.bestCandidate->item.declRef.getDecl()); + if (builtinConversionKind == kBuiltinConversion_FloatToDouble) + { + if (!as<FloatingPointLiteralExpr>(fromExpr)) + getSink()->diagnose(fromExpr, Diagnostics::implicitConversionToDouble); + } + } } if(outCost) *outCost = cost; @@ -1146,6 +1173,7 @@ namespace Slang // during the coercion process. // bool rs = _coerce( + CoercionSite::General, toType, nullptr, fromType, @@ -1215,11 +1243,13 @@ namespace Slang Expr* SemanticsVisitor::coerce( + CoercionSite site, Type* toType, Expr* fromExpr) { Expr* expr = nullptr; if (!_coerce( + site, toType, &expr, fromExpr->type, |
