llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

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

Open Rush10233 opened this issue 2 years ago • 10 comments

The following code is apparently illegal, as the declaration of A(A<V,T>) should be treated as a copy constructor, which forces passing the argument by reference. While clang ignores that error and accepts it:

template < class T, class V > struct A
{
    A ();
    A (A &);
    A (A < V,T >);
};

void f () 
{
    A <int, int> (A < int, int >());
}

Note that if replace A<V,T> with A<T,T> or A<V,V>, the code is still accepted. But when replacing it with A<T,V>, the code is correctly rejected.

Reducing the number of template class arguments to 1 (template <class T> A and A(A<T>)) also makes sense. We suspect the bug will occur only if the template argument list in copy ctor parameter is not identical to that in class declaration.

https://godbolt.org/z/GKv3E6c4f

Rush10233 avatar Feb 07 '24 10:02 Rush10233

@llvm/issue-subscribers-clang-frontend

Author: None (Rush10233)

The following code is apparently illegal, as the declaration of `A(A<V,T>)` should be treated as a copy constructor, which forces passing the argument by reference. While clang ignores that error and accepts it:
template &lt; class T, class V &gt; struct A
{
    A ();
    A (A &amp;);
    A (A &lt; V,T &gt;);
};

void f () 
{
    A &lt;int, int&gt; (A &lt; int, int &gt;());
}

Note that if replace A&lt;V,T&gt; with A&lt;T,T&gt; or A&lt;V,V&gt;, the code is still accepted. But when replacing it with A&lt;T,V&gt;, the code is correctly rejected.

Reducing the number of template class arguments to 1 (template &lt;class T&gt; A and A(A&lt;T&gt;)) also makes sense. We suspect the bug will occur only if the template argument list in copy ctor parameter is not identical to that in class declaration.

https://godbolt.org/z/GKv3E6c4f

llvmbot avatar Feb 07 '24 16:02 llvmbot

I believe it should only be treated as an ill-formed copy constructor if T and V are the same: https://godbolt.org/z/sjKzzMxdn

template < class T, class V > struct A
{
    A ();
    A (A &);
    A (A < V,T >);
};

void f () 
{
   A<double,int> (A <int,double>{} );

   A<int,double> a{};
   A<double,int> a2(a);

   A<int,int> a3{};
   A<int,int> a4{a3};
}

shafik avatar Feb 07 '24 20:02 shafik

The bug is here https://github.com/llvm/llvm-project/blob/369b82218419a0218400e7483255523b8dfd6cf0/clang/lib/Sema/SemaDeclCXX.cpp#L10921-L10922

This was introduced by https://github.com/llvm/llvm-project/commit/ffe14e3 , but it looks wrong to me. If the type of the parameter is the same as the type of the specialization, we should diagnose it.

https://eel.is/c++draft/class.copy.ctor#1

There are quite a few tests to fix.

Note that we should not diagnose the case where the constructor is a template so we should check !Constructor->isFunctionTemplateSpecialization()

struct A
{
    A ();
    template < class T > 
    A (T);
};

void f () 
{
  A a;
  A b(a); // no warning expected
}

cor3ntin avatar Feb 07 '24 21:02 cor3ntin

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. In the comments of the issue, request for it to be assigned to you.
  2. Fix the issue locally.
  3. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  4. Create a Git commit.
  5. Run git clang-format HEAD~1 to format your changes.
  6. Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation.

If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.

llvmbot avatar Feb 07 '24 21:02 llvmbot

@llvm/issue-subscribers-good-first-issue

Author: None (Rush10233)

The following code is apparently illegal, as the declaration of `A(A<V,T>)` should be treated as a copy constructor, which forces passing the argument by reference. While clang ignores that error and accepts it:
template &lt; class T, class V &gt; struct A
{
    A ();
    A (A &amp;);
    A (A &lt; V,T &gt;);
};

void f () 
{
    A &lt;int, int&gt; (A &lt; int, int &gt;());
}

Note that if replace A&lt;V,T&gt; with A&lt;T,T&gt; or A&lt;V,V&gt;, the code is still accepted. But when replacing it with A&lt;T,V&gt;, the code is correctly rejected.

Reducing the number of template class arguments to 1 (template &lt;class T&gt; A and A(A&lt;T&gt;)) also makes sense. We suspect the bug will occur only if the template argument list in copy ctor parameter is not identical to that in class declaration.

https://godbolt.org/z/GKv3E6c4f

llvmbot avatar Feb 07 '24 21:02 llvmbot

I am starting to analyze the issue.

akshaykumars614 avatar Feb 08 '24 21:02 akshaykumars614

Hi! I’m new to LLVM and would like to work on this issue. I noticed this is open but hasn’t been updated since February. Is this still being actively worked on, or can I take over? I’m happy to collaborate or start fresh if needed. Thanks!

Megan0704-1 avatar Mar 10 '25 23:03 Megan0704-1

Hi! I’m new to LLVM and would like to work on this issue. I noticed this is open but hasn’t been updated since February. Is this still being actively worked on, or can I take over? I’m happy to collaborate or start fresh if needed. Thanks!

That sounds great, let us know if you have any questions.

Once you have a PR then update the issue and we can assign the issue to you then.

We are trying a new approach CC @erichkeane

shafik avatar Mar 11 '25 00:03 shafik

Hello, thanks for the guidance! I’ve opened a PR to address this: PR #130866. Please assign the issue to me for tracking.

Megan0704-1 avatar Mar 12 '25 00:03 Megan0704-1

Might be helpful to actually look at a case where the copy is being created from an rvalue...

https://godbolt.org/z/xfM6zqrna

template < class T, class V > struct A {
  A();
  A(A &);
  A(A<V, T>);
};
A<int, int> &&a(void);
void f() {
  A<int, int> a2 = a();
}

hubert-reinterpretcast avatar Mar 12 '25 03:03 hubert-reinterpretcast