Skip to content

Commit 6c5074f

Browse files
authored
Merge pull request #67749 from slavapestov/tuple-conformance-test
AST/Sema: Fix a couple of minor issues with tuple conformances and add a new test case
2 parents a4c5b9d + 979acd9 commit 6c5074f

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

lib/AST/Type.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ bool CanType::isReferenceTypeImpl(CanType type, const GenericSignatureImpl *sig,
261261
case TypeKind::PackExpansion:
262262
case TypeKind::PackElement:
263263
case TypeKind::SILPack:
264+
case TypeKind::BuiltinTuple:
264265
#define REF_STORAGE(Name, ...) \
265266
case TypeKind::Name##Storage:
266267
#include "swift/AST/ReferenceStorage.def"
@@ -270,8 +271,6 @@ bool CanType::isReferenceTypeImpl(CanType type, const GenericSignatureImpl *sig,
270271
case TypeKind::DependentMember:
271272
assert(sig && "dependent types can't answer reference semantics query");
272273
return sig->requiresClass(type);
273-
case TypeKind::BuiltinTuple:
274-
llvm_unreachable("Should not get a BuiltinTupleType here");
275274
}
276275

277276
llvm_unreachable("Unhandled type kind!");

lib/Sema/TypeCheckDecl.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,10 +2029,13 @@ bool swift::isMemberOperator(FuncDecl *decl, Type type) {
20292029
return true;
20302030

20312031
auto *DC = decl->getDeclContext();
2032+
20322033
auto selfNominal = DC->getSelfNominalTypeDecl();
20332034

20342035
// Check the parameters for a reference to 'Self'.
20352036
bool isProtocol = isa_and_nonnull<ProtocolDecl>(selfNominal);
2037+
bool isTuple = isa_and_nonnull<BuiltinTupleDecl>(selfNominal);
2038+
20362039
for (auto param : *decl->getParameters()) {
20372040
// Look through a metatype reference, if there is one.
20382041
auto paramType = param->getInterfaceType()->getMetatypeInstanceType();
@@ -2057,11 +2060,12 @@ bool swift::isMemberOperator(FuncDecl *decl, Type type) {
20572060
if (selfNominal == existential->getConstraintType()->getAnyNominal())
20582061
return true;
20592062
}
2063+
}
20602064

2061-
// For a protocol, is it the 'Self' type parameter?
2062-
if (auto genericParam = paramType->getAs<GenericTypeParamType>())
2063-
if (genericParam->isEqual(DC->getSelfInterfaceType()))
2064-
return true;
2065+
if (isProtocol || isTuple) {
2066+
// For a protocol or tuple extension, is it the 'Self' type parameter?
2067+
if (paramType->isEqual(DC->getSelfInterfaceType()))
2068+
return true;
20652069
}
20662070
}
20672071

test/Generics/tuple-conformances.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,28 @@ func useConformance() {
3434

3535
(1, 2, 3).f()
3636
}
37+
38+
////
39+
40+
extension Builtin.TheTupleType: Equatable where repeat each Elements: Equatable {
41+
public static func ==(lhs: Self, rhs: Self) -> Bool {
42+
var result = true
43+
func update<E: Equatable>(lhs: E, rhs: E) {
44+
result = result && (lhs == rhs)
45+
}
46+
47+
repeat update(lhs: each lhs, rhs: each rhs)
48+
return result
49+
}
50+
}
51+
52+
extension Builtin.TheTupleType: Hashable where repeat each Elements: Hashable {
53+
public func hash(into hasher: inout Hasher) {
54+
repeat (each self).hash(into: &hasher)
55+
}
56+
57+
// FIXME: This should be unnecessary
58+
public var hashValue: Int {
59+
return 0
60+
}
61+
}

0 commit comments

Comments
 (0)