File: marker_protocol.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 (99 lines) | stat: -rw-r--r-- 2,911 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
// RUN: %target-swift-frontend -primary-file %s -emit-ir -o - | %FileCheck %s

// Marker protocols should have no ABI impact at all, so this source file checks
// for the absence of symbols related to marker protocols.

// CHECK-NOT: $s15marker_protocol1PP
// CHECK-NOT: $s15marker_protocol1PMp

// REQUIRES: PTRSIZE=64

@_marker public protocol P { }

extension Int: P { }
extension Array: P where Element: P { }

// No mention of the marker protocol for runtime type instantiation.
// CHECK-LABEL: @"$sSS_15marker_protocol1P_ptMD" =
// CHECK-SAME: @"symbolic SS_ypt"

// CHECK-LABEL: @"$s15marker_protocol1QMp" = {{(dllexport |protected )?}}constant
// CHECK-SAME: i32 trunc{{.*}}s15marker_protocolMXM{{.*}}s15marker_protocol1QMp
// CHECK-SAME: i32 0, i32 5, i32 0
public protocol Q: P {
  func f()
  func g()
  func h()
  func i()
  func j()
}

protocol R { }

@_marker protocol S: AnyObject { }

// Note: no mention of marker protocols here.
// CHECK-LABEL: @"$s15marker_protocol10HasMarkersVMF" =
// CHECK-SAME: @"symbolic yp"
// CHECK-SAME: @"symbolic ______p 15marker_protocol1QP"
// CHECK-SAME: @"symbolic ______p 15marker_protocol1RP"
// CHECK-SAME: @"symbolic yXl"
struct HasMarkers {
  var field1: P
  var field2: P & Q
  var field3: P & R
  var field4: S
}

// Note: no mention of marker protocols when forming a dictionary.
// CHECK-LABEL: define{{.*}}@"$s15marker_protocol0A12InDictionaryypyF"
// CHECK: call ptr @__swift_instantiateConcreteTypeFromMangledName({{.*}} @"$sSS_15marker_protocol1P_ptMD")
public func markerInDictionary() -> Any {
  let dict: [String: P] = ["answer" : 42]
  return dict
}

// Note: no witness tables
// CHECK: swiftcc void @"$s15marker_protocol7genericyyxAA1PRzlF"(ptr noalias %0, ptr %T)
public func generic<T: P>(_: T) { }

public struct GenericType<T: Hashable & P> { }

// CHECK-LABEL: @"$s15marker_protocol11testGeneric1i5arrayySi_SaySiGtF"(
public func testGeneric(i: Int, array: [Int]) {
  generic(i)
  generic(array)
  // CHECK: __swift_instantiateConcreteTypeFromMangledName{{.*}}$s15marker_protocol11GenericTypeVySaySiGGmMD
  print(GenericType<[Int]>.self)
}

// Forming an existential involving a marker protocol would crash the compiler
protocol SelfConstrainedProtocol {
  static var shared: Self { get }
}

struct Foo: SelfConstrainedProtocol {
  let x: P
  static var shared: Self {
    Foo(x: 123)
  }
}

protocol P2 {
  associatedtype Foo
}

// CHECK: define{{.*}}$s15marker_protocol3fooyy3FooQz_xtAA1PRzAA2P2RzlF
func foo<T: P & P2>(_: T.Foo, _: T) { }

class C {}

let v1 = (any C & P).self
let v2 = C.self

// CHECK-LABEL: define hidden swiftcc void @"$s15marker_protocol23testProtocolCompositionyyF"()
// CHECK: [[V1:%.*]] = call ptr @__swift_instantiateConcreteTypeFromMangledName(ptr @"$s15marker_protocol1P_AA1CCXcMD")
// CHECK: [[V2:%.*]] = load ptr, ptr @"$s15marker_protocol2v2AA1CCmvp"
func testProtocolComposition() {
  print(v1 == v2)
}