Skip to content

[Sema] Make typealiases in protocols available for nested type lookup. #2885

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
Jun 7, 2016
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/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,9 @@ NOTE(optional_req_near_match_accessibility,none,
// Protocols and existentials
ERROR(assoc_type_outside_of_protocol,none,
"cannot use associated type %0 outside of its protocol", (Identifier))
ERROR(typealias_to_assoc_type_outside_of_protocol,none,
"cannot use typealias %0 of associated type %1 outside of its protocol",
(Identifier, TypeLoc))

ERROR(circular_protocol_def,none,
"circular protocol inheritance %0", (StringRef))
Expand Down
19 changes: 12 additions & 7 deletions lib/Sema/TypeCheckNameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,17 +362,22 @@ LookupTypeResult TypeChecker::lookupMemberType(DeclContext *dc,
}
}

// Ignore typealiases found in protocol members.
if (auto alias = dyn_cast<TypeAliasDecl>(typeDecl)) {
auto aliasParent = alias->getParent();
if (aliasParent != dc && isa<ProtocolDecl>(aliasParent))
continue;
}

// Substitute the base into the member's type.
if (Type memberType = substMemberTypeWithBase(dc->getParentModule(),
typeDecl, type,
/*isTypeReference=*/true)) {

// Similar to the associated type case, ignore typealiases containing
// associated types when looking into a non-protocol.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be clearer to factor out the entire predicate into its own method, because then in addition to comments you can rely on the method name to understand what's going on

if (auto alias = dyn_cast<TypeAliasDecl>(typeDecl)) {
auto parentProtocol = alias->getDeclContext()->
getAsProtocolOrProtocolExtensionContext();
if (parentProtocol && memberType->hasTypeParameter() &&
!type->is<ArchetypeType>() && !type->isExistentialType()) {
continue;
}
}

// If we haven't seen this type result yet, add it to the result set.
if (types.insert(memberType->getCanonicalType()).second)
result.Results.push_back({typeDecl, memberType});
Expand Down
21 changes: 19 additions & 2 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1110,13 +1110,22 @@ static Type resolveNestedIdentTypeComponent(
member = memberTypes.back().first;
}

if (parentTy->isExistentialType()) {
if (parentTy->isExistentialType() && isa<AssociatedTypeDecl>(member)) {
if (diagnoseErrors)
TC.diagnose(comp->getIdLoc(), diag::assoc_type_outside_of_protocol,
comp->getIdentifier());

return ErrorType::get(TC.Context);
}
if (auto alias = dyn_cast<TypeAliasDecl>(member)) {
if (parentTy->isExistentialType() && memberType->hasTypeParameter()) {
if (diagnoseErrors)
TC.diagnose(comp->getIdLoc(), diag::typealias_to_assoc_type_outside_of_protocol,
comp->getIdentifier(), alias->getUnderlyingTypeLoc());

return ErrorType::get(TC.Context);
}
}

// If there are generic arguments, apply them now.
if (auto genComp = dyn_cast<GenericIdentTypeRepr>(comp))
Expand Down Expand Up @@ -3208,10 +3217,18 @@ class UnsupportedProtocolVisitor
}

bool walkToTypeReprPre(TypeRepr *T) {
if (T->isInvalid())
return false;
if (auto compound = dyn_cast<CompoundIdentTypeRepr>(T)) {
// Only visit the last component to check, because nested typealiases in
// existentials are okay.
visit(compound->getComponentRange().back());
return false;
}
visit(T);
return true;
}

std::pair<bool, Stmt*> walkToStmtPre(Stmt *S) {
if (recurseIntoSubstatements) {
return { true, S };
Expand Down
13 changes: 13 additions & 0 deletions test/decl/typealias/typealias.swift
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,17 @@ protocol P4 {
func getSelf() -> X
}

// Availability of typealiases in protocols for nested type lookup
protocol P5 {
associatedtype A
typealias T1 = Int
typealias T2 = A
var a: T2 { get }
}

struct T5 : P5 {
var a: P5.T1 // OK
var v2: P5.T2 // expected-error {{cannot use typealias 'T2' of associated type 'A' outside of its protocol}}
}