Skip to content

Commit 01710aa

Browse files
authored
[clang] Fix cast for injected types in case name lookup for dependent bases (#119024)
An assertion failure occurs in Clang when attempting to compile such an example: ```c++ template <typename, typename, bool> struct MozPromise { class Private; private: int mMagic4 = 42; }; template <typename ResolveValueT, typename RejectValueT, bool IsExclusive> struct MozPromise<ResolveValueT, RejectValueT, IsExclusive>::Private : MozPromise { void SetTaskPriority() { mMagic4 ; } }; ``` Output: ``` clang: llvm-project/llvm/include/llvm/Support/Casting.h:566: decltype(auto) llvm::cast(const From&) [with To = clang::RecordType; From = clang::QualType]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed. ``` The reason is in the incorrect way of casting types when searching for names in base classes ```c++ return Specifier->getType()->castAs<RecordType>()->getDecl()->getCanonicalDecl() == BaseRecord; ``` It loses injected types for template class names. This patch provides fix for such cases
1 parent 98b694b commit 01710aa

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

clang/lib/AST/CXXInheritance.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ bool CXXRecordDecl::FindBaseClass(const CXXBaseSpecifier *Specifier,
368368
const CXXRecordDecl *BaseRecord) {
369369
assert(BaseRecord->getCanonicalDecl() == BaseRecord &&
370370
"User data for FindBaseClass is not canonical!");
371-
return Specifier->getType()->castAs<RecordType>()->getDecl()
372-
->getCanonicalDecl() == BaseRecord;
371+
return cast<CXXRecordDecl>(Specifier->getType()->getAsRecordDecl())
372+
->getCanonicalDecl() == BaseRecord;
373373
}
374374

375375
bool CXXRecordDecl::FindVirtualBaseClass(const CXXBaseSpecifier *Specifier,
@@ -378,8 +378,8 @@ bool CXXRecordDecl::FindVirtualBaseClass(const CXXBaseSpecifier *Specifier,
378378
assert(BaseRecord->getCanonicalDecl() == BaseRecord &&
379379
"User data for FindBaseClass is not canonical!");
380380
return Specifier->isVirtual() &&
381-
Specifier->getType()->castAs<RecordType>()->getDecl()
382-
->getCanonicalDecl() == BaseRecord;
381+
cast<CXXRecordDecl>(Specifier->getType()->getAsRecordDecl())
382+
->getCanonicalDecl() == BaseRecord;
383383
}
384384

385385
static bool isOrdinaryMember(const NamedDecl *ND) {
@@ -692,7 +692,7 @@ AddIndirectPrimaryBases(const CXXRecordDecl *RD, ASTContext &Context,
692692
"Cannot get indirect primary bases for class with dependent bases.");
693693

694694
const CXXRecordDecl *BaseDecl =
695-
cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
695+
cast<CXXRecordDecl>(I.getType()->getAsRecordDecl());
696696

697697
// Only bases with virtual bases participate in computing the
698698
// indirect primary virtual base classes.
@@ -714,7 +714,7 @@ CXXRecordDecl::getIndirectPrimaryBases(CXXIndirectPrimaryBaseSet& Bases) const {
714714
"Cannot get indirect primary bases for class with dependent bases.");
715715

716716
const CXXRecordDecl *BaseDecl =
717-
cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
717+
cast<CXXRecordDecl>(I.getType()->getAsRecordDecl());
718718

719719
// Only bases with virtual bases participate in computing the
720720
// indirect primary virtual base classes.

clang/test/CXX/drs/cwg5xx.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,11 @@ namespace cwg591 { // cwg591: 20
12091209
};
12101210
};
12111211

1212+
template <typename, bool> struct M {
1213+
class P;
1214+
int M;
1215+
};
1216+
12121217
template<typename T> struct A<T>::B::C : A<T> {
12131218
M m;
12141219
};
@@ -1224,6 +1229,10 @@ namespace cwg591 { // cwg591: 20
12241229
M m;
12251230
};
12261231

1232+
template<typename T, bool B> class M<T,B>::P : M {
1233+
int foo() { (void) M; }
1234+
};
1235+
12271236
template<typename T> struct A<T>::B::D : A<T*> {
12281237
M m;
12291238
// expected-error@-1 {{field has incomplete type 'M' (aka 'void'}}

0 commit comments

Comments
 (0)