[clang] [clang] Fix -Wdouble-promotion in C++ list-initialization (PR #159992)

Marcel Jacobse via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 22 08:51:08 PDT 2025


https://github.com/mjacobse updated https://github.com/llvm/llvm-project/pull/159992

>From 4f63f703b5bb56d2def94b99cd05212652605e42 Mon Sep 17 00:00:00 2001
From: mjacobse <44684927+mjacobse at users.noreply.github.com>
Date: Sun, 21 Sep 2025 18:06:20 +0200
Subject: [PATCH 1/7] [clang] Fix -Wdouble-promotion in C++ list-init

A C++ list-initialization explicitly asks for the promotion to happen,
much like an explicit static_cast, so it should not be warned about.
Fixes #33409
---
 clang/docs/ReleaseNotes.rst     | 1 +
 clang/lib/Sema/SemaChecking.cpp | 5 +++++
 2 files changed, 6 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 46d56bb3f07f5..37d5e09c3f4e8 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -417,6 +417,7 @@ Bug Fixes to C++ Support
   ``__builtin_addressof``, and related issues with builtin arguments. (#GH154034)
 - Fix an assertion failure when taking the address on a non-type template parameter argument of
   object type. (#GH151531)
+- Suppress ``-Wdouble-promotion`` when explicitly asked for with C++ list initialization (#GH33409).
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 00f40cfa910d2..d5b2cde0d1c09 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -12382,6 +12382,11 @@ void Sema::CheckImplicitConversion(Expr *E, QualType T, SourceLocation CC,
       }
       // ... or possibly if we're increasing rank, too
       else if (Order < 0) {
+        // Don't warn if we are in a C++ list initialization expression, as
+        // that means the promotion was asked for explicitly.
+        if (IsListInit)
+          return;
+
         if (SourceMgr.isInSystemMacro(CC))
           return;
 

>From 7a8192b294436ceea93f4ce38371e85d8b331dd3 Mon Sep 17 00:00:00 2001
From: mjacobse <44684927+mjacobse at users.noreply.github.com>
Date: Sun, 21 Sep 2025 18:13:22 +0200
Subject: [PATCH 2/7] [clang] Test -Wdouble-promotion with explicit cast

Add tests to ensure that -Wdouble-promotion does not warn when promotion
is asked for explicitly by an explicit cast or C++ list initialization.
For the latter this creates a .cpp version of warn-double-promotion.c.
Test case for #33409
---
 clang/test/Sema/warn-double-promotion.c   |  27 +++++
 clang/test/Sema/warn-double-promotion.cpp | 128 ++++++++++++++++++++++
 2 files changed, 155 insertions(+)
 create mode 100644 clang/test/Sema/warn-double-promotion.cpp

diff --git a/clang/test/Sema/warn-double-promotion.c b/clang/test/Sema/warn-double-promotion.c
index 5742a4fb3cbd4..ac9e9499bc2b7 100644
--- a/clang/test/Sema/warn-double-promotion.c
+++ b/clang/test/Sema/warn-double-promotion.c
@@ -24,10 +24,25 @@ long double ReturnLongDoubleFromDouble(double d) {
   return d;  //expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
 }
 
+double ReturnDoubleFromFloatWithExplicitCast(float f) {
+  return (double)f;
+}
+
+long double ReturnLongDoubleFromFloatWithExplicitCast(float f) {
+  return (long double)f;
+}
+
+long double ReturnLongDoubleFromDoubleWithExplicitCast(double d) {
+  return (long double)d;
+}
+
 void Assignment(float f, double d, long double ld) {
   d = f;  //expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
   ld = f; //expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
   ld = d; //expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
+  d = (double)f;
+  ld = (long double)f;
+  ld = (long double)d;
   f = d;
   f = ld;
   d = ld;
@@ -40,6 +55,9 @@ void ArgumentPassing(float f, double d) {
   DoubleParameter(f); // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
   LongDoubleParameter(f); // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
   LongDoubleParameter(d); // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
+  DoubleParameter((double)f);
+  LongDoubleParameter((long double)f);
+  LongDoubleParameter((long double)d);
 }
 
 void BinaryOperator(float f, double d, long double ld) {
@@ -49,12 +67,21 @@ void BinaryOperator(float f, double d, long double ld) {
   f = ld * f; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
   d = d * ld; // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
   d = ld * d; // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
+  f = (double)f * d;
+  f = d * (double)f;
+  f = (long double)f * ld;
+  f = ld * (long double)f;
+  d = (long double)d * ld;
+  d = ld * (long double)d;
 }
 
 void MultiplicationAssignment(float f, double d, long double ld) {
   d *= f; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
   ld *= f; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
   ld *= d; // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
+  d *= (double)f;
+  ld *= (long double)f;
+  ld *= (long double)d;
 
   // FIXME: These cases should produce warnings as above.
   f *= d;
diff --git a/clang/test/Sema/warn-double-promotion.cpp b/clang/test/Sema/warn-double-promotion.cpp
new file mode 100644
index 0000000000000..677f59a219521
--- /dev/null
+++ b/clang/test/Sema/warn-double-promotion.cpp
@@ -0,0 +1,128 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin -verify -fsyntax-only %s -Wdouble-promotion
+
+float ReturnFloatFromDouble(double d) {
+  return d;
+}
+
+float ReturnFloatFromLongDouble(long double ld) {
+  return ld;
+}
+
+double ReturnDoubleFromLongDouble(long double ld) {
+  return ld;
+}
+
+double ReturnDoubleFromFloat(float f) {
+  return f;  //expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
+}
+
+long double ReturnLongDoubleFromFloat(float f) {
+  return f;  //expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
+}
+
+long double ReturnLongDoubleFromDouble(double d) {
+  return d;  //expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
+}
+
+double ReturnDoubleFromFloatWithExplicitCast(float f) {
+  return static_cast<double>(f);
+}
+
+long double ReturnLongDoubleFromFloatWithExplicitCast(float f) {
+  return static_cast<long double>(f);
+}
+
+long double ReturnLongDoubleFromDoubleWithExplicitCast(double d) {
+  return static_cast<long double>(d);
+}
+
+double ReturnDoubleFromFloatWithExplicitListInitialization(float f) {
+  return double{f};
+}
+
+long double ReturnLongDoubleFromFloatWithExplicitListInitialization(float f) {
+  return (long double){f};
+}
+
+long double ReturnLongDoubleFromDoubleWithExplicitListInitialization(double d) {
+  return (long double){d};
+}
+
+void Assignment(float f, double d, long double ld) {
+  d = f;  //expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
+  ld = f; //expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
+  ld = d; //expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
+  d = static_cast<double>(f);
+  ld = static_cast<long double>(f);
+  ld = static_cast<long double>(d);
+  d = double{f};
+  ld = (long double){f};
+  ld = (long double){d};
+  f = d;
+  f = ld;
+  d = ld;
+}
+
+extern void DoubleParameter(double);
+extern void LongDoubleParameter(long double);
+
+void ArgumentPassing(float f, double d) {
+  DoubleParameter(f); // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
+  LongDoubleParameter(f); // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
+  LongDoubleParameter(d); // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
+  DoubleParameter(static_cast<double>(f));
+  LongDoubleParameter(static_cast<long double>(f));
+  LongDoubleParameter(static_cast<long double>(d));
+  DoubleParameter(double{f});
+  LongDoubleParameter((long double){f});
+  LongDoubleParameter((long double){d});
+}
+
+void BinaryOperator(float f, double d, long double ld) {
+  f = f * d; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
+  f = d * f; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
+  f = f * ld; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
+  f = ld * f; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
+  d = d * ld; // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
+  d = ld * d; // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
+  f = static_cast<double>(f) * d;
+  f = d * static_cast<double>(f);
+  f = static_cast<long double>(f) * ld;
+  f = ld * static_cast<long double>(f);
+  d = static_cast<long double>(d) * ld;
+  d = ld * static_cast<long double>(d);
+  f = double{f} * d;
+  f = d * double{f};
+  f = (long double){f} * ld;
+  f = ld * (long double){f};
+  d = (long double){d} * ld;
+  d = ld * (long double){d};
+}
+
+void MultiplicationAssignment(float f, double d, long double ld) {
+  d *= f; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
+  ld *= f; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
+  ld *= d; // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
+  d *= static_cast<double>(f);
+  ld *= static_cast<long double>(f);
+  ld *= static_cast<long double>(d);
+  d *= double{f};
+  ld *= (long double){f};
+  ld *= (long double){d};
+
+  // FIXME: These cases should produce warnings as above.
+  f *= d;
+  f *= ld;
+  d *= ld;
+}
+
+// FIXME: As with a binary operator, the operands to the conditional operator are
+// converted to a common type and should produce a warning.
+void ConditionalOperator(float f, double d, long double ld, int i) {
+  f = i ? f : d;
+  f = i ? d : f;
+  f = i ? f : ld;
+  f = i ? ld : f;
+  d = i ? d : ld;
+  d = i ? ld : d;
+}

>From 2e32a977bea9b9ae07db7f49cec87976e61ac8c8 Mon Sep 17 00:00:00 2001
From: mjacobse <mjacobse at uni-bremen.de>
Date: Mon, 22 Sep 2025 16:30:20 +0200
Subject: [PATCH 3/7] Use long double alias to avoid compound literals

With (long double){f} we were getting the compound literal construction
from C99 instead of the intended C++ function-style cast with
list-initialization syntax.
---
 clang/test/Sema/warn-double-promotion.cpp | 26 ++++++++++++-----------
 1 file changed, 14 insertions(+), 12 deletions(-)

diff --git a/clang/test/Sema/warn-double-promotion.cpp b/clang/test/Sema/warn-double-promotion.cpp
index 677f59a219521..5f4e327ab9d06 100644
--- a/clang/test/Sema/warn-double-promotion.cpp
+++ b/clang/test/Sema/warn-double-promotion.cpp
@@ -1,5 +1,7 @@
 // RUN: %clang_cc1 -triple x86_64-apple-darwin -verify -fsyntax-only %s -Wdouble-promotion
 
+using LongDouble = long double;
+
 float ReturnFloatFromDouble(double d) {
   return d;
 }
@@ -41,11 +43,11 @@ double ReturnDoubleFromFloatWithExplicitListInitialization(float f) {
 }
 
 long double ReturnLongDoubleFromFloatWithExplicitListInitialization(float f) {
-  return (long double){f};
+  return LongDouble{f};
 }
 
 long double ReturnLongDoubleFromDoubleWithExplicitListInitialization(double d) {
-  return (long double){d};
+  return LongDouble{d};
 }
 
 void Assignment(float f, double d, long double ld) {
@@ -56,8 +58,8 @@ void Assignment(float f, double d, long double ld) {
   ld = static_cast<long double>(f);
   ld = static_cast<long double>(d);
   d = double{f};
-  ld = (long double){f};
-  ld = (long double){d};
+  ld = LongDouble{f};
+  ld = LongDouble{d};
   f = d;
   f = ld;
   d = ld;
@@ -74,8 +76,8 @@ void ArgumentPassing(float f, double d) {
   LongDoubleParameter(static_cast<long double>(f));
   LongDoubleParameter(static_cast<long double>(d));
   DoubleParameter(double{f});
-  LongDoubleParameter((long double){f});
-  LongDoubleParameter((long double){d});
+  LongDoubleParameter(LongDouble{f});
+  LongDoubleParameter(LongDouble{d});
 }
 
 void BinaryOperator(float f, double d, long double ld) {
@@ -93,10 +95,10 @@ void BinaryOperator(float f, double d, long double ld) {
   d = ld * static_cast<long double>(d);
   f = double{f} * d;
   f = d * double{f};
-  f = (long double){f} * ld;
-  f = ld * (long double){f};
-  d = (long double){d} * ld;
-  d = ld * (long double){d};
+  f = LongDouble{f} * ld;
+  f = ld * LongDouble{f};
+  d = LongDouble{d} * ld;
+  d = ld * LongDouble{d};
 }
 
 void MultiplicationAssignment(float f, double d, long double ld) {
@@ -107,8 +109,8 @@ void MultiplicationAssignment(float f, double d, long double ld) {
   ld *= static_cast<long double>(f);
   ld *= static_cast<long double>(d);
   d *= double{f};
-  ld *= (long double){f};
-  ld *= (long double){d};
+  ld *= LongDouble{f};
+  ld *= LongDouble{d};
 
   // FIXME: These cases should produce warnings as above.
   f *= d;

>From 16a3ddd9b1c0d5e60b8f99ec2581b177f5534b38 Mon Sep 17 00:00:00 2001
From: mjacobse <mjacobse at uni-bremen.de>
Date: Mon, 22 Sep 2025 16:36:19 +0200
Subject: [PATCH 4/7] Avoid duplication of non-C++ specific tests

Run warn-double-promotion.c in both C and C++ mode and only add
those C++ tests with warn-double-promotion.cpp that are not
valid C.
---
 clang/test/Sema/warn-double-promotion.c   |  1 +
 clang/test/Sema/warn-double-promotion.cpp | 59 +----------------------
 2 files changed, 2 insertions(+), 58 deletions(-)

diff --git a/clang/test/Sema/warn-double-promotion.c b/clang/test/Sema/warn-double-promotion.c
index ac9e9499bc2b7..0e56e1c93ce5d 100644
--- a/clang/test/Sema/warn-double-promotion.c
+++ b/clang/test/Sema/warn-double-promotion.c
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -triple x86_64-apple-darwin -verify -fsyntax-only %s -Wdouble-promotion
+// RUN: %clang_cc1 -triple x86_64-apple-darwin -verify -fsyntax-only -x c++ %s -Wdouble-promotion
 
 float ReturnFloatFromDouble(double d) {
   return d;
diff --git a/clang/test/Sema/warn-double-promotion.cpp b/clang/test/Sema/warn-double-promotion.cpp
index 5f4e327ab9d06..e618b79264408 100644
--- a/clang/test/Sema/warn-double-promotion.cpp
+++ b/clang/test/Sema/warn-double-promotion.cpp
@@ -1,31 +1,8 @@
 // RUN: %clang_cc1 -triple x86_64-apple-darwin -verify -fsyntax-only %s -Wdouble-promotion
+// expected-no-diagnostics
 
 using LongDouble = long double;
 
-float ReturnFloatFromDouble(double d) {
-  return d;
-}
-
-float ReturnFloatFromLongDouble(long double ld) {
-  return ld;
-}
-
-double ReturnDoubleFromLongDouble(long double ld) {
-  return ld;
-}
-
-double ReturnDoubleFromFloat(float f) {
-  return f;  //expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
-}
-
-long double ReturnLongDoubleFromFloat(float f) {
-  return f;  //expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
-}
-
-long double ReturnLongDoubleFromDouble(double d) {
-  return d;  //expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
-}
-
 double ReturnDoubleFromFloatWithExplicitCast(float f) {
   return static_cast<double>(f);
 }
@@ -51,27 +28,18 @@ long double ReturnLongDoubleFromDoubleWithExplicitListInitialization(double d) {
 }
 
 void Assignment(float f, double d, long double ld) {
-  d = f;  //expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
-  ld = f; //expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
-  ld = d; //expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
   d = static_cast<double>(f);
   ld = static_cast<long double>(f);
   ld = static_cast<long double>(d);
   d = double{f};
   ld = LongDouble{f};
   ld = LongDouble{d};
-  f = d;
-  f = ld;
-  d = ld;
 }
 
 extern void DoubleParameter(double);
 extern void LongDoubleParameter(long double);
 
 void ArgumentPassing(float f, double d) {
-  DoubleParameter(f); // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
-  LongDoubleParameter(f); // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
-  LongDoubleParameter(d); // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
   DoubleParameter(static_cast<double>(f));
   LongDoubleParameter(static_cast<long double>(f));
   LongDoubleParameter(static_cast<long double>(d));
@@ -81,12 +49,6 @@ void ArgumentPassing(float f, double d) {
 }
 
 void BinaryOperator(float f, double d, long double ld) {
-  f = f * d; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
-  f = d * f; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
-  f = f * ld; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
-  f = ld * f; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
-  d = d * ld; // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
-  d = ld * d; // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
   f = static_cast<double>(f) * d;
   f = d * static_cast<double>(f);
   f = static_cast<long double>(f) * ld;
@@ -102,29 +64,10 @@ void BinaryOperator(float f, double d, long double ld) {
 }
 
 void MultiplicationAssignment(float f, double d, long double ld) {
-  d *= f; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
-  ld *= f; // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
-  ld *= d; // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
   d *= static_cast<double>(f);
   ld *= static_cast<long double>(f);
   ld *= static_cast<long double>(d);
   d *= double{f};
   ld *= LongDouble{f};
   ld *= LongDouble{d};
-
-  // FIXME: These cases should produce warnings as above.
-  f *= d;
-  f *= ld;
-  d *= ld;
-}
-
-// FIXME: As with a binary operator, the operands to the conditional operator are
-// converted to a common type and should produce a warning.
-void ConditionalOperator(float f, double d, long double ld, int i) {
-  f = i ? f : d;
-  f = i ? d : f;
-  f = i ? f : ld;
-  f = i ? ld : f;
-  d = i ? d : ld;
-  d = i ? ld : d;
 }

>From 77def9792947a6c0c188da3d1a6ab9a1b752a708 Mon Sep 17 00:00:00 2001
From: mjacobse <mjacobse at uni-bremen.de>
Date: Mon, 22 Sep 2025 16:45:14 +0200
Subject: [PATCH 5/7] Add tests for function style casts

---
 clang/test/Sema/warn-double-promotion.cpp | 27 +++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/clang/test/Sema/warn-double-promotion.cpp b/clang/test/Sema/warn-double-promotion.cpp
index e618b79264408..cfdbabbcdadc1 100644
--- a/clang/test/Sema/warn-double-promotion.cpp
+++ b/clang/test/Sema/warn-double-promotion.cpp
@@ -27,6 +27,18 @@ long double ReturnLongDoubleFromDoubleWithExplicitListInitialization(double d) {
   return LongDouble{d};
 }
 
+double ReturnDoubleFromFloatWithFunctionStyleCast(float f) {
+  return double(f);
+}
+
+long double ReturnLongDoubleFromFloatWithFunctionStyleCast(float f) {
+  return LongDouble(f);
+}
+
+long double ReturnLongDoubleFromDoubleWithFunctionStyleCast(double d) {
+  return LongDouble(d);
+}
+
 void Assignment(float f, double d, long double ld) {
   d = static_cast<double>(f);
   ld = static_cast<long double>(f);
@@ -34,6 +46,9 @@ void Assignment(float f, double d, long double ld) {
   d = double{f};
   ld = LongDouble{f};
   ld = LongDouble{d};
+  d = double(f);
+  ld = LongDouble(f);
+  ld = LongDouble(d);
 }
 
 extern void DoubleParameter(double);
@@ -46,6 +61,9 @@ void ArgumentPassing(float f, double d) {
   DoubleParameter(double{f});
   LongDoubleParameter(LongDouble{f});
   LongDoubleParameter(LongDouble{d});
+  DoubleParameter(double(f));
+  LongDoubleParameter(LongDouble(f));
+  LongDoubleParameter(LongDouble(d));
 }
 
 void BinaryOperator(float f, double d, long double ld) {
@@ -61,6 +79,12 @@ void BinaryOperator(float f, double d, long double ld) {
   f = ld * LongDouble{f};
   d = LongDouble{d} * ld;
   d = ld * LongDouble{d};
+  f = double(f) * d;
+  f = d * double(f);
+  f = LongDouble(f) * ld;
+  f = ld * LongDouble(f);
+  d = LongDouble(d) * ld;
+  d = ld * LongDouble(d);
 }
 
 void MultiplicationAssignment(float f, double d, long double ld) {
@@ -70,4 +94,7 @@ void MultiplicationAssignment(float f, double d, long double ld) {
   d *= double{f};
   ld *= LongDouble{f};
   ld *= LongDouble{d};
+  d *= double(f);
+  ld *= LongDouble(f);
+  ld *= LongDouble(d);
 }

>From 900976cb9fb8a57158d64e4a8d8271ff3f9d7d73 Mon Sep 17 00:00:00 2001
From: mjacobse <mjacobse at uni-bremen.de>
Date: Mon, 22 Sep 2025 16:55:12 +0200
Subject: [PATCH 6/7] Avoid unintended suppression of -Wdouble-promotion

This reverts commit 4f63f703b5bb56d2def94b99cd05212652605e42
which was the previous attempt to suppress -Wdouble-promotion when
explicitly asked for with C++ list initialization. Just checking if
we are somehow in a list initialization was way too loose and
suppressed many cases that should not have been.

The new fix works with the observation that the case of explicit
C++ list initialization in this case turns into a
CXXFunctionalCastExpr with InitListExpr as direct child. While the
CXXFunctionalCastExpr is ignored when checking for implicit
conversions, the InitListExpr is not, which causes the
-Wdouble-promotion warning. To fix this, treat the InitListExpr as
part of the CXXFunctionalCastExpr and skip it too.
---
 clang/lib/Sema/SemaChecking.cpp | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index d5b2cde0d1c09..f25496915a5da 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -12382,11 +12382,6 @@ void Sema::CheckImplicitConversion(Expr *E, QualType T, SourceLocation CC,
       }
       // ... or possibly if we're increasing rank, too
       else if (Order < 0) {
-        // Don't warn if we are in a C++ list initialization expression, as
-        // that means the promotion was asked for explicitly.
-        if (IsListInit)
-          return;
-
         if (SourceMgr.isInSystemMacro(CC))
           return;
 
@@ -12802,6 +12797,22 @@ static void CheckCommaOperand(
     S.CheckImplicitConversion(E, T, CC);
 }
 
+static Expr* IgnoreExplicitCastForImplicitConversionCheck(ExplicitCastExpr *E) {
+  // In the special case of C++ function-style cast with braces,
+  // CXXFunctionalCastExpr has InitListExpr as direct child with a single
+  // initializer. It basically belongs to the cast itself, so for the purposes
+  // of checking for implicit conversions to warn about it should be skipped
+  // too.
+  if (auto *FCE = dyn_cast<CXXFunctionalCastExpr>(E)) {
+    if (auto *IFCE = dyn_cast<InitListExpr>(FCE->getSubExpr())) {
+      if (IFCE->getNumInits() == 1) {
+        return IFCE->getInit(0);
+      }
+    }
+  }
+  return E->getSubExpr();
+}
+
 /// Data recursive variant of AnalyzeImplicitConversions. Subexpressions
 /// that should be visited are added to WorkList.
 static void AnalyzeImplicitConversions(
@@ -12914,7 +12925,7 @@ static void AnalyzeImplicitConversions(
 
   // Skip past explicit casts.
   if (auto *CE = dyn_cast<ExplicitCastExpr>(E)) {
-    E = CE->getSubExpr()->IgnoreParenImpCasts();
+    E = IgnoreExplicitCastForImplicitConversionCheck(CE)->IgnoreParenImpCasts();
     if (!CE->getType()->isVoidType() && E->getType()->isAtomicType())
       S.Diag(E->getBeginLoc(), diag::warn_atomic_implicit_seq_cst);
     WorkList.push_back({E, CC, IsListInit});

>From 2cdee44f2d9cf26b28138c9d64038b770853e62d Mon Sep 17 00:00:00 2001
From: mjacobse <mjacobse at uni-bremen.de>
Date: Mon, 22 Sep 2025 17:18:39 +0200
Subject: [PATCH 7/7] Add C++-specific init tests for -Wdouble-promotion

---
 clang/test/Sema/warn-double-promotion.cpp | 47 ++++++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)

diff --git a/clang/test/Sema/warn-double-promotion.cpp b/clang/test/Sema/warn-double-promotion.cpp
index cfdbabbcdadc1..12ac7e6fc7fe5 100644
--- a/clang/test/Sema/warn-double-promotion.cpp
+++ b/clang/test/Sema/warn-double-promotion.cpp
@@ -1,5 +1,4 @@
 // RUN: %clang_cc1 -triple x86_64-apple-darwin -verify -fsyntax-only %s -Wdouble-promotion
-// expected-no-diagnostics
 
 using LongDouble = long double;
 
@@ -39,6 +38,52 @@ long double ReturnLongDoubleFromDoubleWithFunctionStyleCast(double d) {
   return LongDouble(d);
 }
 
+void InitializationWithParens(float f, double d) {
+  {
+    double d(f);  // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
+    long double ld0(f);  // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
+    long double ld1(d);  // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
+  }
+  {
+    double d(static_cast<double>(f));
+    long double ld0(static_cast<long double>(f));
+    long double ld1(static_cast<long double>(d));
+  }
+  {
+    double d(double{f});
+    long double ld0(LongDouble{f});
+    long double ld1(LongDouble{d});
+  }
+  {
+    double d((double(f)));
+    long double ld0((LongDouble(f)));
+    long double ld1((LongDouble(d)));
+  }
+}
+
+void InitializationWithBraces(float f, double d) {
+  {
+    double d{f};  // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'double'}}
+    long double ld0{f};  // expected-warning{{implicit conversion increases floating-point precision: 'float' to 'long double'}}
+    long double ld1{d};  // expected-warning{{implicit conversion increases floating-point precision: 'double' to 'long double'}}
+  }
+  {
+    double d{static_cast<double>(f)};
+    long double ld0{static_cast<long double>(f)};
+    long double ld1{static_cast<long double>(d)};
+  }
+  {
+    double d{double{f}};
+    long double ld0{LongDouble{f}};
+    long double ld1{LongDouble{d}};
+  }
+  {
+    double d{double(f)};
+    long double ld0{LongDouble(f)};
+    long double ld1{LongDouble(d)};
+  }
+}
+
 void Assignment(float f, double d, long double ld) {
   d = static_cast<double>(f);
   ld = static_cast<long double>(f);



More information about the cfe-commits mailing list