Skip to content

Commit 852ac4c

Browse files
committed
[TypeJoin] Replace getKind() with is<>(), and some other things.
Add a comment explaining that the distributive law isn't generally true for lattices but should be for ours, and fix up the test case to not use the real stdlib Strideable protocol in a couple places.
1 parent 6f098ab commit 852ac4c

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

lib/AST/TypeJoinMeet.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,14 @@ CanType TypeJoin::visitGenericFunctionType(CanType second) {
350350
//
351351
// (A ^ B) v (C ^ D)
352352
// = (A v C) ^ (A v D) ^ (B v C) ^ (B v D)
353+
//
354+
// In general this law only applies to distributive lattices.
355+
//
356+
// In our case, this should be safe because our meet operation only
357+
// produces an existing nominal type when it is one of the operands of
358+
// the operation. So we can never arbitrarily climb down the lattice
359+
// in ways that would break distributivity.
360+
//
353361
CanType TypeJoin::computeProtocolCompositionJoin(ArrayRef<Type> firstMembers,
354362
ArrayRef<Type> secondMembers) {
355363
SmallVector<Type, 8> result;
@@ -388,13 +396,13 @@ CanType TypeJoin::visitProtocolCompositionType(CanType second) {
388396
assert(First != second);
389397

390398
// FIXME: Handle other types here.
391-
if (First->getKind() != TypeKind::Protocol &&
392-
First->getKind() != TypeKind::ProtocolComposition)
399+
if (!First->is<ProtocolType>() &&
400+
!First->is<ProtocolCompositionType>())
393401
return TheAnyType;
394402

395403
SmallVector<Type, 1> protocolType;
396404
ArrayRef<Type> firstMembers;
397-
if (First->getKind() == TypeKind::Protocol) {
405+
if (First->is<ProtocolType>()) {
398406
protocolType.push_back(First);
399407
firstMembers = protocolType;
400408
} else {
@@ -434,8 +442,8 @@ static bool isSupertypeOf(ProtocolDecl *super, ProtocolDecl *sub) {
434442
CanType TypeJoin::visitProtocolType(CanType second) {
435443
assert(First != second);
436444

437-
assert(First->getKind() != TypeKind::ProtocolComposition &&
438-
second->getKind() != TypeKind::ProtocolComposition);
445+
assert(!First->is<ProtocolCompositionType>() &&
446+
!second->is<ProtocolCompositionType>());
439447

440448
// FIXME: Handle other types here.
441449
if (First->getKind() != second->getKind())

test/Sema/type_join.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ expectEqualType(Builtin.type_join(FakeEquatable.self, FakeEquatable.self), FakeE
7575
expectEqualType(Builtin.type_join(FakeHashable.self, FakeEquatable.self), FakeEquatable.self)
7676
expectEqualType(Builtin.type_join(FakeEquatable.self, FakeHashable.self), FakeEquatable.self)
7777
expectEqualType(Builtin.type_join(FakeNumeric.self, FakeHashable.self), FakeEquatable.self)
78-
expectEqualType(Builtin.type_join((FakeHashable & Strideable).self, (FakeHashable & FakeNumeric).self),
78+
expectEqualType(Builtin.type_join((FakeHashable & FakeStrideable).self, (FakeHashable & FakeNumeric).self),
7979
FakeHashable.self)
80-
expectEqualType(Builtin.type_join((FakeNumeric & Strideable).self,
80+
expectEqualType(Builtin.type_join((FakeNumeric & FakeStrideable).self,
8181
(FakeHashable & FakeNumeric).self), FakeNumeric.self)
8282
expectEqualType(Builtin.type_join(FakeBinaryInteger.self, FakeFloatingPoint.self),
8383
(FakeHashable & FakeNumeric & FakeStrideable).self)

0 commit comments

Comments
 (0)