|
| 1 | +// RUN: %target-typecheck-verify-swift %clang-importer-sdk |
| 2 | +// RUN: %target-swift-emit-silgen(mock-sdk: %clang-importer-sdk) -swift-version 5 -verify %s | %FileCheck %s |
| 3 | + |
| 4 | +// REQUIRES: objc_interop |
| 5 | + |
| 6 | +import Foundation |
| 7 | +import CoreGraphics |
| 8 | + |
| 9 | +func test_to_cgfloat(_: CGFloat) {} |
| 10 | +func test_from_cgfloat(_: Double) {} |
| 11 | + |
| 12 | +func test_returns_double(_: CGFloat) -> Double { |
| 13 | + 42.0 |
| 14 | +} |
| 15 | + |
| 16 | +func test_returns_cgfloat(_: Double) -> CGFloat { |
| 17 | + 42.0 |
| 18 | +} |
| 19 | + |
| 20 | +let d: Double = 0.0 |
| 21 | +let cgf: CGFloat = 0.0 |
| 22 | + |
| 23 | +// CHECK: test_various_situations_converting_to_cgfloat() |
| 24 | +func test_various_situations_converting_to_cgfloat() { |
| 25 | + // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC : $@convention(method) (Double, @thin CGFloat.Type) -> CGFloat |
| 26 | + let _: CGFloat = d |
| 27 | + |
| 28 | + // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC : $@convention(method) (Double, @thin CGFloat.Type) -> CGFloat |
| 29 | + // CHECK: function_ref @$s34implicit_double_cgfloat_conversion08test_to_C0yy12CoreGraphics7CGFloatVF : $@convention(thin) (CGFloat) -> () |
| 30 | + test_to_cgfloat(d) |
| 31 | + |
| 32 | + // CHECK: function_ref @$sSd1poiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double |
| 33 | + // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC : $@convention(method) (Double, @thin CGFloat.Type) -> CGFloat |
| 34 | + test_to_cgfloat(d + d) // One implicit conversion, `+` is (Double, Double) -> Double |
| 35 | + |
| 36 | + // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double |
| 37 | + // CHECK: function_ref @$sSd1poiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double |
| 38 | + // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC : $@convention(method) (Double, @thin CGFloat.Type) -> CGFloat |
| 39 | + test_to_cgfloat(d + cgf) // Two conversions but `+` on Double is still preferred. |
| 40 | + |
| 41 | + // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double |
| 42 | + // CHECK: function_ref @$sSd1poiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double |
| 43 | + // CHECK: function_ref @$sSd1soiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double |
| 44 | + // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC : $@convention(method) (Double, @thin CGFloat.Type) -> CGFloat |
| 45 | + test_to_cgfloat(d + cgf - d) // One conversion (`cgf` -> Double) inside and one outside, both operators preferred on Double. |
| 46 | + |
| 47 | + // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double |
| 48 | + // CHECK: function_ref @$sSd1poiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double |
| 49 | + // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double |
| 50 | + // CHECK: function_ref @$sSd1soiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double |
| 51 | + // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC : $@convention(method) (Double, @thin CGFloat.Type) -> CGFloat |
| 52 | + test_to_cgfloat(d + cgf - cgf) // Double is always preferred over CGFloat, so three conversion here. |
| 53 | + |
| 54 | + // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC : $@convention(method) (Double, @thin CGFloat.Type) -> CGFloat |
| 55 | + // CHECK: function_ref @$s34implicit_double_cgfloat_conversion013test_returns_B0ySd12CoreGraphics7CGFloatVF : $@convention(thin) (CGFloat) -> Double |
| 56 | + // CHECK: function_ref @$s12CoreGraphics7CGFloatVyACSdcfC : $@convention(method) (Double, @thin CGFloat.Type) -> CGFloat |
| 57 | + // CHECK: function_ref @$s34implicit_double_cgfloat_conversion08test_to_C0yy12CoreGraphics7CGFloatVF : $@convention(thin) (CGFloat) -> () |
| 58 | + test_to_cgfloat(test_returns_double(d)) // Two conversions |
| 59 | + |
| 60 | + // Overloads with CGFloat are preferred if that allows to avoid any implicit conversions. |
| 61 | + func test_loading_tuple_elements(values: inout (CGFloat, CGFloat)) { |
| 62 | + struct S { |
| 63 | + init(x: Double, y: Double) {} |
| 64 | + init(x: CGFloat, y: CGFloat) {} |
| 65 | + } |
| 66 | + |
| 67 | + // 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 |
| 68 | + _ = S(x: 0.0, y: values.0) // Ok |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// CHECK: test_various_situations_converting_to_double() |
| 73 | +func test_various_situations_converting_to_double() { |
| 74 | + // function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double |
| 75 | + let _: Double = cgf |
| 76 | + |
| 77 | + // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double |
| 78 | + // CHECK: function_ref @$s34implicit_double_cgfloat_conversion010test_from_C0yySdF : $@convention(thin) (Double) -> () |
| 79 | + test_from_cgfloat(cgf) |
| 80 | + // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double |
| 81 | + // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double |
| 82 | + // CHECK: function_ref @$sSd1poiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double |
| 83 | + test_from_cgfloat(cgf + cgf) // Two conversions for `cgf` and `+` is on `Double` |
| 84 | + // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double |
| 85 | + // CHECK: function_ref @$sSd1poiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double |
| 86 | + test_from_cgfloat(d + cgf) // One conversion `cgf` to Double and `+` is on `Double` |
| 87 | + // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double |
| 88 | + // CHECK: function_ref @$sSd1poiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double |
| 89 | + // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double |
| 90 | + // CHECK: function_ref @$sSd1soiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double |
| 91 | + test_from_cgfloat(cgf + d - cgf) // Two conversions of `cgf` and both `+` and `-` are on Double |
| 92 | + // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double |
| 93 | + // CHECK: function_ref @$sSd1poiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double |
| 94 | + // CHECK: function_ref @$sSd1soiyS2d_SdtFZ : $@convention(method) (Double, Double, @thin Double.Type) -> Double |
| 95 | + // CHECK: function_ref @$s34implicit_double_cgfloat_conversion010test_from_C0yySdF : $@convention(thin) (Double) -> () |
| 96 | + test_from_cgfloat(cgf + d - d) // One conversion and both operators are on Double |
| 97 | + |
| 98 | + // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double |
| 99 | + // CHECK: function_ref @$s34implicit_double_cgfloat_conversion013test_returns_C0y12CoreGraphics7CGFloatVSdF : $@convention(thin) (Double) -> CGFloat |
| 100 | + // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double |
| 101 | + // CHECK: function_ref @$s34implicit_double_cgfloat_conversion010test_from_C0yySdF : $@convention(thin) (Double) -> () |
| 102 | + test_from_cgfloat(test_returns_cgfloat(cgf)) // Two conversions - argument and result. |
| 103 | +} |
| 104 | + |
| 105 | +func test_conversions_with_optionals(v: CGFloat?) { |
| 106 | + // CHECK: function_ref @$s34implicit_double_cgfloat_conversion31test_conversions_with_optionals1vy12CoreGraphics7CGFloatVSg_tFAFyKXEfu_ : $@convention(thin) () -> (CGFloat, @error Error) |
| 107 | + // CHECK: function_ref @$sSd12CoreGraphicsEySdAA7CGFloatVcfC : $@convention(method) (CGFloat, @thin Double.Type) -> Double |
| 108 | + let _: Double = (v ?? 0) |
| 109 | +} |
0 commit comments