[llvm-branch-commits] [clang] release/22.x: [Clang] Fix a concept subsumption bug when template depths are adjusted (#186735) (PR #187226)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Mar 18 02:54:20 PDT 2026


https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/187226

Backport 13138b3cb90cda070e47d24f5f73f47dc2ad9c4a

Requested by: @zyn0217

>From ac3ecd060cd01ade5e9a8b48e499697b4c90d059 Mon Sep 17 00:00:00 2001
From: Younan Zhang <zyn7109 at gmail.com>
Date: Tue, 17 Mar 2026 08:23:52 +0800
Subject: [PATCH] [Clang] Fix a concept subsumption bug when template depths
 are adjusted (#186735)

We cannot reuse the cached normalization results if any template depth
adjustments (in subsumption checking) are involved.

Fixes https://github.com/llvm/llvm-project/issues/186624

(cherry picked from commit 13138b3cb90cda070e47d24f5f73f47dc2ad9c4a)
---
 clang/lib/Sema/SemaConcept.cpp       | 10 +++++++++-
 clang/test/SemaTemplate/concepts.cpp | 19 +++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaConcept.cpp b/clang/lib/Sema/SemaConcept.cpp
index f55f3a9a61ab8..e17332ac90980 100644
--- a/clang/lib/Sema/SemaConcept.cpp
+++ b/clang/lib/Sema/SemaConcept.cpp
@@ -2544,7 +2544,15 @@ bool Sema::IsAtLeastAsConstrained(const NamedDecl *D1,
   }
 
   SubsumptionChecker SC(*this);
-  std::optional<bool> Subsumes = SC.Subsumes(D1, AC1, D2, AC2);
+  // Associated declarations are used as a cache key in the event they were
+  // normalized earlier during concept checking. However we cannot reuse these
+  // cached results if any of the template depths have been adjusted.
+  const NamedDecl *DeclAC1 = D1, *DeclAC2 = D2;
+  if (Depth2 > Depth1)
+    DeclAC1 = nullptr;
+  else if (Depth1 > Depth2)
+    DeclAC2 = nullptr;
+  std::optional<bool> Subsumes = SC.Subsumes(DeclAC1, AC1, DeclAC2, AC2);
   if (!Subsumes) {
     // Normalization failed
     return true;
diff --git a/clang/test/SemaTemplate/concepts.cpp b/clang/test/SemaTemplate/concepts.cpp
index d93391baf9926..da6e29003f7f0 100644
--- a/clang/test/SemaTemplate/concepts.cpp
+++ b/clang/test/SemaTemplate/concepts.cpp
@@ -1659,6 +1659,25 @@ void foo() { call(""); }
 
 }
 
+namespace GH186624 {
+
+template <class T>
+concept C = __is_unsigned(T);
+
+template <C T>
+struct encoder_interface {};
+
+template <template <C> class CodecInterface, C T>
+CodecInterface<T>* create_codec() {
+  return nullptr;
+}
+
+encoder_interface<unsigned>* create_encoder() {
+  return create_codec<encoder_interface, unsigned>();
+}
+
+}
+
 namespace GH170856 {
 
 template <unsigned N, unsigned M> struct symbol_text {



More information about the llvm-branch-commits mailing list