File: property_wrappers.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 (122 lines) | stat: -rw-r--r-- 3,813 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
117
118
119
120
121
122
// RUN: %target-swift-frontend -typecheck -disable-availability-checking -dump-ast %s | %FileCheck %s

struct Transaction {
  var state: Int?
}

@propertyWrapper
struct WrapperWithClosureArg<Value> {
  var wrappedValue: Value

  init(wrappedValue: Value,
       reset: @escaping (Value, inout Transaction) -> Void) {
    self.wrappedValue = wrappedValue
  }
}

// rdar://problem/59685601
// CHECK-LABEL: R_59685601
struct R_59685601 {
  // CHECK:      argument_list implicit labels="wrappedValue:reset:"
  // CHECK-NEXT:   argument label="wrappedValue"
  // CHECK-NEXT:     property_wrapper_value_placeholder_expr implicit type="String"
  // CHECK-NEXT:     opaque_value_expr implicit type="String"
  // CHECK-NEXT:     string_literal_expr type="String"
  @WrapperWithClosureArg(reset: { value, transaction in
    transaction.state = 10
  })
  private var value = "hello"
}

@propertyWrapper
struct Wrapper<Value> {
  var wrappedValue: Value
}

// CHECK-LABEL: struct_decl{{.*}}TestInitSubscript
struct TestInitSubscript {
  enum Color: CaseIterable { case pink }

  // CHECK:      argument_list labels="wrappedValue:"
  // CHECK-NEXT:   argument label="wrappedValue"
  // CHECK-NEXT:     subscript_expr type="TestInitSubscript.Color"
  // CHECK:            argument_list implicit
  // CHECK-NEXT:         argument
  // CHECK-NOT:            property_wrapper_value_placeholder_expr implicit type="Int"
  // CHECK:                integer_literal_expr type="Int"
  @Wrapper(wrappedValue: Color.allCases[0])
  var color: Color
}

// https://github.com/apple/swift/issues/58201

@propertyWrapper
public class W_58201<Value> {
  private var _value: Value

  public var wrappedValue: Value {
    get { _value }
    set {
      _value = newValue
    }
  }

  public init(wrappedValue value: @autoclosure @escaping () -> Value) {
    self._value = value()
  }
}

// CHECK-LABEL: struct_decl{{.*}}S1_58201
struct S1_58201 {
  // CHECK:      argument_list implicit labels="wrappedValue:"
  // CHECK-NEXT:   argument label="wrappedValue"
  // CHECK-NEXT:     autoclosure_expr implicit type="() -> Bool?" discriminator=0 nonisolated captures=(<opaque_value> ) escaping
  // CHECK:            autoclosure_expr implicit type="() -> Bool?" discriminator=1 nonisolated escaping
  @W_58201 var a: Bool?
}

// CHECK-LABEL: struct_decl{{.*}}S2_58201
struct S2_58201 {
  // CHECK:      argument_list implicit labels="wrappedValue:"
  // CHECK-NEXT:   argument label="wrappedValue"
  // CHECK-NEXT:     autoclosure_expr implicit type="() -> Bool" location={{.*}}.swift:[[@LINE+2]]:26 range=[{{.+}}] discriminator=0 nonisolated captures=(<opaque_value> ) escaping
  // CHECK:            autoclosure_expr implicit type="() -> Bool" location={{.*}}.swift:[[@LINE+1]]:26 range=[{{.+}}] discriminator=1 nonisolated escaping
  @W_58201 var b: Bool = false
}

// https://github.com/apple/swift/issues/61570
// rdar://problem/101813792 - Check that captured variables are re-parented to backing var initializer
do {
  @propertyWrapper
  struct Wrapper {
    init(_: String) {}

    var wrappedValue: Int { return 0 }
  }

  struct TestExplicitCapture {
    @Wrapper("\([1,2,3].map({[x = 42] in "\($0 + x)" }).reduce("", +))")
    var wrapped: Int // Ok, becomes var _wrapped: Wrapper<Int> = Wrapper(wrappedValue: "\([1,2,3].map({[x = 42] in "\($0 + x)" }).reduce("", +)))
  }

  @propertyWrapper
  struct Option {
    let help: String
    var value: Double = 0.0

    var wrappedValue: Double {
      get { value }
      set { value = newValue }
    }
  }

  enum TestEnum: String, CaseIterable {
    case hello = "hello"
    case world = "world"
  }

  struct TestImplicitCapture {
    @Option(help: "Values: \(TestEnum.allCases.map(\.rawValue))")
    var value // Ok - $kp$ of key path is captured implicitly by backing variable init
  }
}