1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
// RUN: %target-typecheck-verify-swift
protocol P1 {
subscript (i: Int) -> Int { get } // expected-note{{protocol requires subscript with type '(Int) -> Int'}}
}
class C1 : P1 {
subscript (i: Int) -> Int {
get {
return i
}
set {}
}
}
struct S1 : P1 {
subscript (i: Int) -> Int {
get {
return i
}
set {}
}
}
struct S1Error : P1 { // expected-error{{type 'S1Error' does not conform to protocol 'P1'}}
subscript (i: Int) -> Double { // expected-note{{candidate has non-matching type '(Int) -> Double'}}
get {
return Double(i)
}
set {}
}
}
//===----------------------------------------------------------------------===//
// Get-only subscript requirements
//===----------------------------------------------------------------------===//
protocol SubscriptGet {
subscript(a : Int) -> Int { get } // expected-note {{protocol requires subscript with type '(Int) -> Int'; add a stub for conformance}}
}
class SubscriptGet_Get : SubscriptGet {
subscript(a : Int) -> Int { return 0 } // ok
// for static cross-conformance test below: expected-note@-1 {{candidate operates on an instance, not a type as required}}
}
class SubscriptGet_GetSet : SubscriptGet {
subscript(a : Int) -> Int { get { return 42 } set {} } // ok
}
//===----------------------------------------------------------------------===//
// Get-set subscript requirements
//===----------------------------------------------------------------------===//
protocol SubscriptGetSet {
subscript(a : Int) -> Int { get set } // expected-note {{protocol requires subscript with type '(Int) -> Int'}}
}
class SubscriptGetSet_Get : SubscriptGetSet { // expected-error {{type 'SubscriptGetSet_Get' does not conform to protocol 'SubscriptGetSet'}}
subscript(a : Int) -> Int { return 0 } // expected-note {{candidate is not settable, but protocol requires it}}
}
class SubscriptGetSet_GetSet : SubscriptGetSet {
subscript(a : Int) -> Int { get { return 42 } set {} } // ok
}
//===----------------------------------------------------------------------===//
// Generic subscript requirements
//===----------------------------------------------------------------------===//
protocol Initable {
init()
}
protocol GenericSubscriptProtocol {
subscript<T : Initable>(t: T.Type) -> T { get set }
// expected-note@-1 {{protocol requires subscript with type '<T> (T.Type) -> T'; add a stub for conformance}}
}
struct GenericSubscriptWitness : GenericSubscriptProtocol {
subscript<T : Initable>(t: T.Type) -> T {
get {
return t.init()
}
set { }
}
}
struct GenericSubscriptNoWitness : GenericSubscriptProtocol {}
// expected-error@-1 {{type 'GenericSubscriptNoWitness' does not conform to protocol 'GenericSubscriptProtocol'}}
//===----------------------------------------------------------------------===//
// Static subscript requirements
//===----------------------------------------------------------------------===//
protocol StaticSubscriptGet {
static subscript(a : Int) -> Int { get } // expected-note {{protocol requires subscript with type '(Int) -> Int'; add a stub for conformance}}
}
class StaticSubscriptGet_Get : StaticSubscriptGet {
static subscript(a : Int) -> Int { return 0 } // ok
// for static cross-conformance test below: expected-note@-1 {{candidate operates on a type, not an instance as required}}
}
class StaticSubscriptGet_GetSet : StaticSubscriptGet {
static subscript(a : Int) -> Int { get { return 42 } set {} } // ok
}
protocol StaticSubscriptGetSet {
static subscript(a : Int) -> Int { get set } // expected-note {{protocol requires subscript with type '(Int) -> Int'}}
}
class StaticSubscriptGetSet_Get : StaticSubscriptGetSet { // expected-error {{type 'StaticSubscriptGetSet_Get' does not conform to protocol 'StaticSubscriptGetSet'}}
static subscript(a : Int) -> Int { return 0 } // expected-note {{candidate is not settable, but protocol requires it}}
}
class StaticSubscriptGetSet_GetSet : StaticSubscriptGetSet {
static subscript(a : Int) -> Int { get { return 42 } set {} } // ok
}
extension SubscriptGet_Get: StaticSubscriptGet {} // expected-error {{type 'SubscriptGet_Get' does not conform to protocol 'StaticSubscriptGet'}}
extension StaticSubscriptGet_Get: SubscriptGet {} // expected-error {{type 'StaticSubscriptGet_Get' does not conform to protocol 'SubscriptGet'}}
|