Skip to content

Commit 0023e44

Browse files
committed
Don't consider marker protocols when mangling associated type refs.
Associated type references can be mangled without reference to the protocol they are in when there is only one protocol to which the base type conforms. Because marker protocols can never have associated types, don't consider them in this computation. This allows marker protocols to be added more freely to, e.g., generic type requirements. Fixes rdar://95994469.
1 parent 970457b commit 0023e44

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/AST/ASTMangler.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2979,6 +2979,21 @@ void ASTMangler::appendGenericSignatureParts(
29792979
appendOperator("r", StringRef(OpStorage.data(), OpStorage.size()));
29802980
}
29812981

2982+
/// Determine whether an associated type reference into the given set of
2983+
/// protocols is unambiguous.
2984+
static bool associatedTypeRefIsUnambiguous(ArrayRef<ProtocolDecl *> protocols) {
2985+
unsigned numProtocols = 0;
2986+
for (auto proto : protocols) {
2987+
// Skip marker protocols, which cannot have associated types.
2988+
if (proto->isMarkerProtocol())
2989+
continue;
2990+
2991+
++numProtocols;
2992+
}
2993+
2994+
return numProtocols <= 1;
2995+
}
2996+
29822997
// If the base type is known to have a single protocol conformance
29832998
// in the current generic context, then we don't need to disambiguate the
29842999
// associated type name by protocol.
@@ -2988,7 +3003,7 @@ ASTMangler::dropProtocolFromAssociatedType(DependentMemberType *dmt,
29883003
auto baseTy = dmt->getBase();
29893004
bool unambiguous =
29903005
(!dmt->getAssocType() ||
2991-
sig->getRequiredProtocols(baseTy).size() <= 1);
3006+
associatedTypeRefIsUnambiguous(sig->getRequiredProtocols(baseTy)));
29923007

29933008
if (auto *baseDMT = baseTy->getAs<DependentMemberType>())
29943009
baseTy = dropProtocolFromAssociatedType(baseDMT, sig);
@@ -3024,7 +3039,8 @@ void ASTMangler::appendAssociatedTypeName(DependentMemberType *dmt,
30243039
// in the current generic context, then we don't need to disambiguate the
30253040
// associated type name by protocol.
30263041
if (!OptimizeProtocolNames || !sig ||
3027-
sig->getRequiredProtocols(dmt->getBase()).size() > 1) {
3042+
!associatedTypeRefIsUnambiguous(
3043+
sig->getRequiredProtocols(dmt->getBase()))) {
30283044
appendAnyGenericType(assocTy->getProtocol());
30293045
}
30303046
return;

test/IRGen/marker_protocol.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,10 @@ struct Foo: SelfConstrainedProtocol {
7878
Foo(x: 123)
7979
}
8080
}
81+
82+
protocol P2 {
83+
associatedtype Foo
84+
}
85+
86+
// CHECK: define{{.*}}$s15marker_protocol3fooyy3FooQz_xtAA1PRzAA2P2RzlF
87+
func foo<T: P & P2>(_: T.Foo, _: T) { }

0 commit comments

Comments
 (0)