[llvm] [Clang] Fix crash when ill-formed code is treated as a deduction guide (PR #67373)

Shafik Yaghmour via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 29 08:47:48 PDT 2023


https://github.com/shafik updated https://github.com/llvm/llvm-project/pull/67373

>From beab5db738483795ecb0bace2842acdbb1c9869a Mon Sep 17 00:00:00 2001
From: Shafik Yaghmour <shafik.yaghmour at intel.com>
Date: Mon, 25 Sep 2023 13:56:43 -0700
Subject: [PATCH] [Clang] Fix crash when ill-formed code is treated as a
 deduction guide

In some cases where ill-formed code could be interpreted as a deduction guide
we can crash because we reach an unreachable path. This fixes this issue by
introducing a diagnostic instead.

Fixes: https://github.com/llvm/llvm-project/issues/65522
---
 clang/include/clang/Basic/DiagnosticSemaKinds.td |  2 ++
 clang/lib/Sema/SemaTemplateInstantiate.cpp       |  4 +++-
 clang/test/SemaCXX/gh65522.cpp                   | 11 +++++++++++
 3 files changed, 16 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaCXX/gh65522.cpp

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index f4eb02fd9570c2f..c3c1810fd7934a2 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5414,6 +5414,8 @@ def note_constraint_normalization_here : Note<
 def note_parameter_mapping_substitution_here : Note<
   "while substituting into concept arguments here; substitution failures not "
   "allowed in concept arguments">;
+def note_building_deduction_guide_here : Note<
+  "while building implicit deduction guide first needed here">;
 def note_lambda_substitution_here : Note<
   "while substituting into a lambda expression here">;
 def note_instantiation_contexts_suppressed : Note<
diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index 00a36696cf90450..1ed3df5a011bcb6 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -1075,7 +1075,9 @@ void Sema::PrintInstantiationStack() {
           << Active->InstantiationRange;
       break;
     case CodeSynthesisContext::BuildingDeductionGuides:
-      llvm_unreachable("unexpected deduction guide in instantiation stack");
+      Diags.Report(Active->PointOfInstantiation,
+                   diag::note_building_deduction_guide_here);
+      break;
     }
   }
 }
diff --git a/clang/test/SemaCXX/gh65522.cpp b/clang/test/SemaCXX/gh65522.cpp
new file mode 100644
index 000000000000000..2d6331b0372a31d
--- /dev/null
+++ b/clang/test/SemaCXX/gh65522.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -std=c++20 -Wc++17-compat -verify -Wno-unused %s
+
+class X {};
+
+template<typename T>
+class B3 { // expected-note {{candidate template ignored: could not match 'B3<T>' against 'int'}}
+  template<X x> B3(T); // expected-warning 2{{non-type template parameter of type 'X' is incompatible with C++ standards before C++20}} \
+                       // expected-note {{candidate template ignored: couldn't infer template argument 'x'}}
+};
+B3 b3 = 0; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'B3'}} \
+           // expected-note {{while building implicit deduction guide first needed here}}



More information about the llvm-commits mailing list