Skip to content

Commit 93d8599

Browse files
committed
Generalize extra inhabitants of tuples.
Like we did for structs, make it so that tuple types can also get extra inhabitants from whichever element with the most, not only the first. This lets us move all of the extra inhabitant handling functionality between structs and tuples in IRGen up to the common RecordTypeInfo CRTP base.
1 parent 541c48f commit 93d8599

File tree

10 files changed

+415
-396
lines changed

10 files changed

+415
-396
lines changed

foo.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
enum SinglePayloadGenericEnumWithDefaultMirror<T, U> {
3+
case Well
4+
case Faucet
5+
case Pipe(T, U)
6+
}
7+
8+
func foo(x: Int, y: [Int], out: (SinglePayloadGenericEnumWithDefaultMirror<Int, [Int]>) -> ()) {
9+
out(.Well)
10+
out(.Faucet)
11+
out(.Pipe(x, y))
12+
}
13+
14+
func bar<T, U>(_ x: SinglePayloadGenericEnumWithDefaultMirror<T, U>) {
15+
switch x {
16+
case .Well:
17+
print("well")
18+
case .Faucet:
19+
print("faucet")
20+
case .Pipe:
21+
print("pipe")
22+
}
23+
}
24+
25+
foo(x: 1, y: [1,2,3], out: bar)

include/swift/ABI/Metadata.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ struct TargetTupleTypeMetadata : public TargetMetadata<Runtime> {
14431443
using StoredSize = typename Runtime::StoredSize;
14441444
TargetTupleTypeMetadata() = default;
14451445
constexpr TargetTupleTypeMetadata(const TargetMetadata<Runtime> &base,
1446-
StoredSize numElements,
1446+
uint32_t numElements,
14471447
TargetPointer<Runtime, const char> labels)
14481448
: TargetMetadata<Runtime>(base),
14491449
NumElements(numElements),
@@ -1487,14 +1487,19 @@ struct TargetTupleTypeMetadata : public TargetMetadata<Runtime> {
14871487
return getElements()[i];
14881488
}
14891489

1490-
static constexpr StoredSize OffsetToNumElements = sizeof(TargetMetadata<Runtime>);
1491-
1490+
static constexpr StoredSize getOffsetToNumElements();
14921491
static bool classof(const TargetMetadata<Runtime> *metadata) {
14931492
return metadata->getKind() == MetadataKind::Tuple;
14941493
}
14951494
};
14961495
using TupleTypeMetadata = TargetTupleTypeMetadata<InProcess>;
14971496

1497+
template <typename Runtime>
1498+
constexpr inline auto
1499+
TargetTupleTypeMetadata<Runtime>::getOffsetToNumElements() -> StoredSize {
1500+
return offsetof(TargetTupleTypeMetadata<Runtime>, NumElements);
1501+
}
1502+
14981503
template <typename Runtime> struct TargetProtocolDescriptor;
14991504

15001505
#if SWIFT_OBJC_INTEROP

include/swift/ABI/MetadataValues.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ class TargetExtraInhabitantFlags {
244244

245245
public:
246246
constexpr TargetExtraInhabitantFlags() : Data(0) {}
247-
248247
/// The number of extra inhabitants in the type's representation.
249248
int getNumExtraInhabitants() const { return Data & NumExtraInhabitantsMask; }
250249

include/swift/Remote/MetadataReader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,8 +1087,8 @@ class MetadataReader {
10871087
return _readMetadata<TargetStructMetadata>(address);
10881088
case MetadataKind::Tuple: {
10891089
auto numElementsAddress = address +
1090-
TargetTupleTypeMetadata<Runtime>::OffsetToNumElements;
1091-
StoredSize numElements;
1090+
TargetTupleTypeMetadata<Runtime>::getOffsetToNumElements();
1091+
uint32_t numElements;
10921092
if (!Reader->readInteger(RemoteAddress(numElementsAddress),
10931093
&numElements))
10941094
return nullptr;

0 commit comments

Comments
 (0)