Skip to content

Commit 14acfb2

Browse files
authored
[Concurrency] isolated fixes; ban Array<T> and handle Self in actor (#70955)
1 parent dd3f6ef commit 14acfb2

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4506,22 +4506,27 @@ TypeResolver::resolveIsolatedTypeRepr(IsolatedTypeRepr *repr,
45064506
return ErrorType::get(getASTContext());
45074507
}
45084508

4509+
// Keep the `type` to be returned, while we unwrap and inspect the inner
4510+
// type for whether it can be isolated on.
45094511
Type type = resolveType(repr->getBase(), options);
4512+
Type unwrappedType = type;
45104513

4511-
if (auto ty = dyn_cast<DynamicSelfType>(type)) {
4512-
type = ty->getSelfType();
4514+
// Optional actor types are fine - `nil` represents `nonisolated`.
4515+
auto allowOptional = getASTContext().LangOpts
4516+
.hasFeature(Feature::OptionalIsolatedParameters);
4517+
if (allowOptional) {
4518+
if (auto wrappedOptionalType = unwrappedType->getOptionalObjectType()) {
4519+
unwrappedType = wrappedOptionalType;
4520+
}
4521+
}
4522+
if (auto dynamicSelfType = dyn_cast<DynamicSelfType>(unwrappedType)) {
4523+
unwrappedType = dynamicSelfType->getSelfType();
45134524
}
45144525

45154526
// isolated parameters must be of actor type
4516-
if (!type->hasTypeParameter() && !type->isAnyActorType() && !type->hasError()) {
4517-
// Optional actor types are fine - `nil` represents `nonisolated`.
4518-
auto wrapped = type->getOptionalObjectType();
4519-
auto allowOptional = getASTContext().LangOpts
4520-
.hasFeature(Feature::OptionalIsolatedParameters);
4521-
if (allowOptional && wrapped && wrapped->isAnyActorType()) {
4522-
return type;
4523-
}
4524-
4527+
if (!unwrappedType->isTypeParameter() &&
4528+
!unwrappedType->isAnyActorType() &&
4529+
!unwrappedType->hasError()) {
45254530
diagnoseInvalid(
45264531
repr, repr->getSpecifierLoc(), diag::isolated_parameter_not_actor, type);
45274532
return ErrorType::get(type);

test/Concurrency/isolated_parameters.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,12 @@ func isolated_generic_bad_2<T: Equatable>(_ t: isolated T) {}
356356
func isolated_generic_bad_3<T: AnyActor>(_ t: isolated T) {}
357357
// expected-error@-1 {{'isolated' parameter 'T' must conform to 'Actor' or 'DistributedActor' protocol}}
358358

359+
func isolated_generic_bad_4<T>(_ t: isolated Array<T>) {}
360+
// expected-error@-1 {{'isolated' parameter has non-actor type 'Array<T>'}}
361+
359362
func isolated_generic_ok_1<T: Actor>(_ t: isolated T) {}
360363

364+
361365
class NotSendable {} // expected-complete-note 5 {{class 'NotSendable' does not conform to the 'Sendable' protocol}}
362366

363367
func optionalIsolated(_ ns: NotSendable, to actor: isolated (any Actor)?) async {}
@@ -396,9 +400,19 @@ nonisolated func callFromNonisolated(ns: NotSendable) async {
396400
// expected-complete-warning@-3 {{passing argument of non-sendable type 'NotSendable' into actor-isolated context may introduce data races}}
397401
}
398402

399-
actor A2 {}
400-
extension A2 {
401-
nonisolated func f() async {
403+
// TODO: Consider making an actor's Self behave like in a struct, removing this special casing.
404+
// We could consider changing this, so that self is always Self because we don't allow inheritance of actors.
405+
// See: https://github.com/apple/swift/issues/70954 and rdar://121091417
406+
actor A2 {
407+
nonisolated func f1() async {
408+
await { (self: isolated Self) in }(self)
409+
// expected-error@-1 {{cannot convert value of type 'A2' to expected argument type 'Self'}}
410+
await { (self: isolated Self?) in }(self)
411+
// expected-error@-1 {{cannot convert value of type 'A2' to expected argument type 'Self?'}}
412+
}
413+
nonisolated func f2() async -> Self {
402414
await { (self: isolated Self) in }(self)
415+
await { (self: isolated Self?) in }(self)
416+
return self
403417
}
404418
}

0 commit comments

Comments
 (0)