Skip to content

Commit c4e0586

Browse files
committed
[cxx-interop] Import ObjCPropertyDecl of type NS_OPTIONS fields a struct type
Try importing ObjCPropertyDecl field types the C++-Interop-NS_OPTIONS way. This will fix cases such as: ``` import UIKit func f(gesture: UISwipeGestureRecognizer, direction: UISwipeGestureRecognizer.Direction) { gesture.direction = direction // error } ``` because it will make sure the field inside class UIGestureRecognizer is of the enum-struct type and not the typedef-rawValue type when importing an ObjC class. (cherry picked from commit 7cef628)
1 parent 52b91b1 commit c4e0586

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5201,7 +5201,11 @@ namespace {
52015201
}
52025202
}
52035203

5204-
auto importedType = Impl.importPropertyType(decl, isInSystemModule(dc));
5204+
auto fieldType = decl->getType();
5205+
ImportedType importedType = tryImportOptionsTypeForField(fieldType, Impl);
5206+
5207+
if (!importedType)
5208+
importedType = Impl.importPropertyType(decl, isInSystemModule(dc));
52055209
if (!importedType) {
52065210
Impl.addImportDiagnostic(
52075211
decl, Diagnostic(diag::objc_property_not_imported, decl),

lib/ClangImporter/ImportType.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3212,6 +3212,9 @@ ImportedType ClangImporter::Implementation::importMethodParamsAndReturnType(
32123212
importedType.isImplicitlyUnwrapped()};
32133213
}
32143214

3215+
ImportedType tryImportOptionsTypeForField(const clang::QualType type,
3216+
ClangImporter::Implementation &Impl);
3217+
32153218
ImportedType ClangImporter::Implementation::importAccessorParamsAndReturnType(
32163219
const DeclContext *dc, const clang::ObjCPropertyDecl *property,
32173220
const clang::ObjCMethodDecl *clangDecl, bool isFromSystemModule,
@@ -3235,9 +3238,13 @@ ImportedType ClangImporter::Implementation::importAccessorParamsAndReturnType(
32353238
DeclContext *origDC = importDeclContextOf(property,
32363239
property->getDeclContext());
32373240
assert(origDC);
3241+
auto fieldType = isGetter ? clangDecl->getReturnType() : clangDecl->getParamDecl(0)->getType();
3242+
ImportedType importedNSOptionsType = tryImportOptionsTypeForField(fieldType, *this);
32383243

32393244
// Import the property type, independent of what kind of accessor this is.
32403245
auto importedType = importPropertyType(property, isFromSystemModule);
3246+
if (importedNSOptionsType)
3247+
importedType = importedNSOptionsType;
32413248
if (!importedType)
32423249
return {Type(), false};
32433250

test/Interop/Cxx/enum/Inputs/c-enums-NS_OPTIONS.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ struct HasNSOptionField {
8282
Bar bar;
8383
};
8484

85+
@interface HasNSOptionFieldObjC
86+
@property Bar bar;
87+
@end
88+
8589
Baz CFunctionReturningNSOption();
8690
void CFunctionTakingNSOption(Baz);
8791

test/Interop/Cxx/enum/c-enums-NS_OPTIONS.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@ import CenumsNSOptions
2525
// CHECK: struct HasNSOptionField {
2626
// CHECK: var bar: Bar
2727
// CHECK: }
28+
29+
// CHECK: class HasNSOptionFieldObjC {
30+
// CHECK-NEXT: var bar: Bar
31+
// CHECK-NEXT: class func bar() -> Bar
32+
// CHECK-NEXT: class func setBar(_ bar: Bar)
33+
// CHECK-NEXT: }

0 commit comments

Comments
 (0)