Skip to content

Commit a53507f

Browse files
authored
[clang][bytecode] Fix a crash with covariant return types (#201354)
`Context::collectBaseOffset()` will assert if the passed-in classes are the same.
1 parent 70256df commit a53507f

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

clang/lib/AST/ByteCode/Interp.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,6 +2001,12 @@ bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func,
20012001
Overrider->getReturnType()->getPointeeType();
20022002
QualType InitialPointeeType =
20032003
InitialFunction->getReturnType()->getPointeeType();
2004+
2005+
// Nothing to do if the types already match.
2006+
if (S.getASTContext().hasSimilarType(InitialPointeeType,
2007+
OverriderPointeeType))
2008+
return true;
2009+
20042010
// We've called Overrider above, but calling code expects us to return what
20052011
// InitialFunction returned. According to the rules for covariant return
20062012
// types, what InitialFunction returns needs to be a base class of what

clang/lib/AST/ByteCode/Pointer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,8 @@ inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Pointer &P) {
931931
OS << " dummy";
932932
if (!P.isLive())
933933
OS << " dead";
934+
if (P.isBaseClass())
935+
OS << " base-class";
934936
return OS;
935937
}
936938

clang/test/AST/ByteCode/cxx20.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,22 @@ namespace IndirectFieldInitializer {
13651365
constexpr A() {}
13661366
};
13671367
static_assert(A().x == 3, "");
1368+
}
13681369

1370+
namespace Covariant {
1371+
struct A {
1372+
int a;
1373+
constexpr virtual const A* getA() const = 0;
1374+
};
1375+
struct B { int b; };
1376+
1377+
struct C: A, B {
1378+
constexpr const C* getA() const override {
1379+
return this;
1380+
}
1381+
};
1382+
constexpr C c{};
1383+
static_assert(c.getA() == &c);
13691384
}
13701385

13711386
namespace InvalidOMPRequiredSimdAlign {

0 commit comments

Comments
 (0)