[clang] [Clang] Allow user defined conversion implicit cast to _Complex types in constant expressions (PR #108758)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Sep 15 07:51:08 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Mital Ashok (MitalAshok)
<details>
<summary>Changes</summary>
This already worked with `-fexperimental-new-constant-interpreter`
Fixes #<!-- -->108750
---
Full diff: https://github.com/llvm/llvm-project/pull/108758.diff
3 Files Affected:
- (modified) clang/docs/ReleaseNotes.rst (+1)
- (modified) clang/lib/AST/ExprConstant.cpp (+1-1)
- (modified) clang/test/AST/ByteCode/complex.cpp (+12)
``````````diff
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, "");
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/108758
More information about the cfe-commits
mailing list