File: TypeNameInterpolation.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 (123 lines) | stat: -rw-r--r-- 3,320 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
// RUN: %target-build-swift -O %s -module-name=test -Xfrontend -sil-verify-all -emit-sil | %FileCheck %s
// RUN: %target-build-swift -Onone %s -module-name=test -Xfrontend -sil-verify-all -emit-sil | %FileCheck %s

// RUN: %empty-directory(%t)
// RUN: %target-build-swift -O -module-name=test %s -o %t/O.out
// RUN: %target-codesign %t/O.out
// RUN: %target-run %t/O.out
// RUN: %target-build-swift -Onone -module-name=test %s -o %t/Onone.out
// RUN: %target-codesign %t/Onone.out
// RUN: %target-run %t/Onone.out

// REQUIRES: executable_test

// Freestanding/minimal runtime does not support printing type names at runtime.
// UNSUPPORTED: freestanding

import StdlibUnittest

let TypeNameTests = TestSuite("TypeName")

class C {}
struct S {}
enum E {}

// Test unicode type names.
struct 🙂 { }
struct 🙃 { }

protocol P {}
protocol P2 {}
protocol P3 {}
protocol AssociatedTypes {
  associatedtype A
  associatedtype B
  associatedtype C
}

class Model : AssociatedTypes {
  typealias A = C
  typealias B = S
  typealias C = E
}

struct Model2 : AssociatedTypes {
  typealias A = C
  typealias B = S
  typealias C = E
}

class GC<T : AssociatedTypes> {}
struct GS<T : AssociatedTypes> {}
enum GE<T : AssociatedTypes> {}
class GC2<T : AssociatedTypes, U : AssociatedTypes> {}

class SomeOuterClass {
  struct SomeInnerStruct {}
  struct SomeInnerGenericStruct<T> {}
}

class SomeOuterGenericClass<T> {
  struct SomeInnerStruct {}
  struct SomeInnerGenericStruct<U> {}
}

extension SomeOuterGenericClass {
  struct OtherInnerStruct {}
  struct OtherInnerGenericStruct<U> {}
}

@inline(never)
func printTypename(_ s: String) {
  print(s)
}

// CHECK-LABEL: sil {{.*}} @$s4test0A21TypenameInterpolationyyF :
// CHECK-NOT:     $ss26DefaultStringInterpolationV06appendC0yyxlF
// CHECK-NOT:     _print_unlocked
// CHECK-NOT:     _typeName
// CHECK:       } // end sil function '$s4test0A21TypenameInterpolationyyF'
@inline(never)
func testTypenameInterpolation() {
  expectEqual("Int", "\(Int.self)")
  expectEqual("C", "\(C.self)")
  expectEqual("S", "\(S.self)")
  expectEqual("E", "\(E.self)")
  expectEqual("GC<Model>",
    "\(GC<Model>.self)")
  expectEqual("GS<Model>",
    "\(GS<Model>.self)")
  expectEqual("GE<Model>",
    "\(GE<Model>.self)")
  expectEqual("GC2<Model, Model2>",
    "\(GC2<Model, Model2>.self)")
  
  expectEqual("P", "\(P.self)")
  typealias PP2 = P & P2
  expectEqual("P & P2",
    "\(PP2.self)")
  expectEqual("Any", "\(Any.self)")
  expectEqual("P & P2", "\((P & P2).self)")

  expectEqual("🙂+🙃", "\(🙂.self)+\(🙃.self)")


  expectEqual("SomeInnerStruct",
              "\(SomeOuterClass.SomeInnerStruct.self)")
  expectEqual("SomeInnerGenericStruct<Int>",
              "\(SomeOuterClass.SomeInnerGenericStruct<Int>.self)")
  expectEqual("SomeInnerStruct",
              "\(SomeOuterGenericClass<Int>.SomeInnerStruct.self)")
  expectEqual("SomeInnerGenericStruct<Int>",
              "\(SomeOuterGenericClass<String>.SomeInnerGenericStruct<Int>.self)")
  expectEqual("OtherInnerStruct",
              "\(SomeOuterGenericClass<Int>.OtherInnerStruct.self)")
  expectEqual("OtherInnerGenericStruct<String>",
              "\(SomeOuterGenericClass<Int>.OtherInnerGenericStruct<String>.self)")
}

TypeNameTests.test("Interpolation") {
  testTypenameInterpolation()
}

runAllTests()