Skip to content

[5.0] [GSB] Avoid unresolved dependent member types in generic signatures. #14626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions lib/AST/GenericSignatureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5233,11 +5233,28 @@ void GenericSignatureBuilder::checkConformanceConstraints(
}
}

/// Compare dependent types for use in anchors.
///
/// FIXME: This is a hack that will go away once we eliminate potential
/// archetypes that refer to concrete type declarations.
static int compareAnchorDependentTypes(Type type1, Type type2) {
// We don't want any unresolved dependent member types to be anchors, so
// prefer that they don't get through.
bool hasUnresolvedDependentMember1 =
type1->findUnresolvedDependentMemberType() != nullptr;
bool hasUnresolvedDependentMember2 =
type2->findUnresolvedDependentMemberType() != nullptr;
if (hasUnresolvedDependentMember1 != hasUnresolvedDependentMember2)
return hasUnresolvedDependentMember2 ? -1 : +1;

return compareDependentTypes(type1, type2);
}

namespace swift {
bool operator<(const DerivedSameTypeComponent &lhs,
const DerivedSameTypeComponent &rhs) {
return compareDependentTypes(getUnresolvedType(lhs.anchor, { }),
getUnresolvedType(rhs.anchor, { })) < 0;
return compareAnchorDependentTypes(getUnresolvedType(lhs.anchor, { }),
getUnresolvedType(rhs.anchor, { })) < 0;
}
} // namespace swift

Expand Down Expand Up @@ -5373,7 +5390,7 @@ static void computeDerivedSameTypeComponents(
componentOf[depType] = componentIndex;

// If this is a better anchor, record it.
if (compareDependentTypes(
if (compareAnchorDependentTypes(
depType,
getUnresolvedType(components[componentIndex].anchor, { })) < 0)
components[componentIndex].anchor = pa;
Expand Down Expand Up @@ -5692,7 +5709,7 @@ static void collapseSameTypeComponents(
auto &newComponent = newComponents[newRepresentativeIndex];

// If the old component has a better anchor, keep it.
if (compareDependentTypes(
if (compareAnchorDependentTypes(
getUnresolvedType(oldComponent.anchor, { }),
getUnresolvedType(newComponent.anchor, { })) < 0)
newComponent.anchor = oldComponent.anchor;
Expand Down Expand Up @@ -6092,7 +6109,7 @@ static int compareSameTypeComponents(const SameTypeComponentRef *lhsPtr,
rhsPtr->first->derivedSameTypeComponents[rhsPtr->second].anchor,
{ });

return compareDependentTypes(lhsType, rhsType);
return compareAnchorDependentTypes(lhsType, rhsType);
}

void GenericSignatureBuilder::enumerateRequirements(
Expand Down Expand Up @@ -6321,6 +6338,9 @@ static void collectRequirements(GenericSignatureBuilder &builder,
if (depTy->hasError())
return;

assert(!depTy->findUnresolvedDependentMemberType() &&
"Unresolved dependent member type in requirements");

Type repTy;
if (auto concreteTy = type.dyn_cast<Type>()) {
// Maybe we were equated to a concrete or dependent type...
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %target-swift-frontend %s -emit-ir -o /dev/null
protocol S {
associatedtype I: IteratorProtocol
typealias E = I.Element
}

func foo<T: S>(_ s: T) -> Int where T.E == Int {
return 42
}