File: attr_requires_stored_property_inits.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 (59 lines) | stat: -rw-r--r-- 2,316 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
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck %s -verify

// REQUIRES: objc_interop

import ObjectiveC

// Attribute requires that all stored properties have in-class
// initializers.
@requires_stored_property_inits
class RequiresOkay { // expected-note{{superclass 'RequiresOkay' requires all stored properties to have initial values}}
  var a = 5
}

// Diagnose missing initializers.
@requires_stored_property_inits
class RequiresBad { // expected-note 5{{class 'RequiresBad' requires all stored properties to have initial values}}
  var a: Int // expected-error{{stored property 'a' requires an initial value}}
  var (b, c): (Int, Int) // expected-error{{stored properties 'b' and 'c' require initial values}}
  var (d, e, f): (Int, Int, Int) // expected-error{{stored properties 'd', 'e', and 'f' require initial values}}
  var (g, h, i, j): (Int, Int, Int, Int) // expected-error{{stored properties 'g', 'h', 'i', and others require initial values}}

  @NSManaged var managed_property: Int

  var k: RequiresOkay?
  let l: RequiresOkay? // expected-error{{stored property 'l' requires an initial value}}

  init() {
    a = 0
    b = 0
    c = 0
    d = 0
    e = 0
    f = 0
    g = 0
    h = 0
    i = 0
    j = 0
  }
}

class RequiresInheritedBad : RequiresOkay { // expected-error{{class 'RequiresInheritedBad' has no initializers}}
  var b: Int // expected-error{{stored property 'b' requires an initial value}}
  @NSManaged var managed_property_sub: Int
}

// Diagnose attempts to use this attribute on a non-class.
@requires_stored_property_inits struct S { } // expected-error{{@requires_stored_property_inits may only be used on 'class' declarations}} {{1-33=}}

@requires_stored_property_inits enum E { } // expected-error{{@requires_stored_property_inits may only be used on 'class' declarations}} {{1-33=}}

@requires_stored_property_inits func f() { } // expected-error{{@requires_stored_property_inits may only be used on 'class' declarations}} {{1-33=}}


@requires_stored_property_inits
class NSSomething : NSObject { 
  // expected-note@-1 {{class 'NSSomething' requires all stored properties to have initial values}}
  // expected-error@-2 {{class 'NSSomething' has no initializers}}
  var x: Int // expected-error{{stored property 'x' requires an initial value}}
}