Skip to content
Open
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
12 changes: 10 additions & 2 deletions lib/Sema/CSOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "OpenedExistentials.h"
#include "TypeChecker.h"
#include "swift/AST/ConformanceLookup.h"
#include "swift/AST/Decl.h"
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/Expr.h"
#include "swift/AST/GenericSignature.h"
Expand Down Expand Up @@ -768,7 +769,7 @@ static std::optional<DisjunctionInfo> preserveFavoringOfUnlabeledUnaryArgument(
SmallVector<Constraint *, 2> favoredChoices;
forEachDisjunctionChoice(
cs, disjunction,
[&argumentType, &favoredChoices, &argument](
[&cs, &argumentType, &favoredChoices, &argument](
Constraint *choice, ValueDecl *decl, FunctionType *overloadType) {
if (decl->getAttrs().hasAttribute<DisfavoredOverloadAttr>())
return;
Expand All @@ -784,8 +785,15 @@ static std::optional<DisjunctionInfo> preserveFavoringOfUnlabeledUnaryArgument(
(isa<LiteralExpr>(argument) || isa<BinaryExpr>(argument)))
return;

if (argumentType->isEqual(param.getPlainType()))
if (argumentType->isEqual(param.getPlainType())) {
if (auto *func = dyn_cast<AbstractFunctionDecl>(decl)) {
if (func->isAsyncContext() !=
cs.isAsynchronousContext(choice->getDeclContext()))
return;
}

favoredChoices.push_back(choice);
}
});

return DisjunctionInfoBuilder(/*score=*/favoredChoices.empty() ? 0 : 1,
Expand Down
31 changes: 31 additions & 0 deletions test/Constraints/old_hack_related_ambiguities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,34 @@ do {
}
}
}

// Calls with single unlabeled arguments shouldn't favor overloads that don't match on async.
do {
struct V {
var data: Int = 0
}

func test(_: Int) -> Int { 42 }
func test(_: Int, v: Int = 42) async -> V? { nil }

func doAsync<T>(_ fn: () async -> T) async -> T { await fn() }

func computeAsync(v: Int) async {
let v1 = await test(v)
if let v1 {
_ = v1.data // Ok
}

let v2 = await doAsync { await test(v) }
if let v2 {
_ = v2.data // Ok
}

_ = await doAsync {
let v = await test(v)
if let v {
_ = v.data // Ok
}
}
}
}