Skip to content

Commit 2be766d

Browse files
authored
Merge pull request #17164 from xedin/rdar-39849926
[ClangImporter] Forward generic parameters while importing generic @c…
2 parents 5bbd67c + 1f3714f commit 2be766d

File tree

5 files changed

+60
-15
lines changed

5 files changed

+60
-15
lines changed

lib/ClangImporter/ImportDecl.cpp

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

4970+
if (auto *GTD = dyn_cast<GenericTypeDecl>(typeDecl)) {
4971+
typealias->setGenericEnvironment(GTD->getGenericEnvironment());
4972+
if (GTD->isGeneric())
4973+
typealias->setGenericParams(GTD->getGenericParams()->clone(typealias));
4974+
}
4975+
49704976
typealias->setUnderlyingType(Impl.getSugaredTypeReference(typeDecl));
49714977
return typealias;
49724978
}
@@ -5183,18 +5189,6 @@ Decl *SwiftDeclConverter::importCompatibilityTypeAlias(
51835189
if (!typeDecl)
51845190
return nullptr;
51855191

5186-
// Deliberately use an UnboundGenericType to avoid having to translate over
5187-
// generic parameters.
5188-
Type underlyingType;
5189-
if (auto *underlyingAlias = dyn_cast<TypeAliasDecl>(typeDecl)) {
5190-
if (underlyingAlias->isGeneric())
5191-
underlyingType = underlyingAlias->getUnboundGenericType();
5192-
else
5193-
underlyingType = Impl.getSugaredTypeReference(underlyingAlias);
5194-
} else {
5195-
underlyingType = cast<NominalTypeDecl>(typeDecl)->getDeclaredType();
5196-
}
5197-
51985192
auto dc = Impl.importDeclContextOf(decl,
51995193
compatibilityName.getEffectiveContext());
52005194
if (!dc)
@@ -5205,7 +5199,15 @@ Decl *SwiftDeclConverter::importCompatibilityTypeAlias(
52055199
decl, AccessLevel::Public, Impl.importSourceLoc(decl->getLocStart()),
52065200
SourceLoc(), compatibilityName.getDeclName().getBaseIdentifier(),
52075201
Impl.importSourceLoc(decl->getLocation()), /*generic params*/nullptr, dc);
5208-
alias->setUnderlyingType(underlyingType);
5202+
5203+
auto *GTD = dyn_cast<GenericTypeDecl>(typeDecl);
5204+
if (GTD && !isa<ProtocolDecl>(GTD)) {
5205+
alias->setGenericEnvironment(GTD->getGenericEnvironment());
5206+
if (GTD->isGeneric())
5207+
alias->setGenericParams(GTD->getGenericParams()->clone(alias));
5208+
}
5209+
5210+
alias->setUnderlyingType(Impl.getSugaredTypeReference(typeDecl));
52095211

52105212
// Record that this is the official version of this declaration.
52115213
Impl.ImportedDecls[{decl->getCanonicalDecl(), getVersion()}] = alias;

test/APINotes/versioned-objc.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ func testRenamedGeneric() {
3434

3535
class SwiftClass {}
3636

37-
// CHECK-DIAGS-3:[[@LINE+1]]:{{[0-9]+}}: error: 'RenamedGeneric' requires that 'SwiftClass' inherit from 'Base'
37+
// CHECK-DIAGS-3:[[@LINE+1]]:{{[0-9]+}}: error: 'OldRenamedGeneric' requires that 'SwiftClass' inherit from 'Base'
3838
let _: OldRenamedGeneric<SwiftClass> = RenamedGeneric<SwiftClass>()
39-
// CHECK-DIAGS-4:[[@LINE-1]]:{{[0-9]+}}: error: 'RenamedGeneric' requires that 'SwiftClass' inherit from 'Base'
39+
// CHECK-DIAGS-4:[[@LINE-1]]:{{[0-9]+}}: error: 'OldRenamedGeneric' requires that 'SwiftClass' inherit from 'Base'
4040

4141
// CHECK-DIAGS-3:[[@LINE+1]]:{{[0-9]+}}: error: 'RenamedGeneric' requires that 'SwiftClass' inherit from 'Base'
4242
let _: RenamedGeneric<SwiftClass> = OldRenamedGeneric<SwiftClass>()

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)