Skip to content

Commit d328512

Browse files
[Clang] Fix finding instantiated decls for class template specializations during instantiation (#72346)
This change aims to fix #70375 It appears to me that the logic here should be handling specializations in general, not just partial specialization. It also seems that both the comment before the block and the `isInstantiationOf(ClassTemplate, SpecTemplate)` below agree with my judgement. The issue might just be a mistake that someone mistaken specialization as a special case of partial specializations, while it's actually the other way around. Needs some experts to comment here if this is the right fix. The code that caused clang ICE is added as a test case.
1 parent eaffcc8 commit d328512

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,8 @@ Bug Fixes in This Version
608608

609609
- Fixed an issue that a benign assertion might hit when instantiating a pack expansion
610610
inside a lambda. (`#61460 <https://github.com/llvm/llvm-project/issues/61460>`_)
611+
- Fix crash during instantiation of some class template specializations within class
612+
templates. Fixes (`#70375 <https://github.com/llvm/llvm-project/issues/70375>`_)
611613

612614
Bug Fixes to Compiler Builtins
613615
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6207,13 +6207,13 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
62076207
return D;
62086208

62096209
// Determine whether this record is the "templated" declaration describing
6210-
// a class template or class template partial specialization.
6210+
// a class template or class template specialization.
62116211
ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
62126212
if (ClassTemplate)
62136213
ClassTemplate = ClassTemplate->getCanonicalDecl();
6214-
else if (ClassTemplatePartialSpecializationDecl *PartialSpec
6215-
= dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
6216-
ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl();
6214+
else if (ClassTemplateSpecializationDecl *Spec =
6215+
dyn_cast<ClassTemplateSpecializationDecl>(Record))
6216+
ClassTemplate = Spec->getSpecializedTemplate()->getCanonicalDecl();
62176217

62186218
// Walk the current context to find either the record or an instantiation of
62196219
// it.

clang/test/SemaCXX/template-specialization.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,36 @@ int main() {
1919
B::foo<4>(); // expected-note {{in instantiation of function template specialization 'B::foo<4>'}}
2020
return 0;
2121
}
22+
23+
namespace GH70375 {
24+
25+
template <typename Ty>
26+
struct S {
27+
static void bar() {
28+
Ty t;
29+
t.foo();
30+
}
31+
32+
static void take(Ty&) {}
33+
};
34+
35+
template <typename P>
36+
struct Outer {
37+
template <typename C>
38+
struct Inner;
39+
40+
using U = S<Inner<P>>;
41+
42+
template <>
43+
struct Inner<void> {
44+
void foo() {
45+
U::take(*this);
46+
}
47+
};
48+
};
49+
50+
void instantiate() {
51+
Outer<void>::U::bar();
52+
}
53+
54+
}

0 commit comments

Comments
 (0)