Skip to content

[GSB] *Almost* eliminate the "AlwaysPartial" archetype resolution kind #9981

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 2 commits into from
May 30, 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: 1 addition & 2 deletions lib/AST/GenericSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -869,11 +869,10 @@ ConformanceAccessPath GenericSignature::getConformanceAccessPath(
Type storedType = eraseAssociatedTypes(source->getStoredType());

// Dig out the potential archetype for this stored type.
// FIXME: CompleteWellFormed here?
auto pa =
reqSigBuilder.resolveArchetype(
storedType,
ArchetypeResolutionKind::AlwaysPartial);
ArchetypeResolutionKind::CompleteWellFormed);
auto equivClass = pa->getOrCreateEquivalenceClass();

// Find the conformance of this potential archetype to the protocol in
Expand Down
29 changes: 29 additions & 0 deletions lib/AST/GenericSignatureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4759,6 +4759,27 @@ void GenericSignatureBuilder::checkSameTypeConstraints(
}
}

/// Resolve any unresolved dependent member types using the given builder.
static Type resolveDependentMemberTypes(GenericSignatureBuilder &builder,
Type type) {
if (!type->hasTypeParameter()) return type;

return type.transformRec([&builder](TypeBase *type) -> Optional<Type> {
if (auto depTy = dyn_cast<DependentMemberType>(type)) {
if (depTy->getAssocType()) return None;

auto pa = builder.resolveArchetype(
type, ArchetypeResolutionKind::CompleteWellFormed);
if (!pa)
return ErrorType::get(depTy);

return pa->getDependentType({ }, /*allowUnresolved=*/false);
}

return None;
});
}

void GenericSignatureBuilder::checkConcreteTypeConstraints(
ArrayRef<GenericTypeParamType *> genericParams,
PotentialArchetype *representative) {
Expand All @@ -4783,6 +4804,10 @@ void GenericSignatureBuilder::checkConcreteTypeConstraints(
None,
diag::redundant_same_type_to_concrete,
diag::same_type_redundancy_here);

// Resolve any this-far-unresolved dependent types.
equivClass->concreteType =
resolveDependentMemberTypes(*this, equivClass->concreteType);
}

void GenericSignatureBuilder::checkSuperclassConstraints(
Expand Down Expand Up @@ -4821,6 +4846,10 @@ void GenericSignatureBuilder::checkSuperclassConstraints(
diag::redundant_superclass_constraint,
diag::superclass_redundancy_here);

// Resolve any this-far-unresolved dependent types.
equivClass->superclass =
resolveDependentMemberTypes(*this, equivClass->superclass);

// If we have a concrete type, check it.
// FIXME: Substitute into the concrete type.
if (equivClass->concreteType) {
Expand Down
7 changes: 0 additions & 7 deletions lib/Sema/GenericTypeResolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,7 @@ class GenericTypeResolver {
/// This generic type resolver leaves generic type parameter types alone
/// and only trivially resolves dependent member types.
class DependentGenericTypeResolver : public GenericTypeResolver {
GenericSignatureBuilder &Builder;
ArrayRef<GenericTypeParamType *> GenericParams;

public:
DependentGenericTypeResolver(GenericSignatureBuilder &builder,
ArrayRef<GenericTypeParamType *> genericParams)
: Builder(builder), GenericParams(genericParams) { }

virtual Type resolveGenericTypeParamType(GenericTypeParamType *gp);

virtual Type resolveDependentMemberType(Type baseTy,
Expand Down
39 changes: 11 additions & 28 deletions lib/Sema/TypeCheckGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,12 @@ Type DependentGenericTypeResolver::resolveDependentMemberType(
DeclContext *DC,
SourceRange baseRange,
ComponentIdentTypeRepr *ref) {
auto archetype =
Builder.resolveArchetype(baseTy, ArchetypeResolutionKind::AlwaysPartial);
assert(archetype && "Bad generic context nesting?");

return archetype
->getNestedType(ref->getIdentifier(), Builder)
->getDependentType(GenericParams, /*allowUnresolved=*/true);
return DependentMemberType::get(baseTy, ref->getIdentifier());
}

Type DependentGenericTypeResolver::resolveSelfAssociatedType(
Type selfTy, AssociatedTypeDecl *assocType) {
auto archetype =
Builder.resolveArchetype(selfTy, ArchetypeResolutionKind::AlwaysPartial);
assert(archetype && "Bad generic context nesting?");

return archetype
->getNestedType(assocType, Builder)
->getDependentType(GenericParams, /*allowUnresolved=*/true);
return DependentMemberType::get(selfTy, assocType);
}

Type DependentGenericTypeResolver::resolveTypeOfContext(DeclContext *dc) {
Expand All @@ -74,14 +62,8 @@ bool DependentGenericTypeResolver::areSameType(Type type1, Type type2) {
if (!type1->hasTypeParameter() && !type2->hasTypeParameter())
return type1->isEqual(type2);

auto pa1 =
Builder.resolveArchetype(type1, ArchetypeResolutionKind::AlwaysPartial);
auto pa2 =
Builder.resolveArchetype(type2, ArchetypeResolutionKind::AlwaysPartial);
if (pa1 && pa2)
return pa1->isInSameEquivalenceClassAs(pa2);

return type1->isEqual(type2);
// Conservative answer: they could be the same.
return true;
}

void DependentGenericTypeResolver::recordParamType(ParamDecl *decl, Type type) {
Expand Down Expand Up @@ -281,11 +263,12 @@ bool CompleteGenericTypeResolver::areSameType(Type type1, Type type2) {
if (!type1->hasTypeParameter() && !type2->hasTypeParameter())
return type1->isEqual(type2);

// FIXME: Want CompleteWellFormed here?
auto pa1 =
Builder.resolveArchetype(type1, ArchetypeResolutionKind::AlwaysPartial);
Builder.resolveArchetype(type1,
ArchetypeResolutionKind::CompleteWellFormed);
auto pa2 =
Builder.resolveArchetype(type2, ArchetypeResolutionKind::AlwaysPartial);
Builder.resolveArchetype(type2,
ArchetypeResolutionKind::CompleteWellFormed);
if (pa1 && pa2)
return pa1->isInSameEquivalenceClassAs(pa2);

Expand Down Expand Up @@ -801,7 +784,7 @@ TypeChecker::validateGenericFuncSignature(AbstractFunctionDecl *func) {

// Type check the function declaration, treating all generic type
// parameters as dependent, unresolved.
DependentGenericTypeResolver dependentResolver(builder, allGenericParams);
DependentGenericTypeResolver dependentResolver;
if (checkGenericFuncSignature(*this, &builder, func, dependentResolver))
invalid = true;

Expand Down Expand Up @@ -1032,7 +1015,7 @@ TypeChecker::validateGenericSubscriptSignature(SubscriptDecl *subscript) {

// Type check the function declaration, treating all generic type
// parameters as dependent, unresolved.
DependentGenericTypeResolver dependentResolver(builder, allGenericParams);
DependentGenericTypeResolver dependentResolver;
if (checkGenericSubscriptSignature(*this, &builder, subscript,
dependentResolver))
invalid = true;
Expand Down Expand Up @@ -1151,7 +1134,7 @@ GenericEnvironment *TypeChecker::checkGenericEnvironment(

// Type check the generic parameters, treating all generic type
// parameters as dependent, unresolved.
DependentGenericTypeResolver dependentResolver(builder, allGenericParams);
DependentGenericTypeResolver dependentResolver;
if (recursivelyVisitGenericParams) {
visitOuterToInner(genericParams,
[&](GenericParamList *gpList) {
Expand Down
2 changes: 1 addition & 1 deletion test/Generics/associated_type_typo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protocol P2 {
protocol P3 { }
protocol P4 { }

// expected-error@+1{{'T' does not have a member type named 'assoc'; did you mean 'Assoc'?}}{{30-35=Assoc}}
// expected-error@+1{{'assoc' is not a member type of 'T'}}
func typoAssoc1<T : P1>(x: T.assoc, _: T) { }

// expected-error@+2{{'T' does not have a member type named 'assoc'; did you mean 'Assoc'?}}{{53-58=Assoc}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: not %target-swift-frontend -swift-version 4 %s -typecheck -o /dev/null
// RUN: %target-swift-frontend -swift-version 4 %s -typecheck -o /dev/null

// This should actually type check successfully.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

// REQUIRES: asserts
// RUN: not --crash %target-swift-frontend %s -emit-ir
// RUN: not %target-swift-frontend %s -emit-ir
protocol P{typealias e:P{}class a{{}func a:Self.a.e