Skip to content

Commit faf1d2b

Browse files
committed
[Decl] Add tests for invalid extension and multi file access of init accessors
1 parent b82fb85 commit faf1d2b

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
struct S {
2+
var _x: Int
3+
public var xImmutablePublic: Int {
4+
@storageRestrictions(initializes: _x)
5+
init {
6+
_x = initialValue
7+
}
8+
get {
9+
return _x
10+
}
11+
}
12+
13+
public var xMutablePublic: Int {
14+
@storageRestrictions(initializes: _x)
15+
init {
16+
_x = initialValue
17+
}
18+
get {
19+
return _x
20+
}
21+
set {
22+
_x = newValue
23+
}
24+
}
25+
26+
internal var xImmutableInternal: Int {
27+
@storageRestrictions(initializes: _x)
28+
init {
29+
_x = initialValue
30+
}
31+
get {
32+
return _x
33+
}
34+
}
35+
36+
internal var xMutableInternal: Int {
37+
@storageRestrictions(initializes: _x)
38+
init {
39+
_x = initialValue
40+
}
41+
get {
42+
return _x
43+
}
44+
set {
45+
_x = newValue
46+
}
47+
}
48+
}

test/decl/var/init_accessors.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,3 +690,19 @@ do {
690690
_ = Test2(q: "some question") // Ok `a` is default initialized to `nil`
691691
_ = Test2(q: "ultimate question", a: 42)
692692
}
693+
694+
struct Test2ForExtension {
695+
var _x: Int
696+
}
697+
698+
extension Test2ForExtension {
699+
var extendedX: Int {
700+
@storageRestrictions(initializes: _x)
701+
init {
702+
// expected-error@-1 {{init accessors could only be declared in the primary declaration}}
703+
self._x = newValue
704+
}
705+
get { _x }
706+
set { _x = newValue }
707+
}
708+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend %s -verify -O -primary-file %s %S/Inputs/imported_init_accessor.swift -c -o %t/init_accessors_multi_file.o
3+
4+
extension S {
5+
init(extendedX: Int) {
6+
self.xImmutablePublic = extendedX
7+
}
8+
}
9+
10+
func test() {
11+
let s = S(extendedX: 42)
12+
_ = s.xImmutablePublic
13+
}

0 commit comments

Comments
 (0)