Skip to content

Commit 670f7b5

Browse files
committed
Optionals of imported value types are not @objc.
While we could bridge 'NSRange' to 'NSRange' and 'NSRange?' to 'NSValue * _Nullable', that (a) would be confusing, and (b) isn't actually implemented at the moment. Fix my mistake in 477933b to start accepting these.
1 parent e75dae4 commit 670f7b5

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

lib/AST/Type.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "swift/AST/LazyResolver.h"
2525
#include "swift/AST/Module.h"
2626
#include "swift/AST/TypeLoc.h"
27+
#include "swift/Basic/Fallthrough.h"
2728
#include "llvm/ADT/APFloat.h"
2829
#include "llvm/ADT/SmallPtrSet.h"
2930
#include "llvm/ADT/SmallString.h"
@@ -2112,25 +2113,23 @@ getForeignRepresentable(Type type, ForeignLanguage language,
21122113
if (nominal->hasClangNode() || nominal->isObjC()) {
21132114
switch (language) {
21142115
case ForeignLanguage::C:
2115-
// Imported structs and enums are trivially representable in C.
2116-
// FIXME: This is not entirely true; we need to check that
2117-
// all of the exposed parts are representable in C.
2118-
if (isa<StructDecl>(nominal) || isa<EnumDecl>(nominal)) {
2119-
if (wasOptional)
2120-
break;
2121-
return { ForeignRepresentableKind::Trivial, nullptr };
2122-
}
2123-
2124-
// Imported classes and protocols are not.
2116+
// Imported classes and protocols are not representable in C.
21252117
if (isa<ClassDecl>(nominal) || isa<ProtocolDecl>(nominal))
21262118
return failure();
2127-
2128-
llvm_unreachable("Unhandled nominal type declaration");
2119+
SWIFT_FALLTHROUGH;
21292120

21302121
case ForeignLanguage::ObjectiveC:
2131-
if (isa<StructDecl>(nominal) || isa<EnumDecl>(nominal))
2132-
if (wasOptional)
2122+
if (isa<StructDecl>(nominal) || isa<EnumDecl>(nominal)) {
2123+
// Optional structs are not representable in (Objective-)C if they
2124+
// originally came from C, whether or not they are bridged. If they
2125+
// are defined in Swift, they are only representable if they are
2126+
// bridged (checked below).
2127+
if (wasOptional) {
2128+
if (nominal->hasClangNode())
2129+
return failure();
21332130
break;
2131+
}
2132+
}
21342133

21352134
return { ForeignRepresentableKind::Trivial, nullptr };
21362135
}

test/attr/attr_objc.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,6 @@ class infer_instanceVar1 {
11061106
var var_Optional12: OpaquePointer?
11071107
var var_Optional13: UnsafeMutablePointer<Int>?
11081108
var var_Optional14: UnsafeMutablePointer<Class_ObjC1>?
1109-
var var_Optional15: NSRange? // another bridged struct
11101109

11111110
// CHECK-LABEL: @objc var var_Optional1: Class_ObjC1?
11121111
// CHECK-LABEL: @objc var var_Optional2: Protocol_ObjC1?
@@ -1122,7 +1121,6 @@ class infer_instanceVar1 {
11221121
// CHECK-LABEL: @objc var var_Optional12: OpaquePointer?
11231122
// CHECK-LABEL: @objc var var_Optional13: UnsafeMutablePointer<Int>?
11241123
// CHECK-LABEL: @objc var var_Optional14: UnsafeMutablePointer<Class_ObjC1>?
1125-
// CHECK-LABEL: @objc var var_Optional15: NSRange?
11261124

11271125

11281126
var var_ImplicitlyUnwrappedOptional1: Class_ObjC1!
@@ -1162,6 +1160,7 @@ class infer_instanceVar1 {
11621160
var var_Optional_fail20: AnyObject??
11631161
var var_Optional_fail21: AnyObject.Type??
11641162
var var_Optional_fail22: NSComparisonResult? // a non-bridged imported value type
1163+
var var_Optional_fail23: NSRange? // a bridged struct imported from C
11651164
// CHECK-NOT: @objc{{.*}}Optional_fail
11661165

11671166
// CHECK-LABEL: @objc var var_CFunctionPointer_1: @convention(c) () -> ()

0 commit comments

Comments
 (0)