Sema: Allow explicitly available overrides to be as available as their context#59857
Merged
tshortli merged 1 commit intoswiftlang:mainfrom Jul 11, 2022
Merged
Conversation
61609ab to
e8bbe23
Compare
…r context.
Previously, the following test case produced an erroneous diagnostic:
```
class A {
init() {}
}
@available(macOS 12, *)
class B: A {
@available(macOS 12, *)
override init() { // error: overriding 'init' must be as available as declaration it overrides
super.init()
}
}
```
The overridden `init()` constructor is as available as it can possibly be. Removing the explicit `@available` annotation suppressed the diagnostic.
To fix this, we check to see if the override is as available as its self type and accept it if it is.
You may be wondering how this works when the `@available` annotation is removed from `override init()` in the example. It turns out that `AvailabilityInference::availableRange()` returns a result that is based only on the explicit availability of the decl in question without taking the availability of the context into account (except when the context is an extension). So with the explicit annotation gone, both the base `init()` and the override are both considered to be "always" available. This is pretty unintuitive and arguably wrong. However, it seems like a lot of existing code depends on this behavior so I've left it for now.
Resolves rdar://96253347
e8bbe23 to
bd7a394
Compare
Contributor
Author
|
@swift-ci please smoke test |
xymus
approved these changes
Jul 7, 2022
Contributor
xymus
left a comment
There was a problem hiding this comment.
That's a nice change, I'm surprised we did without it for so long.
Contributor
|
Can we get a matching PR for release/5.7 as well? |
Contributor
Author
Done: #60185 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Previously, the following test case produced an erroneous diagnostic:
The overridden
init()constructor is as available as it can possibly be. Removing the explicit@availableannotation suppressed the diagnostic.To fix this, we check to see if the override is as available as its self type and accept it if it is.
You may be wondering how this works when the
@availableannotation is removed fromoverride init()in the example. It turns out thatAvailabilityInference::availableRange()returns a result that is based only on the explicit availability of the decl in question without taking the availability of the context into account (except when the context is an extension). So with the explicit annotation gone, both the baseinit()and the override are both considered to be "always" available. This is pretty unintuitive and arguably wrong. However, it seems like a lot of existing code depends on this behavior so I've left it for now.Resolves rdar://96253347