Skip to content

Fixes for typealiases involving generics #3811

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
Jul 28, 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
53 changes: 18 additions & 35 deletions lib/Sema/TypeCheckCaptures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class FindCapturedVars : public ASTWalker {
if (!type)
return;

// We want to look through type aliases here.
type = type->getCanonicalType();

// If the type contains dynamic 'Self', conservatively assume we will
// need 'Self' metadata at runtime. We could generalize the analysis
// used below for usages of generic parameters in Objective-C
Expand All @@ -95,42 +98,22 @@ class FindCapturedVars : public ASTWalker {
});
}

// Nothing to do if the type is concrete.
if (!type->hasArchetype())
return;

// Walk the type to see if we have any archetypes that are *not* open
// existentials and that aren't type-erased.
class CapturesTypeWalker final : public TypeWalker {
SourceLoc &GenericParamCaptureLoc;
SourceLoc CurLoc;

public:
CapturesTypeWalker(SourceLoc &GenericParamCaptureLoc,
SourceLoc curLoc)
: GenericParamCaptureLoc(GenericParamCaptureLoc),
CurLoc(curLoc) {}

Action walkToTypePre(Type t) override {
// Similar to dynamic 'Self', IRGen doesn't really need type metadata
// for class-bound archetypes in nearly as many cases as with opaque
// archetypes.
//
// Perhaps this entire analysis should happen at the SILGen level,
// instead, but even there we don't really have enough information to
// perform it accurately.
if (t->is<ArchetypeType>() && !t->isOpenedExistential()) {
if (GenericParamCaptureLoc.isInvalid())
GenericParamCaptureLoc = CurLoc;
return Action::Continue;
// Similar to dynamic 'Self', IRGen doesn't really need type metadata
// for class-bound archetypes in nearly as many cases as with opaque
// archetypes.
//
// Perhaps this entire analysis should happen at the SILGen level,
// instead, but even there we don't really have enough information to
// perform it accurately.
if (type->hasArchetype()) {
type.visit([&](Type t) {
if (t->is<ArchetypeType>() &&
!t->isOpenedExistential() &&
GenericParamCaptureLoc.isInvalid()) {
GenericParamCaptureLoc = loc;
}

return Action::Continue;
}
};

type.walk(CapturesTypeWalker(GenericParamCaptureLoc,
loc));
});
}
}

/// Add the specified capture to the closure's capture list, diagnosing it
Expand Down
2 changes: 2 additions & 0 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,8 @@ Decl *ModuleFile::resolveCrossReference(Module *M, uint32_t pathLen) {
// Simple case: use the nominal type's generic parameters.
paramList = nominal->getGenericParams();
}
} else if (auto alias = dyn_cast<TypeAliasDecl>(base)) {
paramList = alias->getGenericParams();
} else if (auto fn = dyn_cast<AbstractFunctionDecl>(base))
paramList = fn->getGenericParams();

Expand Down
9 changes: 9 additions & 0 deletions test/expr/capture/generic_params.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,13 @@ func outerGeneric<T>(t: T, x: AnyObject) {
// they're not mentioned in the function body
// CHECK: func_decl "innerGeneric(u:)"<U> type='<U> (u: U) -> ()' {{.*}} captures=(<generic> )
func innerGeneric<U>(u: U) {}

// Make sure we look through typealiases
typealias TT = (a: T, b: T)

// CHECK: func_decl "localFunction(tt:)" type='<T> (tt: TT) -> ()' {{.*}} captures=(<generic> )
func localFunction(tt: TT) {}

// CHECK: closure_expr type='(TT) -> ()' {{.*}} captures=(<generic> )
let _: (TT) -> () = { _ in }
}
9 changes: 9 additions & 0 deletions test/multifile/typealias/one-module/library.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: true

public enum Result<T, U>
{
case success(T)
case failure(U)
}

public typealias GenericResult<T> = Result<T, Error>
9 changes: 9 additions & 0 deletions test/multifile/typealias/one-module/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: rm -rf %t && mkdir %t

// RUN: %target-build-swift %S/main.swift %S/library.swift
// RUN: %target-build-swift -g %S/main.swift %S/library.swift

// REQUIRES: executable_test

func testFunction<T>(withCompletion completion: (Result<T, Error>) -> Void) { }
testFunction { (result: GenericResult<Int>) in }
4 changes: 4 additions & 0 deletions test/multifile/typealias/two-modules/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
// RUN: %target-build-swift -emit-library -c %S/library.swift -o %t/linker/library.o
// RUN: %target-build-swift %S/main.swift %t/linker/library.o -I %t/linker/ -L %t/linker/ -o %t/linker/main

// RUN: %target-build-swift -g -emit-module -c %S/library.swift -o %t/linker/library.o
// RUN: %target-build-swift -g -emit-library -c %S/library.swift -o %t/linker/library.o
// RUN: %target-build-swift -g %S/main.swift %t/linker/library.o -I %t/linker/ -L %t/linker/ -o %t/linker/main

// REQUIRES: executable_test

import library
Expand Down