[clang] [clang] Avoid doing C++20 aggregate init during copy-initialization (PR #131320)

via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 14 04:46:19 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: None (offsetof)

<details>
<summary>Changes</summary>

Fixes #<!-- -->88089

---
Full diff: https://github.com/llvm/llvm-project/pull/131320.diff


2 Files Affected:

- (modified) clang/lib/Sema/SemaInit.cpp (+2-1) 
- (modified) clang/test/CXX/dcl.decl/dcl.init/dcl.init.general/p16-cxx20.cpp (+29-13) 


``````````diff
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 56ec33fe37bf3..11db8608960ce 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -6714,7 +6714,8 @@ void InitializationSequence::InitializeFrom(Sema &S,
         OverloadCandidateSet::iterator Best;
         OverloadingResult OR = getFailedCandidateSet().BestViableFunction(
             S, Kind.getLocation(), Best);
-        if (OR != OverloadingResult::OR_Deleted) {
+        if (OR != OverloadingResult::OR_Deleted &&
+            Kind.getKind() == InitializationKind::IK_Direct) {
           // C++20 [dcl.init] 17.6.2.2:
           //   - Otherwise, if no constructor is viable, the destination type is
           //   an
diff --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.general/p16-cxx20.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.general/p16-cxx20.cpp
index 1ad205d769c38..1ef74bb66b4de 100644
--- a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.general/p16-cxx20.cpp
+++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.general/p16-cxx20.cpp
@@ -1,18 +1,34 @@
 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s
 
-// If the initializer is (), the object is value-initialized.
-
-// expected-no-diagnostics
 namespace GH69890 {
-struct A {
-    constexpr A() {}
-    int x;
-};
+    // If the initializer is (), the object is value-initialized.
+    struct A {
+        constexpr A() {}
+        int x;
+    };
+
+    struct B : A {
+        int y;
+    };
+
+    static_assert(B().x == 0);
+    static_assert(B().y == 0);
+} // namespace GH69890
+
+namespace P0960R3 {
+    struct A { // expected-note 9 {{candidate constructor}}
+        int i;
+        operator int() volatile;
+    };
+
+    volatile A va;
+    A a = va; // expected-error {{no matching constructor for initialization of 'A'}}
 
-struct B : A {
-    int y;
-};
+    A f() {
+        return va; // expected-error {{no matching constructor for initialization of 'A'}}
+    }
 
-static_assert(B().x == 0);
-static_assert(B().y == 0);
-}
+    int g(A); // expected-note {{passing argument to parameter here}}
+    int g(auto&&);
+    int i = g(va); // expected-error {{no matching constructor for initialization of 'A'}}
+} // namespace P0960R3

``````````

</details>


https://github.com/llvm/llvm-project/pull/131320


More information about the cfe-commits mailing list