llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.11k stars 12.01k forks source link

[clang][C++] Bad error recovery when classes are defined inside template aliases #91564

Open mizvekov opened 6 months ago

mizvekov commented 6 months ago

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.

llvmbot commented 6 months ago

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. 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.
  2. 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.
  3. Fix the issue locally.
  4. 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.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. 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.

llvmbot commented 6 months ago

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

Author: Matheus Izvekov (mizvekov)

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: ```C++ 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: ```C++ 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.
llvmbot commented 6 months ago

@llvm/issue-subscribers-clang-frontend

Author: Matheus Izvekov (mizvekov)

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: ```C++ 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: ```C++ 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.
changkhothuychung commented 6 months ago

Can I take this issue if its available? thanks.

mizvekov commented 6 months ago

Yeah go ahead.

changkhothuychung commented 6 months ago

@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 commented 6 months ago

@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.

rajkumarananthu commented 3 months ago

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.

shafik commented 2 months ago

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

firstmoonlight commented 1 month ago

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.