Skip to content

Commit a29f9a1

Browse files
author
ematejska
authored
Merge pull request #5556 from jrose-apple/swift-3.0-PrintAsObjC-generics-again
[PrintAsObjC] Use of imported generics require the full definition. (#5518)
2 parents c6131d8 + b0b96b7 commit a29f9a1

File tree

5 files changed

+36
-18
lines changed

5 files changed

+36
-18
lines changed

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,7 @@ class ReferencedTypeFinder : private TypeVisitor<ReferencedTypeFinder> {
15361536
friend TypeVisitor;
15371537

15381538
llvm::function_ref<void(ReferencedTypeFinder &, const TypeDecl *)> Callback;
1539-
bool IsWithinConstrainedObjCGeneric = false;
1539+
bool NeedsDefinition = false;
15401540

15411541
ReferencedTypeFinder(decltype(Callback) callback) : Callback(callback) {}
15421542

@@ -1614,28 +1614,30 @@ class ReferencedTypeFinder : private TypeVisitor<ReferencedTypeFinder> {
16141614
}
16151615

16161616
void visitBoundGenericType(BoundGenericType *boundGeneric) {
1617-
bool isObjCGeneric = boundGeneric->getDecl()->hasClangNode();
1617+
auto *decl = boundGeneric->getDecl();
1618+
1619+
NeedsDefinition = true;
1620+
Callback(*this, decl);
1621+
NeedsDefinition = false;
1622+
1623+
bool isObjCGeneric = decl->hasClangNode();
1624+
auto *sig = decl->getGenericSignature();
16181625

16191626
for_each(boundGeneric->getGenericArgs(),
16201627
boundGeneric->getDecl()->getGenericParams()->getPrimaryArchetypes(),
16211628
[&](Type argTy, const ArchetypeType *archetype) {
16221629
if (isObjCGeneric && isConstrainedArchetype(archetype))
1623-
IsWithinConstrainedObjCGeneric = true;
1630+
NeedsDefinition = true;
16241631
visit(argTy);
1625-
IsWithinConstrainedObjCGeneric = false;
1632+
NeedsDefinition = false;
16261633
});
1627-
1628-
// Ignore the base type; that either can't be exposed to Objective-C or
1629-
// was an Objective-C type to begin with. Every bound generic Swift type we
1630-
// care about gets mapped to a particular construct in Objective-C.
1631-
// (For example, Optional<NSFoo> is mapped to NSFoo *.)
16321634
}
16331635

16341636
public:
16351637
using TypeVisitor::visit;
16361638

1637-
bool isWithinConstrainedObjCGeneric() const {
1638-
return IsWithinConstrainedObjCGeneric;
1639+
bool needsDefinition() const {
1640+
return NeedsDefinition;
16391641
}
16401642

16411643
static void walk(Type ty, decltype(Callback) callback) {
@@ -1827,8 +1829,7 @@ class ModuleWriter {
18271829
if (TD == container)
18281830
return;
18291831

1830-
if (finder.isWithinConstrainedObjCGeneric() &&
1831-
isa<NominalTypeDecl>(TD)) {
1832+
if (finder.needsDefinition() && isa<NominalTypeDecl>(TD)) {
18321833
// We can delay individual members of classes; do so if necessary.
18331834
if (isa<ClassDecl>(container)) {
18341835
if (!tryRequire(TD)) {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// This file is meant to be included with modules turned off, compiled against
2+
// the fake clang-importer-sdk.
3+
#import <Foundation.h>
4+
5+
@interface SingleImportedObjCGeneric<A> : NSObject
6+
@end

test/PrintAsObjC/Inputs/custom-modules/module.map

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ module Base {
1919
}
2020
}
2121

22+
module SingleGenericClass {
23+
header "SingleGenericClass.h"
24+
export *
25+
}
26+
2227
module OverrideBase [system] {
2328
header "override.h"
2429
export *

test/PrintAsObjC/classes.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
// FIXME: END -enable-source-import hackaround
1414

1515

16-
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -emit-module -o %t %s -disable-objc-attr-requires-foundation-module
17-
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -parse-as-library %t/classes.swiftmodule -parse -emit-objc-header-path %t/classes.h -import-objc-header %S/../Inputs/empty.h -disable-objc-attr-requires-foundation-module
16+
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -emit-module -I %S/Inputs/custom-modules -o %t %s -disable-objc-attr-requires-foundation-module
17+
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -parse-as-library %t/classes.swiftmodule -parse -I %S/Inputs/custom-modules -emit-objc-header-path %t/classes.h -import-objc-header %S/../Inputs/empty.h -disable-objc-attr-requires-foundation-module
1818
// RUN: %FileCheck %s < %t/classes.h
1919
// RUN: %FileCheck --check-prefix=NEGATIVE %s < %t/classes.h
20-
// RUN: %check-in-clang %t/classes.h
21-
// RUN: not %check-in-clang -fno-modules -Qunused-arguments %t/classes.h
22-
// RUN: %check-in-clang -fno-modules -Qunused-arguments %t/classes.h -include Foundation.h -include CoreFoundation.h -include objc_generics.h
20+
// RUN: %check-in-clang -I %S/Inputs/custom-modules/ %t/classes.h
21+
// RUN: not %check-in-clang -I %S/Inputs/custom-modules/ -fno-modules -Qunused-arguments %t/classes.h
22+
// RUN: %check-in-clang -I %S/Inputs/custom-modules/ -fno-modules -Qunused-arguments %t/classes.h -include Foundation.h -include CoreFoundation.h -include objc_generics.h -include SingleGenericClass.h
2323

2424
// CHECK-NOT: AppKit;
2525
// CHECK-NOT: Properties;
@@ -28,12 +28,14 @@
2828
// CHECK-NEXT: @import CoreGraphics;
2929
// CHECK-NEXT: @import CoreFoundation;
3030
// CHECK-NEXT: @import objc_generics;
31+
// CHECK-NEXT: @import SingleGenericClass;
3132
// CHECK-NOT: AppKit;
3233
// CHECK-NOT: Swift;
3334
import Foundation
3435
import objc_generics
3536
import AppKit // only used in implementations
3637
import CoreFoundation
38+
import SingleGenericClass
3739

3840
// CHECK-LABEL: @interface A1{{$}}
3941
// CHECK-NEXT: init
@@ -715,6 +717,9 @@ public class NonObjCClass { }
715717
typealias Dipper = Spoon
716718
// CHECK: - (FungibleContainer<FungibleObject> * _Nonnull)fungibleContainerWithAliases:(FungibleContainer<Spoon *> * _Nullable)x;
717719
@objc func fungibleContainerWithAliases(_ x: FungibleContainer<Dipper>?) -> FungibleContainer<FungibleObject> { fatalError("") }
720+
721+
// CHECK: - (void)referenceSingleGenericClass:(SingleImportedObjCGeneric<id> * _Nullable)_;
722+
func referenceSingleGenericClass(_: SingleImportedObjCGeneric<AnyObject>?) {}
718723
}
719724
// CHECK: @end
720725

test/PrintAsObjC/lit.local.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ config.substitutions.insert(0, ('%check-in-clang',
55
'%%clang -fsyntax-only -x objective-c-header -fobjc-arc -fmodules '
66
'-fmodules-validate-system-headers '
77
'-Weverything -Werror -Wno-unused-macros -Wno-incomplete-module '
8+
'-Wno-auto-import '
89
'-I %%clang-include-dir '
910
'-isysroot %r/Inputs/clang-importer-sdk' % config.test_source_root) )

0 commit comments

Comments
 (0)