From 733b0294682ccdccdce2372cc6dba1ccf1e55cdf Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Fri, 17 Oct 2025 21:23:49 +0800 Subject: [PATCH 1/2] [Clang] Add the template depth when parsing type constraints 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. --- clang/docs/ReleaseNotes.rst | 1 + clang/lib/Parse/ParseTemplate.cpp | 6 ++++++ clang/test/SemaTemplate/concepts.cpp | 25 +++++++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index db2b0f6fd5027..2f0c9f66e2501 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -469,6 +469,7 @@ Bug Fixes to C++ Support - Fix a crash when attempting to deduce a deduction guide from a non deducible template template parameter. (#130604) - Fix for clang incorrectly rejecting the default construction of a union with nontrivial member when another member has an initializer. (#GH81774) +- Fixed a template depth issue when parsing lambdas inside a type constraint. (#GH162092) - Diagnose unresolved overload sets in non-dependent compound requirements. (#GH51246) (#GH97753) Bug Fixes to AST Handling diff --git a/clang/lib/Parse/ParseTemplate.cpp b/clang/lib/Parse/ParseTemplate.cpp index dbc7cbc6cdc0c..445bee98bf646 100644 --- a/clang/lib/Parse/ParseTemplate.cpp +++ b/clang/lib/Parse/ParseTemplate.cpp @@ -533,6 +533,12 @@ bool Parser::isTypeConstraintAnnotation() { bool Parser::TryAnnotateTypeConstraint() { if (!getLangOpts().CPlusPlus20) return false; + // The type constraint may declare template parameters, notably + // if it contains a generic lambda, so we need to increase + // the template depth as these parameters would not be instantiated + // at the current level. + TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); + ++CurTemplateDepthTracker; CXXScopeSpec SS; bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope); if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr, diff --git a/clang/test/SemaTemplate/concepts.cpp b/clang/test/SemaTemplate/concepts.cpp index 3fbe7c0ac650f..4dad17f0a6e46 100644 --- a/clang/test/SemaTemplate/concepts.cpp +++ b/clang/test/SemaTemplate/concepts.cpp @@ -1489,6 +1489,31 @@ static_assert( requires {{ &f } -> C;} ); // expected-error {{reference to overl } +namespace GH162092 { + +template +struct vector; + +template +concept C = __is_same_as(T, U); + +template +concept generic_range_value = requires { + Cpt.template operator()(); +}; + + +template + >() {}> T> +void x() {} + +void foo() { + x>(); +} + +} + namespace GH162770 { enum e {}; template struct s {}; From 104e4159639b15102a3f2ef205591dbd2040a172 Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Sat, 18 Oct 2025 12:37:18 +0800 Subject: [PATCH 2/2] Address feedback --- clang/lib/Parse/ParseTemplate.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/lib/Parse/ParseTemplate.cpp b/clang/lib/Parse/ParseTemplate.cpp index 445bee98bf646..330a9c6aea0c5 100644 --- a/clang/lib/Parse/ParseTemplate.cpp +++ b/clang/lib/Parse/ParseTemplate.cpp @@ -534,9 +534,9 @@ bool Parser::TryAnnotateTypeConstraint() { if (!getLangOpts().CPlusPlus20) return false; // The type constraint may declare template parameters, notably - // if it contains a generic lambda, so we need to increase + // if it contains a generic lambda, so we need to increment // the template depth as these parameters would not be instantiated - // at the current level. + // at the current depth. TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth); ++CurTemplateDepthTracker; CXXScopeSpec SS;