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

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


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/67373.diff


3 Files Affected:

- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+2) 
- (modified) clang/lib/Sema/SemaTemplateInstantiate.cpp (+3-1) 
- (modified) clang/test/SemaCXX/cxx1z-copy-omission.cpp (+9) 


``````````diff
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}}
+}

``````````

</details>


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


More information about the cfe-commits mailing list