[clang] [Sema] add cast from IncompleteArrayType to ConstantArrayType in TryReferenceListInitialization (PR #65918)

via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 10 18:19:20 PDT 2023


llvmbot wrote:

@llvm/pr-subscribers-clang

<details>
<summary>Changes</summary>

Fixed: https://github.com/llvm/llvm-project/issues/62945
c++20 supports "Permit conversions to arrays of unknown bound". This need additional cast from IncompleteArrayType to ConstantArrayType in TryReferenceListInitialization
--
Full diff: https://github.com/llvm/llvm-project/pull/65918.diff

2 Files Affected:

- (modified) clang/lib/Sema/SemaInit.cpp (+9) 
- (modified) clang/test/CodeGenCXX/cxx20-p0388-unbound-ary.cpp (+9) 


<pre>
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 93f05e2e47285e4..966d35226eec748 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -4532,6 +4532,15 @@ static void TryReferenceListInitialization(Sema &S,
       if (T1Quals.hasAddressSpace())
         Sequence.AddQualificationConversionStep(
             cv1T1, DestType->isRValueReferenceType() ? VK_XValue : VK_LValue);
+      else if (S.getLangOpts().CPlusPlus20 &&
+               isa<IncompleteArrayType>(T1->getUnqualifiedDesugaredType()) &&
+               DestType->isRValueReferenceType()) {
+        // [dcl.init.list] p3.10
+        // unless T is “reference to array of unknown bound of U”, in which case
+        // the type of the prvalue is the type of x in the declaration U x[] H,
+        // where H is the initializer list.
+        Sequence.AddQualificationConversionStep(cv1T1, VK_XValue);
+      }
     } else
       Sequence.SetFailed(
           InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary);
diff --git a/clang/test/CodeGenCXX/cxx20-p0388-unbound-ary.cpp b/clang/test/CodeGenCXX/cxx20-p0388-unbound-ary.cpp
index 78f35a024a54014..a29f4d720c1de4e 100644
--- a/clang/test/CodeGenCXX/cxx20-p0388-unbound-ary.cpp
+++ b/clang/test/CodeGenCXX/cxx20-p0388-unbound-ary.cpp
@@ -23,4 +23,13 @@ auto &frob2(int (&arp)[1]) {
 
   return r2;
 }
+
+// CHECK-LABEL: @_ZN3One3fooEi
+// CHECK-NEXT: entry:
+// CHECK-NEXT: ret void
+void foo(int a) {
+  auto f = [](int(&&)[]) {};
+  f({a});
+}
+
 } // namespace One
</pre>

</details>

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


More information about the cfe-commits mailing list