File: result_builder_conjunction_selection.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (83 lines) | stat: -rw-r--r-- 2,598 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
// RUN: %target-typecheck-verify-swift -debug-constraints -target %target-swift-5.1-abi-triple 2>%t.err
// RUN: %FileCheck %s < %t.err

protocol P<Output> {
  associatedtype Output
}

struct S<Output> : P {
  init(_: Output) {}
}

@resultBuilder
struct Builder {
  static func buildExpression<T>(_ e: T) -> T { e }

  static func buildBlock<T1>(_ t1: T1) -> some P<T1> {
    return S(t1)
  }

  static func buildBlock<T1, T2>(_ t1: T1, _ t2: T2) -> some P<(T1, T2)> {
    return S((t1, t2))
  }

  static func buildBlock<T1, T2, T3>(_ t1: T1, _ t2: T2, _ t3: T3) -> some P<(T1, T2, T3)> {
    return S((t1, t2, t3))
  }

  static func buildOptional<T>(_ value: T?) -> T? { return value }
}

do {
  func test<T, U>(_: T, @Builder _: () -> some P<U>) {}

  // CHECK: ---Initial constraints for the given expression---
  // CHECK: (integer_literal_expr type="[[LITERAL_VAR:\$T[0-9]+]]" {{.*}}
  // CHECK: (attempting type variable binding [[CLOSURE:\$T[0-9]+]] := () -> {{.*}}
  // CHECK-NOT: (attempting type variable binding [[LITERAL_VAR]] := {{.*}}
  // CHECK: (attempting conjunction element pattern binding element @ 0
  // CHECK: (applying conjunction result to outer context
  // CHECK: (attempting type variable binding [[LITERAL_VAR]] := Int
  test(42) {
    1
    ""
  }
}

do {
  func test<T, U>(_: T, @Builder _: (T) -> some P<U>) {}

  // CHECK: ---Initial constraints for the given expression---
  // CHECK: (integer_literal_expr type="[[LITERAL_VAR:\$T[0-9]+]]" {{.*}}
  // CHECK: (attempting type variable binding [[LITERAL_VAR]] := Int
  // CHECK: (attempting conjunction element pattern binding element @ 0
  test(42) { v in
    v
    ""
  }
}

do {
  func test<T: BinaryInteger, U>(@Builder _: (Bool) -> some P<T>, transform: (T?) -> U) {}

  // CHECK: ---Initial constraints for the given expression---
  // CHECK: (attempting type variable {{.*}} := () -> {{.*}}
  // CHECK: (attempting conjunction element pattern binding element @ 0 :
  // CHECK-NEXT: (pattern_named "x")
  // CHECK: (attempting conjunction element syntactic element
  // CHECK-NEXT: (call_expr {{.*}}
  // CHECK: (attempting type variable {{.*}} := (Bool) -> {{.*}}
  // CHECK: (attempting conjunction element pattern binding element @ 0
  // CHECK: (pattern_named implicit "$__builder{{.*}}")
  // CHECK: (applying conjunction result to outer context
  // CHECK: (attempting type variable {{.*}} := (Int?) -> {{.*}}
  // CHECK: (attempting disjunction choice {{.*}} bound to decl {{.*}}.Int.init(_:)
  let _ = {
    let x = 42
    test { cond in
      x
    } transform: { v in
      Int(v ?? 0)
    }
  }
}