[clang] [Clang] Fix a concept subsumption bug when template depths are adjusted (PR #186735)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Mar 15 21:54:29 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Younan Zhang (zyn0217)
<details>
<summary>Changes</summary>
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
I hope this can be backported without breaking ABI
---
Full diff: https://github.com/llvm/llvm-project/pull/186735.diff
3 Files Affected:
- (modified) clang/include/clang/Sema/SemaConcept.h (+2-1)
- (modified) clang/lib/Sema/SemaConcept.cpp (+7-4)
- (modified) clang/test/SemaTemplate/concepts.cpp (+19)
``````````diff
diff --git a/clang/include/clang/Sema/SemaConcept.h b/clang/include/clang/Sema/SemaConcept.h
index bdd997b18cb08..e6644f1bd1bee 100644
--- a/clang/include/clang/Sema/SemaConcept.h
+++ b/clang/include/clang/Sema/SemaConcept.h
@@ -431,7 +431,8 @@ class SubsumptionChecker {
std::optional<bool> Subsumes(const NamedDecl *DP,
ArrayRef<AssociatedConstraint> P,
const NamedDecl *DQ,
- ArrayRef<AssociatedConstraint> Q);
+ ArrayRef<AssociatedConstraint> Q,
+ bool DepthAdjusted);
bool Subsumes(const NormalizedConstraint *P, const NormalizedConstraint *Q);
diff --git a/clang/lib/Sema/SemaConcept.cpp b/clang/lib/Sema/SemaConcept.cpp
index 38791940247cb..16cbec22c823b 100644
--- a/clang/lib/Sema/SemaConcept.cpp
+++ b/clang/lib/Sema/SemaConcept.cpp
@@ -2544,7 +2544,8 @@ bool Sema::IsAtLeastAsConstrained(const NamedDecl *D1,
}
SubsumptionChecker SC(*this);
- std::optional<bool> Subsumes = SC.Subsumes(D1, AC1, D2, AC2);
+ std::optional<bool> Subsumes =
+ SC.Subsumes(D1, AC1, D2, AC2, /*DepthAdjusted=*/Depth1 != Depth2);
if (!Subsumes) {
// Normalization failed
return true;
@@ -2778,14 +2779,16 @@ void SubsumptionChecker::AddUniqueClauseToFormula(Formula &F, Clause C) {
std::optional<bool> SubsumptionChecker::Subsumes(
const NamedDecl *DP, ArrayRef<AssociatedConstraint> P, const NamedDecl *DQ,
- ArrayRef<AssociatedConstraint> Q) {
+ ArrayRef<AssociatedConstraint> Q, bool DepthAdjusted) {
const NormalizedConstraint *PNormalized =
- SemaRef.getNormalizedAssociatedConstraints(DP, P);
+ SemaRef.getNormalizedAssociatedConstraints(DepthAdjusted ? nullptr : DP,
+ P);
if (!PNormalized)
return std::nullopt;
const NormalizedConstraint *QNormalized =
- SemaRef.getNormalizedAssociatedConstraints(DQ, Q);
+ SemaRef.getNormalizedAssociatedConstraints(DepthAdjusted ? nullptr : DQ,
+ Q);
if (!QNormalized)
return std::nullopt;
diff --git a/clang/test/SemaTemplate/concepts.cpp b/clang/test/SemaTemplate/concepts.cpp
index 1d2ada3e0a398..628499bebea83 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 {
``````````
</details>
https://github.com/llvm/llvm-project/pull/186735
More information about the cfe-commits
mailing list