Skip to content
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

[Concurrency] Tune overloading to to allow sync overloads in async contexts #36247

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -775,9 +775,11 @@ enum ScoreKind {
SK_Hole,
/// A reference to an @unavailable declaration.
SK_Unavailable,
/// A reference to an async function in a synchronous context, or
/// vice versa.
SK_AsyncSyncMismatch,
/// A reference to an async function in a synchronous context.
SK_AsyncInSyncMismatch,
/// Synchronous function in an asynchronous context or a conversion of
/// a synchronous function to an asynchronous one.
SK_SyncInAsync,
/// A use of the "forward" scan for trailing closures.
SK_ForwardTrailingClosure,
/// A use of a disfavored overload.
Expand Down
7 changes: 5 additions & 2 deletions lib/Sema/CSRanking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ static StringRef getScoreKindName(ScoreKind kind) {
case SK_Unavailable:
return "use of an unavailable declaration";

case SK_AsyncSyncMismatch:
return "async/synchronous mismatch";
case SK_AsyncInSyncMismatch:
return "async-in-synchronous mismatch";

case SK_SyncInAsync:
return "sync-in-asynchronous";

case SK_ForwardTrailingClosure:
return "forward scan when matching a trailing closure";
Expand Down
2 changes: 2 additions & 0 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1983,6 +1983,8 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
if (recordFix(fix))
return getTypeMatchFailure(locator);
}

increaseScore(SK_SyncInAsync);
}

// A @concurrent function can be a subtype of a non-@concurrent function.
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CSStep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ bool DisjunctionStep::shouldStopAt(const DisjunctionChoice &choice) const {
auto delta = LastSolvedChoice->second - getCurrentScore();
bool hasUnavailableOverloads = delta.Data[SK_Unavailable] > 0;
bool hasFixes = delta.Data[SK_Fix] > 0;
bool hasAsyncMismatch = delta.Data[SK_AsyncSyncMismatch] > 0;
bool hasAsyncMismatch = delta.Data[SK_AsyncInSyncMismatch] > 0;
auto isBeginningOfPartition = choice.isBeginningOfPartition();

// Attempt to short-circuit evaluation of this disjunction only
Expand Down
6 changes: 4 additions & 2 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2820,8 +2820,10 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
// If we're choosing an asynchronous declaration within a synchronous
// context, or vice-versa, increase the async/async mismatch score.
if (auto func = dyn_cast<AbstractFunctionDecl>(decl)) {
if (func->isAsyncContext() != isAsynchronousContext(useDC))
increaseScore(SK_AsyncSyncMismatch);
if (func->isAsyncContext() != isAsynchronousContext(useDC)) {
increaseScore(
func->isAsyncContext() ? SK_AsyncInSyncMismatch : SK_SyncInAsync);
}
}

// If we're binding to an init member, the 'throws' need to line up
Expand Down
23 changes: 23 additions & 0 deletions test/Constraints/async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,26 @@ struct FunctionTypes {
asyncNonThrowing = syncThrowing // expected-error{{invalid conversion}}
}
}

// Overloading when there is conversion from sync to async.
func bar(_ f: (Int) -> Int) -> Int {
return f(2)
}

func bar(_ f: (Int) async -> Int) async -> Int {
return await f(2)
}

func incrementSync(_ x: Int) -> Int {
return x + 1
}

func incrementAsync(_ x: Int) async -> Int {
return x + 1
}

func testAsyncWithConversions() async {
_ = bar(incrementSync)
_ = bar { -$0 }
_ = bar(incrementAsync) // expected-error{{call is 'async' but is not marked with 'await'}}
}