You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using `resolveValueMember` can result in the wrong, or unrelated main
functions being selected. If an error occurs while resolving, such as an
ambiguous resolution, the solution will contain everything called
'main'. During the resolution, the `BestOverload` will not be set, so
the code skips down to iterating over the member decls and selecting the
first declaration that is a `MainTypeMainMethod`.
The order of candidates in the failure state is related to the order
that the declarations appear in source. The selected and potentially
unrelated main function is called from `$main`. The call in the
expression will then detect the invalid state later.
When only a single main function could exist in a given context, this
was not a problem. Any other possible solutions would result in an
error, either due to a duplicate declaration, or from calling an
unrelated function. When we introduced the asynchronous main function,
the resolution can be ambiguous because we allow asynchronous overloads
of synchronous functions. This enables us to legally write
```
struct MainType {
static func main() { }
static func main() async { }
}
```
From the perspective of duplicate declarations, this is not an issue. It
is, however, ambiguous since we have no calling context with which to
break the tie.
In the original code, this example would select the synchronous main
function because it comes first in the source. If we flip the
declarations, the asynchronous main function is selected because it
comes first in the source.
Instead, using the constraint solver to solve for the correct overload
will ensure that we only get back a valid main function. If the
constraint solver reports no solutions or many solutions, we can emit an
error immediately, as it will not consider unrelated functions.
0 commit comments