File: stmts.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 (143 lines) | stat: -rw-r--r-- 3,046 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
134
135
136
137
138
139
140
141
142
143
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -dump-parse -disable-availability-checking -enable-experimental-move-only -enable-experimental-feature ThenStatements -enable-experimental-feature ParserASTGen > %t/astgen.ast.raw
// RUN: %target-swift-frontend %s -dump-parse -disable-availability-checking -enable-experimental-move-only -enable-experimental-feature ThenStatements > %t/cpp-parser.ast.raw

// Filter out any addresses in the dump, since they can differ.
// RUN: sed -E 's#0x[0-9a-fA-F]+##g' %t/cpp-parser.ast.raw > %t/cpp-parser.ast
// RUN: sed -E 's#0x[0-9a-fA-F]+##g' %t/astgen.ast.raw > %t/astgen.ast

// RUN: %diff -u %t/astgen.ast %t/cpp-parser.ast

// RUN: %target-run-simple-swift(-Xfrontend -disable-availability-checking -enable-experimental-feature ThenStatements -enable-experimental-feature SwiftParser -enable-experimental-feature ParserASTGen)

// REQUIRES: executable_test
// REQUIRES: swift_swift_parser

// -enable-experimental-feature requires an asserts build
// REQUIRES: asserts
// rdar://116686158
// UNSUPPORTED: asan

// NB: Ridiculous formatting to test that we do not include leading trivia in locations.

func test1(e b: Bool) {
  if b
  {
    print(
      "TRUE"
    )
  }
  else
  {
    print("FALSE")
  }
}

struct UnknownError: Error {}

func testSwitch(arg1: Int, arg2: Int) throws {
  LABEL: switch (arg1, arg2) {
  case (1, 2):
    print("")
    fallthrough
  case (1, _) where true, (4, 3):
    break LABEL
  case (var foo, let bar) where foo.isMultiple(of: 2):
    defer {
      print(foo)
    }
    foo += 1
    print(bar)
    break
  default:
    throw UnknownError()
  }
}

func canThrow() throws {}

func testDo() {
  do {
    try canThrow() 
  } catch is UnknownError {
    return 
  } catch where error is UnknownError {
    return 
  } catch let error, {
    _ = error
    return 
  } catch {
    return
  }

  LOOP: do {
    print("foo")
    if true {
      continue  LOOP
    }
  }
}

func testGuard(arg1: Int?, arg2: String) {
  guard let arg1, var arg2First = arg2.first, arg1 > 2 else {
    return
  }
  arg2First = "a"

  print(arg1, arg2First)
}

func testFor(arg1: [Int?]) {
  for _ in 0..<1 { }
  for case let .some(elem) in arg1 where elem < 42 { print(elem) }
  for var elem in [1,2,3] {
    elem += 1
    print(elem)
  }
}

func testRepeat() {
  repeat {
    print(1)
  } while true
}

func testRepeat(arg: [Int]) {
  var iter = arg.makeIterator()
  while let a = iter.next(), a > Int.random(in: 0..<10) {
    print(a)
  }
}

func testThen() {
  let x: Int = if .random() {
    then .zero
  } else {
    then 0
  }
}

struct GenericTypeWithYields<T> {
  var storedProperty: T?

  var property: T {
    _read {
      yield storedProperty!
    }
    _modify {
      yield &storedProperty!
    }
  }

/* FXIME: yield(...) is parsed as an expression. ASTGen needs to onvert it to a statments.
  subscript<U>(u: U) -> (T,U) {
    _read {
      yield ((storedProperty!, u))
    }
    _modify {
      var temp = (storedProperty!, u)
      yield &temp
    }
  }
  */
}