Skip to content

[cxx-interop] Fix two inheritance crashes in IRGen. #61216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions lib/IRGen/GenStruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,47 @@ namespace {
}
}

void packIntoEnumPayload(IRGenFunction &IGF,
EnumPayload &payload,
Explosion &src,
unsigned startOffset) const override {
forEachNonEmptyBase([&](clang::QualType, clang::CharUnits offset,
clang::CharUnits size) {
auto &typeInfo = IGM.getOpaqueStorageTypeInfo(Size(size.getQuantity()),
Alignment(1));
typeInfo.packIntoEnumPayload(IGF, payload, src, offset.getQuantity());
});

for (auto &field : getFields()) {
if (!field.isEmpty()) {
unsigned offset = field.getFixedByteOffset().getValueInBits()
+ startOffset;
cast<LoadableTypeInfo>(field.getTypeInfo())
.packIntoEnumPayload(IGF, payload, src, offset);
}
}
}

void unpackFromEnumPayload(IRGenFunction &IGF, const EnumPayload &payload,
Explosion &dest, unsigned startOffset)
const override {
forEachNonEmptyBase([&](clang::QualType, clang::CharUnits offset,
clang::CharUnits size) {
auto &typeInfo = IGM.getOpaqueStorageTypeInfo(Size(size.getQuantity()),
Alignment(1));
typeInfo.unpackFromEnumPayload(IGF, payload, dest, offset.getQuantity());
});

for (auto &field : getFields()) {
if (!field.isEmpty()) {
unsigned offset = field.getFixedByteOffset().getValueInBits()
+ startOffset;
cast<LoadableTypeInfo>(field.getTypeInfo())
.unpackFromEnumPayload(IGF, payload, dest, offset);
}
}
}

llvm::NoneType getNonFixedOffsets(IRGenFunction &IGF) const {
return None;
}
Expand Down Expand Up @@ -1165,6 +1206,17 @@ class ClangRecordLowering {
}

void collectRecordFields() {
if (auto cxxRecord = dyn_cast<clang::CXXRecordDecl>(ClangDecl)) {
NextExplosionIndex += llvm::count_if(cxxRecord->bases(), [](auto base) {
auto baseType = base.getType().getCanonicalType();

auto baseRecord = cast<clang::RecordType>(baseType)->getDecl();
auto baseCxxRecord = cast<clang::CXXRecordDecl>(baseRecord);

return !baseCxxRecord->isEmpty();
});
}

if (ClangDecl->isUnion()) {
collectUnionFields();
} else {
Expand Down
6 changes: 6 additions & 0 deletions test/Interop/Cxx/class/inheritance/Inputs/fields.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ struct DerivedFromAll : HasOneField, DerivedWithOneField {
int f = 6;
};

struct OneField {
int value = 42;
};

struct DerivedFromOneField : OneField {};

// Non trivial types:

struct __attribute__((swift_attr("import_unsafe"))) NonTrivial {
Expand Down
23 changes: 23 additions & 0 deletions test/Interop/Cxx/class/inheritance/fields.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import Fields

var FieldsTestSuite = TestSuite("Getting and setting fields in base classes")

struct SwiftStructWrapper {
let value: DerivedFromOneField
}

func optionalDerivedFromAll() -> DerivedFromAll? { DerivedFromAll() }

FieldsTestSuite.test("Fields from derived from all") {
let derived = DerivedFromAll()
expectEqual(derived.a, 1)
Expand All @@ -31,6 +37,23 @@ FieldsTestSuite.test("Fields from derived from all") {
expectEqual(mutable.f, 48)
}

FieldsTestSuite.test("Optional") {
let derived = optionalDerivedFromAll()
expectEqual(derived!.a, 1)
expectEqual(derived!.b, 2)
expectEqual(derived!.c, 3)
expectEqual(derived!.d, 4)
expectEqual(derived!.e, 5)
expectEqual(derived!.f, 6)
}

FieldsTestSuite.test("Struct holding derived from one field") {
let derived = DerivedFromOneField()
let s = SwiftStructWrapper(value: derived)

expectEqual(s.value.value, 42)
}

FieldsTestSuite.test("Fields from derived from non trivial") {
let derived = NonTrivialDerivedFromAll()
expectEqual(derived.a, 1)
Expand Down