[clang] [Clang] Handle unsatisfied NestedRequirement correctly (PR #223001)

Younan Zhang via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 11 10:51:47 PDT 2026


https://github.com/zyn0217 created https://github.com/llvm/llvm-project/pull/223001

Sema::BuildNestedRequirement(Expr *Constraint) built an incorrect NestedRequirement even when unsatisfied. It was never triggered because they were never diagnosed (we didn't diagnose concept details for static_assert until ab896c6c6f2) and the other error handlings were done properly in instantiator.

No release note because of regression-on-trunk.

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


>From cbbd57fba2f3f57f82b4598e8825601292fcbb49 Mon Sep 17 00:00:00 2001
From: Younan Zhang <zyn7109 at gmail.com>
Date: Sat, 12 Sep 2026 01:45:16 +0800
Subject: [PATCH] [Clang] Handle unsatisfied NestedRequirement correctly

Sema::BuildNestedRequirement(Expr *Constraint) built an incorrect
NestedRequirement even when unsatisfied. It was never triggered because
they are never diagnosed (we didn't diagnose concept details for static_assert
until ab896c6c6f2) and the other error handlings were done properly in
instantiator.
---
 clang/lib/Sema/SemaExprCXX.cpp                     |  9 +++++++++
 clang/test/SemaCXX/concept-crash-on-diagnostic.cpp | 13 +++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index cc9f85e2bb0cb..c3dee56d8af78 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -8175,6 +8175,15 @@ Sema::BuildNestedRequirement(Expr *Constraint) {
                                   /*TemplateArgs=*/{},
                                   Constraint->getSourceRange(), Satisfaction))
     return nullptr;
+
+  if (!Satisfaction.IsSatisfied) {
+    SmallString<128> Entity;
+    llvm::raw_svector_ostream OS(Entity);
+    Constraint->printPretty(OS, nullptr, SemaRef.getPrintingPolicy());
+    return new (Context) concepts::NestedRequirement(
+        Context, Context.backupStr(Entity), std::move(Satisfaction));
+  }
+
   return new (Context) concepts::NestedRequirement(Context, Constraint,
                                                    Satisfaction);
 }
diff --git a/clang/test/SemaCXX/concept-crash-on-diagnostic.cpp b/clang/test/SemaCXX/concept-crash-on-diagnostic.cpp
index d3ac650fd8c82..b0b1e1e3b2bb5 100644
--- a/clang/test/SemaCXX/concept-crash-on-diagnostic.cpp
+++ b/clang/test/SemaCXX/concept-crash-on-diagnostic.cpp
@@ -73,3 +73,16 @@ namespace GH138823 {
 
   void test() { bar(1); }
 }
+
+namespace GH222954 {
+
+template <typename T> struct foo {};
+template <typename T>
+concept bar = foo<T>::baz;
+
+static_assert(requires { requires bar<int>; });
+// expected-error at -1 {{static assertion failed}}
+// expected-note at -2 {{because 'int' does not satisfy 'bar'}}
+// expected-note at -4 {{because 'bar<int>' would be invalid}}
+
+}



More information about the cfe-commits mailing list