File: default-initialization.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 (130 lines) | stat: -rw-r--r-- 2,467 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
// RUN: %target-swift-frontend -emit-sil -verify %s

// REQUIRES: swift_in_compiler

struct A {
  var i : Int
  init(i : Int) { self.i = i }
}

struct B {
  var a : A  // expected-note 3 {{'self.a' not initialized}}
}

func locals() {
  var al : A
  var bl : B
  
  al = A(i: 1)
  bl = B(a: al)
  _ = bl
}

var ag : A
var bg : B

struct C {
  var x : (Int, Int)
}

var c : C


extension B {
  init() {
    // The usage is that self.a is returned from init() as part of self.
  } // expected-error {{return from initializer without initializing all stored properties}}

  init(inA : A) { // okay
    a = inA
  }

  init(otherA : A, x : Bool) { // okay
    self.a = otherA
  }

  init(i : Int) { // okay
    a = A(i: i)
  }

  init(a : A, x : Bool, y : Bool) {
    self.a = a
  }

  init(j : Int, x : Bool) {
    if 1 == 1 { a = A(i: j) }
  } // expected-error {{return from initializer without initializing all stored properties}}

  init(j : Int, x2 : Bool) {
    if true { a = A(i: j) }
  }

  init(i : Int, x : Bool, y : Bool) {
    a = A(i: a.i)    // expected-error {{'self' used before all stored properties are initialized}}
  }

  // Initializing the whole struct at once.
  init(k : Int, x : Bool, y : Bool, z : Bool) {
    let b : B     // expected-note {{constant defined here}}
    self = b      // expected-error {{constant 'b' used before being initialized}}
  }
}

struct D {
  var (a1, a2) : (A, A) = (A(i: 1), A(i: 2))
}

var d : D // okay; uses initializer provided for a1, a2

protocol P { }
struct PImpl : P {}
var p : P = PImpl()

// Properties don't need to be default-initializable.
var prop : P {
  get {
    return p
  }
}

var prop2 : (P) {
  get {
    return p
  }
}

class Base { }
class Derived : Base { }

class NoInitBase {
  init(x : Int) { }
}

class NoInitDerived : NoInitBase { }

Base() // expected-warning{{unused}}
Derived() // expected-warning{{unused}}

class MultipleInitBase {
  init() { }
  init(i: Int) { }
}

class MultipleInitDerived : MultipleInitBase {
  override init() { } // expected-error{{'super.init' isn't called on all paths before returning from initializer}}
}

// rdar://20944100
// Include a 'try' when synthesizing an override of a throwing constructor.
public class HasThrowingInit {
  public init() throws { }
}
public class AlsoHasThrowingInit : HasThrowingInit {
  public convenience init(flag: Bool) throws {
    try self.init()
  }

  public convenience init(flag2: Bool) {
    try! self.init()
  }
}