Skip to content

Commit 094949a

Browse files
committed
[ClangImporter] Forward generic parameters while importing generic @compatibility_alias
When importing @compatibility_alias declarations check if underlying declaration is generic and if so, forward generic environment and generic parameters (if any) to newly created typealias declaration, otherwise there is going to be a mismatch between type associated with typealias and its declaration which leads to crashes. Resolves: rdar://problem/39849926
1 parent 7d26fe0 commit 094949a

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4966,6 +4966,12 @@ namespace {
49664966
Impl.importSourceLoc(decl->getLocation()),
49674967
/*genericparams=*/nullptr, dc);
49684968

4969+
if (auto *GTD = dyn_cast<GenericTypeDecl>(typeDecl)) {
4970+
typealias->setGenericEnvironment(GTD->getGenericEnvironment());
4971+
if (GTD->isGeneric())
4972+
typealias->setGenericParams(GTD->getGenericParams()->clone(typealias));
4973+
}
4974+
49694975
typealias->setUnderlyingType(Impl.getSugaredTypeReference(typeDecl));
49704976
return typealias;
49714977
}

test/ClangImporter/Inputs/custom-modules/ObjCIRExtras.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,16 @@
6060
@interface SwiftNameTestErrorSub : SwiftNameTestError
6161
@end
6262

63+
@interface SwiftGenericNameTest<T> : NSObject
64+
@end
65+
66+
@interface SwiftConstrGenericNameTest<T:NSNumber *> : NSNumber
67+
@end
68+
6369
int global_int SWIFT_NAME(GlobalInt);
6470

6571
@compatibility_alias SwiftNameAlias SwiftNameTest;
72+
@compatibility_alias SwiftGenericNameAlias SwiftGenericNameTest;
73+
@compatibility_alias SwiftConstrGenericNameAlias SwiftConstrGenericNameTest;
6674

6775
#pragma clang assume_nonnull end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %build-clang-importer-objc-overlays
3+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t) -module-name generic_compat_alias -I %S/Inputs/custom-modules -typecheck -verify %s
4+
5+
// REQUIRES: objc_interop
6+
// REQUIRES: OS=macosx
7+
8+
import ObjectiveC
9+
import Foundation
10+
import ObjCIRExtras
11+
12+
func foo(_: SwiftConstrGenericNameAlias<String>) {
13+
// expected-error@-1 {{'SwiftConstrGenericNameAlias' requires that 'String' inherit from 'NSNumber'}}
14+
// expected-note@-2 {{requirement specified as 'T' : 'NSNumber' [with T = String]}}
15+
}
16+
17+
func faz(_: SwiftGenericNameAlias<Int>) {
18+
// expected-error@-1 {{'SwiftGenericNameAlias' requires that 'Int' be a class type}}
19+
// expected-note@-2 {{requirement specified as 'T' : 'AnyObject' [with T = Int]}}
20+
}
21+
22+
func bar(_: SwiftGenericNameAlias<NSNumber>) {} // Ok
23+
func baz<T: AnyObject>(_: SwiftGenericNameAlias<T>) {} // Ok

test/ClangImporter/objc_ir.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,13 @@ func testCompatibilityAliasMangling(obj: SwiftNameAlias) {
336336
// CHECK: call void @llvm.dbg.declare(metadata %TSo13SwiftNameTestC** {{%.+}}, metadata ![[SWIFT_NAME_ALIAS_VAR:[0-9]+]], metadata !DIExpression())
337337
}
338338

339+
func testGenericCompatibilityAliasMangling(generic_obj: SwiftGenericNameAlias<NSNumber>) {
340+
// CHECK: call void @llvm.dbg.declare(metadata %TSo20SwiftGenericNameTestCySo8NSNumberCG** {{%.+}}, metadata ![[SWIFT_GENERIC_NAME_ALIAS_VAR:[0-9]+]], metadata !DIExpression())
341+
}
342+
343+
func testConstrGenericCompatibilityAliasMangling(constr_generic_obj: SwiftConstrGenericNameAlias<NSNumber>) {
344+
// CHECK: call void @llvm.dbg.declare(metadata %TSo26SwiftConstrGenericNameTestCySo8NSNumberCG** {{%.+}}, metadata ![[SWIFT_CONSTR_GENERIC_NAME_ALIAS_VAR:[0-9]+]], metadata !DIExpression())
345+
}
339346

340347
// CHECK: linkonce_odr hidden {{.*}} @"$SSo1BC3intABSgs5Int32V_tcfcTO"
341348
// CHECK: load i8*, i8** @"\01L_selector(initWithInt:)"
@@ -347,3 +354,8 @@ func testCompatibilityAliasMangling(obj: SwiftNameAlias) {
347354
// CHECK: ![[SWIFT_NAME_ALIAS_VAR]] = !DILocalVariable(name: "obj", arg: 1, scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: ![[SWIFT_NAME_ALIAS_TYPE:[0-9]+]])
348355
// CHECK: ![[SWIFT_NAME_ALIAS_TYPE]] = !DIDerivedType(tag: DW_TAG_typedef, name: "$SSo14SwiftNameAliasaD", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, baseType: !{{[0-9]+}})
349356

357+
// CHECK: ![[SWIFT_GENERIC_NAME_ALIAS_VAR]] = !DILocalVariable(name: "generic_obj", arg: 1, scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: ![[SWIFT_GENERIC_NAME_ALIAS_TYPE:[0-9]+]])
358+
// CHECK: ![[SWIFT_GENERIC_NAME_ALIAS_TYPE]] = !DIDerivedType(tag: DW_TAG_typedef, name: "$SSo21SwiftGenericNameAliasaySo8NSNumberCGD", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, baseType: !{{[0-9]+}})
359+
360+
// CHECK: ![[SWIFT_CONSTR_GENERIC_NAME_ALIAS_VAR]] = !DILocalVariable(name: "constr_generic_obj", arg: 1, scope: !{{[0-9]+}}, file: !{{[0-9]+}}, line: {{[0-9]+}}, type: ![[SWIFT_CONSTR_GENERIC_NAME_ALIAS_TYPE:[0-9]+]])
361+
// CHECK: ![[SWIFT_CONSTR_GENERIC_NAME_ALIAS_TYPE]] = !DIDerivedType(tag: DW_TAG_typedef, name: "$SSo27SwiftConstrGenericNameAliasaySo8NSNumberCGD", scope: !{{[0-9]+}}, file: !{{[0-9]+}}, baseType: !{{[0-9]+}})

0 commit comments

Comments
 (0)