File: implicit_double_cgfloat_conversion.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 (344 lines) | stat: -rw-r--r-- 15,653 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// RUN: %target-typecheck-verify-swift %clang-importer-sdk
// RUN: %target-swift-emit-silgen(mock-sdk: %clang-importer-sdk) -swift-version 5 -verify %s | %FileCheck %s

// REQUIRES: objc_interop

import Foundation
import CoreGraphics

func test_to_cgfloat(_: CGFloat) {}
func test_from_cgfloat(_: Double) {}

func test_returns_double(_: CGFloat) -> Double {
  42.0
}

func test_returns_cgfloat(_: Double) -> CGFloat {
  42.0
}

let d: Double    = 0.0
let cgf: CGFloat = 0.0

// CHECK: test_various_situations_converting_to_cgfloat()
func test_various_situations_converting_to_cgfloat() {
  // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC : $@convention(method) (Double, @thin CGFloat.Type) -> CGFloat
  let _: CGFloat = d

  // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC : $@convention(method) (Double, @thin CGFloat.Type) -> CGFloat
  // CHECK: function_ref @$s34implicit_double_cgfloat_conversion08test_to_C0yy12CoreGraphics7CGFloatVF : $@convention(thin) (CGFloat) -> ()
  test_to_cgfloat(d)

  // CHECK: function_ref @$sSd1poiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double
  // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC : $@convention(method) (Double, @thin CGFloat.Type) -> CGFloat
  test_to_cgfloat(d + d) // One implicit conversion, `+` is (Double, Double) -> Double

  // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double
  // CHECK: function_ref @$sSd1poiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double
  // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC : $@convention(method) (Double, @thin CGFloat.Type) -> CGFloat
  test_to_cgfloat(d + cgf) // Two conversions but `+` on Double is still preferred.

  // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double
  // CHECK: function_ref @$sSd1poiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double
  // CHECK: function_ref @$sSd1soiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double
  // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC : $@convention(method) (Double, @thin CGFloat.Type) -> CGFloat
  test_to_cgfloat(d + cgf - d) // One conversion (`cgf` -> Double) inside and one outside, both operators preferred on Double.

  // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double
  // CHECK: function_ref @$sSd1poiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double
  // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double
  // CHECK: function_ref @$sSd1soiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double
  // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC : $@convention(method) (Double, @thin CGFloat.Type) -> CGFloat
  test_to_cgfloat(d + cgf - cgf) // Double is always preferred over CGFloat, so three conversion here.

  // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC : $@convention(method) (Double, @thin CGFloat.Type) -> CGFloat
  // CHECK: function_ref @$s34implicit_double_cgfloat_conversion013test_returns_B0ySd12CoreGraphics7CGFloatVF : $@convention(thin) (CGFloat) -> Double
  // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC : $@convention(method) (Double, @thin CGFloat.Type) -> CGFloat
  // CHECK: function_ref @$s34implicit_double_cgfloat_conversion08test_to_C0yy12CoreGraphics7CGFloatVF : $@convention(thin) (CGFloat) -> ()
  test_to_cgfloat(test_returns_double(d)) // Two conversions

  // Overloads with CGFloat are preferred if that allows to avoid any implicit conversions.
  func test_loading_tuple_elements(values: inout (CGFloat, CGFloat)) {
    struct S {
      init(x: Double, y: Double) {}
      init(x: CGFloat, y: CGFloat) {}
    }

    // CHECK: function_ref @$s34implicit_double_cgfloat_conversion038test_various_situations_converting_to_C0yyF0E23_loading_tuple_elementsL_6valuesy12CoreGraphics7CGFloatV_AGtz_tF1SL_V1x1yAiG_AGtcfC : $@convention(method) (CGFloat, CGFloat, @thin S.Type) -> S
    _ = S(x: 0.0, y: values.0) // Ok
  }
}

// CHECK: test_various_situations_converting_to_double()
func test_various_situations_converting_to_double() {
  // function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double
  let _: Double = cgf

  // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double
  // CHECK: function_ref @$s34implicit_double_cgfloat_conversion010test_from_C0yySdF : $@convention(thin) (Double) -> ()
  test_from_cgfloat(cgf)
  // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double
  // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double
  // CHECK: function_ref @$sSd1poiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double
  test_from_cgfloat(cgf + cgf) // Two conversions for `cgf` and `+` is on `Double`
  // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double
  // CHECK: function_ref @$sSd1poiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double
  test_from_cgfloat(d + cgf) // One conversion `cgf` to Double and `+` is on `Double`
  // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double
  // CHECK: function_ref @$sSd1poiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double
  // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double
  // CHECK: function_ref @$sSd1soiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double
  test_from_cgfloat(cgf + d - cgf) // Two conversions of `cgf` and both `+` and `-` are on Double
  // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double
  // CHECK: function_ref @$sSd1poiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double
  // CHECK: function_ref @$sSd1soiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double
  // CHECK: function_ref @$s34implicit_double_cgfloat_conversion010test_from_C0yySdF : $@convention(thin) (Double) -> ()
  test_from_cgfloat(cgf + d - d) // One conversion and both operators are on Double

  // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double
  // CHECK: function_ref @$s34implicit_double_cgfloat_conversion013test_returns_C0y12CoreGraphics7CGFloatVSdF : $@convention(thin) (Double) -> CGFloat
  // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double
  // CHECK: function_ref @$s34implicit_double_cgfloat_conversion010test_from_C0yySdF : $@convention(thin) (Double) -> ()
  test_from_cgfloat(test_returns_cgfloat(cgf)) // Two conversions - argument and result.
}

func test_conversions_with_optionals(v: CGFloat?) {
  // CHECK: function_ref @$s34implicit_double_cgfloat_conversion31test_conversions_with_optionals1vy12CoreGraphics7CGFloatVSg_tFAFyKXEfu_
  // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double
  let _: Double = (v ?? 0)
}

func test_static_members_are_contextually_convertible() {
  struct S {
    static var testProp: CGFloat { 42 }
    static func testFunc() -> CGFloat { 42 }
  }

  func test_prop(s: S) -> Double {
    // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double
    return S.testProp // Ok
  }

  func test_method(s: S) -> Double {
    // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double
    return S.testFunc() // Ok
  }
}

func test_narrowing_is_delayed(x: Double, y: CGFloat) {
  func test(_: CGFloat) {}

  func overloaded(_: Double, _: Double) -> Double {}
  func overloaded(_: CGFloat, _: CGFloat) -> CGFloat {}

  // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC
  // CHECK: function_ref @$sSd1doiyS2d_SdtFZ
  // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC
  let _: CGFloat = x / y // CGFloat.init(x / Double.init(y))
  // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC
  // CHECK: function_ref @$sSd1doiyS2d_SdtFZ
  // CHECK: function_ref @$sSd22_builtinIntegerLiteralSdBI_tcfC
  // CHECK: function_ref @$sSd1poiyS2d_SdtFZ
  // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC
  let _: CGFloat = x / y + 1 // CGFloat.init(x / Double(y) + 1 as Double)
  // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC
  // CHECK: function_ref @$s34implicit_double_cgfloat_conversion25test_narrowing_is_delayed1x1yySd_12CoreGraphics7CGFloatVtF10overloadedL_yS2d_SdtF
  // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcf
  let _: CGFloat = overloaded(x, y) // Prefers `overloaded(Double, Double) -> Double`
  // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC
  // CHECK: function_ref @$s34implicit_double_cgfloat_conversion25test_narrowing_is_delayed1x1yySd_12CoreGraphics7CGFloatVtF10overloadedL_yS2d_SdtF
  // CHECK: @$s34implicit_double_cgfloat_conversion25test_narrowing_is_delayed1x1yySd_12CoreGraphics7CGFloatVtF10overloadedL_yS2d_SdtF
  // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcf
  let _: CGFloat = overloaded(x, overloaded(x, y)) // Prefers `overloaded(Double, Double) -> Double` in both occurrences.

  // Calls should behave exactly the same as contextual conversions.

  // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC
  // CHECK: function_ref @$sSd1doiyS2d_SdtFZ
  // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC
  test(x / y)
  // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC
  // CHECK: function_ref @$sSd1doiyS2d_SdtFZ
  // CHECK: function_ref @$sSd22_builtinIntegerLiteralSdBI_tcfC
  // CHECK: function_ref @$sSd1poiyS2d_SdtFZ
  // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC
  test(x / y + 1)
  // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC
  // CHECK: function_ref @$s34implicit_double_cgfloat_conversion25test_narrowing_is_delayed1x1yySd_12CoreGraphics7CGFloatVtF10overloadedL_yS2d_SdtF
  // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcf
  test(overloaded(x, y))
  // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC
  // CHECK: function_ref @$s34implicit_double_cgfloat_conversion25test_narrowing_is_delayed1x1yySd_12CoreGraphics7CGFloatVtF10overloadedL_yS2d_SdtF
  // CHECK: @$s34implicit_double_cgfloat_conversion25test_narrowing_is_delayed1x1yySd_12CoreGraphics7CGFloatVtF10overloadedL_yS2d_SdtF
  // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcf
  test(overloaded(x, overloaded(x, y)))
}

extension CGFloat {
  static func /(_: CGFloat, _: CGFloat) -> CGFloat { fatalError() }

  static prefix func -(_: Self) -> Self { fatalError() }
}

// Make sure that solution with no Double/CGFloat conversions is preferred
func test_no_ambiguity_with_unary_operators(width: CGFloat, height: CGFloat) {
  struct R {
    init(x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat) {}
    init(x: Double,  y: Double,  width: Double,  height: Double) {}
    init(x: Int,     y: Int,     width: Int,     height: Int) {}
  }

  // CHECK: function_ref @$s12CoreGraphics7CGFloatV34implicit_double_cgfloat_conversionE1doiyA2C_ACtFZ
  // CHECK: function_ref @$s34implicit_double_cgfloat_conversion38test_no_ambiguity_with_unary_operators5width6heighty12CoreGraphics7CGFloatV_AGtF1RL_V1x1yAcdiG_A3GtcfC
  _ = R(x: width / 4, y: -height / 2, width: width, height: height)
}

func test_conversions_with_optional_promotion(d: Double, cgf: CGFloat) {
  func test_double(_: Double??, _: Double???) {}
  func test_cgfloat(_: CGFloat??, _: CGFloat???) {}

  // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC
  // CHECK-NEXT: apply
  // CHECK-NEXT: enum $Optional<Double>, #Optional.some!enumelt
  // CHECK-NEXT: enum $Optional<Optional<Double>>, #Optional.some!enumelt
  test_double(cgf, cgf)

  // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC
  // CHECK-NEXT: apply
  // CHECK-NEXT: enum $Optional<CGFloat>, #Optional.some!enumelt
  // CHECK-NEXT: enum $Optional<Optional<CGFloat>>, #Optional.some!enumelt
  test_cgfloat(d, d)
}

// https://github.com/apple/swift/issues/59374
func test_multi_argument_conversion_with_optional(d: Double, cgf: CGFloat) {
  func test(_: Double, _: CGFloat?) {}

  test(cgf, d) // Ok (CGFloat -> Double and Double? -> CGFloat?)
}

extension CGFloat: @retroactive Hashable {
  public func hash(into hasher: inout Hasher) { fatalError() }
}

func test_collection_literals_as_call_arguments() {
  enum E {
    case test_arr([CGFloat])
    case test_dict_key([CGFloat: String])
    case test_dict_value([String: CGFloat])
    case test_arr_nested([String: [[CGFloat]: String]])
    case test_dict_nested([String: [String: CGFloat]])
  }

  struct Container {
    var prop: E
  }

  struct Point {
    var x: Double
    var y: Double
  }

  func test(cont: inout Container, point: Point) {
    cont.prop = .test_arr([point.x]) // Ok
    cont.prop = .test_dict_key([point.y: ""]) // Ok
    cont.prop = .test_dict_value(["": point.y]) // Ok
    cont.prop = .test_arr_nested(["": [[point.x]: ""]]) // Ok
    cont.prop = .test_dict_nested(["": ["": point.x]]) // Ok
  }
}

func assignments_with_and_without_optionals() {
  class C {
    var prop: CGFloat = 0
  }

  func test(c: C?, v: Double, cgf: CGFloat) {
    c?.prop = v / 2.0 // Ok
    c?.prop = (false ? cgf : v)

    let copy = c!
    copy.prop = Optional(v) ?? 0 // Ok
    copy.prop = (true ? cgf : (false ? v : cgf))
  }
}

extension CGFloat {
  static let `default` = 42.0
}

// rdar://97261826 - crash during constraint application with leading-dot syntax
func assignment_with_leading_dot_syntax() {
  class Container {
    var prop: CGFloat = 0
  }

  struct Test {
    let test: Void = {
      let c = Container()
      c.prop = .default // Ok (Double -> CGFloat)
    }()
  }
}

func test_conversion_inside_tuple_elements() -> (a: CGFloat, b: (c: Int, d: CGFloat)) {
  let x: Double = 0.0
  return (a: x, b: (c: 42, d: x)) // Ok
}

do {
  struct Data {
    var prop: CGFloat
  }

  func single(get: () -> Double) {}
  func multiple(get1: () -> Double,
                get2: () -> CGFloat = { Double(1) },
                get3: () -> Double) {}

  func test(data: Data) {
    single { data.prop } // Ok
    single { return data.prop } // Ok

    single {
      _ = 42
      if true {
        return data.prop // Ok
      }
      return data.prop // Ok
    }

    multiple {
      data.prop // Ok
    } get3: {
      return data.prop // Ok
    }
  }
}

// rdar://99282938
func test_implicit_conversion_clash_with_partial_application_check() {
  class C {
    var duration: CGFloat { 0.3 }

    var use: Double {
      duration // Ok
    }

    func transitionDuration() -> TimeInterval {
      duration // Ok
    }
  }
}

// rdar://99352676
func test_init_validation() {
  class Foo {
    static let bar = 100.0

    func getBar() -> CGFloat? {
      return Self.bar
      // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC : $@convention(method) (Double, @thin CGFloat.Type) -> CGFloat
    }
  }
}