Skip to content

Commit 06f81ee

Browse files
committed
[ConstraintSystem] Make any Sendable -> Any behave the same in all language modes
(cherry picked from commit a2f711c)
1 parent 9920980 commit 06f81ee

File tree

3 files changed

+23
-50
lines changed

3 files changed

+23
-50
lines changed

lib/Sema/CSApply.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7066,9 +7066,9 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
70667066
}
70677067
}
70687068

7069-
if (!(ctx.isSwiftVersionAtLeast(6) ||
7070-
ctx.LangOpts.StrictConcurrencyLevel ==
7071-
StrictConcurrency::Complete)) {
7069+
// `any Sendable` -> `Any` conversion is allowed in generic
7070+
// argument positions.
7071+
{
70727072
auto erasedFromType = fromType->stripConcurrency(
70737073
/*recursive=*/true, /*dropGlobalActor=*/false);
70747074
auto erasedToType = toType->stripConcurrency(

lib/Sema/CSSimplify.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3643,11 +3643,6 @@ static ConstraintSystem::TypeMatchResult matchDeepTypeArguments(
36433643
static bool matchSendableExistentialToAnyInGenericArgumentPosition(
36443644
ConstraintSystem &cs, Type lhs, Type rhs,
36453645
ConstraintLocatorBuilder locator) {
3646-
auto &ctx = cs.getASTContext();
3647-
if (ctx.isSwiftVersionAtLeast(6) ||
3648-
ctx.LangOpts.StrictConcurrencyLevel == StrictConcurrency::Complete)
3649-
return false;
3650-
36513646
// Avoid heavier checks if are not `any Sendable` and `Any`.
36523647
if (!(lhs->isSendableExistential() || lhs->isAny()) ||
36533648
!(rhs->isSendableExistential() || rhs->isAny()))

test/Concurrency/sendable_to_any_for_generic_arguments.swift

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,16 @@ class User {
99
}
1010

1111
extension Dictionary where Key == String, Value == Any {
12-
func onlyWhenValueAny() {} // expected-swift6-note {{'onlyWhenValueAny()' declared here}}
12+
func onlyWhenValueAny() {}
1313
}
1414

1515
extension Array where Element == Any {
16-
func onlyWhenValueAny() {} // expected-swift6-note {{'onlyWhenValueAny()' declared here}}
16+
func onlyWhenValueAny() {}
1717
}
1818

1919
func test_conditional_on_collections(u: User) {
20-
u.dict.onlyWhenValueAny() // Ok with non-strict concurrency
21-
// expected-swift6-error@-1 {{referencing instance method 'onlyWhenValueAny()' on '[String : any Sendable]' requires the types 'any Sendable' and 'Any' be equivalent}}
22-
u.arr.onlyWhenValueAny() // Ok with non-strict concurrency
23-
// expected-swift6-error@-1 {{referencing instance method 'onlyWhenValueAny()' on '[any Sendable]' requires the types 'any Sendable' and 'Any' be equivalent}}
20+
u.dict.onlyWhenValueAny() // Ok
21+
u.arr.onlyWhenValueAny() // Ok
2422
}
2523

2624
// Check that `any Sendable` extension is preferred.
@@ -49,16 +47,13 @@ func test_no_ambiguity_with_Sendable_extension(u: User) {
4947
struct S<T> {
5048
// expected-note@-1 3 {{arguments to generic parameter 'T' ('any Sendable' and 'Any') are expected to be equal}}
5149
// expected-note@-2 2 {{arguments to generic parameter 'T' ('(any Sendable) -> Void' and '(Any) -> Void') are expected to be equal}}
52-
// expected-swift6-note@-3 3 {{arguments to generic parameter 'T' ('any Sendable' and 'Any') are expected to be equal}}
53-
// expected-swift6-note@-4 2 {{arguments to generic parameter 'T' ('(any Sendable) -> Void' and '(Any) -> Void') are expected to be equal}}
54-
// expected-note@-5 {{arguments to generic parameter 'T' ('(Any) -> Any' and '(any Sendable) -> Any') are expected to be equal}}
55-
// expected-note@-6 {{arguments to generic parameter 'T' ('(Any) -> Any' and '(any Sendable) -> any Sendable') are expected to be equal}}
56-
// expected-note@-7 {{arguments to generic parameter 'T' ('(Any) -> Any' and '(Any) -> any Sendable') are expected to be equal}}
57-
// expected-swift6-note@-8 {{arguments to generic parameter 'T' ('((any Sendable) -> Void) -> Void' and '((Any) -> Void) -> Void') are expected to be equal}}
50+
// expected-note@-3 {{arguments to generic parameter 'T' ('(Any) -> Any' and '(any Sendable) -> Any') are expected to be equal}}
51+
// expected-note@-4 {{arguments to generic parameter 'T' ('(Any) -> Any' and '(any Sendable) -> any Sendable') are expected to be equal}}
52+
// expected-note@-5 {{arguments to generic parameter 'T' ('(Any) -> Any' and '(Any) -> any Sendable') are expected to be equal}}
5853
}
5954

6055
extension S where T == Any {
61-
func anyOnly() {} // expected-note {{'anyOnly()' declared here}} expected-swift6-note {{'anyOnly()' declared here}}
56+
func anyOnly() {} // expected-note {{'anyOnly()' declared here}}
6257
}
6358

6459
struct TestGeneral {
@@ -80,54 +75,42 @@ struct TestGeneral {
8075
func accepts_func_with_any_result(_: S<() -> Any?>) {}
8176

8277
func test_contextual() -> S<Any> {
83-
v // Ok with non-strict concurrency
84-
// expected-swift6-error@-1 {{cannot convert return expression of type 'S<any Sendable>' to return type 'S<Any>'}}
78+
v // Ok
8579
}
8680

8781
func test_contextual_error() -> S<Any> {
8882
regularV // expected-error {{cannot convert return expression of type 'S<any Sendable>' to return type 'S<Any>'}}
8983
}
9084

9185
func test_member_ref() {
92-
v.anyOnly() // Ok with non-strict concurrency
93-
// expected-swift6-error@-1 {{referencing instance method 'anyOnly()' on 'S<any Sendable>' requires the types 'any Sendable' and 'Any' be equivalent}}
86+
v.anyOnly() // Ok
9487
regularV.anyOnly()
9588
// expected-error@-1 {{referencing instance method 'anyOnly()' on 'S<any Sendable>' requires the types 'any Sendable' and 'Any' be equivalent}}
9689
}
9790

9891
func test_passing_as_argument(t: TestGeneral) {
99-
accepts_any(v) // Ok with non-strict concurrency
100-
// expected-swift6-error@-1 {{cannot convert value of type 'S<any Sendable>' to expected argument type 'S<Any>'}}
101-
accepts_any(t.v) // Ok with non-strict concurrency
102-
// expected-swift6-error@-1 {{cannot convert value of type 'S<any Sendable>' to expected argument type 'S<Any>'}}
92+
accepts_any(v) // Ok
93+
accepts_any(t.v) // Ok
10394

10495
accepts_any(regularV) // expected-error {{cannot convert value of type 'S<any Sendable>' to expected argument type 'S<Any>'}}
10596
accepts_any(t.regularV) // expected-error {{cannot convert value of type 'S<any Sendable>' to expected argument type 'S<Any>'}}
10697
}
10798

10899
func test_complex_contextual() -> S<[Any?]> {
109-
optV // Ok with non-strict concurrency
110-
// expected-swift6-error@-1 {{cannot convert return expression of type 'S<[(any Sendable)?]>' to return type 'S<[Any?]>'}}
111-
// expected-swift6-note@-2 {{arguments to generic parameter 'Wrapped' ('any Sendable' and 'Any') are expected to be equal}}
100+
optV // Ok
112101
}
113102

114103
func test_complex_contextual_error() {
115-
let _: S<[Any?]> = optV // Ok with non-strict concurrency
116-
// expected-swift6-error@-1 {{cannot assign value of type 'S<[(any Sendable)?]>' to type 'S<[Any?]>'}}
117-
// expected-swift6-note@-2 {{arguments to generic parameter 'Wrapped' ('any Sendable' and 'Any') are expected to be equal}}
104+
let _: S<[Any?]> = optV // Ok
118105
let _: S<[Any?]> = nonOptV // expected-error {{cannot assign value of type 'S<[any Sendable]>' to type 'S<[Any?]>'}}
119106
// expected-note@-1 {{arguments to generic parameter 'Element' ('any Sendable' and 'Any?') are expected to be equal}}
120107
let _: S<[Any?]> = regularOptV // expected-error {{cannot assign value of type 'S<[(any Sendable)?]>' to type 'S<[Any?]>'}}
121108
// expected-note@-1 {{arguments to generic parameter 'Wrapped' ('any Sendable' and 'Any') are expected to be equal}}
122109
}
123110

124111
func test_complex_with_argument(t: TestGeneral) {
125-
accepts_opt_any(optV) // Ok with non-strict concurrency
126-
// expected-swift6-error@-1 {{cannot convert value of type 'S<[(any Sendable)?]>' to expected argument type 'S<[Any?]>'}}
127-
// expected-swift6-note@-2 {{arguments to generic parameter 'Wrapped' ('any Sendable' and 'Any') are expected to be equal}}
128-
accepts_opt_any(t.optV) // Ok with non-strict concurrency
129-
// expected-swift6-error@-1 {{cannot convert value of type 'S<[(any Sendable)?]>' to expected argument type 'S<[Any?]>'}}
130-
// expected-swift6-note@-2 {{arguments to generic parameter 'Wrapped' ('any Sendable' and 'Any') are expected to be equal}}
112+
accepts_opt_any(optV) // Ok
113+
accepts_opt_any(t.optV) // Ok
131114

132115
accepts_opt_any(nonOptV) // expected-error {{cannot convert value of type 'S<[any Sendable]>' to expected argument type 'S<[Any?]>'}}
133116
// expected-note@-1 {{arguments to generic parameter 'Element' ('any Sendable' and 'Any?') are expected to be equal}}
@@ -138,18 +121,14 @@ struct TestGeneral {
138121
}
139122

140123
func test_no_function_conversions() {
141-
let _: S<(Any) -> Void> = funcV // Ok with non-strict concurrency
142-
// expected-swift6-error@-1 {{cannot assign value of type 'S<(any Sendable) -> Void>' to type 'S<(Any) -> Void>'}}
124+
let _: S<(Any) -> Void> = funcV // Ok
143125
let _: S<(Any) -> Void> = regularFuncV // expected-error {{cannot assign value of type 'S<(any Sendable) -> Void>' to type 'S<(Any) -> Void>'}}
144126

145-
accepts_func_any(funcV) // Ok with non-strict concurrency
146-
// expected-swift6-error@-1 {{cannot convert value of type 'S<(any Sendable) -> Void>' to expected argument type 'S<(Any) -> Void>'}}
127+
accepts_func_any(funcV) // Ok
147128
accepts_func_any(regularFuncV)
148129
// expected-error@-1 {{cannot convert value of type 'S<(any Sendable) -> Void>' to expected argument type 'S<(Any) -> Void>'}}
149130

150-
accepts_func_with_any_result(funcWithResult) // Ok with non-strict concurrency
151-
// expected-swift6-error@-1 {{cannot convert value of type 'S<() -> (any Sendable)?>' to expected argument type 'S<() -> Any?>'}}
152-
// expected-swift6-note@-2 {{arguments to generic parameter 'Wrapped' ('any Sendable' and 'Any') are expected to be equal}}
131+
accepts_func_with_any_result(funcWithResult) // Ok
153132

154133
func sameType<T>(_: S<T>, _: T.Type) {}
155134

@@ -168,7 +147,6 @@ struct TestGeneral {
168147
let _: S<(any Sendable) -> any Sendable> = anyFunc
169148
// expected-error@-1 {{cannot assign value of type 'S<(Any) -> Any>' to type 'S<(any Sendable) -> any Sendable>'}}
170149

171-
let _: S<((Any) -> Void) -> Void> = funcInFunc // Ok with non-strict concurrency
172-
// expected-swift6-error@-1 {{cannot assign value of type 'S<((any Sendable) -> Void) -> Void>' to type 'S<((Any) -> Void) -> Void>'}}
150+
let _: S<((Any) -> Void) -> Void> = funcInFunc // Ok
173151
}
174152
}

0 commit comments

Comments
 (0)