Clang fails to detect illegal copy constructor with template class as its parameter
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.
@llvm/issue-subscribers-clang-frontend
Author: None (Rush10233)
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.
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};
}
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
}
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:
- In the comments of the issue, request for it to be assigned to you.
- Fix the issue locally.
- Run the test suite locally. Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a Git commit.
- Run
git clang-format HEAD~1to format your changes. - 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.
@llvm/issue-subscribers-good-first-issue
Author: None (Rush10233)
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.
I am starting to analyze the issue.
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!
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
Hello, thanks for the guidance! I’ve opened a PR to address this: PR #130866. Please assign the issue to me for tracking.
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();
}