You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Functions can in fact conform to some protocols, notably,
marker protocols. Dynamically they have no influence on whether the cast
will succeed or not, as there is no way to tell if a type conforms to
such a protocol at runtime. But we should check them statically because
they otherwise can be cast to Any eventually.
Copy file name to clipboardExpand all lines: test/expr/cast/as_coerce.swift
+17-4
Original file line number
Diff line number
Diff line change
@@ -119,6 +119,7 @@ protocol P_48579 {}
119
119
do{
120
120
func f1()->Any{}
121
121
func f2(){}
122
+
@Sendablefunc f3(){}
122
123
123
124
_ = f1 is P_48579 // expected-warning {{cast from '() -> Any' to unrelated type 'any P_48579' always fails}} // expected-note {{did you mean to call 'f1' with '()'?}}{{9-9=()}}
124
125
_ = f1 as!P_48579 // expected-warning {{cast from '() -> Any' to unrelated type 'any P_48579' always fails}} // expected-note {{did you mean to call 'f1' with '()'?}}{{9-9=()}}
@@ -133,20 +134,32 @@ do {
133
134
_ = f2 as?Any // expected-warning {{conditional cast from '() -> ()' to 'Any' always succeeds}}
_ = f1 is T // expected-warning {{cast from '() -> Any' to unrelated type 'T' always fails}} // expected-note {{did you mean to call 'f1' with '()'?}}{{11-11=()}}
138
-
_ = f1 as!T // expected-warning {{cast from '() -> Any' to unrelated type 'T' always fails}} // expected-note {{did you mean to call 'f1' with '()'?}}{{11-11=()}}
139
+
_ = f1 as!V // expected-warning {{cast from '() -> Any' to unrelated type 'V' always fails}} // expected-note {{did you mean to call 'f1' with '()'?}}{{11-11=()}}
139
140
_ = f1 as?T // expected-warning {{cast from '() -> Any' to unrelated type 'T' always fails}} // expected-note {{did you mean to call 'f1' with '()'?}}{{11-11=()}}
140
141
_ = f2 is T // expected-warning {{cast from '() -> ()' to unrelated type 'T' always fails}}
141
-
_ = f2 as!T // expected-warning {{cast from '() -> ()' to unrelated type 'T' always fails}}
142
+
_ = f2 as!V // expected-warning {{cast from '() -> ()' to unrelated type 'V' always fails}}
142
143
_ = f2 as?T // expected-warning {{cast from '() -> ()' to unrelated type 'T' always fails}}
144
+
_ = f3 is T // expected-warning {{cast from '@Sendable () -> ()' to unrelated type 'T' always fails}}
145
+
_ = f3 as!V // expected-warning {{cast from '@Sendable () -> ()' to unrelated type 'V' always fails}}
146
+
_ = f3 as?T // expected-warning {{cast from '@Sendable () -> ()' to unrelated type 'T' always fails}}
143
147
}
144
148
145
-
func test2<U>(_:U.Type){
149
+
func test2<U, S:Sendable>(_:U.Type, _:S.Type){
150
+
_ = f1 is U // Okay
146
151
_ = f1 as!U // Okay
147
152
_ = f1 as?U // Okay
153
+
_ = f1 is U // Okay
148
154
_ = f2 as!U // Okay
149
155
_ = f2 as?U // Okay
156
+
157
+
_ = f2 is S // expected-warning {{cast from '() -> ()' to unrelated type 'S' always fails}}
158
+
_ = f2 as!S // expected-warning {{cast from '() -> ()' to unrelated type 'S' always fails}}
159
+
_ = f2 as?S // expected-warning {{cast from '() -> ()' to unrelated type 'S' always fails}}
0 commit comments