[clang][C++] Bad error recovery when classes are defined inside template aliases
When classes are defined inside template aliases, they are currently recovered as non templated classes, as if they were defined in the template alias' context. However, these classes let escape any references to the template parameters.
Consider this example:
template <class T> using A = struct B {
template <class> void f() requires (T()); // BAD: T aliases f's unnamed template parameter.
};
template void B::f<void>();
Produces:
test.cc:1:37: error: 'B' cannot be defined in a type alias template
1 | template <class T> using A = struct B {
| ^
test.cc:2:39: error: atomic constraint must be of type 'bool' (found 'void')
2 | template <class> void f() requires (T());
This can easily lead to crashes as well:
template <class T> using A = struct B {
using C = T;
};
template <class> void f() requires (B::C());
template void f<void>();
asserts in the constexpr evaluator:
Assertion failed: (!isValueDependent() && "Expression evaluator can't be called on a dependent expression."), function EvaluateAsConstantExpr, file ExprConstant.cpp, line 15953.
Probably a good strategy to fix this issue is to mark the alias' template parameters as invalid, and make sure their resulting type is something sensible for error recovery.
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:
- Check that no other contributor has already been assigned to this issue. If you believe that no one is actually working on it despite an assignment, ping the person. After one week without a response, the assignee may be changed.
- In the comments of this issue, request for it to be assigned to you, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
- 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. Mention this issue in the description of the pull request.
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: Matheus Izvekov (mizvekov)
Consider this example:
template <class T> using A = struct B {
template <class> void f() requires (T()); // BAD: T aliases f's unnamed template parameter.
};
template void B::f<void>();
Produces:
test.cc:1:37: error: 'B' cannot be defined in a type alias template
1 | template <class T> using A = struct B {
| ^
test.cc:2:39: error: atomic constraint must be of type 'bool' (found 'void')
2 | template <class> void f() requires (T());
This can easily lead to crashes as well:
template <class T> using A = struct B {
using C = T;
};
template <class> void f() requires (B::C());
template void f<void>();
asserts in the constexpr evaluator:
Assertion failed: (!isValueDependent() && "Expression evaluator can't be called on a dependent expression."), function EvaluateAsConstantExpr, file ExprConstant.cpp, line 15953.
Probably a good strategy to fix this issue is to mark the alias' template parameters as invalid, and make sure their resulting type is something sensible for error recovery.
@llvm/issue-subscribers-clang-frontend
Author: Matheus Izvekov (mizvekov)
Consider this example:
template <class T> using A = struct B {
template <class> void f() requires (T()); // BAD: T aliases f's unnamed template parameter.
};
template void B::f<void>();
Produces:
test.cc:1:37: error: 'B' cannot be defined in a type alias template
1 | template <class T> using A = struct B {
| ^
test.cc:2:39: error: atomic constraint must be of type 'bool' (found 'void')
2 | template <class> void f() requires (T());
This can easily lead to crashes as well:
template <class T> using A = struct B {
using C = T;
};
template <class> void f() requires (B::C());
template void f<void>();
asserts in the constexpr evaluator:
Assertion failed: (!isValueDependent() && "Expression evaluator can't be called on a dependent expression."), function EvaluateAsConstantExpr, file ExprConstant.cpp, line 15953.
Probably a good strategy to fix this issue is to mark the alias' template parameters as invalid, and make sure their resulting type is something sensible for error recovery.
Can I take this issue if its available? thanks.
Yeah go ahead.
@mizvekov M Hi, thanks for much, can you assign the issue to me? Also, this is my first time contributing to clang:frontend, can you give me some instructions what file/function I should modify?
@mizvekov M
Hi, thanks for much, can you assign the issue to me?
I believe you should be able to just assign yourself, what happens if you click that on the right?
Also, this is my first time contributing to clang:frontend, can you give me some instructions what file/function I should modify?
I think your first order of business should be to locate where the first diagnostic is produced. You should be able to grep the english text, find the tablegen entry for it in a .td file, and then grep for it's identifier in the source code.
Hi Team, is this issue still open for contribution.. I would like to work on this. I don't have control to assign the issue myself.. so can someone assign this to me if it is open for contribution.
Thanks Rajkumar Ananthu.
Hi Team, is this issue still open for contribution.. I would like to work on this. I don't have control to assign the issue myself.. so can someone assign this to me if it is open for contribution.
Thanks Rajkumar Ananthu.
Done
Hi, @rajkumarananthu, Are you still working on this issue? I found it has being opening for more than one month. If not, I'd like to working on this issue.
Hi @firstmoonlight , my day job did not allow me to take a look at the issue, I am un-assigning myself from the issue, please feel free to work on it, sorry for the delay in response.
Thanks Rajkumar.
Unassigning, no movement on this/response back from @firstmoonlight, so opening so that anyone (including him!) can work on it ).
Hi, I would love to work on this issue
Hi, I would love to work on this issue.
Hi, I would love to work on this issue.
We don't assign issues until there is a PR and we have verified it is moving in the right direction
Hi, is this issue still open? If so, I'd like to work on it