File: generic_inline_self.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 (64 lines) | stat: -rw-r--r-- 3,004 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
// RUN: %target-swift-frontend -emit-sil -primary-file %s | %FileCheck %s

// REQUIRES: swift_in_compiler

// Test to ensure that mandatory inlining of generics with a dynamic Self
// substitution works correctly with thick_metatype instructions and SIL
// type lowering.

func makeInstance<T: C>(_: T.Type) -> T {
    return T()
}

@_transparent
func makeInstanceTransparent<T: C>(_: T.Type) -> T {
    return T()
}

@_transparent
func makeInstanceTransparentProtocol<T: P>(_: T.Type) -> T {
    return T()
}

protocol P {
  init()
}

class C : P {
  required init() {}

// CHECK-LABEL: sil hidden @$s19generic_inline_self1CC18returnsNewInstanceACXDyF : $@convention(method) (@guaranteed C) -> @owned C
// CHECK:       bb0(%0 : $C):
// CHECK:         [[METATYPE:%.*]] = value_metatype $@thick @dynamic_self C.Type, %0 : $C
// CHECK:         [[FN:%.*]] = function_ref @$s19generic_inline_self12makeInstanceyxxmAA1CCRbzlF : $@convention(thin) <τ_0_0 where τ_0_0 : C> (@thick τ_0_0.Type) -> @owned τ_0_0
// CHECK-NEXT:    [[RESULT:%.*]] = apply [[FN]]<@dynamic_self C>([[METATYPE]]) : $@convention(thin) <τ_0_0 where τ_0_0 : C> (@thick τ_0_0.Type) -> @owned τ_0_0
// CHECK-NEXT:    return [[RESULT]] : $C
  func returnsNewInstance() -> Self {
    return makeInstance(type(of: self))
  }

// CHECK-LABEL: sil hidden @$s19generic_inline_self1CC29returnsNewInstanceTransparentACXDyF : $@convention(method) (@guaranteed C) -> @owned C
// CHECK:       bb0(%0 : $C):
// CHECK:         [[METATYPE:%.*]] = metatype $@thick @dynamic_self C.Type
// CHECK-NEXT:    [[STATIC_METATYPE:%.*]] = upcast [[METATYPE]] : $@thick @dynamic_self C.Type to $@thick C.Type
// CHECK-NEXT:    [[FN:%.*]] = class_method [[STATIC_METATYPE]] : $@thick C.Type, #C.init!allocator : (C.Type) -> () -> C, $@convention(method) (@thick C.Type) -> @owned C
// CHECK-NEXT:    [[RESULT2:%.*]] = apply [[FN]]([[STATIC_METATYPE]]) : $@convention(method) (@thick C.Type) -> @owned C
// CHECK-NEXT:    [[RESULT:%.*]] = unchecked_ref_cast [[RESULT2]] : $C to $C
// CHECK-NEXT:    return [[RESULT]] : $C
  func returnsNewInstanceTransparent() -> Self {
    return makeInstanceTransparent(type(of: self))
  }

// CHECK-LABEL: sil hidden @$s19generic_inline_self1CC37returnsNewInstanceTransparentProtocolACXDyF : $@convention(method) (@guaranteed C) -> @owned C
// CHECK:       bb0(%0 : $C):
// CHECK:         [[METATYPE:%.*]] = metatype $@thick @dynamic_self C.Type
// CHECK-NEXT:    [[STATIC_METATYPE:%.*]] = upcast [[METATYPE]] : $@thick @dynamic_self C.Type to $@thick C.Type
// CHECK-NEXT:    [[FN:%.*]] = class_method [[STATIC_METATYPE]] : $@thick C.Type, #C.init!allocator : (C.Type) -> () -> C, $@convention(method) (@thick C.Type) -> @owned C
// CHECK-NEXT:    [[RESULT2:%.*]] = apply [[FN]]([[STATIC_METATYPE]]) : $@convention(method) (@thick C.Type) -> @owned C
// CHECK-NEXT:    return %5 : $C
  func returnsNewInstanceTransparentProtocol() -> Self {
    return makeInstanceTransparentProtocol(type(of: self))
  }
}

class D : C {}