Skip to content

Commit f37bdc2

Browse files
authored
[Clang] Add the template depth when parsing type constraints (#163960)
The lambdas can introduce new template parameters, and things would go wrong if the new template parameters still lived in the depth of the parent, when we evaluate the type constraints. Fixes llvm/llvm-project#162092
1 parent f4fe714 commit f37bdc2

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ Bug Fixes to C++ Support
478478
- Fix a crash when attempting to deduce a deduction guide from a non deducible template template parameter. (#130604)
479479
- Fix for clang incorrectly rejecting the default construction of a union with
480480
nontrivial member when another member has an initializer. (#GH81774)
481+
- Fixed a template depth issue when parsing lambdas inside a type constraint. (#GH162092)
481482
- Diagnose unresolved overload sets in non-dependent compound requirements. (#GH51246) (#GH97753)
482483

483484
Bug Fixes to AST Handling

clang/lib/Parse/ParseTemplate.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,12 @@ bool Parser::isTypeConstraintAnnotation() {
533533
bool Parser::TryAnnotateTypeConstraint() {
534534
if (!getLangOpts().CPlusPlus20)
535535
return false;
536+
// The type constraint may declare template parameters, notably
537+
// if it contains a generic lambda, so we need to increment
538+
// the template depth as these parameters would not be instantiated
539+
// at the current depth.
540+
TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
541+
++CurTemplateDepthTracker;
536542
CXXScopeSpec SS;
537543
bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope);
538544
if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,

clang/test/SemaTemplate/concepts.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,31 @@ static_assert( requires {{ &f } -> C;} ); // expected-error {{reference to overl
15141514

15151515
}
15161516

1517+
namespace GH162092 {
1518+
1519+
template <typename T>
1520+
struct vector;
1521+
1522+
template <typename T, typename U>
1523+
concept C = __is_same_as(T, U);
1524+
1525+
template<class T, auto Cpt>
1526+
concept generic_range_value = requires {
1527+
Cpt.template operator()<int>();
1528+
};
1529+
1530+
1531+
template<generic_range_value<[]<
1532+
C<int>
1533+
>() {}> T>
1534+
void x() {}
1535+
1536+
void foo() {
1537+
x<vector<int>>();
1538+
}
1539+
1540+
}
1541+
15171542
namespace GH162770 {
15181543
enum e {};
15191544
template<e> struct s {};

0 commit comments

Comments
 (0)