Skip to content

Commit c06fd1a

Browse files
committed
Serialization: When deserializing a typealias, build its generic signature
We were forgetting to do this, triggering crashes when using a generic typealias from another module. Fixes <https://bugs.swift.org/browse/SR-1889>.
1 parent 40ba708 commit c06fd1a

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

lib/Serialization/Deserialization.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,6 +2163,21 @@ Decl *ModuleFile::getDecl(DeclID DID, Optional<DeclContext *> ForcedContext) {
21632163
genericParams, DC);
21642164
declOrOffset = alias;
21652165

2166+
if (genericParams) {
2167+
SmallVector<GenericTypeParamType *, 4> paramTypes;
2168+
for (auto &genericParam : *genericParams) {
2169+
paramTypes.push_back(genericParam->getDeclaredType()
2170+
->castTo<GenericTypeParamType>());
2171+
}
2172+
2173+
// Read the generic requirements.
2174+
SmallVector<Requirement, 4> requirements;
2175+
readGenericRequirements(requirements);
2176+
2177+
auto sig = GenericSignature::get(paramTypes, requirements);
2178+
alias->setGenericSignature(sig);
2179+
}
2180+
21662181
alias->computeType();
21672182

21682183
if (auto accessLevel = getActualAccessibility(rawAccessLevel)) {

lib/Serialization/Serialization.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,6 +2191,7 @@ void Serializer::writeDecl(const Decl *D) {
21912191
typeAlias->isImplicit(),
21922192
rawAccessLevel);
21932193
writeGenericParams(typeAlias->getGenericParams(), DeclTypeAbbrCodes);
2194+
writeRequirements(typeAlias->getGenericRequirements());
21942195
break;
21952196
}
21962197

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: true
2+
3+
public enum Result<T, U>
4+
{
5+
case success(T)
6+
case failure(U)
7+
}
8+
9+
public typealias GenericResult<T> = Result<T, ErrorProtocol>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: rm -rf %t && mkdir %t
2+
3+
// RUN: mkdir %t/linker
4+
// RUN: %target-build-swift -emit-module -c %S/library.swift -o %t/linker/library.o
5+
// RUN: %target-build-swift -emit-library -c %S/library.swift -o %t/linker/library.o
6+
// RUN: %target-build-swift %S/main.swift %t/linker/library.o -I %t/linker/ -L %t/linker/ -o %t/linker/main
7+
8+
// REQUIRES: executable_test
9+
10+
import library
11+
12+
func testFunction<T>(withCompletion completion: (Result<T, ErrorProtocol>) -> Void) { }
13+
testFunction { (result: GenericResult<Int>) in }

0 commit comments

Comments
 (0)