[clang] [clang] Clang should detect illegal copy constructor with template class as its parameter (PR #81251)

Rajveer Singh Bharadwaj via cfe-commits cfe-commits at lists.llvm.org
Sun Feb 25 03:57:24 PST 2024


https://github.com/Rajveer100 updated https://github.com/llvm/llvm-project/pull/81251

>From 5c5a91dbdf7239025d7bc5961afc0f375d3b1627 Mon Sep 17 00:00:00 2001
From: Rajveer <rajveer.developer at icloud.com>
Date: Fri, 9 Feb 2024 19:20:39 +0530
Subject: [PATCH] [clang] Clang should detect illegal copy constructor with
 template class as its parameter

Resolves #80963
---
 clang/docs/ReleaseNotes.rst    |  3 +++
 clang/lib/Sema/SemaDeclCXX.cpp |  4 +---
 clang/test/SemaCXX/GH81251.cpp | 17 +++++++++++++++++
 3 files changed, 21 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/SemaCXX/GH81251.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 529dd783ab7382..8bd80a199ed3b0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -281,6 +281,9 @@ Bug Fixes to C++ Support
   a requires-clause lie at the same depth as those of the surrounding lambda. This,
   in turn, results in the wrong template argument substitution during constraint checking.
   (`#78524 <https://github.com/llvm/llvm-project/issues/78524>`_)
+  (`#782154 <https://github.com/llvm/llvm-project/issues/82154>`_`)
+- Clang now detects illegal copy constructor with template class as its parameter.
+  Fixes (`#80963 https://github.com/llvm/llvm-project/issues/80963`_)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 7c009d9c8ec093..622bf0c258f138 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -10921,9 +10921,7 @@ void Sema::CheckConstructor(CXXConstructorDecl *Constructor) {
   //   either there are no other parameters or else all other
   //   parameters have default arguments.
   if (!Constructor->isInvalidDecl() &&
-      Constructor->hasOneParamOrDefaultArgs() &&
-      Constructor->getTemplateSpecializationKind() !=
-          TSK_ImplicitInstantiation) {
+      Constructor->hasOneParamOrDefaultArgs()) {
     QualType ParamType = Constructor->getParamDecl(0)->getType();
     QualType ClassTy = Context.getTagDeclType(ClassDecl);
     if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) {
diff --git a/clang/test/SemaCXX/GH81251.cpp b/clang/test/SemaCXX/GH81251.cpp
new file mode 100644
index 00000000000000..a3e0ba07728dcc
--- /dev/null
+++ b/clang/test/SemaCXX/GH81251.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+template < class T, class V > struct A
+{
+    A ();
+    A (A &);
+    A (A < V,T >);
+    // expected-error at -1 {{copy constructor must pass its first argument by reference}}
+};
+
+void f ()
+{
+    A <int, int> (A < int, int >());
+    // expected-note at -1 {{in instantiation of template class 'A<int, int>' requested here}}
+    
+    A <int, double> (A < int, double >());
+}



More information about the cfe-commits mailing list