Skip to content

[IRGen] Fix extra inhabitant offset for simple single payload enums w… #67418

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

Merged
merged 1 commit into from
Jul 20, 2023
Merged
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
8 changes: 4 additions & 4 deletions lib/IRGen/TypeLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2225,7 +2225,7 @@ bool EnumTypeLayoutEntry::buildSinglePayloadRefCountString(
unsigned extraTagByteCount = 0;
uint64_t zeroTagValue = 0;
unsigned xiBitCount = 0;
unsigned xiOffset = 0;
unsigned xiBitOffset = 0;
bool isSimple = true;

auto &payloadTI = **cases[0]->getFixedTypeInfo();
Expand All @@ -2249,8 +2249,8 @@ bool EnumTypeLayoutEntry::buildSinglePayloadRefCountString(
isSimple = false;
} else {
xiBitCount = std::min(64u, mask.countPopulation());
xiOffset = mask.countTrailingZeros();
zeroTagValue = lowValue.extractBitsAsZExtValue(xiBitCount, xiOffset);
xiBitOffset = mask.countTrailingZeros();
zeroTagValue = lowValue.extractBitsAsZExtValue(xiBitCount, xiBitOffset);
}
}
}
Expand Down Expand Up @@ -2279,7 +2279,7 @@ bool EnumTypeLayoutEntry::buildSinglePayloadRefCountString(

if (isSimple) {
B.addSinglePayloadEnumSimple(zeroTagValue, xiTagValues, extraTagByteCount,
xiBitCount / 8, xiOffset, cases[0]);
xiBitCount / 8, xiBitOffset / 8, cases[0]);
} else {
auto tagFn = createFixedEnumLoadTag(IGM, *this);
B.addSinglePayloadEnumFN(tagFn, extraTagByteCount, cases[0]);
Expand Down
8 changes: 8 additions & 0 deletions test/Interpreter/Inputs/layout_string_witnesses_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,14 @@ public struct ComplexNesting<A, B, C, D> {
}
}

public enum SinglePayloadAnyHashableEnum {
case empty0
case empty1
case empty2
case empty3
case nonEmpty(AnyHashable)
}

internal enum InternalEnum {
case a(Int, AnyObject)
case b(Int)
Expand Down
31 changes: 31 additions & 0 deletions test/Interpreter/layout_string_witnesses_static.swift
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,37 @@ func testContainsSinglePayloadSimpleClassEnumEmpty() {

testContainsSinglePayloadSimpleClassEnumEmpty()

func testSinglePayloadAnyHashableEnum() {
let ptr = UnsafeMutablePointer<SinglePayloadAnyHashableEnum>.allocate(capacity: 1)

do {
let x = SinglePayloadAnyHashableEnum.empty0
testInit(ptr, to: x)
}

do {
// CHECK: empty0
if case .empty0 = ptr.pointee {
print("empty0")
}

let y = SinglePayloadAnyHashableEnum.empty0

testAssign(ptr, from: y)
}

// CHECK-NEXT: empty0
if case .empty0 = ptr.pointee {
print("empty0")
}

testDestroy(ptr)

ptr.deallocate()
}

testSinglePayloadAnyHashableEnum()

func testMultiPayloadEnum() {
let ptr = UnsafeMutablePointer<MultiPayloadEnumWrapper>.allocate(capacity: 1)

Expand Down