Skip to content

Allow @isolated(any) mismatches in witness matching. #72641

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 2 commits into from
Mar 28, 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
29 changes: 21 additions & 8 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: would be great to make it a method on ConstraintLocatorBuilder because it could be made more efficient there.

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 Expand Up @@ -5579,9 +5589,11 @@ bool ConstraintSystem::repairFailures(
}

if (auto *VD = getAsDecl<ValueDecl>(anchor)) {
// Matching a witness to a ObjC protocol requirement.
if (VD->isObjC() && VD->isProtocolRequirement() &&
path[0].is<LocatorPathElt::Witness>() &&
// Matching a witness to an ObjC protocol requirement.
if (VD->isObjC() &&
isa<ProtocolDecl>(VD->getDeclContext()) &&
VD->isProtocolRequirement() &&
path[0].is<LocatorPathElt::Witness>() &&
// Note that the condition below is very important,
// we need to wait until the very last moment to strip
// the concurrency annotations from the inner most type.
Expand All @@ -5593,10 +5605,11 @@ bool ConstraintSystem::repairFailures(
if (!(Context.isSwiftVersionAtLeast(6) ||
Context.LangOpts.StrictConcurrencyLevel ==
StrictConcurrency::Complete)) {
auto strippedLHS = lhs->stripConcurrency(/*resursive=*/true,
auto strippedLHS = lhs->stripConcurrency(/*recursive=*/true,
/*dropGlobalActor=*/true);
auto strippedRHS = rhs->stripConcurrency(/*resursive=*/true,
auto strippedRHS = rhs->stripConcurrency(/*recursive=*/true,
/*dropGlobalActor=*/true);

auto result = matchTypes(strippedLHS, strippedRHS, matchKind,
flags | TMF_ApplyingFix, locator);
if (!result.isFailure()) {
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 {}