[clang] [Clang] Do not mark ambiguous specialization invalid. (PR #147275)

Corentin Jabot via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 7 07:04:50 PDT 2025


https://github.com/cor3ntin updated https://github.com/llvm/llvm-project/pull/147275

>From 346ac892d7575ac779295b9db11bc895f37f160e Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Mon, 7 Jul 2025 12:41:53 +0200
Subject: [PATCH 1/2] [Clang] Do not mark ambiguous specialization invalid.

When a specialization was ambiguous, we would mark it as invalid,
even if the specialization occured in an immediate context.

This would subsequently lead to scenarios where invalid
specialization produced no diagnostics, causing crashes
during codegen.

Fixes #51866
---
 clang/docs/ReleaseNotes.rst                |  1 +
 clang/lib/Sema/SemaTemplateInstantiate.cpp |  1 -
 clang/test/SemaTemplate/partial-order.cpp  | 26 ++++++++++++++++++++++
 3 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a6be59f1d6bd7..9dea124e47c31 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -903,6 +903,7 @@ Bug Fixes to C++ Support
 - Fix a bug where private access specifier of overloaded function not respected. (#GH107629)
 - Correctly handle allocations in the condition of a ``if constexpr``.(#GH120197) (#GH134820)
 - Fixed a crash when handling invalid member using-declaration in C++20+ mode. (#GH63254)
+- Fix a crash when trying to instantiate an ambiguous specialization. (#GH51866)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index a1c7143e3b874..9140f00fcd740 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -4111,7 +4111,6 @@ static ActionResult<CXXRecordDecl *> getPatternForClassTemplateSpecialization(
         if (Ambiguous) {
           // Partial ordering did not produce a clear winner. Complain.
           Inst.Clear();
-          ClassTemplateSpec->setInvalidDecl();
           S.Diag(PointOfInstantiation,
                  diag::err_partial_spec_ordering_ambiguous)
               << ClassTemplateSpec;
diff --git a/clang/test/SemaTemplate/partial-order.cpp b/clang/test/SemaTemplate/partial-order.cpp
index db2624d1766bc..2619524b147ae 100644
--- a/clang/test/SemaTemplate/partial-order.cpp
+++ b/clang/test/SemaTemplate/partial-order.cpp
@@ -47,3 +47,29 @@ namespace GH132562 {
     // expected-note at -2 {{value of type 'const J' is not implicitly convertible to 'int'}}
   } // namespace t3
 } // namespace GH132562
+
+namespace GH51866 {
+
+template <class> struct Trait;
+template <class T>
+    requires T::one
+struct Trait<T> {}; // #gh51866-one
+template <class T>
+    requires T::two
+struct Trait<T> {}; // #gh51866-two
+
+struct Y {
+    static constexpr bool one = true;
+    static constexpr bool two = true;
+};
+
+template <class T>
+concept C = sizeof(Trait<T>) != 0;
+
+static_assert(!C<Y>);
+
+Trait<Y> t;
+// expected-error at -1{{ambiguous partial specializations of 'Trait<GH51866::Y>'}}
+// expected-note@#gh51866-one{{partial specialization matches}}
+// expected-note@#gh51866-two{{partial specialization matches}}
+}

>From e648ea731ab7fafde2d82ae8eef30739f522e0a8 Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Mon, 7 Jul 2025 16:04:23 +0200
Subject: [PATCH 2/2] marck the class as invalid outside of immediate contexts

---
 clang/lib/Sema/SemaTemplateInstantiate.cpp | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index 9140f00fcd740..cb1e417b82b31 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -4111,6 +4111,10 @@ static ActionResult<CXXRecordDecl *> getPatternForClassTemplateSpecialization(
         if (Ambiguous) {
           // Partial ordering did not produce a clear winner. Complain.
           Inst.Clear();
+
+          if (!S.isSFINAEContext())
+            ClassTemplateSpec->setInvalidDecl();
+
           S.Diag(PointOfInstantiation,
                  diag::err_partial_spec_ordering_ambiguous)
               << ClassTemplateSpec;



More information about the cfe-commits mailing list