Skip to content

Commit 7483a6f

Browse files
committed
Sema: Any is representable-in-ObjC in bridged positions.
1 parent 231db9d commit 7483a6f

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

lib/AST/Type.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,6 +1951,14 @@ getObjCObjectRepresentable(Type type, const DeclContext *dc) {
19511951
if (type->isObjCExistentialType())
19521952
return ForeignRepresentableKind::Object;
19531953

1954+
// Any can be bridged to id.
1955+
if (type->getASTContext().LangOpts.EnableIdAsAny) {
1956+
if (auto protoCompTy = type->getAs<ProtocolCompositionType>()) {
1957+
if (protoCompTy->getProtocols().empty())
1958+
return ForeignRepresentableKind::Bridged;
1959+
}
1960+
}
1961+
19541962
// Class-constrained generic parameters, from ObjC generic classes.
19551963
if (auto tyContext = dc->getInnermostTypeContext())
19561964
if (auto clas = tyContext->getAsClassOrClassExtensionContext())

test/attr/attr_objc_any.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %target-swift-frontend -parse -verify %s -enable-id-as-any
2+
// REQUIRES: objc_interop
3+
4+
import Foundation
5+
6+
@objc var foo: Any // expected-error {{@objc can only be used with members of classes, @objc protocols, and concrete extensions of classes}}
7+
8+
class Foo: NSObject {
9+
override init() {}
10+
11+
@objc var property: Any
12+
13+
@objc func method(x: Any) -> Any { return x }
14+
15+
@objc func indirectAny(x: UnsafePointer<Any>) {} // expected-error{{type of the parameter cannot be represented in Objective-C}}
16+
}

0 commit comments

Comments
 (0)