Skip to content

Commit 5248550

Browse files
committed
Resolve collisions when typealias used in associatedtype constraint
Uses Anthony's fix (3c87252) to resolve collisions
1 parent 96a38f6 commit 5248550

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -565,9 +565,7 @@ Type TypeChecker::resolveTypeInContext(TypeDecl *typeDecl, DeclContext *foundDC,
565565

566566
if (selfType->is<GenericTypeParamType>()) {
567567
if (typeDecl->getDeclContext()->getSelfProtocolDecl()) {
568-
if (isa<AssociatedTypeDecl>(typeDecl) ||
569-
(isa<TypeAliasDecl>(typeDecl) &&
570-
!cast<TypeAliasDecl>(typeDecl)->isGeneric())) {
568+
if (isa<AssociatedTypeDecl>(typeDecl)) {
571569
// FIXME: We should use this lookup method for the Interface
572570
// stage too, but right now that causes problems with
573571
// Sequence.SubSequence vs Collection.SubSequence; the former
@@ -1295,10 +1293,23 @@ resolveTopLevelIdentTypeComponent(TypeResolution resolution,
12951293
// Otherwise, check for an ambiguity.
12961294
if (!resolution.areSameType(current, type)) {
12971295
isAmbiguous = true;
1298-
break;
1296+
// If a typealias collides with an associatedtype in generic requirement
1297+
// position, prefer the associated type.
1298+
if (options.getContext() == TypeResolverContext::GenericRequirement) {
1299+
if (isa<TypeAliasDecl>(currentDecl) && isa<AssociatedTypeDecl>(typeDecl)) {
1300+
current = type;
1301+
currentDecl = typeDecl;
1302+
currentDC = foundDC;
1303+
isAmbiguous = false;
1304+
} else if (isa<AssociatedTypeDecl>(currentDecl) &&
1305+
isa<TypeAliasDecl>(typeDecl)) {
1306+
isAmbiguous = false;
1307+
}
1308+
}
1309+
if (isAmbiguous) break;
12991310
}
13001311

1301-
// We have a found multiple type aliases that refer to the same thing.
1312+
// We have found multiple type aliases that refer to the same thing.
13021313
// Ignore the duplicate.
13031314
}
13041315

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: not %target-typecheck-verify-swift
2+
3+
protocol ProtocolA {
4+
associatedtype T1
5+
}
6+
7+
struct S<T> : ProtocolA {
8+
typealias T1 = T
9+
}
10+
11+
protocol ProtocolB: ProtocolA {
12+
associatedtype T2: ProtocolB where T2.T1 == T3.T1
13+
associatedtype X
14+
typealias T3 = S<X>
15+
}
16+

0 commit comments

Comments
 (0)