@@ -737,15 +737,15 @@ void ASTMangler::appendType(Type type) {
737
737
738
738
if (aliasTy->getSubstitutionMap ().hasAnySubstitutableParams ()) {
739
739
// Try to mangle the entire name as a substitution.
740
- if (tryMangleSubstitution (tybase))
740
+ if (tryMangleTypeSubstitution (tybase))
741
741
return ;
742
742
743
743
appendAnyGenericType (decl);
744
744
bool isFirstArgList = true ;
745
745
appendBoundGenericArgs (type, isFirstArgList);
746
746
appendRetroactiveConformances (type);
747
747
appendOperator (" G" );
748
- addSubstitution (type. getPointer () );
748
+ addTypeSubstitution (type);
749
749
return ;
750
750
}
751
751
@@ -840,7 +840,7 @@ void ASTMangler::appendType(Type type) {
840
840
Decl = type->getAnyGeneric ();
841
841
if (shouldMangleAsGeneric (type)) {
842
842
// Try to mangle the entire name as a substitution.
843
- if (tryMangleSubstitution (tybase))
843
+ if (tryMangleTypeSubstitution (tybase))
844
844
return ;
845
845
846
846
if (isStdlibType (Decl) && Decl->getName ().str () == " Optional" ) {
@@ -855,7 +855,7 @@ void ASTMangler::appendType(Type type) {
855
855
appendRetroactiveConformances (type);
856
856
appendOperator (" G" );
857
857
}
858
- addSubstitution (type. getPointer () );
858
+ addTypeSubstitution (type);
859
859
return ;
860
860
}
861
861
appendAnyGenericType (type->getAnyGeneric ());
@@ -901,7 +901,7 @@ void ASTMangler::appendType(Type type) {
901
901
902
902
case TypeKind::DependentMember: {
903
903
auto *DepTy = cast<DependentMemberType>(tybase);
904
- if (tryMangleSubstitution (DepTy))
904
+ if (tryMangleTypeSubstitution (DepTy))
905
905
return ;
906
906
907
907
bool isAssocTypeAtDepth = false ;
@@ -920,7 +920,7 @@ void ASTMangler::appendType(Type type) {
920
920
appendAssociatedTypeName (DepTy);
921
921
appendOperator (" qa" );
922
922
}
923
- addSubstitution (DepTy);
923
+ addTypeSubstitution (DepTy);
924
924
return ;
925
925
}
926
926
@@ -1633,7 +1633,7 @@ void ASTMangler::appendAnyGenericType(const GenericTypeDecl *decl) {
1633
1633
}
1634
1634
1635
1635
// Try to mangle the entire name as a substitution.
1636
- if (tryMangleSubstitution (key. getPointer () ))
1636
+ if (tryMangleTypeSubstitution (key))
1637
1637
return ;
1638
1638
1639
1639
// Try to mangle a symbolic reference for a nominal type.
@@ -1642,7 +1642,7 @@ void ASTMangler::appendAnyGenericType(const GenericTypeDecl *decl) {
1642
1642
if (nominal && (!CanSymbolicReference || CanSymbolicReference (nominal))) {
1643
1643
appendSymbolicReference (nominal);
1644
1644
// Substitutions can refer back to the symbolic reference.
1645
- addSubstitution (key. getPointer () );
1645
+ addTypeSubstitution (key);
1646
1646
return ;
1647
1647
}
1648
1648
}
@@ -1705,7 +1705,7 @@ void ASTMangler::appendAnyGenericType(const GenericTypeDecl *decl) {
1705
1705
}
1706
1706
}
1707
1707
1708
- addSubstitution (key. getPointer () );
1708
+ addTypeSubstitution (key);
1709
1709
}
1710
1710
1711
1711
void ASTMangler::appendFunction (AnyFunctionType *fn, bool isFunctionMangling) {
@@ -1941,7 +1941,7 @@ void ASTMangler::appendRequirement(const Requirement &reqt) {
1941
1941
1942
1942
if (auto *DT = FirstTy->getAs <DependentMemberType>()) {
1943
1943
bool isAssocTypeAtDepth = false ;
1944
- if (tryMangleSubstitution (DT)) {
1944
+ if (tryMangleTypeSubstitution (DT)) {
1945
1945
switch (reqt.getKind ()) {
1946
1946
case RequirementKind::Conformance:
1947
1947
return appendOperator (" RQ" );
@@ -1957,7 +1957,7 @@ void ASTMangler::appendRequirement(const Requirement &reqt) {
1957
1957
llvm_unreachable (" bad requirement type" );
1958
1958
}
1959
1959
GenericTypeParamType *gpBase = appendAssocType (DT, isAssocTypeAtDepth);
1960
- addSubstitution (DT);
1960
+ addTypeSubstitution (DT);
1961
1961
assert (gpBase);
1962
1962
switch (reqt.getKind ()) {
1963
1963
case RequirementKind::Conformance:
@@ -2043,24 +2043,54 @@ void ASTMangler::appendGenericSignatureParts(
2043
2043
appendOperator (" r" , StringRef (OpStorage.data (), OpStorage.size ()));
2044
2044
}
2045
2045
2046
+ // If the base type is known to have a single protocol conformance
2047
+ // in the current generic context, then we don't need to disambiguate the
2048
+ // associated type name by protocol.
2049
+ DependentMemberType *
2050
+ ASTMangler::dropProtocolFromAssociatedType (DependentMemberType *dmt) {
2051
+ auto baseTy = dmt->getBase ();
2052
+ bool unambiguous = (!dmt->getAssocType () ||
2053
+ CurGenericSignature->getConformsTo (baseTy).size () <= 1 );
2054
+
2055
+ if (auto *baseDMT = baseTy->getAs <DependentMemberType>())
2056
+ baseTy = dropProtocolFromAssociatedType (baseDMT);
2057
+
2058
+ if (unambiguous)
2059
+ return DependentMemberType::get (baseTy, dmt->getName ());
2060
+
2061
+ return DependentMemberType::get (baseTy, dmt->getAssocType ());
2062
+ }
2063
+
2064
+ Type
2065
+ ASTMangler::dropProtocolsFromAssociatedTypes (Type type) {
2066
+ if (!OptimizeProtocolNames || !CurGenericSignature)
2067
+ return type;
2068
+
2069
+ if (!type->hasDependentMember ())
2070
+ return type;
2071
+
2072
+ return type.transform ([&](Type t) -> Type {
2073
+ if (auto *dmt = dyn_cast<DependentMemberType>(t.getPointer ()))
2074
+ return dropProtocolFromAssociatedType (dmt);
2075
+ return t;
2076
+ });
2077
+ }
2078
+
2046
2079
void ASTMangler::appendAssociatedTypeName (DependentMemberType *dmt) {
2047
- auto assocTy = dmt->getAssocType ();
2048
-
2049
- // If the base type is known to have a single protocol conformance
2050
- // in the current generic context, then we don't need to disambiguate the
2051
- // associated type name by protocol.
2052
- // This can result in getting the same mangled string for different
2053
- // DependentMemberTypes. This is not a problem but re-mangling might do more
2054
- // aggressive substitutions, which means that the re-mangled name may differ
2055
- // from the original mangled name.
2056
- // FIXME: We ought to be able to get to the generic signature from a
2057
- // dependent type, but can't yet. Shouldn't need this side channel.
2058
-
2059
- appendIdentifier (assocTy->getName ().str ());
2060
- if (!OptimizeProtocolNames || !CurGenericSignature
2061
- || CurGenericSignature->getConformsTo (dmt->getBase ()).size () > 1 ) {
2062
- appendAnyGenericType (assocTy->getProtocol ());
2080
+ if (auto assocTy = dmt->getAssocType ()) {
2081
+ appendIdentifier (assocTy->getName ().str ());
2082
+
2083
+ // If the base type is known to have a single protocol conformance
2084
+ // in the current generic context, then we don't need to disambiguate the
2085
+ // associated type name by protocol.
2086
+ if (!OptimizeProtocolNames || !CurGenericSignature
2087
+ || CurGenericSignature->getConformsTo (dmt->getBase ()).size () > 1 ) {
2088
+ appendAnyGenericType (assocTy->getProtocol ());
2089
+ }
2090
+ return ;
2063
2091
}
2092
+
2093
+ appendIdentifier (dmt->getName ().str ());
2064
2094
}
2065
2095
2066
2096
void ASTMangler::appendClosureEntity (
0 commit comments