Skip to content

Commit 5dfc6b8

Browse files
committed
Ignore async/sync mismatch on main
The main function is different from other function resolutions. It isn't being called from a synchronous or asynchronous context, but defines whether the program starts in a synchronous or async context. As a result, we should not prefer one over the other, so the scoring mechanism shouldn't involve the async/sync score when resolving the main function. This patch adds a constraint solver flag to ignore async/sync context mismatches so that we do not favor one over the other, but otherwise use the normal resolution behavior.
1 parent 94e30a8 commit 5dfc6b8

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,9 @@ enum class ConstraintSystemFlags {
15591559
/// calling conventions, say due to Clang attributes such as
15601560
/// `__attribute__((ns_consumed))`.
15611561
UseClangFunctionTypes = 0x80,
1562+
1563+
/// When set, ignore async/sync mismatches
1564+
IgnoreAsyncSyncMismatch = 0x100,
15621565
};
15631566

15641567
/// Options that affect the constraint system as a whole.

lib/Sema/ConstraintSystem.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3304,7 +3304,8 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
33043304
// If we're choosing an asynchronous declaration within a synchronous
33053305
// context, or vice-versa, increase the async/async mismatch score.
33063306
if (auto func = dyn_cast<AbstractFunctionDecl>(decl)) {
3307-
if (!func->hasPolymorphicEffect(EffectKind::Async) &&
3307+
if (!Options.contains(ConstraintSystemFlags::IgnoreAsyncSyncMismatch) &&
3308+
!func->hasPolymorphicEffect(EffectKind::Async) &&
33083309
func->isAsyncContext() != isAsynchronousContext(useDC)) {
33093310
increaseScore(
33103311
func->isAsyncContext() ? SK_AsyncInSyncMismatch : SK_SyncInAsync);

lib/Sema/TypeCheckAttr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2172,7 +2172,8 @@ SynthesizeMainFunctionRequest::evaluate(Evaluator &evaluator,
21722172
// type-checking the call to mainType.main().
21732173

21742174
auto resolution = resolveValueMember(
2175-
*declContext, nominal->getInterfaceType(), context.Id_main);
2175+
*declContext, nominal->getInterfaceType(), context.Id_main,
2176+
constraints::ConstraintSystemFlags::IgnoreAsyncSyncMismatch);
21762177
FuncDecl *mainFunction =
21772178
resolveMainFunctionDecl(declContext, resolution, context);
21782179
if (!mainFunction) {

0 commit comments

Comments
 (0)