Skip to content

Commit d1e0f51

Browse files
authored
Merge pull request #3453 from milseman/id_as_any
[id as Any] Import with the proper Any type
2 parents 504035c + aa7b206 commit d1e0f51

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
@@ -1533,7 +1533,9 @@ namespace {
15331533
// should be removed from MappedTypes.def, and this conditional should
15341534
// become unnecessary.
15351535
if (Name.str() == "id" && Impl.SwiftContext.LangOpts.EnableIdAsAny) {
1536-
return nullptr;
1536+
Impl.SpecialTypedefNames[Decl->getCanonicalDecl()] =
1537+
MappedTypeNameKind::DoNothing;
1538+
return Impl.SwiftContext.getAnyDecl();
15371539
}
15381540

15391541
bool IsError;

lib/ClangImporter/ImportType.cpp

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

969969
// id maps to Any in bridgeable contexts, AnyObject otherwise.
970970
if (type->isObjCIdType()) {
971-
if (Impl.SwiftContext.LangOpts.EnableIdAsAny) {
972-
return { proto->getDeclaredType(),
973-
ImportHint(ImportHint::ObjCBridged,
974-
ProtocolCompositionType::get(Impl.SwiftContext, {})) };
975-
} else {
976-
return { proto->getDeclaredType(), ImportHint::ObjCPointer };
977-
}
971+
if (Impl.SwiftContext.LangOpts.EnableIdAsAny)
972+
return {
973+
proto->getDeclaredType(),
974+
ImportHint(ImportHint::ObjCBridged,
975+
Impl.SwiftContext.getAnyDecl()->getDeclaredType())};
976+
return {proto->getDeclaredType(), ImportHint::ObjCPointer};
978977
}
979978

980979
// 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)