Skip to content

Commit e413739

Browse files
committed
docs: Document the fields of the score in TypeChecker.md
1 parent 4127a4e commit e413739

File tree

1 file changed

+46
-11
lines changed

1 file changed

+46
-11
lines changed

docs/TypeChecker.md

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -744,15 +744,51 @@ possible that it will find multiple solutions to the constraint system
744744
as given. Such cases are not necessarily ambiguities, because the
745745
solver can then compare the solutions to determine whether one of
746746
the solutions is better than all of the others. To do so, it computes
747-
a "score" for each solution based on a number of factors:
748-
749-
- How many user-defined conversions were applied.
750-
- How many non-trivial function conversions were applied.
751-
- How many literals were given "non-default" types.
752-
753-
Solutions with smaller scores are considered better solutions. When
754-
two solutions have the same score, the type variables and overload
755-
choices of the two systems are compared to produce a relative score:
747+
a "score" for each solution. The score is an integer vector with the
748+
following components:
749+
750+
- Number of fixes applied. Fixes generally only appear in "salvage mode"
751+
when emitting diagnostics.
752+
- Number of holes introduced. This is another diagnostic phenomenon.
753+
- Number of unavailable declarations referenced.
754+
- Number of declarations referenced from modules that were not imported.
755+
- Number of async declarations referenced from a sync context.
756+
- Number of sync declarations referenced from an async context.
757+
- Number of times "forward" scan for a trailing closure was performed.
758+
- Number of `@_disfavoredOverload` declarations referenced.
759+
- Number of members found via leading-dot syntax with unwrapped optional base.
760+
- Number of implicitly unwrapped optionals.
761+
- Number of implicit value conversions. Some but not all non-trivial subtype relations
762+
fall into this category.
763+
- Number of implicit conversions. Some but not all non-trivial subtype relations
764+
fall into this category.
765+
- Number of function conversions performed.
766+
- Number of literals with non-default types.
767+
- Number of collection conversions. (`Array<X>` to `Array<Y>`, etc. See below.)
768+
- Number of value-to-optional conversions (`X` to `Optional<X>`, etc. See below.)
769+
- Number of conversions to `Any`.
770+
- Number of keypath subscript applications (`foo[keyPath: \.bar`).
771+
- Number of pointer conversions where the destination is a function parameter
772+
with type parameter type.
773+
- Number of function to autoclosure conversions (usually, an argument of type `X`
774+
is "injected" into the `@autoclosure () -> X` parameter, which does not increase
775+
the score).
776+
- Number of missing conformances that we might only warn about later. (For example,
777+
missing `Sendable` conformance in Swift 5 language mode. Such a solution is valid,
778+
but has a higher score.)
779+
- Number of unapplied function references. (For example, if you have a `var count`
780+
and a `func count(...)` member on a type, `foo.count` will prefer the former,
781+
because the latter solution has a higher score.)
782+
783+
Most fields in the score are expected to be zero at any given time. Two scores
784+
are compared by comparing their elements lexicographically from left to right.
785+
A smaller score is "better" than a larger score.
786+
787+
### Solution Ranking
788+
789+
When two solutions have the same score, a further ranking step considers all
790+
type variables and overload choices. This computes an integer for each
791+
solution, called the "solution rank":
756792

757793
- If the two solutions have selected different type variable bindings
758794
for a type variable where a "more specific" type variable is a
@@ -765,8 +801,7 @@ choices of the two systems are compared to produce a relative score:
765801
the overload picked in the other solution, then first solution earns
766802
+1.
767803

768-
The solution with the greater relative score is considered to be
769-
better than the other solution.
804+
The highest-ranked solution is considered to be better than the other solution.
770805

771806
### Solution Application
772807

0 commit comments

Comments
 (0)