[clang][bytecode] Fix a crash with covariant return types#201354
Open
tbaederr wants to merge 1 commit into
Open
[clang][bytecode] Fix a crash with covariant return types#201354tbaederr wants to merge 1 commit into
tbaederr wants to merge 1 commit into
Conversation
`Context::collectBaseOffset()` will assert if the passed-in classes are the same.
|
@llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) Changes
Full diff: https://github.com/llvm/llvm-project/pull/201354.diff 3 Files Affected:
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 6bcebf3ee892b..092c054857aee 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -2000,6 +2000,12 @@ bool CallVirt(InterpState &S, CodePtr OpPC, const Function *Func,
Overrider->getReturnType()->getPointeeType();
QualType InitialPointeeType =
InitialFunction->getReturnType()->getPointeeType();
+
+ // Nothing to do if the types already match.
+ if (S.getASTContext().hasSimilarType(InitialPointeeType,
+ OverriderPointeeType))
+ return true;
+
// We've called Overrider above, but calling code expects us to return what
// InitialFunction returned. According to the rules for covariant return
// types, what InitialFunction returns needs to be a base class of what
diff --git a/clang/lib/AST/ByteCode/Pointer.h b/clang/lib/AST/ByteCode/Pointer.h
index b48f880d8796b..a77918f667fd3 100644
--- a/clang/lib/AST/ByteCode/Pointer.h
+++ b/clang/lib/AST/ByteCode/Pointer.h
@@ -931,6 +931,8 @@ inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Pointer &P) {
OS << " dummy";
if (!P.isLive())
OS << " dead";
+ if (P.isBaseClass())
+ OS << " base-class";
return OS;
}
diff --git a/clang/test/AST/ByteCode/cxx20.cpp b/clang/test/AST/ByteCode/cxx20.cpp
index d3e6265a9cbe7..3f8278643a50f 100644
--- a/clang/test/AST/ByteCode/cxx20.cpp
+++ b/clang/test/AST/ByteCode/cxx20.cpp
@@ -1365,5 +1365,20 @@ namespace IndirectFieldInitializer {
constexpr A() {}
};
static_assert(A().x == 3, "");
+}
+namespace Covariant {
+ struct A {
+ int a;
+ constexpr virtual const A* getA() const = 0;
+ };
+ struct B { int b; };
+
+ struct C: A, B {
+ constexpr const C* getA() const override {
+ return this;
+ }
+ };
+ constexpr C c{};
+ static_assert(c.getA() == &c);
}
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context::collectBaseOffset()will assert if the passed-in classes are the same.