File: protocol_requirement.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (116 lines) | stat: -rw-r--r-- 3,400 bytes parent folder | download
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
// Test limitations on SPI protocol requirements.

// RUN: %target-typecheck-verify-swift -enable-library-evolution

// Reject SPI protocol requirements without a default implementation.
public protocol PublicProtoRejected {
  @_spi(Private)
  func reqWithoutDefault() // expected-error{{protocol requirement 'reqWithoutDefault()' cannot be declared '@_spi' without a default implementation in a protocol extension}}

  @_spi(Private)
  func reqWithSharedName(_: Int) // expected-error{{protocol requirement 'reqWithSharedName' cannot be declared '@_spi' without a default implementation in a protocol extension}}

  @_spi(Private)
  var property: Int { get set } // expected-error{{protocol requirement 'property' cannot be declared '@_spi' without a default implementation in a protocol extension}}

  @_spi(Private)
  var propertyWithoutSetter: Int { get set } // expected-error{{protocol requirement 'propertyWithoutSetter' cannot be declared '@_spi' without a default implementation in a protocol extension}}

  @_spi(Private)
  subscript(index: Int) -> Int { get set } // expected-error{{protocol requirement 'subscript(_:)' cannot be declared '@_spi' without a default implementation in a protocol extension}}

  @_spi(Private)
  init() // expected-error{{protocol requirement 'init()' cannot be declared '@_spi' without a default implementation in a protocol extension}}

  @_spi(Private) // expected-error{{'@_spi' attribute cannot be applied to this declaration}}
  associatedtype T
}

extension PublicProtoRejected {
  @_spi(Private)
  public var propertyWithoutSetter: Int { get { return 42 } }

  @_spi(Private)
  public func reqWithSharedName(_: String) {}
}

extension PublicProtoRejected where Self : Equatable {
  @_spi(Private)
  public func reqWithoutDefault() {
    // constrained implementation
  }
}

// Accept SPI protocol requirements with an implementation.
public protocol PublicProto {
  @_spi(Private)
  func reqWithDefaultImplementation()

  @_spi(Private)
  var property: Int { get set }

  @_spi(Private)
  subscript(index: Int) -> Int { get set }

  @_spi(Private)
  init()
}

extension PublicProto {
  @_spi(Private)
  public func reqWithDefaultImplementation() { }

  @_spi(Private)
  public var property: Int {
    get { return 42 }
    set { }
  }

  @_spi(Private)
  public subscript(index: Int) -> Int {
    get { return 42 }
    set { }
  }

  @_spi(Private)
  public init() { }
}

@_spi(Private)
public protocol SPIProtocol {
  @_spi(Private)
  func reqWithoutDefault()

  @_spi(Private)
  var property: Int { get set }

  @_spi(Private)
  var propertyWithoutSetter: Int { get set }

  @_spi(Private)
  subscript(index: Int) -> Int { get set }

  @_spi(Private)
  init()

  @_spi(Private)
  static var staticProperty: Int { get }
}

public protocol OtherProto {}
public protocol Proto {
  associatedtype A : Sequence where A.Element : OtherProto
}

public struct BadStruct {}
@_spi(Horse) extension BadStruct : OtherProto {}
public struct BadConforms : Proto { // expected-error {{cannot use conformance of 'BadStruct' to 'OtherProto' here; the conformance is declared as SPI}}
// expected-note@-1 {{in associated type 'Self.A.Element' (inferred as 'BadStruct')}}
  public typealias A = [BadStruct]
}

public struct OKStruct {}
@_spi(Horse) extension OKStruct : OtherProto {}
@_spi(Horse) public struct OKConforms : Proto {
  public typealias A = [OKStruct]
}