mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[Serialization] Store whether an override depends on its base for ABI (#27784)
In some circumstances, a Swift declaration in module A will depend on another declaration (usually from Objective-C) that can't be loaded, for whatever reason. If the Swift declaration is *overriding* the missing declaration, this can present a problem, because the way methods are dispatched in Swift can depend on knowing the original class that introduced the method. However, if the compiler can prove that the override can still be safely invoked/used in all cases, it doesn't need to worry about the overridden declaration being missing. This is especially relevant for property accessors, because there's currently no logic to recover from a property being successfully deserialized and then finding out that an accessor couldn't be. The decision of whether or not an override can be safely invoked without knowledge of the base method is something to be cautious about---a mistaken analysis would effectively be a miscompile. So up until now, this was limited to one case: when a method is known to be `@objc dynamic`, i.e. always dispatched through objc_msgSend. (Even this may become questionable if we have first-class method references, like we do for key paths.) This worked particularly well because the compiler infers 'dynamic' for any overload of an imported Objective-C method or accessor, in case it imports differently in a different -swift-version and a client ends up subclassing it. However...that inference does not apply if the class is final, because then there are no subclasses to worry about. This commit changes the test to be more careful: if the /missing/ declaration was `@objc dynamic`, we know that it can't affect ABI, because either the override is properly `@objc dynamic` as well, or the override has introduced its own calling ABI (in practice, a direct call for final methods) that doesn't depend on the superclass. Again, this isn't 100% correct in the presence of first-class methods, but it does fix the issue in practice where a property accessor in a parent class goes missing. And since Objective-C allows adding property setters separately from the original property declaration, that's something that can happen even under normal circumstances. Sadly. This approach could probably be extended to constructors as well. I'm a little more cautious about throwing vars and subscripts into the mix because of the presence of key paths, which do allow identity-based comparison of overrides and bases. rdar://problem/56388950
This commit is contained in:
@@ -52,7 +52,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
|
||||
/// describe what change you made. The content of this comment isn't important;
|
||||
/// it just ensures a conflict if two people change the module format.
|
||||
/// Don't worry about adhering to the 80-column limit for this line.
|
||||
const uint16_t SWIFTMODULE_VERSION_MINOR = 519; // SIL function availability
|
||||
const uint16_t SWIFTMODULE_VERSION_MINOR = 521; // "overridden affects ABI" flag
|
||||
|
||||
/// A standard hash seed used for all string hashes in a serialized module.
|
||||
///
|
||||
@@ -1200,6 +1200,7 @@ namespace decls_block {
|
||||
BCFixed<1>, // IUO result?
|
||||
DeclIDField, // operator decl
|
||||
DeclIDField, // overridden function
|
||||
BCFixed<1>, // whether the overridden decl affects ABI
|
||||
BCVBR<5>, // 0 for a simple name, otherwise the number of parameter name
|
||||
// components plus one
|
||||
AccessLevelField, // access level
|
||||
@@ -1241,6 +1242,7 @@ namespace decls_block {
|
||||
TypeIDField, // result interface type
|
||||
BCFixed<1>, // IUO result?
|
||||
DeclIDField, // overridden function
|
||||
BCFixed<1>, // whether the overridden decl affects ABI
|
||||
DeclIDField, // AccessorStorageDecl
|
||||
AccessorKindField, // accessor kind
|
||||
AccessLevelField, // access level
|
||||
|
||||
Reference in New Issue
Block a user