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

Shafik Yaghmour via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 25 14:28:11 PDT 2023


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

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

>From 1877bccd38d15b74883d7bd24f4cacd477c9d820 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/cxx1z-copy-omission.cpp       | 9 +++++++++
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index f4eb02fd9570c2f..a6db3f3d0105742 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 deduction guide 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/cxx1z-copy-omission.cpp b/clang/test/SemaCXX/cxx1z-copy-omission.cpp
index a850cf6143cd480..819cba258915eef 100644
--- a/clang/test/SemaCXX/cxx1z-copy-omission.cpp
+++ b/clang/test/SemaCXX/cxx1z-copy-omission.cpp
@@ -171,3 +171,12 @@ namespace CtorTemplateBeatsNonTemplateConversionFn {
   Foo f(Derived d) { return d; } // expected-error {{invokes a deleted function}}
   Foo g(Derived d) { return Foo(d); } // ok, calls constructor
 }
+
+namespace GH65522 {
+template<typename A3>
+class B3 : A3 {
+  template<bool = C3<B3>()> // expected-warning 2{{use of function template name with no prior declaration in function call with explicit}}
+  B3();
+}; B3(); // expected-error {{deduction guide declaration without trailing return type}} \
+         // expected-note {{while building deduction guide here}}
+}



More information about the cfe-commits mailing list