Skip to content

Commit 609f9b5

Browse files
committed
[Clang importer] When importing a property as accessors, use accessor types.
When importing a property as accessor methods (rather than as a property), we were still importing the type of the accessor methods as if they were Swift getters and setters of a property, which (necessarily) homogenizes the types. The homogenization is unnecessary when importing as accessor methods, because the methods are independent, so just import the accessor method types as if the property did not exist. This is particularly useful for maintaining Swift 3 source compatibility for cases where Swift 4 turns a getter/setter pair into a null_resettable property. Fixes rdar://problem/30075571.
1 parent e564bdc commit 609f9b5

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

lib/ClangImporter/ImportDecl.cpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -3457,10 +3457,21 @@ namespace {
34573457
Optional<ForeignErrorConvention> errorConvention;
34583458
bodyParams.push_back(nullptr);
34593459
Type type;
3460+
3461+
// If we have a property accessor, find the corresponding property
3462+
// declaration.
3463+
const clang::ObjCPropertyDecl *prop = nullptr;
34603464
if (decl->isPropertyAccessor()) {
3461-
const clang::ObjCPropertyDecl *prop = decl->findPropertyDecl();
3462-
if (!prop)
3463-
return nullptr;
3465+
prop = decl->findPropertyDecl();
3466+
if (!prop) return nullptr;
3467+
3468+
// If we're importing just the accessors (not the property), ignore
3469+
// the property.
3470+
if (shouldImportPropertyAsAccessors(prop))
3471+
prop = nullptr;
3472+
}
3473+
3474+
if (prop) {
34643475
// If the matching property is in a superclass, or if the getter and
34653476
// setter are redeclared in a potentially incompatible way, bail out.
34663477
if (prop->getGetterMethodDecl() != decl &&

test/APINotes/Inputs/custom-frameworks/APINotesFrameworkTest.framework/Headers/APINotesFrameworkTest.apinotes

+27
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ SwiftVersions:
3939
- Version: 3.0
4040
Classes:
4141
- Name: TestProperties
42+
Methods:
43+
- Selector: accessorsOnlyRenamedRetyped
44+
MethodKind: Instance
45+
SwiftName: 'renamedAndRetyped()'
46+
ResultType: 'id _Nonnull'
47+
- Selector: 'setAccessorsOnlyRenamedRetyped:'
48+
MethodKind: Instance
49+
SwiftName: 'setRenamedAndRetyped(_:)'
50+
Parameters:
51+
- Position: 0
52+
Type: 'id _Nullable'
53+
- Selector: accessorsOnlyRenamedRetypedClass
54+
MethodKind: Class
55+
SwiftName: 'renamedAndRetypedClass()'
56+
ResultType: 'id _Nonnull'
57+
- Selector: 'setAccessorsOnlyRenamedRetypedClass:'
58+
MethodKind: Class
59+
SwiftName: 'setRenamedAndRetypedClass(_:)'
60+
Parameters:
61+
- Position: 0
62+
Type: 'id _Nullable'
4263
Properties:
4364
- Name: accessorsOnlyInVersion3
4465
PropertyKind: Instance
@@ -52,6 +73,12 @@ SwiftVersions:
5273
- Name: accessorsOnlyForClassExceptInVersion3
5374
PropertyKind: Class
5475
SwiftImportAsAccessors: false
76+
- Name: accessorsOnlyRenamedRetyped
77+
PropertyKind: Instance
78+
SwiftImportAsAccessors: true
79+
- Name: accessorsOnlyRenamedRetypedClass
80+
PropertyKind: Class
81+
SwiftImportAsAccessors: true
5582
Functions:
5683
- Name: acceptDoublePointer
5784
SwiftName: 'acceptPointer(_:)'

test/APINotes/Inputs/custom-frameworks/APINotesFrameworkTest.framework/Headers/Properties.h

+5
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@ __attribute__((objc_root_class))
2727
@property (nonatomic, readwrite, retain) id accessorsOnlyWithNewType;
2828
@end
2929

30+
@interface TestProperties (AccessorsOnlyCustomized)
31+
@property (nonatomic, readwrite, retain, null_resettable) id accessorsOnlyRenamedRetyped;
32+
@property (class, nonatomic, readwrite, retain, null_resettable) id accessorsOnlyRenamedRetypedClass;
33+
@end
34+
3035
#pragma clang assume_nonnull end

test/APINotes/properties.swift

+10
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,13 @@
4545
// CHECK-BOTH-DAG: func setAccessorsOnlyWithNewType(_ accessorsOnlyWithNewType: Base)
4646

4747
// CHECK-BOTH: {{^}$}}
48+
49+
// CHECK-SWIFT-3-DAG: func renamedAndRetyped() -> Any{{$}}
50+
// CHECK-SWIFT-3-DAG: func setRenamedAndRetyped(_ accessorsOnlyRenamedRetyped: Any?)
51+
// CHECK-SWIFT-4-DAG: var accessorsOnlyRenamedRetyped: Any!
52+
53+
// CHECK-SWIFT-3-DAG: class func renamedAndRetypedClass() -> Any{{$}}
54+
// CHECK-SWIFT-3-DAG: class func setRenamedAndRetypedClass(_ accessorsOnlyRenamedRetypedClass: Any?)
55+
// CHECK-SWIFT-4-DAG: class var accessorsOnlyRenamedRetypedClass: Any!
56+
57+
// CHECK-BOTH: {{^}$}}

0 commit comments

Comments
 (0)