https://github.com/camc created https://github.com/llvm/llvm-project/pull/157174
Resolves #157067 >From 14ab4f59e7a6df5e93ddf953653765c351443abb Mon Sep 17 00:00:00 2001 From: camc <69519329+c...@users.noreply.github.com> Date: Fri, 5 Sep 2025 20:47:06 +0000 Subject: [PATCH] [clang] Detect int-to-float narrowing when backconversion is unspecified --- clang/lib/Sema/SemaOverload.cpp | 10 ++++++---- .../test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp | 2 ++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index 14fa8478fe317..71d23906f2b17 100644 --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -412,10 +412,12 @@ NarrowingKind StandardConversionSequence::getNarrowingKind( // And back. llvm::APSInt ConvertedValue = *IntConstantValue; bool ignored; - Result.convertToInteger(ConvertedValue, - llvm::APFloat::rmTowardZero, &ignored); - // If the resulting value is different, this was a narrowing conversion. - if (*IntConstantValue != ConvertedValue) { + llvm::APFloat::opStatus fs = Result.convertToInteger( + ConvertedValue, llvm::APFloat::rmTowardZero, &ignored); + // If the converted-back integer has unspecified value, or if the + // resulting value is different, this was a narrowing conversion. + if (fs == llvm::APFloat::opInvalidOp || + *IntConstantValue != ConvertedValue) { ConstantValue = APValue(*IntConstantValue); ConstantType = Initializer->getType(); return NK_Constant_Narrowing; diff --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp index 2bceb3e267790..0d389cce19818 100644 --- a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp +++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp @@ -137,6 +137,8 @@ void int_to_float() { Agg<float> f7 = {12345678}; // OK (exactly fits in a float) Agg<float> f8 = {EnumVal}; // OK Agg<float> f9 = {123456789}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} + Agg<float> f10 = {2147483646} // expected-error {{constant expression evaluates to 2147483646 which cannot be narrowed to type 'float'}} expected-note {{silence}} + Agg<float> f11 = {2147483647} // expected-error {{constant expression evaluates to 2147483647 which cannot be narrowed to type 'float'}} expected-note {{silence}} Agg<float> ce1 = { Convert<int>(123456789) }; // expected-error {{constant expression evaluates to 123456789 which cannot be narrowed to type 'float'}} expected-note {{silence}} Agg<double> ce2 = { ConvertVar<long long>() }; // expected-error {{non-constant-expression cannot be narrowed from type 'long long' to 'double'}} expected-note {{silence}} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits