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
11 changes: 9 additions & 2 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4280,8 +4280,15 @@ NeverNullType TypeResolver::resolveASTFunctionType(
if (!repr->isInvalid())
isolation = FunctionTypeIsolation::forNonIsolated();
} else if (!getWithoutClaiming<CallerIsolatedTypeRepr>(attrs)) {
if (ctx.LangOpts.getFeatureState(Feature::NonisolatedNonsendingByDefault)
.isEnabledForMigration()) {
// Infer async function type as `nonisolated(nonsending)` if there is
// no `@concurrent` or `nonisolated(nonsending)` attribute and isolation
// is nonisolated.
if (ctx.LangOpts.hasFeature(Feature::NonisolatedNonsendingByDefault) &&
repr->isAsync() && isolation.isNonIsolated()) {
isolation = FunctionTypeIsolation::forNonIsolatedCaller();
} else if (ctx.LangOpts
.getFeatureState(Feature::NonisolatedNonsendingByDefault)
.isEnabledForMigration()) {
// Diagnose only in the interface stage, which is run once.
if (inStage(TypeResolutionStage::Interface)) {
warnAboutNewNonisolatedAsyncExecutionBehavior(ctx, repr, isolation);
Expand Down
66 changes: 66 additions & 0 deletions test/Concurrency/attr_execution/attr_execution.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,69 @@ func concurrentTest() async {}
// CHECK: sil hidden [ossa] @$s14attr_execution10callerTestyyYaF : $@convention(thin) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>) -> () {
nonisolated(nonsending)
func callerTest() async {}

struct Test {
// CHECK-LABEL: // closure #1 in variable initialization expression of Test.x
// CHECK: // Isolation: caller_isolation_inheriting
// CHECK: sil private [ossa] @$s14attr_execution4TestV1xyyYaYCcvpfiyyYaYCcfU_ : $@convention(thin) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>) -> ()
var x: () async -> Void = {}

// CHECK-LABEL: // Test.test()
// CHECK: // Isolation: caller_isolation_inheriting
// CHECK: sil hidden [ossa] @$s14attr_execution4TestV4testyyYaF : $@convention(method) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>, @guaranteed Test) -> ()
// CHECK: bb0([[ISOLATION:%.*]] : @guaranteed $Optional<any Actor>, [[SELF:%.*]] : @guaranteed $Test)
// CHECK: [[X_REF:%.*]] = struct_extract %1, #Test.x
// CHECK: [[X_REF_COPY:%.]] = copy_value [[X_REF]]
// CHECK: [[BORROWED_X:%.*]] = begin_borrow [[X_REF_COPY]]
// CHECK: apply [[BORROWED_X]]([[ISOLATION]]) : $@async @callee_guaranteed (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>) -> ()
// CHECK: } // end sil function '$s14attr_execution4TestV4testyyYaF'
func test() async {
await x()
}

// CHECK-LABEL: // Test.testParam(fn:)
// CHECK: // Isolation: caller_isolation_inheriting
// CHECK: sil hidden [ossa] @$s14attr_execution4TestV9testParam2fnyyyYaYCcSg_tYaF : $@convention(method) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>, @guaranteed Optional<@async @callee_guaranteed (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>) -> ()>, @guaranteed Test) -> ()
// CHECK: bb0([[ISOLATION:%.*]] : @guaranteed $Optional<any Actor>, [[OPT_FN:%.*]] : @guaranteed $Optional<@async @callee_guaranteed (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>) -> ()>, [[SELF:%.*]] : @guaranteed $Test)
// CHECK: bb1([[FN:%.*]] : @owned $@async @callee_guaranteed (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>) -> ())
// CHECK: [[BORROWED_FN:%.*]] = begin_borrow [[FN]]
// CHECK: apply [[BORROWED_FN]]([[ISOLATION]]) : $@async @callee_guaranteed (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>) -> ()
// CHECK: } // end sil function '$s14attr_execution4TestV9testParam2fnyyyYaYCcSg_tYaF'
func testParam(fn: (() async -> Void)?) async {
await fn?()
}
}

// CHECK-LABEL: // testLocal()
// CHECK: // Isolation: caller_isolation_inheriting
// CHECK: sil hidden [ossa] @$s14attr_execution9testLocalyyYaF : $@convention(thin) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>) -> () {
// CHECK: bb0([[ISOLATION:%.*]] : @guaranteed $Optional<any Actor>)
// CHECK: bb1([[FN:%.*]] : @owned $@async @callee_guaranteed (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>) -> ())
// CHECK: [[BORROWED_FN:%.*]] = begin_borrow [[FN]]
// CHECK: apply [[BORROWED_FN]]([[ISOLATION]]) : $@async @callee_guaranteed (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>) -> ()
// CHECK: } // end sil function '$s14attr_execution9testLocalyyYaF'
func testLocal() async {
let fn: (() async -> Void)? = nil
await fn?()
}

// CHECK-LABEL: // takesClosure(fn:)
// CHECK: // Isolation: unspecified
// CHECK: sil hidden [ossa] @$s14attr_execution12takesClosure2fnyyyYaYCXE_tF : $@convention(thin) (@guaranteed @noescape @async @callee_guaranteed (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>) -> ()) -> ()
func takesClosure(fn: () async -> Void) {
}

// CHECK-LABEL: sil hidden [ossa] @$s14attr_execution11testClosureyyF : $@convention(thin) () -> ()
// CHECK: [[CLOSURE:%.*]] = function_ref @$s14attr_execution11testClosureyyFyyYaYCXEfU_ : $@convention(thin) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>) -> ()
// CHECK: [[THUNKED_CLOSURE:%.*]] = thin_to_thick_function %0 to $@noescape @async @callee_guaranteed (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>) -> ()
// CHECK: [[TAKES_CLOSURE:%.*]] = function_ref @$s14attr_execution12takesClosure2fnyyyYaYCXE_tF : $@convention(thin) (@guaranteed @noescape @async @callee_guaranteed (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>) -> ()) -> ()
// CHECK: apply [[TAKES_CLOSURE]]([[THUNKED_CLOSURE]])
// CHECK: } // end sil function '$s14attr_execution11testClosureyyF'

// CHECK-LABEL: // closure #1 in testClosure()
// CHECK: // Isolation: caller_isolation_inheriting
// CHECK: sil private [ossa] @$s14attr_execution11testClosureyyFyyYaYCXEfU_ : $@convention(thin) @async (@sil_isolated @sil_implicit_leading_param @guaranteed Optional<any Actor>) -> ()
func testClosure() {
takesClosure {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %target-typecheck-verify-swift -swift-version 6 -enable-upcoming-feature NonisolatedNonsendingByDefault %s

// REQUIRES: swift_feature_NonisolatedNonsendingByDefault

func testCasts() {
let defaultedType = (() async -> ()).self
_ = defaultedType as (@concurrent () async -> ()).Type
// expected-error@-1 {{cannot convert value of type '(nonisolated(nonsending) () async -> ()).Type' to type '(() async -> ()).Type' in coercion}}
_ = defaultedType as (nonisolated(nonsending) () async -> ()).Type // Ok
}
Loading