File: definite_init.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 (78 lines) | stat: -rw-r--r-- 1,732 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
// RUN: %target-swift-frontend -emit-sil -verify %s -o /dev/null

class SomeClass {}

// These are tests for values that are only initialized on some path through the
// CFG.
func partialInit() {

  // Value of trivial type.
  func trivial(ni : Int) {
    var n = ni
    while (n > 0) {
      n -= 1
      var x : Int
      if (n > 2) { continue }
      x = 1
      _ = x
    }
  }
  
  // Tuple with only some elements specified.
  func trivial_tuple() {
    var a : (Int, Int)
    a.1 = 1
    _ = a.1
  }


  func tuple_test(cond : Bool) {
    var x : (SomeClass, SomeClass)

    if cond {
      x.0 = SomeClass()
    } else {
      x.1 = SomeClass()
      _ = x.1
    }
  }
}

func tuple_test() -> Int {
  var t : (Int, Int)

  t.1 = 4

  for _ in 0..<45 {
  }

  t.0 = 1

  for _ in 0..<45 {
  }
  
  return t.1+t.0  // No diagnostic, everything is fully initialized.
}

class CheckCompilerInitAttr {
  @_compilerInitialized let poster: Int
  var whatever: Int // expected-note {{'self.whatever' not initialized}}

  init(v1: Void) {
    whatever = 0
    poster = 10 // expected-error {{illegal assignment to '@_compilerInitialized' storage}}
  }

  // NB: this case is testing whether we only get a note for `whatever` and not `poster`
  init(v2: Void) {} // expected-error {{return from initializer without initializing all stored properties}}
}

class AgainCheckCompilerInitAttr {
  @_compilerInitialized let cleanup: Int // expected-note {{'self.cleanup' not initialized}}
  var whenever: Int

  // NB: this case ensures we still get a note for `cleanup` because no other properties are uninitialized
  init() {
    whenever = 0
  } // expected-error {{return from initializer without initializing all stored properties}}
}