Skip to content

Warn about unnecessary availability independently from the deployment target #37350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

artemcm
Copy link
Contributor

@artemcm artemcm commented May 10, 2021

This change separates emission of the "unnecessary availability" diagnostics:

unnecessary check for 'Platform'; enclosing scope ensures guard will always be true

from the deployment target of the current compilation. Instead, these diagnostics will only be emitted if the enclosing scope guard is explicitly specified by the user with an #availability attribute.

This fixes cases like the following:

@available(macOS 11.0, *)
class Foo {
    func foo() {
        if #available(macOS 11.1, *) {}
    }
}

Compiling this with -target x86_64-apple-macos11.2 results in:

warning: unnecessary check for 'macOS'; enclosing scope ensures guard will always be true
        if #available(macOS 11.1, *) {}
.../test.swift:2:7: note: enclosing scope here
class Foo {

Even though in source-code the enclosing scope (Foo) of this guard does not ensure it will always be true.
This happens because availability range is propagated by intersecting ranges top-down from the root TypeRefinementContext, which is defined by the deployment target, causing the availability range on class Foo to become 11.2.

Users may be sharing the same piece of source-code across different projects with their own respective deployment targets which makes such target-dependent warnings confusing at some times, and not-useful at others.
We should rather have the warning simply reflect what is in the source.

Resolves rdar://77607488

@artemcm artemcm requested review from xymus and slavapestov May 10, 2021 22:17
@artemcm
Copy link
Contributor Author

artemcm commented May 10, 2021

@swift-ci please test

Copy link
Contributor

@xymus xymus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good!

We should be able to use that structure for more availability improvements.

@@ -0,0 +1,12 @@
// Ensure that the `unnecessary check` availability warning is emitted when unnecessary due to
// scope's explicit annotation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be the description for the other test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks for catching this!

…yment target

This change separates emission of the diagnostics like:
```
unnecessary check for 'Platform'; enclosing scope ensures guard will always be true
```
from the deployment target of the current compilation. Instead, these diagnostics will only be emitted if the enclosing scope guard is explicitly specified by the user with an `#availability` attribute.

This fixes cases like the following:
```
@available(macOS 11.0, *)
class Foo {
    func foo() {
        if #available(macOS 11.1, *) {}
    }
}
```
Compiling this with `-target x86_64-apple-macos11.2` results in:
```
warning: unnecessary check for 'macOS'; enclosing scope ensures guard will always be true
        if #available(macOS 11.1, *) {}
.../test.swift:2:7: note: enclosing scope here
class Foo {
```
Even though in source-code the enclosing scope (`Foo`) of this guard does not ensure it will always be true.
This happens because availability range is propagated by intersecting ranges top-down from the root `TypeRefinementContext`, which is defined by the deployment target, causing the availability range on class `Foo` to become `11.2`.

Users may be sharing the same piece of source-code across different projects with their own respective deployment targets which makes such target-dependent warnings confusing at some times, and not-useful at others.
We should rather have the warning simply reflect what is in the source.

Resolves rdar://77607488
@artemcm artemcm force-pushed the DeployTargetIndependentAvailabilityDiagnostic branch from 655f724 to 5146bd5 Compare May 12, 2021 18:58
@artemcm
Copy link
Contributor Author

artemcm commented May 12, 2021

@swift-ci please smoke test

@artemcm artemcm merged commit 364f154 into swiftlang:main May 12, 2021
@artemcm artemcm deleted the DeployTargetIndependentAvailabilityDiagnostic branch May 12, 2021 22:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants