Skip to content

Fix ClusteredBitVector::fromAPInt for zero-sized vectors. #20582

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
Nov 15, 2018
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
5 changes: 5 additions & 0 deletions lib/Basic/ClusteredBitVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ ClusteredBitVector ClusteredBitVector::fromAPInt(const llvm::APInt &bits) {
}

llvm::APInt ClusteredBitVector::asAPInt() const {
if (size() == 0) {
// APInt doesn't like zero-bit values.
return llvm::APInt(1, 0);
}

if (isInlineAndAllClear()) {
return llvm::APInt(size(), 0);
} else {
Expand Down
18 changes: 12 additions & 6 deletions test/Interpreter/multi_payload_extra_inhabitant.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %empty-directory(%t)

// RUN: %target-build-swift -parse-stdlib -Xfrontend -verify-type-layout -Xfrontend SpareBitExtraInhabitants -Xfrontend -verify-type-layout -Xfrontend SpareBitSingleExtraInhabitant -Xfrontend -verify-type-layout -Xfrontend SpareBitNoExtraInhabitant -Xfrontend -verify-type-layout -Xfrontend SpareBitNoExtraInhabitant2 -Xfrontend -verify-type-layout -Xfrontend TwoTagExtraInhabitants -Xfrontend -verify-type-layout -Xfrontend ThreeTagExtraInhabitants -Xfrontend -verify-type-layout -Xfrontend NoTagExtraInhabitants -Xfrontend -verify-type-layout -Xfrontend DynamicExtraInhabitantsOneByte -Xfrontend -verify-type-layout -Xfrontend DynamicExtraInhabitantsTwoBytes -O -o %t/a.out %s
// RUN: %target-build-swift -parse-stdlib -Xfrontend -verify-type-layout -Xfrontend SpareBitExtraInhabitants -Xfrontend -verify-type-layout -Xfrontend SpareBitSingleExtraInhabitant -Xfrontend -verify-type-layout -Xfrontend SpareBitNoExtraInhabitant -Xfrontend -verify-type-layout -Xfrontend SpareBitNoExtraInhabitant2 -Xfrontend -verify-type-layout -Xfrontend TwoTagExtraInhabitants -Xfrontend -verify-type-layout -Xfrontend ThreeTagExtraInhabitants -Xfrontend -verify-type-layout -Xfrontend NoTagExtraInhabitants -Xfrontend -verify-type-layout -Xfrontend DynamicExtraInhabitantsNever -Xfrontend -verify-type-layout -Xfrontend DynamicExtraInhabitantsZeroBytes -Xfrontend -verify-type-layout -Xfrontend DynamicExtraInhabitantsOneByte -Xfrontend -verify-type-layout -Xfrontend DynamicExtraInhabitantsTwoBytes -O -o %t/a.out %s
// RUN: %target-run %t/a.out 2>&1

// Type layout verifier is only compiled into the runtime in asserts builds.
Expand Down Expand Up @@ -133,6 +133,8 @@ enum DynamicExtraInhabitants<T> {
case tagDIA, tagDIB, tagDIC, tagDID, tagDIE, tagDIF, tagDIG, tagDIH
}

typealias DynamicExtraInhabitantsNever = DynamicExtraInhabitants<Never>
typealias DynamicExtraInhabitantsZeroBytes = DynamicExtraInhabitants<()>
typealias DynamicExtraInhabitantsOneByte = DynamicExtraInhabitants<UInt8>
typealias DynamicExtraInhabitantsTwoBytes = DynamicExtraInhabitants<UInt16>

Expand Down Expand Up @@ -185,14 +187,18 @@ tests.test("types that have at least one extra inhabitant") {
expectHasExtraInhabitant(SpareBitSingleExtraInhabitant.self, nil: nil)
expectHasExtraInhabitant(TwoTagExtraInhabitants.self, nil: nil)
expectHasExtraInhabitant(ThreeTagExtraInhabitants.self, nil: nil)
expectHasExtraInhabitant(DynamicExtraInhabitantsNever.self, nil: nil)
expectHasExtraInhabitant(DynamicExtraInhabitantsZeroBytes.self, nil: nil)
expectHasExtraInhabitant(DynamicExtraInhabitantsOneByte.self, nil: nil)
expectHasExtraInhabitant(DynamicExtraInhabitantsTwoBytes.self, nil: nil)
}
tests.test("types that have at least two extra inhabitants") {
expectHasExtraInhabitant(SpareBitExtraInhabitants.self, nil: nil)
expectHasExtraInhabitant(TwoTagExtraInhabitants.self, nil: nil)
expectHasExtraInhabitant(ThreeTagExtraInhabitants.self, nil: nil)
expectHasExtraInhabitant(DynamicExtraInhabitantsOneByte.self, nil: nil)
expectHasExtraInhabitant(DynamicExtraInhabitantsTwoBytes.self, nil: nil)
expectHasAtLeastTwoExtraInhabitants(SpareBitExtraInhabitants.self, nil: nil, someNil: .some(nil))
expectHasAtLeastTwoExtraInhabitants(TwoTagExtraInhabitants.self, nil: nil, someNil: .some(nil))
expectHasAtLeastTwoExtraInhabitants(ThreeTagExtraInhabitants.self, nil: nil, someNil: .some(nil))
expectHasAtLeastTwoExtraInhabitants(DynamicExtraInhabitantsNever.self, nil: nil, someNil: .some(nil))
expectHasAtLeastTwoExtraInhabitants(DynamicExtraInhabitantsZeroBytes.self, nil: nil, someNil: .some(nil))
expectHasAtLeastTwoExtraInhabitants(DynamicExtraInhabitantsOneByte.self, nil: nil, someNil: .some(nil))
expectHasAtLeastTwoExtraInhabitants(DynamicExtraInhabitantsTwoBytes.self, nil: nil, someNil: .some(nil))
}
runAllTests()