Skip to content

[Archetype builder] Clean up compareDependentTypes() #6917

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
merged 4 commits into from
Jan 19, 2017
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
3 changes: 3 additions & 0 deletions include/swift/AST/ArchetypeBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,9 @@ class ArchetypeBuilder::PotentialArchetype {
PotentialArchetype *getNestedType(Identifier Name,
ArchetypeBuilder &builder);

/// \brief Retrieve (or create) a nested type with a known associated type.
PotentialArchetype *getNestedType(AssociatedTypeDecl *assocType,
ArchetypeBuilder &builder);

/// \brief Retrieve (or build) the type corresponding to the potential
/// archetype within the given generic environment.
Expand Down
46 changes: 33 additions & 13 deletions lib/AST/ArchetypeBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,35 +402,46 @@ static int compareDependentTypes(
if (int compareProtocols
= ProtocolType::compareProtocols(&protoa, &protob))
return compareProtocols;

// Error case: if we have two associated types with the same name in the
// same protocol, just tie-break based on address.
if (aa != ab)
return aa < ab ? -1 : +1;
} else {
// A resolved archetype is always ordered before an unresolved one.
return -1;
}
} else {
// A resolved archetype is always ordered before an unresolved one.
if (b->getResolvedAssociatedType())
return +1;
}

// A resolved archetype is always ordered before an unresolved one.
if (b->getResolvedAssociatedType())
return +1;

// Make sure typealiases are properly ordered, to avoid crashers.
// FIXME: Ideally we would eliminate typealiases earlier.
if (auto *aa = a->getTypeAliasDecl()) {
if (auto *ab = b->getTypeAliasDecl()) {
// - by protocol, so t_n_m.`P.T` < t_n_m.`Q.T` (given P < Q)
auto protoa = aa->getDeclContext()->getAsProtocolOrProtocolExtensionContext();
auto protob = ab->getDeclContext()->getAsProtocolOrProtocolExtensionContext();
auto protoa =
aa->getDeclContext()->getAsProtocolOrProtocolExtensionContext();
auto protob =
ab->getDeclContext()->getAsProtocolOrProtocolExtensionContext();

if (int compareProtocols
= ProtocolType::compareProtocols(&protoa, &protob))
return compareProtocols;
}

// FIXME: Arbitrarily break the result here.
if (aa != ab)
return aa < ab ? -1 : +1;
} else {
// A resolved archetype is always ordered before an unresolved one.
return -1;
}
} else if (b->getTypeAliasDecl()) {
// A resolved archetype is always ordered before an unresolved one.
return -1;
}

// A resolved archetype is always ordered before an unresolved one.
if (b->getTypeAliasDecl())
return +1;
}

// Along the error path where one or both of the potential archetypes was
// renamed due to typo correction,
Expand Down Expand Up @@ -569,6 +580,12 @@ auto ArchetypeBuilder::PotentialArchetype::getNestedType(
return nested.front();
}

auto ArchetypeBuilder::PotentialArchetype::getNestedType(
AssociatedTypeDecl *assocType,
ArchetypeBuilder &builder) -> PotentialArchetype * {
return getNestedType(assocType->getName(), builder);
}

Type ArchetypeBuilder::PotentialArchetype::getTypeInContext(
ArchetypeBuilder &builder,
GenericEnvironment *genericEnv) {
Expand Down Expand Up @@ -913,6 +930,9 @@ auto ArchetypeBuilder::resolveArchetype(Type type) -> PotentialArchetype * {
if (!base)
return nullptr;

if (auto assocType = dependentMember->getAssocType())
return base->getNestedType(assocType, *this);

return base->getNestedType(dependentMember->getName(), *this);
}

Expand Down Expand Up @@ -1003,7 +1023,7 @@ bool ArchetypeBuilder::addConformanceRequirement(PotentialArchetype *PAT,
for (auto Member : Proto->getMembers()) {
if (auto AssocType = dyn_cast<AssociatedTypeDecl>(Member)) {
// Add requirements placed directly on this associated type.
auto AssocPA = T->getNestedType(AssocType->getName(), *this);
auto AssocPA = T->getNestedType(AssocType, *this);
if (AssocPA != T) {
if (addAbstractTypeParamRequirements(AssocType, AssocPA,
RequirementSource::Protocol,
Expand Down
29 changes: 18 additions & 11 deletions lib/AST/GenericSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,16 @@ getSubstitutions(TypeSubstitutionFn subs,
for (auto req: reqs) {
assert(req.getKind() == RequirementKind::Conformance);
auto protoType = req.getSecondType()->castTo<ProtocolType>();
// TODO: Error handling for failed conformance lookup.
currentConformances.push_back(
*lookupConformance(depTy->getCanonicalType(), currentReplacement,
protoType));
if (auto conformance = lookupConformance(depTy->getCanonicalType(),
currentReplacement,
protoType)) {
currentConformances.push_back(*conformance);
} else {
if (!currentReplacement->hasError())
currentReplacement = ErrorType::get(currentReplacement);
currentConformances.push_back(
ProtocolConformanceRef(protoType->getDecl()));
}
}

// Add it to the final substitution list.
Expand Down Expand Up @@ -605,21 +611,22 @@ CanType GenericSignature::getCanonicalTypeInContext(Type type,
return CanType(type);

// Replace non-canonical type parameters.
type = type.transform([&](Type component) -> Type {
if (!component->isTypeParameter()) return component;
type = type.transformRec([&](TypeBase *component) -> Optional<Type> {
if (!isa<GenericTypeParamType>(component) &&
!isa<DependentMemberType>(component))
return None;

// Resolve the potential archetype. This can be null in nested generic
// types, which we can't immediately canonicalize.
auto pa = builder.resolveArchetype(component);
if (!pa) return component;
auto pa = builder.resolveArchetype(Type(component));
if (!pa) return None;

auto rep = pa->getArchetypeAnchor();
if (rep->isConcreteType()) {
return getCanonicalTypeInContext(rep->getConcreteType(), builder);
} else {
return rep->getDependentType(getGenericParams(),
/*allowUnresolved*/ false);
}

return rep->getDependentType(getGenericParams(), /*allowUnresolved*/ false);
});

auto result = type->getCanonicalType();
Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/TypeCheckGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Type DependentGenericTypeResolver::resolveSelfAssociatedType(
assert(archetype && "Bad generic context nesting?");

return archetype
->getNestedType(assocType->getName(), Builder)
->getNestedType(assocType, Builder)
->getDependentType(/*FIXME: */{ }, /*allowUnresolved=*/true);
}

Expand Down Expand Up @@ -214,7 +214,7 @@ Type CompleteGenericTypeResolver::resolveDependentMemberType(
Type CompleteGenericTypeResolver::resolveSelfAssociatedType(
Type selfTy, AssociatedTypeDecl *assocType) {
return Builder.resolveArchetype(selfTy)
->getNestedType(assocType->getName(), Builder)
->getNestedType(assocType, Builder)
->getDependentType(/*FIXME: */{ }, /*allowUnresolved=*/false);
}

Expand Down
6 changes: 2 additions & 4 deletions test/Generics/associated_type_typo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ func typoAssoc4<T : P2>(_: T) where T.Assocp2.assoc : P3 {}
// CHECK-GENERIC-LABEL: .typoAssoc4@
// CHECK-GENERIC-NEXT: Requirements:
// CHECK-GENERIC-NEXT: T : P2 [explicit
// CHECK-GENERIC-NEXT: T[.P2].AssocP2 == T[.P2].AssocP2 [redundant]
// CHECK-GENERIC-NEXT: T[.P2].AssocP2 : P1 [protocol
// CHECK-GENERIC-NEXT: T[.P2].AssocP2[.P1].Assoc == T[.P2].AssocP2[.P1].Assoc [redundant
// CHECK-GENERIC-NEXT: T[.P2].AssocP2 == T[.P2].AssocP2 [redundant]
// CHECK-GENERIC-NEXT: T[.P2].AssocP2[.P1].Assoc : P3 [explicit
// CHECK-GENERIC-NEXT: T[.P2].AssocP2[.P1].Assoc == T[.P2].AssocP2[.P1].Assoc [redundant]
// CHECK-GENERIC-NEXT: T[.P2].AssocP2[.P1].Assoc == T[.P2].AssocP2[.P1].Assoc [redundant
// CHECK-GENERIC-NEXT: Generic signature

// <rdar://problem/19620340>
Expand Down Expand Up @@ -73,7 +72,6 @@ protocol Pattern {
associatedtype Element : Equatable

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