[clang] [Clang] Do not mark ambiguous specialization invalid. (PR #147275)
Corentin Jabot via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 7 03:51:35 PDT 2025
https://github.com/cor3ntin created https://github.com/llvm/llvm-project/pull/147275
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
>From 7149a73eb6f984f4575768cfe0ba12b6ee2bf1b6 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] [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 9a94c4bcd9980..190eca9a0546e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -901,6 +901,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}}
+}
More information about the cfe-commits
mailing list