File: type_join.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 (133 lines) | stat: -rw-r--r-- 6,525 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
// RUN: %target-typecheck-verify-swift -parse-stdlib

import Swift

class C {}
class D : C {}

protocol L {}
protocol M : L {}
protocol N : L {}
protocol P : M {}
protocol Q : M {}
protocol R : L {}
protocol Y {}

protocol FakeEquatable {}
protocol FakeHashable : FakeEquatable {}
protocol FakeExpressibleByIntegerLiteral {}
protocol FakeNumeric : FakeEquatable, FakeExpressibleByIntegerLiteral {}
protocol FakeSignedNumeric : FakeNumeric {}
protocol FakeComparable : FakeEquatable {}
protocol FakeStrideable : FakeComparable {}
protocol FakeCustomStringConvertible {}
protocol FakeBinaryInteger : FakeHashable, FakeNumeric, FakeCustomStringConvertible, FakeStrideable {}
protocol FakeLosslessStringConvertible {}
protocol FakeFixedWidthInteger : FakeBinaryInteger, FakeLosslessStringConvertible {}
protocol FakeUnsignedInteger : FakeBinaryInteger {}
protocol FakeSignedInteger : FakeBinaryInteger, FakeSignedNumeric {}
protocol FakeFloatingPoint : FakeSignedNumeric, FakeStrideable, FakeHashable {}
protocol FakeExpressibleByFloatLiteral {}
protocol FakeBinaryFloatingPoint : FakeFloatingPoint, FakeExpressibleByFloatLiteral {}

func expectEqualType<T>(_: T.Type, _: T.Type) {}
func commonSupertype<T>(_: T, _: T) -> T {} // expected-note 2 {{generic parameters are always considered '@escaping'}}

expectEqualType(Builtin.type_join(Int.self, Int.self), Int.self)
expectEqualType(Builtin.type_join_meta(D.self, C.self), C.self)

expectEqualType(Builtin.type_join(Int?.self, Int?.self), Int?.self)
expectEqualType(Builtin.type_join(Int.self, Int?.self), Int?.self)
expectEqualType(Builtin.type_join(Int?.self, Int.self), Int?.self)
expectEqualType(Builtin.type_join(Int.self, Int??.self), Int??.self)
expectEqualType(Builtin.type_join(Int??.self, Int.self), Int??.self)
expectEqualType(Builtin.type_join(Int?.self, Int??.self), Int??.self)
expectEqualType(Builtin.type_join(Int??.self, Int?.self), Int??.self)
expectEqualType(Builtin.type_join(D?.self, D?.self), D?.self)
expectEqualType(Builtin.type_join(C?.self, D?.self), C?.self)
expectEqualType(Builtin.type_join(D?.self, C?.self), C?.self)
expectEqualType(Builtin.type_join(D.self, D?.self), D?.self)
expectEqualType(Builtin.type_join(D?.self, D.self), D?.self)
expectEqualType(Builtin.type_join(C.self, D?.self), C?.self)
expectEqualType(Builtin.type_join(D?.self, C.self), C?.self)
expectEqualType(Builtin.type_join(D.self, C?.self), C?.self)
expectEqualType(Builtin.type_join(C?.self, D.self), C?.self)
expectEqualType(Builtin.type_join(Any?.self, D.self), Any?.self)
expectEqualType(Builtin.type_join(D.self, Any?.self), Any?.self)
expectEqualType(Builtin.type_join(Any.self, D?.self), Any?.self)
expectEqualType(Builtin.type_join(D?.self, Any.self), Any?.self)
expectEqualType(Builtin.type_join(Any?.self, Any.self), Any?.self)
expectEqualType(Builtin.type_join(Any.self, Any?.self), Any?.self)

expectEqualType(Builtin.type_join(Builtin.Int1.self, Builtin.Int1.self), Builtin.Int1.self)
expectEqualType(Builtin.type_join(Builtin.Int32.self, Builtin.Int1.self), Any.self)
expectEqualType(Builtin.type_join(Builtin.Int1.self, Builtin.Int32.self), Any.self)

expectEqualType(Builtin.type_join(L.self, L.self), L.self)
expectEqualType(Builtin.type_join(L.self, M.self), L.self)
expectEqualType(Builtin.type_join(L.self, P.self), L.self)
expectEqualType(Builtin.type_join(L.self, Y.self), Any.self)
expectEqualType(Builtin.type_join(N.self, P.self), L.self)
expectEqualType(Builtin.type_join(Q.self, P.self), M.self)
expectEqualType(Builtin.type_join((N & P).self, (Q & R).self), M.self)
expectEqualType(Builtin.type_join((Q & P).self, (Y & R).self), L.self)
expectEqualType(Builtin.type_join(FakeEquatable.self, FakeEquatable.self), FakeEquatable.self)
expectEqualType(Builtin.type_join(FakeHashable.self, FakeEquatable.self), FakeEquatable.self)
expectEqualType(Builtin.type_join(FakeEquatable.self, FakeHashable.self), FakeEquatable.self)
expectEqualType(Builtin.type_join(FakeNumeric.self, FakeHashable.self), FakeEquatable.self)
expectEqualType(Builtin.type_join((FakeHashable & FakeStrideable).self, (FakeHashable & FakeNumeric).self),
                                  FakeHashable.self)
expectEqualType(Builtin.type_join((FakeNumeric & FakeStrideable).self,
                                  (FakeHashable & FakeNumeric).self), FakeNumeric.self)
expectEqualType(Builtin.type_join(FakeBinaryInteger.self, FakeFloatingPoint.self),
                                  (FakeHashable & FakeNumeric & FakeStrideable).self)
expectEqualType(Builtin.type_join(FakeFloatingPoint.self, FakeBinaryInteger.self),
                                  (FakeHashable & FakeNumeric & FakeStrideable).self)

func joinFunctions(
  _ escaping: @escaping () -> (),
  _ nonescaping: () -> ()
) {
  _ = commonSupertype(escaping, escaping)
  _ = commonSupertype(nonescaping, escaping)
  // expected-error@-1 {{converting non-escaping parameter 'nonescaping' to generic parameter 'T' may allow it to escape}}
  _ = commonSupertype(escaping, nonescaping)
  // expected-error@-1 {{converting non-escaping parameter 'nonescaping' to generic parameter 'T' may allow it to escape}}
  let x: Int = 1
  // FIXME: We emit these diagnostics here because we refuse to allow
  //        Any to be inferred for the generic type. That's pretty
  //        arbitrary.
  _ = commonSupertype(escaping, x)
  // expected-error@-1 {{conflicting arguments to generic parameter 'T' ('() -> ()' vs. 'Int')}}
  _ = commonSupertype(x, escaping)
  // expected-error@-1 {{conflicting arguments to generic parameter 'T' ('Int' vs. '() -> ()')}}

  let a: Any = 1
  _ = commonSupertype(nonescaping, a)
  // expected-error@-1 {{converting non-escaping value to 'Any' may allow it to escape}}
  _ = commonSupertype(a, nonescaping)
  // expected-error@-1 {{converting non-escaping value to 'Any' may allow it to escape}}
  _ = commonSupertype(escaping, a)
  _ = commonSupertype(a, escaping)

  expectEqualType(Builtin.type_join(((C) -> C).self, ((C) -> D).self),
    ((C) -> C).self)
}

func rdar37241221(_ a: C?, _ b: D?) {
  let c: C? = C()
  let array_c_opt = [c]
  let inferred = [a!, b]
  expectEqualType(type(of: array_c_opt).self, type(of: inferred).self)
}

extension FixedWidthInteger {
  public static func test_nonstale_join_result<Other: BinaryInteger>(_ lhs: inout Self, _ rhs: Other) {
    let shift = rhs < -Self.bitWidth ? -Self.bitWidth
               : rhs > Self.bitWidth ? Self.bitWidth
               : Int(rhs) // `shift` is `Int`

    func accepts_int(_: Int) {}
    accepts_int(shift) // Ok
  }
}