mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
That is, if there's a problem with a witness, and the witness comes from a different extension from the conformance (or the original type, when the conformance is on an extension), put the main diagnostic on the conformance, with a note on the witness. This involves some shuffling and rephrasing of existing diagnostics too. There's a few reasons for this change: - More context. It may not be obvious why a declaration in file A.swift needs to be marked 'public' if you can't see the conformance in B.swift. - Better locations for imported declarations. If you're checking a conformance in a source file but the witness came from an imported module, it's better to put the diagnostic on the part you have control over. (This is especially true in Xcode, which can't display diagnostics on imported declarations in the source editor.) - Plays better with batch mode. Without this change, you can have diagnostics being reported in file A.swift that are tied to a conformance declared in file B.swift. Of course the contents of A.swift also affect the diagnostic, but compiling A.swift on its own wouldn't produce the diagnostic, and so putting it there is problematic. The change does in some cases make for a worse user experience, though; if you just want to apply the changes and move on, the main diagnostic isn't in the "right place". It's the note that has the info and possible fix-it. It's also a slightly more complicated implementation.
43 lines
1.5 KiB
Swift
43 lines
1.5 KiB
Swift
// RUN: %target-typecheck-verify-swift -swift-version 4 -enable-objc-interop
|
|
|
|
// An @objc protocol can have 'unavailable'
|
|
// methods. They are treated as if they
|
|
// were marked optional
|
|
@objc protocol Proto {
|
|
@objc @available(*,unavailable) optional func bad()
|
|
func good()
|
|
}
|
|
|
|
class Foo : Proto {
|
|
@objc func good() {}
|
|
}
|
|
|
|
// Reject protocols with 'unavailable' requirements
|
|
// if a protocol is not marked @objc.
|
|
protocol NonObjCProto {
|
|
@available(*,unavailable) func bad() // expected-error {{protocol members can only be marked unavailable in an @objc protocol}} expected-note {{protocol requires function 'bad()'}}
|
|
func good()
|
|
}
|
|
|
|
class Bar : NonObjCProto { // expected-error {{type 'Bar' does not conform to protocol 'NonObjCProto'}}
|
|
func good() {}
|
|
}
|
|
|
|
|
|
// Complain about unavailable witnesses (error in Swift 4, warning in Swift 3)
|
|
protocol P {
|
|
func foo(bar: Foo) // expected-note 2 {{requirement 'foo(bar:)' declared here}}
|
|
}
|
|
|
|
struct ConformsToP : P { // expected-error{{type 'ConformsToP' does not conform to protocol 'P'}}
|
|
@available(*, unavailable)
|
|
func foo(bar: Foo) { } // expected-error{{unavailable instance method 'foo(bar:)' was used to satisfy a requirement of protocol 'P'}}
|
|
}
|
|
|
|
struct ConformsToP2 {
|
|
@available(*, unavailable)
|
|
func foo(bar: Foo) { } // expected-note {{'foo(bar:)' declared here}}
|
|
}
|
|
extension ConformsToP2: P {} // expected-error{{type 'ConformsToP2' does not conform to protocol 'P'}}
|
|
// expected-error@-1 {{unavailable instance method 'foo(bar:)' was used to satisfy a requirement of protocol 'P'}}
|