mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Sema: Allow some references to declarations that are unavailable-in-Swift.
Swift generates implicit constructors for inherited designated initializers in case some implicit initialization (such as running property initializer expressions) needs to run after calling the initializer on the superclass. These implicit initializers are printed in .swiftinterfaces and previously they could cause the interface to fail to typecheck when one of the parameters is declared to be unavailable-in-Swift. These initializers need to be generated because they may be called from Objective-C where the unavailable-in-Swift designation is irrelevant. To account for this possibility, relax availability checking to allow this narrow exception only in .swiftinterfaces. Another possible but more complicated solution would be to print the initializers with an attribute that indicates that the initializer is inherited and implicit. This would allow the typechecking exception to be more precise but seems unnecessarily complicated given that the exception is only needed in .swiftinterfaces, which are already compiler generated. Resolves rdar://77221357
This commit is contained in:
@@ -360,6 +360,27 @@ static bool isInsideCompatibleUnavailableDeclaration(
|
||||
inheritsAvailabilityFromPlatform(platform, *referencedPlatform));
|
||||
}
|
||||
|
||||
static bool shouldAllowReferenceToUnavailableInSwiftDeclaration(
|
||||
const Decl *D, const ExportContext &where) {
|
||||
auto *DC = where.getDeclContext();
|
||||
auto *SF = DC->getParentSourceFile();
|
||||
|
||||
// Unavailable-in-Swift declarations shouldn't be referenced directly in
|
||||
// source. However, they can be referenced in implicit declarations that are
|
||||
// printed in .swiftinterfaces.
|
||||
if (!SF || SF->Kind != SourceFileKind::Interface)
|
||||
return false;
|
||||
|
||||
if (auto constructor = dyn_cast_or_null<ConstructorDecl>(DC->getAsDecl())) {
|
||||
// Designated initializers inherited from an Obj-C superclass may have
|
||||
// parameters that are unavailable-in-Swift.
|
||||
if (constructor->isObjC())
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
/// A class to walk the AST to build the type refinement context hierarchy.
|
||||
@@ -3038,7 +3059,9 @@ bool swift::diagnoseExplicitUnavailability(
|
||||
break;
|
||||
|
||||
case PlatformAgnosticAvailabilityKind::UnavailableInSwift:
|
||||
// This API is explicitly unavailable in Swift.
|
||||
if (shouldAllowReferenceToUnavailableInSwiftDeclaration(D, Where))
|
||||
return true;
|
||||
|
||||
platform = "Swift";
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface FrameworkObject : NSObject
|
||||
- (nonnull instancetype)initWithInvocation:(nullable NSInvocation *)invocation NS_SWIFT_UNAVAILABLE("unavailable");
|
||||
- (nonnull instancetype)initWithSelector:(nonnull SEL)selector;
|
||||
- (nonnull instancetype)initWithInteger:(NSInteger)integer;
|
||||
NS_SWIFT_UNAVAILABLE("unavailable")
|
||||
@interface UnavailableInSwift : NSObject
|
||||
@end
|
||||
|
||||
@interface HasAvailableInit : NSObject
|
||||
- (nonnull instancetype)initWithUnavailable:(nonnull UnavailableInSwift *)unavailable;
|
||||
@end
|
||||
|
||||
@interface HasUnavailableInit : NSObject
|
||||
- (nonnull instancetype)initWithUnavailable:(nonnull UnavailableInSwift *)unavailable NS_SWIFT_UNAVAILABLE("unavailable");
|
||||
@end
|
||||
|
||||
|
||||
@@ -1,15 +1,21 @@
|
||||
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %s -I %S/Inputs/inherited-objc-initializers/
|
||||
// RUN: %target-swift-typecheck-module-from-interface(%t.swiftinterface) -I %S/Inputs/inherited-objc-initializers/
|
||||
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %s -I %S/Inputs/inherited-objc-initializers/ -module-name Test
|
||||
// RUN: %target-swift-typecheck-module-from-interface(%t.swiftinterface) -I %S/Inputs/inherited-objc-initializers/ -module-name Test
|
||||
// RUN: %FileCheck %s < %t.swiftinterface
|
||||
|
||||
// REQUIRES: objc_interop
|
||||
|
||||
import InheritedObjCInits
|
||||
|
||||
// CHECK: @objc @_inheritsConvenienceInitializers public class Subclass : InheritedObjCInits.FrameworkObject {
|
||||
public class Subclass: FrameworkObject {
|
||||
// CHECK-NEXT: @objc override dynamic public init(selector: ObjectiveC.Selector)
|
||||
// CHECK-NEXT: @objc override dynamic public init(integer: Swift.Int)
|
||||
// CHECK: @objc @_inheritsConvenienceInitializers public class Subclass2 : InheritedObjCInits.HasAvailableInit {
|
||||
public class Subclass2: HasAvailableInit {
|
||||
// CHECK-NEXT: @objc override dynamic public init(unavailable: InheritedObjCInits.UnavailableInSwift)
|
||||
// CHECK-NEXT: @objc override dynamic public init()
|
||||
// CHECK-NEXT: @objc deinit
|
||||
} // CHECK-NEXT:{{^}$}}
|
||||
|
||||
// CHECK: @objc @_inheritsConvenienceInitializers public class Subclass1 : InheritedObjCInits.HasUnavailableInit {
|
||||
public class Subclass1: HasUnavailableInit {
|
||||
// CHECK-NOT: InheritedObjCInits.UnavailableInSwift
|
||||
// CHECK-NEXT: @objc override dynamic public init()
|
||||
// CHECK-NEXT: @objc deinit
|
||||
} // CHECK-NEXT:{{^}$}}
|
||||
|
||||
Reference in New Issue
Block a user