Skip to content

[5.0][Sema] Fix resolveDependentMemberType to properly handle nested typ… #22021

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 1 commit into from
Jan 22, 2019
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
33 changes: 23 additions & 10 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,23 +212,36 @@ Type TypeResolution::resolveDependentMemberType(
return DependentMemberType::get(baseTy, assocType);
}

// Otherwise, the nested type comes from a concrete type. Substitute the
// base type into it.
// Otherwise, the nested type comes from a concrete type,
// or it's a typealias declared in protocol or protocol extension.
// Substitute the base type into it.
auto concrete = ref->getBoundDecl();
auto lazyResolver = ctx.getLazyResolver();
if (lazyResolver)
lazyResolver->resolveDeclSignature(concrete);
if (!concrete->hasInterfaceType())
return ErrorType::get(ctx);

if (concrete->getDeclContext()->getSelfClassDecl()) {
// We found a member of a class from a protocol or protocol
// extension.
//
// Get the superclass of the 'Self' type parameter.
baseTy = (baseEquivClass->concreteType
? baseEquivClass->concreteType
: baseEquivClass->superclass);
// Make sure that base type didn't get replaced along the way.
assert(baseTy->isTypeParameter());

// There are two situations possible here:
//
// 1. Member comes from the protocol, which means that it has been
// found through a conformance constraint placed on base e.g. `T: P`.
// In this case member is a `typealias` declaration located in
// protocol or protocol extension.
//
// 2. Member comes from struct/enum/class type, which means that it
// has been found through same-type constraint on base e.g. `T == Q`.
//
// If this is situation #2 we need to make sure to switch base to
// a concrete type (according to equivalence class) otherwise we'd
// end up using incorrect generic signature while attempting to form
// a substituted type for the member we found.
if (!concrete->getDeclContext()->getSelfProtocolDecl()) {
baseTy = baseEquivClass->concreteType ? baseEquivClass->concreteType
: baseEquivClass->superclass;
assert(baseTy);
}

Expand Down
10 changes: 10 additions & 0 deletions validation-test/Sema/Inputs/rdar47334176_types.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public protocol P : class {
associatedtype V
}

public protocol R {
associatedtype V
}

public enum E<V> : R {}
public class C<V> : R {}
13 changes: 13 additions & 0 deletions validation-test/Sema/rdar47334176.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -o %t/rdar47334176_types.swiftmodule %S/Inputs/rdar47334176_types.swift
// RUN: %target-swift-frontend -I %t -typecheck %s

import rdar47334176_types

// To test all possibilities let's declare one of the types
// in the same module as function declaration which uses it.
struct S<V> : R {}

func foo<T : P, U>(_: T?, _: (T.V.V) -> Void) where T.V == E<U> {} // Ok
func bar<T : P, U>(_: T?, _: (T.V.V) -> Void) where T.V == S<U> {} // Ok
func baz<T : P, U>(_: T?, _: (T.V.V) -> Void) where T.V == C<U> {} // Ok