Skip to content

Commit aa7b206

Browse files
committed
[id as Any] Import with the proper Any type
Changes how we import from just protocol<> to the proper Any type. This means that uses of AnyObject will either implicitly cast up to Any, or must be explicitly cast through a temporary if used as an lvalue. id as Any is still predicated on the id-as-any flags.
1 parent cd9b480 commit aa7b206

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

lib/AST/Type.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,7 @@ bool TypeBase::hasReferenceSemantics() {
9090
}
9191

9292
bool TypeBase::isAny() {
93-
if (auto comp = getAs<ProtocolCompositionType>())
94-
if (comp->getProtocols().empty())
95-
return true;
96-
97-
return false;
93+
return isEqual(getASTContext().getAnyDecl()->getDeclaredType());
9894
}
9995

10096
bool TypeBase::isAnyClassReferenceType() {

lib/ClangImporter/ImportDecl.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1532,7 +1532,9 @@ namespace {
15321532
// should be removed from MappedTypes.def, and this conditional should
15331533
// become unnecessary.
15341534
if (Name.str() == "id" && Impl.SwiftContext.LangOpts.EnableIdAsAny) {
1535-
return nullptr;
1535+
Impl.SpecialTypedefNames[Decl->getCanonicalDecl()] =
1536+
MappedTypeNameKind::DoNothing;
1537+
return Impl.SwiftContext.getAnyDecl();
15361538
}
15371539

15381540
bool IsError;

lib/ClangImporter/ImportType.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -928,13 +928,12 @@ namespace {
928928

929929
// id maps to Any in bridgeable contexts, AnyObject otherwise.
930930
if (type->isObjCIdType()) {
931-
if (Impl.SwiftContext.LangOpts.EnableIdAsAny) {
932-
return { proto->getDeclaredType(),
933-
ImportHint(ImportHint::ObjCBridged,
934-
ProtocolCompositionType::get(Impl.SwiftContext, {})) };
935-
} else {
936-
return { proto->getDeclaredType(), ImportHint::ObjCPointer };
937-
}
931+
if (Impl.SwiftContext.LangOpts.EnableIdAsAny)
932+
return {
933+
proto->getDeclaredType(),
934+
ImportHint(ImportHint::ObjCBridged,
935+
Impl.SwiftContext.getAnyDecl()->getDeclaredType())};
936+
return {proto->getDeclaredType(), ImportHint::ObjCPointer};
938937
}
939938

940939
// Class maps to AnyObject.Type.

test/ClangModules/objc_id_as_any.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ struct ArbitraryThing {}
1515
idLover.takesId(ArbitraryThing())
1616

1717
var x: AnyObject = NSObject()
18-
idLover.takesArray(ofId: &x)
18+
idLover.takesArray(ofId: &x) // expected-error{{cannot pass immutable value as inout argument: implicit conversion from 'AnyObject' to 'Any' requires a temporary}}
19+
var xAsAny = x as Any
20+
idLover.takesArray(ofId: &xAsAny)
21+
1922
var y: Any = NSObject()
20-
idLover.takesArray(ofId: &y) // expected-error{{}}
23+
idLover.takesArray(ofId: &y)
24+
25+
idLover.takesId(x)
26+
idLover.takesId(y)

0 commit comments

Comments
 (0)