Skip to content

Commit 9addec3

Browse files
committed
[Archetype builder] Make compareDependentTypes() a more sane total ordering.
1 parent e305b20 commit 9addec3

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

lib/AST/ArchetypeBuilder.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -402,35 +402,46 @@ static int compareDependentTypes(
402402
if (int compareProtocols
403403
= ProtocolType::compareProtocols(&protoa, &protob))
404404
return compareProtocols;
405+
406+
// Error case: if we have two associated types with the same name in the
407+
// same protocol, just tie-break based on address.
408+
if (aa != ab)
409+
return aa < ab ? -1 : +1;
405410
} else {
406411
// A resolved archetype is always ordered before an unresolved one.
407412
return -1;
408413
}
414+
} else {
415+
// A resolved archetype is always ordered before an unresolved one.
416+
if (b->getResolvedAssociatedType())
417+
return +1;
409418
}
410419

411-
// A resolved archetype is always ordered before an unresolved one.
412-
if (b->getResolvedAssociatedType())
413-
return +1;
414-
415420
// Make sure typealiases are properly ordered, to avoid crashers.
416421
// FIXME: Ideally we would eliminate typealiases earlier.
417422
if (auto *aa = a->getTypeAliasDecl()) {
418423
if (auto *ab = b->getTypeAliasDecl()) {
419424
// - by protocol, so t_n_m.`P.T` < t_n_m.`Q.T` (given P < Q)
420-
auto protoa = aa->getDeclContext()->getAsProtocolOrProtocolExtensionContext();
421-
auto protob = ab->getDeclContext()->getAsProtocolOrProtocolExtensionContext();
425+
auto protoa =
426+
aa->getDeclContext()->getAsProtocolOrProtocolExtensionContext();
427+
auto protob =
428+
ab->getDeclContext()->getAsProtocolOrProtocolExtensionContext();
429+
422430
if (int compareProtocols
423431
= ProtocolType::compareProtocols(&protoa, &protob))
424432
return compareProtocols;
425-
}
426433

434+
// FIXME: Arbitrarily break the result here.
435+
if (aa != ab)
436+
return aa < ab ? -1 : +1;
437+
} else {
438+
// A resolved archetype is always ordered before an unresolved one.
439+
return -1;
440+
}
441+
} else if (b->getTypeAliasDecl()) {
427442
// A resolved archetype is always ordered before an unresolved one.
428-
return -1;
429-
}
430-
431-
// A resolved archetype is always ordered before an unresolved one.
432-
if (b->getTypeAliasDecl())
433443
return +1;
444+
}
434445

435446
// Along the error path where one or both of the potential archetypes was
436447
// renamed due to typo correction,

test/Generics/associated_type_typo.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@ func typoAssoc4<T : P2>(_: T) where T.Assocp2.assoc : P3 {}
3939
// CHECK-GENERIC-LABEL: .typoAssoc4@
4040
// CHECK-GENERIC-NEXT: Requirements:
4141
// CHECK-GENERIC-NEXT: T : P2 [explicit
42-
// CHECK-GENERIC-NEXT: T[.P2].AssocP2 == T[.P2].AssocP2 [redundant]
4342
// CHECK-GENERIC-NEXT: T[.P2].AssocP2 : P1 [protocol
44-
// CHECK-GENERIC-NEXT: T[.P2].AssocP2[.P1].Assoc == T[.P2].AssocP2[.P1].Assoc [redundant
43+
// CHECK-GENERIC-NEXT: T[.P2].AssocP2 == T[.P2].AssocP2 [redundant]
4544
// CHECK-GENERIC-NEXT: T[.P2].AssocP2[.P1].Assoc : P3 [explicit
46-
// CHECK-GENERIC-NEXT: T[.P2].AssocP2[.P1].Assoc == T[.P2].AssocP2[.P1].Assoc [redundant]
45+
// CHECK-GENERIC-NEXT: T[.P2].AssocP2[.P1].Assoc == T[.P2].AssocP2[.P1].Assoc [redundant
4746
// CHECK-GENERIC-NEXT: Generic signature
4847

4948
// <rdar://problem/19620340>
@@ -73,7 +72,6 @@ protocol Pattern {
7372
associatedtype Element : Equatable
7473

7574
// FIXME: Diagnostics here are very poor
76-
// expected-error@+4{{'C.Slice' does not have a member type named 'Iterator'; did you mean 'Slice'?}}
7775
// expected-error@+3{{'C' does not have a member type named 'Iterator'; did you mean 'Slice'?}}
7876
// expected-error@+2{{'C.Slice' does not have a member type named 'Element'; did you mean 'Slice'?}}
7977
// expected-error@+1{{'C.Slice' does not have a member type named 'Iterator'; did you mean 'Slice'?}}

0 commit comments

Comments
 (0)