Author: Shafik Yaghmour
Date: 2023-10-02T08:08:39-07:00
New Revision: f1fed124524151b73fbf153e946840730986ca6c

URL: 
https://github.com/llvm/llvm-project/commit/f1fed124524151b73fbf153e946840730986ca6c
DIFF: 
https://github.com/llvm/llvm-project/commit/f1fed124524151b73fbf153e946840730986ca6c.diff

LOG: [Clang] Fix crash when ill-formed code is treated as a deduction guide 
(#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

Added: 
    clang/test/SemaCXX/gh65522.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/include/clang/Basic/DiagnosticSemaKinds.td
    clang/lib/Sema/SemaTemplateInstantiate.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 5c70d5690a5d3f1..0c1023f63aae5d6 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -383,6 +383,10 @@ Bug Fixes to C++ Support
 - Fixed a bug causing destructors of constant-evaluated structured bindings
   initialized by array elements to be called in the wrong evaluation context.
 
+- Fix crash where ill-formed code was being treated as a deduction guide and
+  we now produce a diagnostic. Fixes:
+  (`#65522 <https://github.com/llvm/llvm-project/issues/65522>`_)
+
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
 - Fixed an import failure of recursive friend class template.

diff  --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 8c3815b5986c624..ac854a86a2fce23 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5416,6 +5416,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 8a74a5c0d6a236b..23de64080a070ca 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}}


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to