Skip to content

Commit a6516a9

Browse files
committed
[Type checker] Bump warning about unavailable witnesses to an error in Swift 4
Swift 3 allowed a requirement to be satisfied by an unavailable witness, which doesn't make sense. We've been warning about it in Swift 3 for a while; make it an error in Swift 4.
1 parent d84e4b4 commit a6516a9

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1591,7 +1591,10 @@ NOTE(declared_protocol_conformance_here,none,
15911591
"%0 implicitly conforms to protocol %2}1 here",
15921592
(Type, unsigned, DeclName, DeclName))
15931593

1594-
WARNING(witness_unavailable,none,
1594+
WARNING(witness_unavailable_warn,none,
1595+
"unavailable %0 %1 was used to satisfy a requirement of protocol %2",
1596+
(DescriptiveDeclKind, DeclName, DeclName))
1597+
ERROR(witness_unavailable,none,
15951598
"unavailable %0 %1 was used to satisfy a requirement of protocol %2",
15961599
(DescriptiveDeclKind, DeclName, DeclName))
15971600

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3003,11 +3003,15 @@ ConformanceChecker::resolveWitnessViaLookup(ValueDecl *requirement) {
30033003

30043004
break;
30053005

3006-
case CheckKind::WitnessUnavailable:
3007-
diagnoseOrDefer(requirement, /*isError=*/false,
3008-
[witness, requirement](NormalProtocolConformance *conformance) {
3006+
case CheckKind::WitnessUnavailable: {
3007+
bool emitError = !witness->getASTContext().LangOpts.isSwiftVersion3();
3008+
diagnoseOrDefer(requirement, /*isError=*/emitError,
3009+
[witness, requirement, emitError](
3010+
NormalProtocolConformance *conformance) {
30093011
auto &diags = witness->getASTContext().Diags;
3010-
diags.diagnose(witness, diag::witness_unavailable,
3012+
diags.diagnose(witness,
3013+
emitError ? diag::witness_unavailable
3014+
: diag::witness_unavailable_warn,
30113015
witness->getDescriptiveKind(),
30123016
witness->getFullName(),
30133017
conformance->getProtocol()->getFullName());
@@ -3016,6 +3020,7 @@ ConformanceChecker::resolveWitnessViaLookup(ValueDecl *requirement) {
30163020
});
30173021
break;
30183022
}
3023+
}
30193024

30203025
ClassDecl *classDecl = Adoptee->getClassOrBoundGenericClass();
30213026

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %target-typecheck-verify-swift -swift-version 3
2+
3+
class Foo { }
4+
5+
// Complain about unavailable witnesses (error in Swift 4, warning in Swift 3)
6+
protocol P {
7+
func foo(bar: Foo) // expected-note{{requirement 'foo(bar:)' declared here}}
8+
}
9+
10+
struct ConformsToP : P {
11+
@available(*, unavailable)
12+
func foo(bar: Foo) { } // expected-warning{{unavailable instance method 'foo(bar:)' was used to satisfy a requirement of protocol 'P'}}
13+
}
14+

test/decl/protocol/req/unavailable.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift
1+
// RUN: %target-typecheck-verify-swift -swift-version 4
22

33
// An @objc protocol can have 'unavailable'
44
// methods. They are treated as if they
@@ -24,12 +24,12 @@ class Bar : NonObjCProto { // expected-error {{type 'Bar' does not conform to pr
2424
}
2525

2626

27-
// Warn about unavailable witnesses.
27+
// Complain about unavailable witnesses (error in Swift 4, warning in Swift 3)
2828
protocol P {
2929
func foo(bar: Foo) // expected-note{{requirement 'foo(bar:)' declared here}}
3030
}
3131

32-
struct ConformsToP : P {
32+
struct ConformsToP : P { // expected-error{{type 'ConformsToP' does not conform to protocol 'P'}}
3333
@available(*, unavailable)
34-
func foo(bar: Foo) { } // expected-warning{{unavailable instance method 'foo(bar:)' was used to satisfy a requirement of protocol 'P'}}
34+
func foo(bar: Foo) { } // expected-error{{unavailable instance method 'foo(bar:)' was used to satisfy a requirement of protocol 'P'}}
3535
}

0 commit comments

Comments
 (0)