Skip to content

Commit e937510

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 db08503 commit e937510

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
@@ -2925,6 +2925,21 @@ void ASTMangler::appendGenericSignatureParts(
29252925
appendOperator("r", StringRef(OpStorage.data(), OpStorage.size()));
29262926
}
29272927

2928+
/// Determine whether an associated type reference into the given set of
2929+
/// protocols is unambiguous.
2930+
static bool associatedTypeRefIsUnambiguous(ArrayRef<ProtocolDecl *> protocols) {
2931+
unsigned numProtocols = 0;
2932+
for (auto proto : protocols) {
2933+
// Skip marker protocols, which cannot have associated types.
2934+
if (proto->isMarkerProtocol())
2935+
continue;
2936+
2937+
++numProtocols;
2938+
}
2939+
2940+
return numProtocols <= 1;
2941+
}
2942+
29282943
// If the base type is known to have a single protocol conformance
29292944
// in the current generic context, then we don't need to disambiguate the
29302945
// associated type name by protocol.
@@ -2934,7 +2949,7 @@ ASTMangler::dropProtocolFromAssociatedType(DependentMemberType *dmt,
29342949
auto baseTy = dmt->getBase();
29352950
bool unambiguous =
29362951
(!dmt->getAssocType() ||
2937-
sig->getRequiredProtocols(baseTy).size() <= 1);
2952+
associatedTypeRefIsUnambiguous(sig->getRequiredProtocols(baseTy)));
29382953

29392954
if (auto *baseDMT = baseTy->getAs<DependentMemberType>())
29402955
baseTy = dropProtocolFromAssociatedType(baseDMT, sig);
@@ -2970,7 +2985,8 @@ void ASTMangler::appendAssociatedTypeName(DependentMemberType *dmt,
29702985
// in the current generic context, then we don't need to disambiguate the
29712986
// associated type name by protocol.
29722987
if (!OptimizeProtocolNames || !sig ||
2973-
sig->getRequiredProtocols(dmt->getBase()).size() > 1) {
2988+
!associatedTypeRefIsUnambiguous(
2989+
sig->getRequiredProtocols(dmt->getBase()))) {
29742990
appendAnyGenericType(assocTy->getProtocol());
29752991
}
29762992
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)