Skip to content

Commit f1fed12

Browse files
authored
[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: #65522
1 parent 2cb99df commit f1fed12

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,10 @@ Bug Fixes to C++ Support
383383
- Fixed a bug causing destructors of constant-evaluated structured bindings
384384
initialized by array elements to be called in the wrong evaluation context.
385385

386+
- Fix crash where ill-formed code was being treated as a deduction guide and
387+
we now produce a diagnostic. Fixes:
388+
(`#65522 <https://github.com/llvm/llvm-project/issues/65522>`_)
389+
386390
Bug Fixes to AST Handling
387391
^^^^^^^^^^^^^^^^^^^^^^^^^
388392
- Fixed an import failure of recursive friend class template.

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5416,6 +5416,8 @@ def note_constraint_normalization_here : Note<
54165416
def note_parameter_mapping_substitution_here : Note<
54175417
"while substituting into concept arguments here; substitution failures not "
54185418
"allowed in concept arguments">;
5419+
def note_building_deduction_guide_here : Note<
5420+
"while building implicit deduction guide first needed here">;
54195421
def note_lambda_substitution_here : Note<
54205422
"while substituting into a lambda expression here">;
54215423
def note_instantiation_contexts_suppressed : Note<

clang/lib/Sema/SemaTemplateInstantiate.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,9 @@ void Sema::PrintInstantiationStack() {
10751075
<< Active->InstantiationRange;
10761076
break;
10771077
case CodeSynthesisContext::BuildingDeductionGuides:
1078-
llvm_unreachable("unexpected deduction guide in instantiation stack");
1078+
Diags.Report(Active->PointOfInstantiation,
1079+
diag::note_building_deduction_guide_here);
1080+
break;
10791081
}
10801082
}
10811083
}

clang/test/SemaCXX/gh65522.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang_cc1 -std=c++20 -Wc++17-compat -verify -Wno-unused %s
2+
3+
class X {};
4+
5+
template<typename T>
6+
class B3 { // expected-note {{candidate template ignored: could not match 'B3<T>' against 'int'}}
7+
template<X x> B3(T); // expected-warning 2{{non-type template parameter of type 'X' is incompatible with C++ standards before C++20}} \
8+
// expected-note {{candidate template ignored: couldn't infer template argument 'x'}}
9+
};
10+
B3 b3 = 0; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'B3'}} \
11+
// expected-note {{while building implicit deduction guide first needed here}}

0 commit comments

Comments
 (0)