Skip to content

Commit 0418d1a

Browse files
Merge pull request #3440 from devincoughlin/suppress-availability-useless-deployment
[Sema] Remove 'deployment target ensures guard will always be true'
2 parents 0a12750 + c553de4 commit 0418d1a

File tree

7 files changed

+17
-25
lines changed

7 files changed

+17
-25
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2946,10 +2946,6 @@ ERROR(availability_inout_accessor_only_version_newer, none,
29462946
ERROR(availability_query_required_for_platform, none,
29472947
"condition required for target platform '%0'", (StringRef))
29482948

2949-
WARNING(availability_query_useless_min_deployment, none,
2950-
"unnecessary check for '%0'; minimum deployment target ensures guard "
2951-
"will always be true", (StringRef))
2952-
29532949
WARNING(availability_query_useless_enclosing_scope, none,
29542950
"unnecessary check for '%0'; enclosing scope ensures guard "
29552951
"will always be true", (StringRef))

lib/Sema/TypeChecker.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,19 +1316,15 @@ class TypeRefinementContextBuilder : private ASTWalker {
13161316
// spec is useless. If so, report this.
13171317
if (CurrentInfo.isContainedIn(NewConstraint)) {
13181318
DiagnosticEngine &Diags = TC.Diags;
1319-
if (CurrentTRC->getReason() == TypeRefinementContext::Reason::Root) {
1320-
// Diagnose for checks that are useless because the minimum deployment
1321-
// target ensures they will never be false. We suppress this warning
1322-
// when compiling for playgrounds because the developer cannot
1323-
// cannot explicitly set the minimum deployment target to silence
1324-
// the alarm. We also suppress in script mode (where setting the
1325-
// minimum deployment target requires a target triple).
1326-
if (!TC.getLangOpts().Playground && !TC.getInImmediateMode()) {
1327-
Diags.diagnose(Query->getLoc(),
1328-
diag::availability_query_useless_min_deployment,
1329-
platformString(targetPlatform(TC.getLangOpts())));
1330-
}
1331-
} else {
1319+
// Some availability checks will always pass because the minimum
1320+
// deployment target gurantees they will never be false. We don't
1321+
// diagnose these checks as useless because the source file may
1322+
// be shared with other projects/targets having older deployment
1323+
// targets. We don't currently have a mechanism for the user to
1324+
// suppress these warnings (for example, by indicating when the
1325+
// required compatibility version is different than the deployment
1326+
// target).
1327+
if (CurrentTRC->getReason() != TypeRefinementContext::Reason::Root) {
13321328
Diags.diagnose(Query->getLoc(),
13331329
diag::availability_query_useless_enclosing_scope,
13341330
platformString(targetPlatform(TC.getLangOpts())));

test/SILGen/availability_query.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ if #available(macOS 10.52, *) {
4242
// CHECK: [[PATCH:%.*]] = integer_literal $Builtin.Word, 0
4343
// CHECK: [[QUERY_FUNC:%.*]] = function_ref @_TFs26_stdlib_isOSVersionAtLeastFTBwBwBw_Bi1_ : $@convention(thin) (Builtin.Word, Builtin.Word, Builtin.Word) -> Builtin.Int1
4444
// CHECK: [[QUERY_RESULT:%.*]] = apply [[QUERY_FUNC]]([[MAJOR]], [[MINOR]], [[PATCH]]) : $@convention(thin) (Builtin.Word, Builtin.Word, Builtin.Word) -> Builtin.Int1
45-
if #available(OSX 10, *) { // expected-warning {{minimum deployment target ensures guard will always be true}}
45+
if #available(OSX 10, *) {
4646
}
4747

4848
// CHECK: }
4949

5050
func doThing() {}
5151

5252
func testUnreachableVersionAvailable(condition: Bool) {
53-
if #available(OSX 10.0, *) { // expected-warning {{minimum deployment target ensures guard will always be true}}
53+
if #available(OSX 10.0, *) {
5454
doThing() // no-warning
5555
return
5656
} else {

test/Sema/availability_versions.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ func useUnavailableExtension() {
10111011
func functionWithDefaultAvailabilityAndUselessCheck(_ p: Bool) {
10121012
// Default availability reflects minimum deployment: 10.9 and up
10131013

1014-
if #available(OSX 10.9, *) { // expected-warning {{unnecessary check for 'OSX'; minimum deployment target ensures guard will always be true}}
1014+
if #available(OSX 10.9, *) { // no-warning
10151015
let _ = globalFuncAvailableOn10_9()
10161016
}
10171017

@@ -1023,7 +1023,7 @@ func functionWithDefaultAvailabilityAndUselessCheck(_ p: Bool) {
10231023
}
10241024
}
10251025

1026-
if #available(OSX 10.9, *) { // expected-note {{enclosing scope here}} expected-warning {{unnecessary check for 'OSX'; minimum deployment target ensures guard will always be true}}
1026+
if #available(OSX 10.9, *) { // expected-note {{enclosing scope here}}
10271027
} else {
10281028
// Make sure we generate a warning about an unnecessary check even if the else branch of if is dead.
10291029
if #available(OSX 10.51, *) { // expected-warning {{unnecessary check for 'OSX'; enclosing scope ensures guard will always be true}}
@@ -1032,7 +1032,7 @@ func functionWithDefaultAvailabilityAndUselessCheck(_ p: Bool) {
10321032

10331033
// This 'if' is strictly to limit the scope of the guard fallthrough
10341034
if p {
1035-
guard #available(OSX 10.9, *) else { // expected-note {{enclosing scope here}} expected-warning {{unnecessary check for 'OSX'; minimum deployment target ensures guard will always be true}}
1035+
guard #available(OSX 10.9, *) else { // expected-note {{enclosing scope here}}
10361036
// Make sure we generate a warning about an unnecessary check even if the else branch of guard is dead.
10371037
if #available(OSX 10.51, *) { // expected-warning {{unnecessary check for 'OSX'; enclosing scope ensures guard will always be true}}
10381038
}

test/Sema/deprecation_osx.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func functionWithDeprecatedMethodInDeadElseBranch() {
156156
let _ = ClassDeprecatedIn10_9() // no-warning
157157
}
158158

159-
if #available(OSX 10.9, *) { // expected-warning {{unnecessary check for 'OSX'; minimum deployment target ensures guard will always be true}}
159+
if #available(OSX 10.9, *) { // no-warning
160160
} else {
161161
// This branch is dead because our minimum deployment target is 10.51.
162162
let _ = ClassDeprecatedIn10_9() // no-warning

test/attr/attr_availability_tvos.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ if #available(iOS 9.1, tvOS 9.2, *) {
7070
if #available(iOS 8.0, tvOS 9.2, *) {
7171
}
7272

73-
if #available(iOS 9.2, tvOS 8.0, *) { // expected-warning {{unnecessary check for 'tvOS'; minimum deployment target ensures guard will always be true}}
73+
if #available(iOS 9.2, tvOS 8.0, *) { // no-warning
7474
}
7575

7676

test/attr/attr_availability_watchos.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ if #available(iOS 9.1, watchOS 2.2, *) {
7070
if #available(iOS 8.0, watchOS 2.2, *) {
7171
}
7272

73-
if #available(iOS 9.2, watchOS 1.0, *) { // expected-warning {{unnecessary check for 'watchOS'; minimum deployment target ensures guard will always be true}}
73+
if #available(iOS 9.2, watchOS 1.0, *) { // no-warning
7474
}
7575

7676

0 commit comments

Comments
 (0)