Skip to content

Commit c68a17b

Browse files
committed
[Concurrency] Don't infer nonisolated(nonsending) on synchronous witnesses
If the requirement is `nonisolated(nonsending)` but witness is synchronous, prevent actor isolation inference from requirements because this isolation only applies to asynchronous declarations at the moment. Resolves: rdar://153680826 (cherry picked from commit a964282)
1 parent 3bacc32 commit c68a17b

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5271,9 +5271,18 @@ getIsolationFromWitnessedRequirements(ValueDecl *value) {
52715271
case ActorIsolation::Erased:
52725272
llvm_unreachable("requirement cannot have erased isolation");
52735273

5274+
case ActorIsolation::CallerIsolationInheriting: {
5275+
if (value->isAsync())
5276+
break;
5277+
5278+
// It's possible to witness requirement with an non-async
5279+
// declaration, in such cases `nonisolated(nonsending)` does
5280+
// not apply.
5281+
continue;
5282+
}
5283+
52745284
case ActorIsolation::GlobalActor:
52755285
case ActorIsolation::Nonisolated:
5276-
case ActorIsolation::CallerIsolationInheriting:
52775286
case ActorIsolation::NonisolatedUnsafe:
52785287
break;
52795288
}

test/Concurrency/attr_execution/protocols_silgen.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ protocol P {
1313
@MainActor func mainActorTest() async
1414
}
1515

16+
protocol Q {
17+
nonisolated(nonsending) var test: String { get async throws }
18+
nonisolated(nonsending) func fnTest() async
19+
}
20+
1621
struct AllDefault : P {
1722
// CHECK-LABEL: sil private [transparent] [thunk] [ossa] @$s21attr_execution_silgen10AllDefaultVAA1PA2aDP10callerTestyyYaFTW : $@convention(witness_method: P) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>, @in_guaranteed AllDefault) -> () {
1823
// CHECK: bb0([[ACTOR:%.*]] : @guaranteed $Optional<any Actor>, [[SELF:%.*]] : $*AllDefault):
@@ -120,3 +125,49 @@ struct AllMainActor : P {
120125
// CHECK: } // end sil function '$s21attr_execution_silgen12AllMainActorVAA1PA2aDP04mainF4TestyyYaFTW'
121126
@MainActor func mainActorTest() async {}
122127
}
128+
129+
// Make sure that stored/non-async witness doesn't inherit `nonisolated(nonsending)` but thunk dismatches correctly
130+
131+
struct TestWitnessWithStorage: Q {
132+
// CHECK-LABEL: sil hidden [transparent] [ossa] @$s21attr_execution_silgen22TestWitnessWithStorageV4testSSvg : $@convention(method) (@guaranteed TestWitnessWithStorage) -> @owned String
133+
// CHECK-LABEL: sil hidden [transparent] [ossa] @$s21attr_execution_silgen22TestWitnessWithStorageV4testSSvs : $@convention(method) (@owned String, @inout TestWitnessWithStorage) -> ()
134+
var test: String
135+
136+
// CHECK-LABEL: sil hidden [ossa] @$s21attr_execution_silgen22TestWitnessWithStorageV02fnD0yyF : $@convention(method) (@guaranteed TestWitnessWithStorage) -> ()
137+
func fnTest() {}
138+
139+
// CHECK-LABEL: sil private [transparent] [thunk] [ossa] @$s21attr_execution_silgen22TestWitnessWithStorageVAA1QA2aDP4testSSvgTW : $@convention(witness_method: Q) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>, @in_guaranteed TestWitnessWithStorage) -> (@owned String, @error any Error)
140+
// CHECK: bb0([[ISOLATION:%.*]] : @guaranteed $Optional<any Actor>, [[SELF:%.*]] : $*TestWitnessWithStorage):
141+
// CHECK-NEXT: [[BORROWED_SELF:%.*]] = load_borrow [[SELF]]
142+
// CHECK: [[WITNESS:%.*]] = function_ref @$s21attr_execution_silgen22TestWitnessWithStorageV4testSSvg : $@convention(method) (@guaranteed TestWitnessWithStorage) -> @owned String
143+
// CHECK-NEXT: [[RESULT:%.*]] = apply [[WITNESS]]([[BORROWED_SELF]]) : $@convention(method) (@guaranteed TestWitnessWithStorage) -> @owned String
144+
// CHECK-NEXT: end_borrow [[BORROWED_SELF]]
145+
// CHECK-NEXT: return [[RESULT]]
146+
// CHECK-NEXT: } // end sil function '$s21attr_execution_silgen22TestWitnessWithStorageVAA1QA2aDP4testSSvgTW'
147+
148+
// CHECK: sil private [transparent] [thunk] [ossa] @$s21attr_execution_silgen22TestWitnessWithStorageVAA1QA2aDP02fnD0yyYaFTW : $@convention(witness_method: Q) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>, @in_guaranteed TestWitnessWithStorage) -> ()
149+
// CHECK: bb0([[ISOLATION:%.*]] : @guaranteed $Optional<any Actor>, [[SELF:%.*]] : $*TestWitnessWithStorage):
150+
// CHECK-NEXT: [[BORROWED_SELF]] = load_borrow [[SELF]]
151+
// CHECK: [[WITNESS:%.*]] = function_ref @$s21attr_execution_silgen22TestWitnessWithStorageV02fnD0yyF : $@convention(method) (@guaranteed TestWitnessWithStorage) -> ()
152+
// CHECK-NEXT: {{.*}} = apply [[WITNESS]]([[BORROWED_SELF]]) : $@convention(method) (@guaranteed TestWitnessWithStorage) -> ()
153+
// CHECK-NEXT: [[RESULT:%.*]] = tuple ()
154+
// CHECK-NEXT: end_borrow [[BORROWED_SELF]]
155+
// CHECK-NEXT: return [[RESULT]]
156+
// CHECK-NEXT: } // end sil function '$s21attr_execution_silgen22TestWitnessWithStorageVAA1QA2aDP02fnD0yyYaFTW'
157+
}
158+
159+
struct TestSyncWitness: Q {
160+
// CHECK-LABEL: sil hidden [ossa] @$s21attr_execution_silgen15TestSyncWitnessV4testSSvg : $@convention(method) (TestSyncWitness) -> @owned String
161+
var test: String { "" }
162+
163+
// CHECK-LABEL: sil private [transparent] [thunk] [ossa] @$s21attr_execution_silgen15TestSyncWitnessVAA1QA2aDP4testSSvgTW : $@convention(witness_method: Q) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>, @in_guaranteed TestSyncWitness) -> (@owned String, @error any Error)
164+
// CHECK: bb0([[ISOLATION:%.*]] : @guaranteed $Optional<any Actor>, [[SELF:%.*]] : $*TestSyncWitness):
165+
// CHECK-NEXT: [[BORROWED_SELF:%.*]] = load [trivial] [[SELF]]
166+
// CHECK: [[WITNESS:%.*]] = function_ref @$s21attr_execution_silgen15TestSyncWitnessV4testSSvg : $@convention(method) (TestSyncWitness) -> @owned String
167+
// CHECK-NEXT: [[RESULT:%.*]] = apply [[WITNESS]]([[BORROWED_SELF]]) : $@convention(method) (TestSyncWitness) -> @owned String
168+
// CHECK-NEXT: return [[RESULT]]
169+
// CHECK-NEXT: } // end sil function '$s21attr_execution_silgen15TestSyncWitnessVAA1QA2aDP4testSSvgTW'
170+
171+
// Tested in the `TestWitnessWithStorage`
172+
func fnTest() {}
173+
}

0 commit comments

Comments
 (0)