[clang] [clang] Fix CTAD not work for function-type and array-type arguments. (PR #78159)

via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 15 05:35:25 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Haojian Wu (hokein)

<details>
<summary>Changes</summary>

Fixes https://github.com/llvm/llvm-project/issues/51710

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


3 Files Affected:

- (modified) clang/docs/ReleaseNotes.rst (+2) 
- (modified) clang/lib/Sema/SemaTemplate.cpp (+7-1) 
- (added) clang/test/SemaCXX/ctad-decay.cpp (+26) 


``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 4aba054e252af24..82302bae6d6dc28 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -726,6 +726,8 @@ Bug Fixes in This Version
 - Fix an issue where clang cannot find conversion function with template
   parameter when instantiation of template class.
   Fixes (`#77583 <https://github.com/llvm/llvm-project/issues/77583>`_)
+- Fix an issue where CTAD fails for function-type/array-type arguments.
+  Fixes (`#51710 <https://github.com/llvm/llvm-project/issues/51710>`_)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 5fcc39ec7005228..3052b01f7f12ac3 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -2587,12 +2587,18 @@ struct ConvertConstructorToDeductionGuideTransform {
                           : ParamTy->isRValueReferenceType() ? VK_XValue
                                                              : VK_PRValue);
     }
+    // Handle arrays and functions decay.
+    auto NewType = NewDI->getType();
+    if (NewType->isArrayType())
+      NewType = SemaRef.Context.getArrayDecayedType(NewType);
+    else if (NewType->isFunctionType())
+      NewType = SemaRef.Context.getPointerType(NewType);
 
     ParmVarDecl *NewParam = ParmVarDecl::Create(SemaRef.Context, DC,
                                                 OldParam->getInnerLocStart(),
                                                 OldParam->getLocation(),
                                                 OldParam->getIdentifier(),
-                                                NewDI->getType(),
+                                                NewType,
                                                 NewDI,
                                                 OldParam->getStorageClass(),
                                                 NewDefArg.get());
diff --git a/clang/test/SemaCXX/ctad-decay.cpp b/clang/test/SemaCXX/ctad-decay.cpp
new file mode 100644
index 000000000000000..97063e8fb75659f
--- /dev/null
+++ b/clang/test/SemaCXX/ctad-decay.cpp
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wno-unused-value -std=c++20 %s
+// expected-no-diagnostics
+
+namespace GH51710 {
+
+template<typename T>
+struct A{
+  A(T f());
+  A(int f(), T);
+
+  A(T array[10]);
+  A(int array[10], T);
+};
+
+int foo();
+
+void bar() {
+  A test1(foo);
+  A test2(foo, 1);
+
+  int array[10];
+  A test3(array);
+  A test4(array, 1);
+}
+
+} // namespace GH51710

``````````

</details>


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


More information about the cfe-commits mailing list