Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Rajveer100
Copy link
Contributor

@Rajveer100 Rajveer100 commented Feb 9, 2024

Resolves #80963

As described in the snippet of the issue, A<T,V> is correctly detected while it fails to reject in other cases.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Feb 9, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Feb 9, 2024

@llvm/pr-subscribers-clang

Author: Rajveer Singh Bharadwaj (Rajveer100)

Changes

Resolves Issue #80963

As described in the snippet of the issue, A&lt;T,V&gt; is correctly detected while it fails to reject in other cases.


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

2 Files Affected:

  • (modified) clang/lib/Sema/SemaDeclCXX.cpp (+1-1)
  • (added) clang/test/SemaCXX/GH80963.cpp (+15)
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index ab8a967b06a456..9778679ee40d47 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -10918,7 +10918,7 @@ void Sema::CheckConstructor(CXXConstructorDecl *Constructor) {
   //   parameters have default arguments.
   if (!Constructor->isInvalidDecl() &&
       Constructor->hasOneParamOrDefaultArgs() &&
-      Constructor->getTemplateSpecializationKind() !=
+      Constructor->getTemplateSpecializationKind() ==
           TSK_ImplicitInstantiation) {
     QualType ParamType = Constructor->getParamDecl(0)->getType();
     QualType ClassTy = Context.getTagDeclType(ClassDecl);
diff --git a/clang/test/SemaCXX/GH80963.cpp b/clang/test/SemaCXX/GH80963.cpp
new file mode 100644
index 00000000000000..55779e6ff191b7
--- /dev/null
+++ b/clang/test/SemaCXX/GH80963.cpp
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+template < class T, class V > struct A
+{
+    A ();
+    A (A &);
+    A (A < V,T >);
+    // expected-error@-1 {{copy constructor must pass its first argument by reference}}
+};
+
+void f ()
+{
+    A <int, int> (A < int, int >());
+    // expected-note@-1 {{in instantiation of template class 'A<int, int>' requested here}}
+}

@Rajveer100
Copy link
Contributor Author

@cor3ntin @shafik

I am not quite sure if this is the intended fix, also I will check the few other failing tests once the CI completes its job.

@shafik
Copy link
Collaborator

shafik commented Feb 22, 2024

@cor3ntin the fix looks like the one you recommended but that is quite a lot of tests that fail.

Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also needs a release note.

@cor3ntin
Copy link
Contributor

Could you merge main inside that branch? There are some test failures (which i hope are unrelated). Thanks

@Rajveer100 Rajveer100 force-pushed the branch-for-issue-80963 branch 2 times, most recently from 5c5a91d to 4bb6d4d Compare February 25, 2024 12:40
@Rajveer100
Copy link
Contributor Author

Rajveer100 commented Feb 29, 2024

I have removed this entirely, although it may still not be optimal, it did reduce few more test failures:

Constructor->hasOneParamOrDefaultArgs() &&
      Constructor->getTemplateSpecializationKind() !=
          TSK_ImplicitInstantiation

Any particular suggestions apart from updating the tests?

@cor3ntin
Copy link
Contributor

cor3ntin commented Mar 7, 2024

I have removed this entirely, although it may still not be optimal, it did reduce few more test failures:

Constructor->hasOneParamOrDefaultArgs() &&
      Constructor->getTemplateSpecializationKind() !=
          TSK_ImplicitInstantiation

Any particular suggestions apart from updating the tests?

I think we should check !Constructor->isFunctionTemplateSpecialization()
See #80963 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Clang fails to detect illegal copy constructor with template class as its parameter
4 participants