Skip to content

AST: GenericEnvironment::getOrCreateArchetypeFromInterfaceType() needs to handle case where parent of concrete anchor is concrete #39965

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
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
56 changes: 19 additions & 37 deletions lib/AST/GenericEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,7 @@ GenericEnvironment::getOrCreateArchetypeFromInterfaceType(Type depType) {

auto requirements = genericSig->getLocalRequirements(depType);

// FIXME: With the RequirementMachine, we will always have an anchor.
if (requirements.concreteType && !requirements.anchor) {
if (requirements.concreteType->is<ErrorType>())
return requirements.concreteType;

if (requirements.concreteType) {
return mapTypeIntoContext(requirements.concreteType,
conformanceLookupFn);
}
Expand All @@ -140,9 +136,7 @@ GenericEnvironment::getOrCreateArchetypeFromInterfaceType(Type depType) {
if (auto depMemTy = requirements.anchor->getAs<DependentMemberType>()) {
parentArchetype =
getOrCreateArchetypeFromInterfaceType(depMemTy->getBase())
->getAs<ArchetypeType>();
if (!parentArchetype)
return ErrorType::get(depMemTy);
->castTo<ArchetypeType>();

auto name = depMemTy->getName();
if (auto type = parentArchetype->getNestedTypeIfKnown(name))
Expand All @@ -156,39 +150,27 @@ GenericEnvironment::getOrCreateArchetypeFromInterfaceType(Type depType) {
addMapping(genericParam, ErrorType::get(ctx));
}

Type result;

// If this equivalence class is mapped to a concrete type, produce that
// type.
if (requirements.concreteType) {
result = mapTypeIntoContext(requirements.concreteType,
conformanceLookupFn);
} else {
// Substitute into the superclass.
Type superclass = requirements.superclass;
if (superclass && superclass->hasTypeParameter()) {
superclass = mapTypeIntoContext(superclass,
conformanceLookupFn);
if (superclass->is<ErrorType>())
superclass = Type();
}

if (parentArchetype) {
auto *depMemTy = requirements.anchor->castTo<DependentMemberType>();
result = NestedArchetypeType::getNew(ctx, parentArchetype, depMemTy,
requirements.protos, superclass,
requirements.layout);
} else {
result = PrimaryArchetypeType::getNew(ctx, this, genericParam,
requirements.protos, superclass,
requirements.layout);
}
// Substitute into the superclass.
Type superclass = requirements.superclass;
if (superclass && superclass->hasTypeParameter()) {
superclass = mapTypeIntoContext(superclass,
conformanceLookupFn);
if (superclass->is<ErrorType>())
superclass = Type();
}

// Cache the new archetype for future lookups.
if (auto depMemTy = requirements.anchor->getAs<DependentMemberType>()) {
Type result;

if (parentArchetype) {
auto *depMemTy = requirements.anchor->castTo<DependentMemberType>();
result = NestedArchetypeType::getNew(ctx, parentArchetype, depMemTy,
requirements.protos, superclass,
requirements.layout);
parentArchetype->registerNestedType(depMemTy->getName(), result);
} else {
result = PrimaryArchetypeType::getNew(ctx, this, genericParam,
requirements.protos, superclass,
requirements.layout);
addMapping(genericParam, result);
}

Expand Down
28 changes: 28 additions & 0 deletions test/Generics/rdar84734584.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// RUN: %target-typecheck-verify-swift

protocol P1 {
associatedtype T1
}

protocol P2 {
associatedtype T2 : P1
}

struct S1 : P1 {
typealias T1 = S2
}

struct S2 {}

func foo<X1: P2, X2: P1>(_: X1, _: X2)
where X2.T1 == S2 { }

func bar<X1: P2, X2: P1>(x: X1, u: X2, uu: X2.T1)
where X1.T2 == S1, X2.T1: P1, X2.T1.T1 == S2 {
// this call should type-check successfully
foo(x, uu)

// so should this
let _: S2.Type = X2.T1.T1.self
let _: S2.Type = X1.T2.T1.self
}