Skip to content

Conversation

zyn0217
Copy link
Contributor

@zyn0217 zyn0217 commented Oct 17, 2025

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 #162092

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.
@zyn0217 zyn0217 requested a review from cor3ntin October 17, 2025 13:28
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 17, 2025

@llvm/pr-subscribers-clang

Author: Younan Zhang (zyn0217)

Changes

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 #162092


Full diff: https://github.com/llvm/llvm-project/pull/163960.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+1)
  • (modified) clang/lib/Parse/ParseTemplate.cpp (+6)
  • (modified) clang/test/SemaTemplate/concepts.cpp (+25)
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 <typename T>
+struct vector;
+
+template <typename T, typename U>
+concept C = __is_same_as(T, U);
+
+template<class T, auto Cpt>
+concept generic_range_value = requires {
+    Cpt.template operator()<int>();
+};
+
+
+template<generic_range_value<[]<
+   C<int>
+   >() {}> T>
+void x() {}
+
+void foo() {
+  x<vector<int>>();
+}
+
+}
+
 namespace GH162770 {
   enum e {};
   template<e> struct s {};

@zyn0217 zyn0217 enabled auto-merge (squash) October 18, 2025 04:38
@zyn0217 zyn0217 merged commit f37bdc2 into llvm:main Oct 18, 2025
10 of 11 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Oct 18, 2025

LLVM Buildbot has detected a new failure on builder clang-arm64-windows-msvc running on linaro-armv8-windows-msvc-04 while building clang at step 6 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/161/builds/8490

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-all) failure: test (failure)
...
91.978 [422/10/891] Building CXX object unittests/MC/CMakeFiles/MCTests.dir/DwarfLineTableHeaders.cpp.obj
91.987 [421/10/892] Building CXX object unittests/MC/CMakeFiles/MCTests.dir/TargetRegistry.cpp.obj
92.008 [420/10/893] Building CXX object unittests/MC/CMakeFiles/MCTests.dir/MCInstPrinter.cpp.obj
92.033 [419/10/894] Building RC object unittests/MC/CMakeFiles/MCTests.dir/__/__/resources/windows_version_resource.rc.res
92.056 [418/10/895] Building CXX object unittests/MC/X86/CMakeFiles/X86MCTests.dir/X86MCDisassemblerTest.cpp.obj
92.064 [417/10/896] Building CXX object unittests/MC/CMakeFiles/MCTests.dir/MCDisassemblerTest.cpp.obj
92.088 [416/10/897] Building RC object unittests/MC/X86/CMakeFiles/X86MCTests.dir/__/__/__/resources/windows_version_resource.rc.res
92.170 [415/10/898] Building RC object unittests/MI/CMakeFiles/MITests.dir/__/__/resources/windows_version_resource.rc.res
92.179 [414/10/899] Building CXX object unittests/MI/CMakeFiles/MITests.dir/LiveIntervalTest.cpp.obj
92.217 [413/10/900] Building RC object unittests/ObjCopy/CMakeFiles/ObjCopyTests.dir/__/__/resources/windows_version_resource.rc.res
FAILED: unittests/ObjCopy/CMakeFiles/ObjCopyTests.dir/__/__/resources/windows_version_resource.rc.res 
C:/Users/tcwg/scoop/apps/cmake/331~1.6/bin/cmcldeps.exe RC C:/Users/tcwg/llvm-worker/clang-arm64-windows-msvc/llvm-project/llvm/resources/windows_version_resource.rc unittests\ObjCopy\CMakeFiles\ObjCopyTests.dir\__\__\resources\windows_version_resource.rc.res.d unittests/ObjCopy/CMakeFiles/ObjCopyTests.dir/__/__/resources/windows_version_resource.rc.res "Note: including file: " "C:/Users/tcwg/scoop/apps/llvm-arm64/current/bin/clang-cl.exe" C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\arm64\rc.exe -DGTEST_HAS_RTTI=0 -DLLVM_BUILD_STATIC -DUNICODE -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_NONSTDC_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS -D_GLIBCXX_ASSERTIONS -D_HAS_EXCEPTIONS=0 -D_SCL_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -D_UNICODE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -DRC_FILE_VERSION=\"0\" -DRC_INTERNAL_NAME=\"ObjCopyTests\" -DRC_PRODUCT_NAME=\"LLVM\" -DRC_PRODUCT_VERSION=\"0\" -DRC_VERSION_FIELD_1=22 -DRC_VERSION_FIELD_2=0 -DRC_VERSION_FIELD_3=0 -DRC_VERSION_FIELD_4=0 -I C:/Users/tcwg/llvm-worker/clang-arm64-windows-msvc/build/unittests/ObjCopy -I C:/Users/tcwg/llvm-worker/clang-arm64-windows-msvc/llvm-project/llvm/unittests/ObjCopy -I C:/Users/tcwg/llvm-worker/clang-arm64-windows-msvc/build/include -I C:/Users/tcwg/llvm-worker/clang-arm64-windows-msvc/llvm-project/llvm/include -I C:/Users/tcwg/llvm-worker/clang-arm64-windows-msvc/llvm-project/third-party/unittest/googletest/include -I C:/Users/tcwg/llvm-worker/clang-arm64-windows-msvc/llvm-project/third-party/unittest/googlemock/include -DWIN32 /nologo /fo unittests/ObjCopy/CMakeFiles/ObjCopyTests.dir/__/__/resources/windows_version_resource.rc.res C:/Users/tcwg/llvm-worker/clang-arm64-windows-msvc/llvm-project/llvm/resources/windows_version_resource.rc
error: unable to open output file 'windows_version_resource.i': 'operation not permitted'


1 error generated.


92.228 [413/9/901] Building CXX object unittests/MIR/CMakeFiles/MIRTests.dir/MachineMetadata.cpp.obj
92.242 [413/8/902] Building CXX object unittests/ObjCopy/CMakeFiles/ObjCopyTests.dir/ObjCopyTest.cpp.obj
92.250 [413/7/903] Building CXX object unittests/MIR/CMakeFiles/MIRTests.dir/MachineStableHashTest.cpp.obj
92.261 [413/6/904] Building RC object unittests/MIR/CMakeFiles/MIRTests.dir/__/__/resources/windows_version_resource.rc.res
92.264 [413/5/905] Building CXX object unittests/Object/CMakeFiles/ObjectTests.dir/ArchiveTest.cpp.obj
92.276 [413/4/906] Building CXX object unittests/Object/CMakeFiles/ObjectTests.dir/COFFObjectFileTest.cpp.obj
92.286 [413/3/907] Building CXX object unittests/Object/CMakeFiles/ObjectTests.dir/BuildIDTest.cpp.obj
92.365 [413/2/908] Building CXX object unittests/Object/CMakeFiles/ObjectTests.dir/DXContainerTest.cpp.obj
92.374 [413/1/909] Building CXX object unittests/Object/CMakeFiles/ObjectTests.dir/ELFObjectFileTest.cpp.obj
ninja: build stopped: subcommand failed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

templated lambda, requires, __is_same and type traits

4 participants