Do not import objc_direct constructors

Unlike normal Objective-C functions, Swift does not directly call
Objective-C constructors because it creates a thunk to allocate the
object and call then constructor dynamically. This can be fixed, but for
now emit a compile-time error rather than a runtime error.
This commit is contained in:
Ellis Hoag
2021-11-17 14:38:28 -08:00
committed by Ellis Hoag
parent d9fd6cbdbf
commit 96bc8210c5
3 changed files with 20 additions and 4 deletions

View File

@@ -8788,10 +8788,22 @@ void ClangImporter::Implementation::importAttributes(
if (method->isDirectMethod() && !AnyUnavailable) {
assert(isa<AbstractFunctionDecl>(MappedDecl) &&
"objc_direct declarations are expected to be an AbstractFunctionDecl");
MappedDecl->getAttrs().add(new (C) FinalAttr(/*IsImplicit=*/true));
if (auto accessorDecl = dyn_cast<AccessorDecl>(MappedDecl)) {
auto attr = new (C) FinalAttr(/*isImplicit=*/true);
accessorDecl->getStorage()->getAttrs().add(attr);
if (isa<ConstructorDecl>(MappedDecl)) {
// TODO: Teach Swift how to directly call these functions.
auto attr = AvailableAttr::createPlatformAgnostic(
C,
"Swift cannot call Objective-C initializers marked with "
"'objc_direct'",
/*Rename*/ "",
PlatformAgnosticAvailabilityKind::UnavailableInSwift);
MappedDecl->getAttrs().add(attr);
AnyUnavailable = true;
} else {
MappedDecl->getAttrs().add(new (C) FinalAttr(/*IsImplicit=*/true));
if (auto accessorDecl = dyn_cast<AccessorDecl>(MappedDecl)) {
auto attr = new (C) FinalAttr(/*isImplicit=*/true);
accessorDecl->getStorage()->getAttrs().add(attr);
}
}
}
}