mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
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:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user