1# Specialization23**TODO**: where does Chalk fit in? Should we mention/discuss it here?45Defined in the `specialize` module.67The basic strategy is to build up a *specialization graph* during8coherence checking (coherence checking looks for [overlapping impls](../coherence.md)). 9Insertion into the graph locates the right place10to put an impl in the specialization hierarchy; if there is no right11place (due to partial overlap but no containment), you get an overlap12error. Specialization is consulted when selecting an impl (of course),13and the graph is consulted when propagating defaults down the14specialization hierarchy.1516You might expect that the specialization graph would be used during17selection – i.e. when actually performing specialization. This is18not done for two reasons:1920- It's merely an optimization: given a set of candidates that apply,21 we can determine the most specialized one by comparing them directly22 for specialization, rather than consulting the graph. Given that we23 also cache the results of selection, the benefit of this24 optimization is questionable.2526- To build the specialization graph in the first place, we need to use27 selection (because we need to determine whether one impl specializes28 another). Dealing with this reentrancy would require some additional29 mode switch for selection. Given that there seems to be no strong30 reason to use the graph anyway, we stick with a simpler approach in31 selection, and use the graph only for propagating default32 implementations.3334Trait impl selection can succeed even when multiple impls can apply,35as long as they are part of the same specialization family. In that36case, it returns a *single* impl on success – this is the most37specialized impl *known* to apply. However, if there are any inference38variables in play, the returned impl may not be the actual impl we39will use at codegen time. Thus, we take special care to avoid projecting40associated types unless either (1) the associated type does not use41`default` and thus cannot be overridden or (2) all input types are42known concretely.4344## Additional Resources4546[This talk][talk] by @sunjay may be useful. Keep in mind that the talk only47gives a broad overview of the problem and the solution (it was presented about48halfway through @sunjay's work). Also, it was given in June 2018, and some49things may have changed by the time you watch it.5051[talk]: https://www.youtube.com/watch?v=rZqS4bLPL24
Findings
✓ No findings reported for this file.