Skip to content

Witness selection should pick exact matches for effect overloads #65840

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
May 12, 2023
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: 5 additions & 1 deletion lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1196,9 +1196,13 @@ swift::matchWitness(WitnessChecker::RequirementEnvironmentCache &reqEnvCache,
matchKind = MatchKind::RenamedMatch;
else if (requiresNonSendable)
matchKind = MatchKind::RequiresNonSendable;
else if (getEffects(witness).containsOnly(getEffects(req)))
else if (getEffects(req) - getEffects(witness))
// when the difference is non-empty, the witness has fewer effects.
matchKind = MatchKind::FewerEffects;

assert(getEffects(req).contains(getEffects(witness))
&& "witness has more effects than requirement?");

// Success. Form the match result.
RequirementMatch result(witness,
matchKind,
Expand Down
26 changes: 26 additions & 0 deletions test/Interpreter/async_witness_matching.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift -Xfrontend -disable-availability-checking %s -o %t/main
// RUN: %target-codesign %t/main
// RUN: %target-run %t/main | %FileCheck %s

// REQUIRES: concurrency
// REQUIRES: executable_test
// REQUIRES: concurrency_runtime

// Ensures the more exact witness from S is chosen
// to fulfill the requirement from P.
// See: https://github.com/apple/swift/issues/60318

protocol P {
func foo() async -> String
}

struct S: P {
func foo() -> String { "plain" }
func foo() async -> String { "async" }
}

let p: P = S()
let ans = await p.foo()
print(ans)
// CHECK: async