Skip to content

Commit ad7bbdf

Browse files
authored
[PrintAsObjC] Handle generic typealiases (#18941)
(either explicitly generic ones, or those embedded in generic contexts) Previously we tried to look at the typealias decl's underlying type, but that might have generic parameters in it. Oops. Use the NameAliasType's desugared type instead. Also, drop half-baked support for `@objc typealias`, which isn't supported. (It's not an unreasonable feature, but the bits that were there weren't implemented correctly.) rdar://problem/43347303
1 parent 8310056 commit ad7bbdf

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,12 +1523,8 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
15231523

15241524
bool printImportedAlias(const TypeAliasDecl *alias,
15251525
Optional<OptionalTypeKind> optionalKind) {
1526-
if (!alias->hasClangNode()) {
1527-
if (!alias->isObjC()) return false;
1528-
1529-
os << alias->getName();
1530-
return true;
1531-
}
1526+
if (!alias->hasClangNode())
1527+
return false;
15321528

15331529
if (auto *clangTypeDecl =
15341530
dyn_cast<clang::TypeDecl>(alias->getClangDecl())) {
@@ -1560,7 +1556,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
15601556
if (printImportedAlias(alias, optionalKind))
15611557
return;
15621558

1563-
visitPart(alias->getUnderlyingTypeLoc().getType(), optionalKind);
1559+
visitPart(aliasTy->getSinglyDesugaredType(), optionalKind);
15641560
}
15651561

15661562
void maybePrintTagKeyword(const TypeDecl *NTD) {
@@ -1993,7 +1989,10 @@ class ReferencedTypeFinder : public TypeVisitor<ReferencedTypeFinder> {
19931989
}
19941990

19951991
void visitNameAliasType(NameAliasType *aliasTy) {
1996-
Callback(*this, aliasTy->getDecl());
1992+
if (aliasTy->getDecl()->hasClangNode())
1993+
Callback(*this, aliasTy->getDecl());
1994+
else
1995+
visit(aliasTy->getSinglyDesugaredType());
19971996
}
19981997

19991998
void visitParenType(ParenType *parenTy) {
@@ -2312,9 +2311,8 @@ class ModuleWriter {
23122311
} else if (auto PD = dyn_cast<ProtocolDecl>(TD)) {
23132312
forwardDeclare(PD);
23142313
} else if (auto TAD = dyn_cast<TypeAliasDecl>(TD)) {
2315-
(void)addImport(TD);
2316-
// Just in case, make sure the underlying type is visible too.
2317-
finder.visit(TAD->getUnderlyingTypeLoc().getType());
2314+
if (TAD->hasClangNode())
2315+
(void)addImport(TD);
23182316
} else if (addImport(TD)) {
23192317
return;
23202318
} else if (auto ED = dyn_cast<EnumDecl>(TD)) {

test/PrintAsObjC/typealias.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend -emit-module -o %t/typealias.swiftmodule %s -disable-objc-attr-requires-foundation-module
4+
// RUN: %target-swift-frontend -parse-as-library %t/typealias.swiftmodule -typecheck -emit-objc-header-path %t/typealias.h -disable-objc-attr-requires-foundation-module
5+
// RUN: %FileCheck %s < %t/typealias.h
6+
// RUN: %check-in-clang %t/typealias.h
7+
8+
// RUN: %target-swift-frontend -typecheck %s -emit-objc-header-path %t/typealias_wmo.h -disable-objc-attr-requires-foundation-module
9+
// RUN: %FileCheck %s < %t/typealias_wmo.h
10+
// RUN: %check-in-clang %t/typealias_wmo.h
11+
12+
// REQUIRES: objc_interop
13+
14+
public typealias ActuallyGeneric<T> = T
15+
16+
public struct GenericHelper<T> {
17+
public typealias TheType = T
18+
}
19+
public typealias PassesThroughGeneric = GenericHelper<Int>.TheType
20+
21+
// CHECK-LABEL: @interface X
22+
@objc public class X {
23+
// CHECK: @property (nonatomic) NSInteger actuallyGeneric;
24+
@objc public var actuallyGeneric: ActuallyGeneric<Int> = 0
25+
// CHECK: @property (nonatomic) NSInteger passesThroughGeneric;
26+
@objc public var passesThroughGeneric: PassesThroughGeneric = 0
27+
} // CHECK: @end

0 commit comments

Comments
 (0)