Skip to content

[6.0] Allow @isolated(any) mismatches in witness matching. #72672

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
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
16 changes: 13 additions & 3 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3041,6 +3041,13 @@ matchFunctionThrowing(ConstraintSystem &cs,
}
}

static bool isWitnessMatching(ConstraintLocatorBuilder locator) {
SmallVector<LocatorPathElt, 4> path;
(void) locator.getLocatorParts(path);
return (path.size() == 1 &&
path[0].is<LocatorPathElt::Witness>());
}

bool
ConstraintSystem::matchFunctionIsolations(FunctionType *func1,
FunctionType *func2,
Expand All @@ -3054,8 +3061,11 @@ ConstraintSystem::matchFunctionIsolations(FunctionType *func1,
// function-conversion score to make sure this solution is worse than
// an exact match.
// FIXME: there may be a better way. see https://github.com/apple/swift/pull/62514
auto matchIfConversion = [&]() -> bool {
if (kind < ConstraintKind::Subtype)
auto matchIfConversion = [&](bool isErasure = false) -> bool {
// We generally require a conversion here, but allow some lassitude
// if we're doing witness-matching.
if (kind < ConstraintKind::Subtype &&
!(isErasure && isWitnessMatching(locator)))
return false;
increaseScore(SK_FunctionConversion, locator);
return true;
Expand Down Expand Up @@ -3154,7 +3164,7 @@ ConstraintSystem::matchFunctionIsolations(FunctionType *func1,
// isolation as a conversion.
case FunctionTypeIsolation::Kind::NonIsolated:
case FunctionTypeIsolation::Kind::GlobalActor:
return matchIfConversion();
return matchIfConversion(/*erasure*/ true);

// Parameter isolation is value-dependent and can't be erased in the
// abstract, though. We need to be able to recover the isolation from
Expand Down
27 changes: 27 additions & 0 deletions test/SILGen/isolated_any_conformance.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: %target-swift-frontend -emit-silgen -enable-experimental-feature IsolatedAny %s -module-name test -swift-version 6 -disable-availability-checking | %FileCheck %s
// REQUIRES: concurrency
// REQUIRES: asserts

// rdar://125394096. This was a source compatibility failure in which adding
// @isolated(any) to TaskGroup.addTask caused problems with a project that
// introduced a protocol for different task group types.

struct A<T> {
func enqueue(operation: @escaping @isolated(any) () async -> T) {}
}

protocol Enqueuer {
associatedtype Result
func enqueue(operation: @escaping @Sendable () async -> Result)
}

extension A : Enqueuer {}

// CHECK-LABEL: sil private [transparent] [thunk] [ossa] @$s4test1AVyxGAA8EnqueuerA2aEP7enqueue9operationy6ResultQzyYaYbc_tFTW :
// CHECK: bb0(%0 : @guaranteed $@Sendable @async @callee_guaranteed @substituted <τ_0_0> () -> @out τ_0_0 for <τ_0_0>, %1 : $*A<τ_0_0>):
// CHECK-NEXT: [[CONVERTED_FN:%.*]] = convert_function %0 : $@Sendable @async @callee_guaranteed @substituted <τ_0_0> () -> @out τ_0_0 for <τ_0_0> to $@Sendable @async @callee_guaranteed () -> @out τ_0_0
// CHECK-NEXT: [[ISOLATION:%.*]] = enum $Optional<any Actor>, #Optional.none
// CHECK-NEXT: [[FN_COPY:%.*]] = copy_value [[CONVERTED_FN]] : $@Sendable @async @callee_guaranteed () -> @out τ_0_0
// CHECK-NEXT: // function_ref
// CHECK-NEXT: [[THUNK:%.*]] = function_ref @$sxIeghHr_xIeAghHr_lTR
// CHECK-NEXT: partial_apply [callee_guaranteed] [isolated_any] [[THUNK]]<τ_0_0>([[ISOLATION]], [[FN_COPY]])
16 changes: 16 additions & 0 deletions test/decl/protocol/conforms/isolated_any.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %target-swift-frontend -typecheck -enable-experimental-feature IsolatedAny -verify %s

struct A<T> {
// expected-note @+1 {{candidate has non-matching type}}
func enqueue(operation: @escaping @Sendable () async -> T) {}
}

protocol AnnotatedEnqueuer {
associatedtype Result

// expected-note @+1 {{protocol requires function}}
func enqueue(operation: @escaping @isolated(any) () async -> Result)
}

// expected-error @+1 {{type 'A<T>' does not conform to protocol 'AnnotatedEnqueuer'}}
extension A : AnnotatedEnqueuer {}