Skip to content

[Clang] Ensure correct template parameter depth for abbreviated templates - #209693

Merged
zyn0217 merged 1 commit into
llvm:mainfrom
zyn0217:205557-fix
Jul 15, 2026
Merged

[Clang] Ensure correct template parameter depth for abbreviated templates#209693
zyn0217 merged 1 commit into
llvm:mainfrom
zyn0217:205557-fix

Conversation

@zyn0217

@zyn0217 zyn0217 commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

This fixes another case of member functions where we overlooked template depths when only abbreviated template parameters are involved.

This mirrors previous fix cfb2520, but I don't intend to put it in ParseTrailingRequiresClause because we might want the similar fix for e.g. noexcept expressions, so let's keep it inline for future refactor.

The example comes from #205557.

…ates

This fixes another case of member functions where we overlooked template
depths when only abbreviated template parameters are involved.

This mirrors previous fix cfb2520, but I don't intend to put it in
ParseTrailingRequiresClause because we might want the similar fix for
e.g. noexcept expressions, so let's keep it inline for future refactor.

The example comes from 205557.
@zyn0217
zyn0217 requested a review from cor3ntin July 15, 2026 07:36
@llvmorg-github-actions llvmorg-github-actions Bot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 15, 2026
@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-clang

Author: Younan Zhang (zyn0217)

Changes

This fixes another case of member functions where we overlooked template depths when only abbreviated template parameters are involved.

This mirrors previous fix cfb2520, but I don't intend to put it in ParseTrailingRequiresClause because we might want the similar fix for e.g. noexcept expressions, so let's keep it inline for future refactor.

The example comes from 205557.


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

2 Files Affected:

  • (modified) clang/lib/Parse/ParseDeclCXX.cpp (+6)
  • (modified) clang/test/SemaCXX/constexpr-late-instantiation.cpp (+32-2)
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 893989bd2398f..d7a9c72eb2da8 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -2534,6 +2534,12 @@ bool Parser::ParseCXXMemberDeclaratorBeforeInitializer(
     if (BitfieldSize.isInvalid())
       SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
   } else if (Tok.is(tok::kw_requires)) {
+    TemplateParameterDepthRAII CurTemplateDepthTracker(TemplateParameterDepth);
+    // With abbreviated function templates - we need to explicitly add depth to
+    // account for the implicit template parameter list induced by the template.
+    if (DeclaratorInfo.getTemplateParameterLists().empty() &&
+        DeclaratorInfo.getInventedTemplateParameterList())
+      ++CurTemplateDepthTracker;
     ParseTrailingRequiresClauseWithScope(DeclaratorInfo);
   } else {
     ParseOptionalCXX11VirtSpecifierSeq(
diff --git a/clang/test/SemaCXX/constexpr-late-instantiation.cpp b/clang/test/SemaCXX/constexpr-late-instantiation.cpp
index 9aec0c90e61dc..94f5ab4a73616 100644
--- a/clang/test/SemaCXX/constexpr-late-instantiation.cpp
+++ b/clang/test/SemaCXX/constexpr-late-instantiation.cpp
@@ -1,5 +1,10 @@
-// RUN: %clang_cc1 %s -fsyntax-only -verify
-// RUN: %clang_cc1 %s -fexperimental-new-constant-interpreter -fsyntax-only -verify
+// RUN: %clang_cc1 %s -std=c++14 -fsyntax-only -verify
+// RUN: %clang_cc1 %s -std=c++20 -fsyntax-only -verify
+// RUN: %clang_cc1 %s -std=c++2c -fsyntax-only -verify
+
+// RUN: %clang_cc1 %s -std=c++14 -fsyntax-only -fexperimental-new-constant-interpreter -verify
+// RUN: %clang_cc1 %s -std=c++20 -fsyntax-only -fexperimental-new-constant-interpreter -verify
+// RUN: %clang_cc1 %s -std=c++2c -fsyntax-only -fexperimental-new-constant-interpreter -verify
 
 template <typename T>
 constexpr T foo(T a);   // expected-note {{declared here}}
@@ -14,3 +19,28 @@ template <typename T>
 constexpr T foo(T a) {
   return a;
 }
+
+#if __cplusplus > 202002L
+
+namespace GH115118 {
+
+struct foo {
+    // expected-note@-1 2{{while}}
+    foo(const foo&) = default;
+    foo(auto)
+        requires([]<int = 0>() -> bool { return true; }())
+        // expected-error@-1 {{non-constant expression}}
+        // expected-note@-2 {{undefined function}} \
+        // expected-note@-2 {{declared}}
+    {}
+};
+
+// FIXME: This will be fixed by https://github.com/llvm/llvm-project/pull/205557
+struct bar {
+    // expected-note@-1 {{while}}
+    foo x; // check that the lambda gets instantiated.
+};
+
+}  // namespace GH115118
+
+#endif

Comment on lines +23 to +44
#if __cplusplus > 202002L

namespace GH115118 {

struct foo {
// expected-note@-1 2{{while}}
foo(const foo&) = default;
foo(auto)
requires([]<int = 0>() -> bool { return true; }())
// expected-error@-1 {{non-constant expression}}
// expected-note@-2 {{undefined function}} \
// expected-note@-2 {{declared}}
{}
};

// FIXME: This will be fixed by https://github.com/llvm/llvm-project/pull/205557
struct bar {
// expected-note@-1 {{while}}
foo x; // check that the lambda gets instantiated.
};

} // namespace GH115118

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@katzdm I stole your test case here, so we don't have duplicated tests

@cor3ntin cor3ntin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change needs a release note.
Please add an entry to clang/docs/ReleaseNotes.md in the section the most adapted to the change, and referencing any Github issue this change fixes. Thanks!

@zyn0217

zyn0217 commented Jul 15, 2026

Copy link
Copy Markdown
Contributor Author

This change needs a release note. Please add an entry to clang/docs/ReleaseNotes.md in the section the most adapted to the change, and referencing any Github issue this change fixes. Thanks!

FYI I'm going to backport it because the assertion is introduced in clang 23 - we should have already suffered from the incorrect depth earlier but nothing fired.

@zyn0217
zyn0217 merged commit 3485d85 into llvm:main Jul 15, 2026
15 checks passed
@zyn0217 zyn0217 added this to the LLVM 23.x Release milestone Jul 15, 2026
@github-project-automation github-project-automation Bot moved this to Needs Triage in LLVM Release Status Jul 15, 2026
@github-project-automation github-project-automation Bot moved this from Needs Triage to Done in LLVM Release Status Jul 15, 2026
@zyn0217

zyn0217 commented Jul 15, 2026

Copy link
Copy Markdown
Contributor Author

/cherry-pick 3485d85

@llvmbot

llvmbot commented Jul 15, 2026

Copy link
Copy Markdown
Member

/pull-request #209712

pedroMVicente pushed a commit to pedroMVicente/llvm-project that referenced this pull request Jul 15, 2026
…ates (llvm#209693)

This fixes another case of member functions where we overlooked template
depths when only abbreviated template parameters are involved.

This mirrors previous fix cfb2520, but I don't intend to put it in
ParseTrailingRequiresClause because we might want the similar fix for
e.g. noexcept expressions, so let's keep it inline for future refactor.

The example comes from llvm#205557.
dyung pushed a commit to llvmbot/llvm-project that referenced this pull request Jul 16, 2026
…ates (llvm#209693)

This fixes another case of member functions where we overlooked template
depths when only abbreviated template parameters are involved.

This mirrors previous fix cfb2520, but I don't intend to put it in
ParseTrailingRequiresClause because we might want the similar fix for
e.g. noexcept expressions, so let's keep it inline for future refactor.

The example comes from llvm#205557.

(cherry picked from commit 3485d85)
dyung pushed a commit to llvmbot/llvm-project that referenced this pull request Jul 22, 2026
…ates (llvm#209693)

This fixes another case of member functions where we overlooked template
depths when only abbreviated template parameters are involved.

This mirrors previous fix cfb2520, but I don't intend to put it in
ParseTrailingRequiresClause because we might want the similar fix for
e.g. noexcept expressions, so let's keep it inline for future refactor.

The example comes from llvm#205557.

(cherry picked from commit 3485d85)
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 release:backport

Projects

Development

Successfully merging this pull request may close these issues.

3 participants