Skip to content
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
54 changes: 32 additions & 22 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2666,7 +2666,8 @@ namespace {

/// Some function conversions synthesized by the constraint solver may not
/// be correct AND the solver doesn't know, so we must emit a diagnostic.
void checkFunctionConversion(Expr *funcConv, Type fromType, Type toType) {
void checkFunctionConversion(ImplicitConversionExpr *funcConv,
Type fromType, Type toType) {
auto diagnoseNonSendableParametersAndResult =
[&](FunctionType *fnType,
std::optional<unsigned> warnUntilSwiftMode = std::nullopt) {
Expand Down Expand Up @@ -2771,6 +2772,18 @@ namespace {
if (!fromFnType->isAsync())
break;

// Applying `nonisolated(nonsending)` to an interface type
// of a declaration.
if (auto *declRef =
dyn_cast<DeclRefExpr>(funcConv->getSubExpr())) {
auto *decl = declRef->getDecl();
if (auto *nonisolatedAttr =
decl->getAttrs().getAttribute<NonisolatedAttr>()) {
if (nonisolatedAttr->isNonSending())
return;
}
}

// @concurrent -> nonisolated(nonsending)
// crosses an isolation boundary.
LLVM_FALLTHROUGH;
Expand All @@ -2780,16 +2793,13 @@ namespace {
diagnoseNonSendableParametersAndResult(toFnType);
break;

case FunctionTypeIsolation::Kind::Parameter:
llvm_unreachable("invalid conversion");

// @Sendable nonisolated(nonsending) -> nonisolated(nonsending)
// doesn't require Sendable checking.
case FunctionTypeIsolation::Kind::NonIsolatedNonsending:
// Non isolated caller is always async. This can only occur if we
// are converting from an `@Sendable` representation to something
// else. So we need to just check that we diagnose non sendable
// parameters and results.
diagnoseNonSendableParametersAndResult(toFnType);
break;

case FunctionTypeIsolation::Kind::Parameter:
llvm_unreachable("invalid conversion");
}
break;
}
Expand Down Expand Up @@ -3460,19 +3470,6 @@ namespace {
}
}

// The constraint solver may not have chosen legal casts.
if (auto funcConv = dyn_cast<FunctionConversionExpr>(expr)) {
checkFunctionConversion(funcConv,
funcConv->getSubExpr()->getType(),
funcConv->getType());
}

if (auto *isolationErasure = dyn_cast<ActorIsolationErasureExpr>(expr)) {
checkFunctionConversion(isolationErasure,
isolationErasure->getSubExpr()->getType(),
isolationErasure->getType());
}

if (auto *defaultArg = dyn_cast<DefaultArgumentExpr>(expr)) {
checkDefaultArgument(defaultArg);
}
Expand Down Expand Up @@ -3564,6 +3561,19 @@ namespace {
}
}

// The constraint solver may not have chosen legal casts.
if (auto funcConv = dyn_cast<FunctionConversionExpr>(expr)) {
checkFunctionConversion(funcConv,
funcConv->getSubExpr()->getType(),
funcConv->getType());
}

if (auto *isolationErasure = dyn_cast<ActorIsolationErasureExpr>(expr)) {
checkFunctionConversion(isolationErasure,
isolationErasure->getSubExpr()->getType(),
isolationErasure->getType());
}

return Action::Continue(expr);
}

Expand Down
22 changes: 22 additions & 0 deletions test/Concurrency/attr_execution/conversions_silgen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -620,4 +620,26 @@ func testSendableClosureNonisolatedNonSendingInference() {
// CHECK: hop_to_executor [[EXECUTOR]]
// CHECK: // end sil function '$s21attr_execution_silgen49testSendableClosureNonisolatedNonSendingInferenceyyFySiYaYbYCcfU_'
let _: nonisolated(nonsending) @Sendable (Int) async -> Void = { _ in }

// CHECK-LABEL: sil private [ossa] @$s21attr_execution_silgen49testSendableClosureNonisolatedNonSendingInferenceyyFyS2SYaKYCcYaYbYCcfU0_ : $@convention(thin) @Sendable @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>, @guaranteed @async @callee_guaranteed (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>, @guaranteed String) -> (@owned String, @error any Error)) -> @error any Error
// CHECK: bb0([[EXECUTOR:%.*]] : @guaranteed $Optional<any Actor>, %1 : @guaranteed $@async @callee_guaranteed (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>, @guaranteed String) -> (@owned String, @error any Error)):
// CHECK: hop_to_executor [[EXECUTOR]]
// CHECK: // end sil function '$s21attr_execution_silgen49testSendableClosureNonisolatedNonSendingInferenceyyFyS2SYaKYCcYaYbYCcfU0_'
let _: nonisolated(nonsending) @Sendable (
nonisolated(nonsending) @escaping (String) async throws -> String
) async throws -> Void = { _ in }
}

// CHECK-LABEL: sil hidden [ossa] @$s21attr_execution_silgen014testSendableToE35ConversionWithNonisilatedNonsendingyyF : $@convention(thin) () -> ()
// CHECK: [[TEST_REF:%.*]] = function_ref @$s21attr_execution_silgen014testSendableToE35ConversionWithNonisilatedNonsendingyyF0D0L_7closureyS2SYaKYCc_tYaYbKF : $@convention(thin) @Sendable @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>, @guaranteed @async @callee_guaranteed (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>, @guaranteed String) -> (@owned String, @error any Error)) -> @error any Error
// CHECK: // end sil function '$s21attr_execution_silgen014testSendableToE35ConversionWithNonisilatedNonsendingyyF'
func testSendableToSendableConversionWithNonisilatedNonsending() {
@Sendable nonisolated(nonsending) func test(
closure: nonisolated(nonsending) @escaping (String) async throws -> String
) async throws {
}

let _: nonisolated(nonsending) @Sendable (
nonisolated(nonsending) @escaping (String) async throws -> String
) async throws -> Void = test
}