Skip to content

Commit 233f4c9

Browse files
committed
test cases for SR-4559 (unapplied reference to method named "self" can be used without qualification)
https://bugs.swift.org/browse/SR-4559
1 parent f405364 commit 233f4c9

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

test/Parse/self_rebinding.swift

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,61 @@ class MyCls {
5858
something() // this should still refer `MyCls.something`.
5959
}
6060
}
61+
62+
// MARK: fix method called 'self' can be confused with regular 'self' https://bugs.swift.org/browse/SR-4559
63+
64+
func funcThatReturnsSomething(_ any: Any) -> Any {
65+
any
66+
}
67+
68+
struct TypeWithSelfMethod {
69+
70+
let property = self // expected-warning {{'self' refers to the method 'TypeWithSelfMethod.self', which may be unexpected}} expected-note{{use 'TypeWithSelfMethod.self' to silence this warning}} {{20-20=TypeWithSelfMethod.}}
71+
72+
// Existing warning expected, not confusable
73+
let property2 = self() // expected-error {{cannot use instance member 'self' within property initializer; property initializers run before 'self' is available}}
74+
75+
let propertyFromClosure: () = {
76+
print(self) // expected-warning {{'self' refers to the method 'TypeWithSelfMethod.self', which may be unexpected}} expected-note{{use 'TypeWithSelfMethod.self' to silence this warning}} {{15-15=TypeWithSelfMethod.}}
77+
}()
78+
79+
let propertyFromFunc = funcThatReturnsSomething(self) // expected-warning {{'self' refers to the method 'TypeWithSelfMethod.self', which may be unexpected}} expected-note{{use 'TypeWithSelfMethod.self' to silence this warning}} {{53-53=TypeWithSelfMethod.}}
80+
81+
let propertyFromFunc2 = funcThatReturnsSomething(TypeWithSelfMethod.self) // OK
82+
83+
func `self`() {
84+
85+
}
86+
}
87+
88+
/// Test fix_unqualified_access_member_named_self doesn't appear for computed var called `self`
89+
/// it can't currently be referenced as a static member -- unlike a method with the same name
90+
struct TypeWithSelfComputedVar {
91+
92+
let property = self // expected-error {{cannot use instance member 'self' within property initializer; property initializers run before 'self' is available}}
93+
94+
let propertyFromClosure: () = {
95+
print(self) // expected-error {{cannot use instance member 'self' within property initializer; property initializers run before 'self' is available}}
96+
}()
97+
98+
let propertyFromFunc = funcThatReturnsSomething(self) // expected-error {{cannot use instance member 'self' within property initializer; property initializers run before 'self' is available}}
99+
100+
var `self`: () {
101+
()
102+
}
103+
}
104+
105+
/// Test fix_unqualified_access_member_named_self doesn't appear appear for property called `self`
106+
/// it can't currently be referenced as a static member -- unlike a method with the same name
107+
struct TypeWithSelfProperty {
108+
109+
let property = self // expected-error {{cannot use instance member 'self' within property initializer; property initializers run before 'self' is available}}
110+
111+
let propertyFromClosure: () = {
112+
print(self) // expected-error {{cannot use instance member 'self' within property initializer; property initializers run before 'self' is available}}
113+
}()
114+
115+
let propertyFromFunc = funcThatReturnsSomething(self) // expected-error {{cannot use instance member 'self' within property initializer; property initializers run before 'self' is available}}
116+
117+
let `self`: () = ()
118+
}

0 commit comments

Comments
 (0)