[clang] [Clang] Allow user defined conversion implicit cast to _Complex types in constant expressions (PR #108758)

Mital Ashok via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 16 22:53:23 PDT 2024


https://github.com/MitalAshok updated https://github.com/llvm/llvm-project/pull/108758

>From 0a6b4e83318af66888f6297549a806b5d81e4933 Mon Sep 17 00:00:00 2001
From: Mital Ashok <mital at mitalashok.co.uk>
Date: Sun, 15 Sep 2024 15:48:59 +0100
Subject: [PATCH] [Clang] Allow user defined conversion implicit cast to
 _Complex types in constant expressions

---
 clang/docs/ReleaseNotes.rst         |  1 +
 clang/lib/AST/ExprConstant.cpp      |  2 +-
 clang/test/AST/ByteCode/complex.cpp | 12 ++++++++++++
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 485b75049927fe..ec6c71a7bdc38b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -388,6 +388,7 @@ Bug Fixes to C++ Support
 - Fixed a crash in the typo correction of an invalid CTAD guide. (#GH107887)
 - Fixed a crash when clang tries to subtitute parameter pack while retaining the parameter
   pack. #GH63819, #GH107560
+- Implicit user-defined conversions to ``_Complex`` extension types now work in constant expressions. (#GH108750)
 
 
 Bug Fixes to AST Handling
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 4af7752d3b238b..717e7fabaa901e 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -15344,11 +15344,11 @@ bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) {
   case CK_NoOp:
   case CK_LValueToRValueBitCast:
   case CK_HLSLArrayRValue:
+  case CK_UserDefinedConversion:
     return ExprEvaluatorBaseTy::VisitCastExpr(E);
 
   case CK_Dependent:
   case CK_LValueBitCast:
-  case CK_UserDefinedConversion:
     return Error(E);
 
   case CK_FloatingRealToComplex: {
diff --git a/clang/test/AST/ByteCode/complex.cpp b/clang/test/AST/ByteCode/complex.cpp
index dc93c786dac7ae..3d0cb47f4fab2a 100644
--- a/clang/test/AST/ByteCode/complex.cpp
+++ b/clang/test/AST/ByteCode/complex.cpp
@@ -420,3 +420,15 @@ namespace ComplexConstexpr {
   static_assert(__imag test6 == 6, "");
   static_assert(&__imag test6 == &__real test6 + 1, "");
 }
+
+namespace Casts {
+  struct UserDefinedConversion {
+    double val[2];
+    constexpr operator _Complex double() const { return { val[0], val[1] }; };
+  };
+
+  constexpr _Complex double zero = UserDefinedConversion{};
+  static_assert(zero == 0.0 + 0.0j, "");
+  constexpr _Complex double three_four = UserDefinedConversion{3, 4};
+  static_assert(three_four == 3.0 + 4.0j, "");
+}



More information about the cfe-commits mailing list