Skip to content

[Concurrency] Always diagnose invalid isolated parameter types in type resolution. #71503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -5528,10 +5528,8 @@ NOTE(protocol_isolated_to_global_actor_here,none,
"%0 is isolated to global actor %1 here", (Type, Type))

ERROR(isolated_parameter_not_actor,none,
"'isolated' parameter has non-actor type %0", (Type))
ERROR(isolated_parameter_no_actor_conformance,none,
"'isolated' parameter %0 must conform to 'Actor' "
"or 'DistributedActor' protocol", (Type))
"'isolated' parameter type %0 does not conform to 'Actor' "
"or 'DistributedActor'", (Type))
ERROR(isolated_parameter_duplicate,none,
"cannot have more than one 'isolated' parameter", ())
ERROR(isolated_parameter_duplicate_type,none,
Expand Down
23 changes: 2 additions & 21 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4759,27 +4759,8 @@ ActorIsolation ActorIsolationRequest::evaluate(
checkDeclWithIsolatedParameter(value);

ParamDecl *param = getParameterList(value)->get(*paramIdx);
Type paramType = param->getInterfaceType();
if (paramType->isTypeParameter()) {
paramType = param->getDeclContext()->mapTypeIntoContext(paramType);

auto &ctx = value->getASTContext();
auto conformsTo = [&](KnownProtocolKind kind) {
if (auto *proto = ctx.getProtocol(kind))
return value->getModuleContext()->checkConformance(paramType, proto);
return ProtocolConformanceRef::forInvalid();
};

// The type parameter must be bound by Actor or DistributedActor, as they
// have an unownedExecutor. AnyActor does NOT have an unownedExecutor!
if (!conformsTo(KnownProtocolKind::Actor)
&& !conformsTo(KnownProtocolKind::DistributedActor)) {
ctx.Diags.diagnose(param->getLoc(),
diag::isolated_parameter_no_actor_conformance,
paramType);
}
}

Type paramType = param->getDeclContext()->mapTypeIntoContext(
param->getInterfaceType());
Type actorType;
if (auto wrapped = paramType->getOptionalObjectType()) {
actorType = wrapped;
Expand Down
42 changes: 23 additions & 19 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3927,18 +3927,6 @@ NeverNullType TypeResolver::resolveASTFunctionType(
repr->setWarned();
}

// Use parameter isolation if we have any. This overrides all other
// forms (and should cause conflict diagnostics).
if (numIsolatedParams > 0) {
isolation = FunctionTypeIsolation::forParameter();

if (isolatedAttr) {
diagnose(repr->getLoc(), diag::isolated_parameter_isolated_attr_type,
isolatedAttr->getIsolationKindName());
isolatedAttr->setInvalid();
}
}

if (attrs) {
CustomAttr *globalActorAttr = nullptr;
Type globalActor = resolveGlobalActor(repr->getLoc(), parentOptions,
Expand Down Expand Up @@ -3996,6 +3984,18 @@ NeverNullType TypeResolver::resolveASTFunctionType(
auto params =
resolveASTFunctionTypeParams(repr->getArgsTypeRepr(), options, diffKind);

// Use parameter isolation if we have any. This overrides all other
// forms (and should cause conflict diagnostics).
if (hasIsolatedParameter(params)) {
isolation = FunctionTypeIsolation::forParameter();

if (isolatedAttr) {
diagnose(repr->getLoc(), diag::isolated_parameter_isolated_attr_type,
isolatedAttr->getIsolationKindName());
isolatedAttr->setInvalid();
}
}

auto resultOptions = options.withoutContext();
resultOptions.setContext(TypeResolverContext::FunctionResult);
auto outputTy = resolveType(repr->getResultTypeRepr(), resultOptions);
Expand Down Expand Up @@ -4891,14 +4891,18 @@ TypeResolver::resolveIsolatedTypeRepr(IsolatedTypeRepr *repr,
if (auto dynamicSelfType = dyn_cast<DynamicSelfType>(unwrappedType)) {
unwrappedType = dynamicSelfType->getSelfType();
}
// here

// isolated parameters must be of actor type
if (!unwrappedType->isTypeParameter() &&
!unwrappedType->isAnyActorType() &&
!unwrappedType->hasError()) {
diagnoseInvalid(
repr, repr->getSpecifierLoc(), diag::isolated_parameter_not_actor, type);
return ErrorType::get(type);
if (inStage(TypeResolutionStage::Interface)) {
if (auto *env = resolution.getGenericSignature().getGenericEnvironment())
unwrappedType = env->mapTypeIntoContext(unwrappedType);

if (!unwrappedType->isAnyActorType() && !unwrappedType->hasError()) {
diagnoseInvalid(
repr, repr->getSpecifierLoc(),
diag::isolated_parameter_not_actor, type);
return ErrorType::get(type);
}
}

return type;
Expand Down
14 changes: 9 additions & 5 deletions test/Concurrency/isolated_parameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension Actor {
func testA<T: Actor>(
a: isolated A, // expected-note{{previous 'isolated' parameter 'a'}}
b: isolated T, // expected-warning{{cannot have more than one 'isolated' parameter; this is an error in Swift 6}}
c: isolated Int // expected-error {{'isolated' parameter has non-actor type 'Int'}}
c: isolated Int // expected-error {{'isolated' parameter type 'Int' does not conform to 'Actor' or 'DistributedActor'}}
) {
a.f()
a.g()
Expand Down Expand Up @@ -350,14 +350,14 @@ func getValues(
}

func isolated_generic_bad_1<T>(_ t: isolated T) {}
// expected-error@-1 {{'isolated' parameter 'T' must conform to 'Actor' or 'DistributedActor' protocol}}
// expected-error@-1 {{'isolated' parameter type 'T' does not conform to 'Actor' or 'DistributedActor'}}
func isolated_generic_bad_2<T: Equatable>(_ t: isolated T) {}
// expected-error@-1 {{'isolated' parameter 'T' must conform to 'Actor' or 'DistributedActor' protocol}}
// expected-error@-1 {{'isolated' parameter type 'T' does not conform to 'Actor' or 'DistributedActor'}}
func isolated_generic_bad_3<T: AnyActor>(_ t: isolated T) {}
// expected-error@-1 {{'isolated' parameter 'T' must conform to 'Actor' or 'DistributedActor' protocol}}
// expected-error@-1 {{'isolated' parameter type 'T' does not conform to 'Actor' or 'DistributedActor'}}

func isolated_generic_bad_4<T>(_ t: isolated Array<T>) {}
// expected-error@-1 {{'isolated' parameter has non-actor type 'Array<T>'}}
// expected-error@-1 {{'isolated' parameter type 'Array<T>' does not conform to 'Actor' or 'DistributedActor'}}

func isolated_generic_ok_1<T: Actor>(_ t: isolated T) {}

Expand Down Expand Up @@ -473,3 +473,7 @@ func preciseIsolated(a: isolated MyActor) async {
nonisolated func fromNonisolated(ns: NotSendable) async -> NotSendable {
await pass(value: ns, isolation: nil)
}

func invalidIsolatedClosureParam<A: AnyActor> (
_: (isolated A) async throws -> Void // expected-error {{'isolated' parameter type 'A' does not conform to 'Actor' or 'DistributedActor'}}
) {}