Skip to content

Commit 1dd04dd

Browse files
committed
Update docs
1 parent 5d520ec commit 1dd04dd

File tree

2 files changed

+54
-22
lines changed

2 files changed

+54
-22
lines changed

compiler/src/dotty/tools/dotc/typer/Implicits.scala

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -617,38 +617,37 @@ trait ImplicitRunInfo:
617617

618618
/** The implicit scope of a type `tp`, which is specified by the following definitions.
619619
*
620-
* A reference is an _anchor_ if it refers to a class or an abstract type or an
621-
* opaque type alias. References to object classes are also anchors, except that
622-
* references to packages and package objects are anchors only under -source:3.0-migration.
620+
* A reference is an _anchor_ if it refers to an object, a class, a trait, an
621+
* abstract type, or an opaque type alias. References to packages and package
622+
* objects are anchors only under -source:3.0-migration.
623623
*
624-
* The _parts_ of a type `T` is a set of type references defined as follows:
624+
* The _anchors_ of a type `T` is a set of references defined as follows:
625625
*
626-
* - If `T` is an alias of `T'`, the parts of `T'`.
627-
* - If `T` is a type reference to an anchor, `T` itself and the prefix parts of `T`.
628-
* - If `T` is a reference to a type parameter, the parts of its bounds.
629-
* - If `T` is a term reference, the parts of its underlying type, and the prefix parts of `T`.
630-
* - If `T` is the this-type of a static object, the parts of the term reference to that object.
631-
* - If `T` is a constant type, the parts of its underlying type.
632-
* - If `T` is some other type, the union of parts of each constituent type of `T`
633-
* that is of one of the forms above.
634-
*
635-
* The _prefix parts_ of a type reference or term reference `P#A` with prefix `P` is
636-
* a set of type references defined as follows:
637-
*
638-
* - If `P` is a term reference `(Q # x).type, the prefix parts of `Q`.
639-
* Under -source:3.0-migration, also include the parts of `P`'s underlying type.
640-
* - If `P` is some other singleton type, the empty set.
641-
* - If `P` is some other type, the parts of `P`.
626+
* - If `T` is a reference to an anchor, `T` itself plus, if `T` is of the form
627+
* `P#A`, the anchors of `P`.
628+
* - If `T` is an alias of `U`, the anchors of `U`.
629+
* - If `T` is a reference to a type parameter, the union of the anchors of both of its bounds.
630+
* - If `T` is a singleton reference, the anchors of its underlying type, plus,
631+
* if `T` is of the form `(P#x).type`, the anchors of `P`.
632+
* - If `T` is the this-type of a static object, the anchors of a term reference to that object.
633+
* - If `T` is some other type, the union of the anchors of each constituent type of `T`.
642634
*
643635
* The _implicit scope_ of a type `tp` is the smallest set S of term references (i.e. TermRefs)
644636
* such that
645637
*
646638
* - If `T` is a reference to a class, S includes a reference to the companion object
647639
* of the class, if it exists, as well as the implicit scopes of all of `T`'s parent classes.
648-
* - If `T` is a reference to an opaque type alias or abstract type named `A`, S includes
640+
* - If `T` is a reference to an object, S includes `T` itself as well as
641+
* the implicit scopes of all of `T`'s parent classes.
642+
* - If `T` is a reference to an opaque type alias named `A`, S includes
649643
* a reference to an object `A` defined in the same scope as the type, if it exists,
650644
* as well as the implicit scope of `T`'s underlying type or bounds.
651-
* - If `T` is some other type, S includes the implicit scopes of all parts of `T`.
645+
* - If `T` is a reference to an an abstract type named `A`, S includes
646+
* a reference to an object `A` defined in the same scope as the type, if it exists,
647+
* as well as the implicit scopes of `T`'s lower and upper bound.
648+
* - If `T` is a reference to an anchor of the form `p.A` then S also includes
649+
* all term references on the path `p`.
650+
* - If `T` is some other type, S includes the implicit scopes of all anchors of `T`.
652651
*/
653652
def implicitScope(tp: Type)(using Context): OfTypeImplicits =
654653
object liftToAnchors extends TypeMap:

docs/docs/reference/changed-features/implicit-resolution.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,39 @@ Both `a` and `b` are visible as implicits at the point of the definition
5252
of `type C`. However, a reference to `p.o.C` outside of package `p` will
5353
have only `b` in its implicit search scope but not `a`.
5454

55+
In more detail, here are the rules for what constitutes the implicit scope of
56+
a type:
57+
58+
**Definition:** A reference is an _anchor_ if it refers to an object, a class, a trait, an abstract type, or an opaque type alias. References to packages and package objects are anchors only under -source:3.0-migration.
59+
60+
**Definition:** The _anchors_ of a type _T_ is a set of references defined as follows:
61+
62+
- If _T_ is a reference to an anchor, _T_ itself plus, if _T_ is of the form _P#A_, the anchors of _P_.
63+
- If _T_ is an alias of _U_, the anchors of _U_.
64+
- If _T_ is a reference to a type parameter, the union of the anchors of both of its bounds.
65+
- If _T_ is a singleton reference, the anchors of its underlying type, plus,
66+
if _T_ is of the form _(P#x).type_, the anchors of _P_.
67+
- If _T_ is the this-type _o.this_ of a static object _o_, the anchors of a term reference _o.type_ to that object.
68+
- If _T_ is some other type, the union of the anchors of each constituent type of _T_.
69+
70+
**Definition:** The _implicit scope_ of a type _T_ is the smallest set _S_ of term references such that
71+
72+
- If _T_ is a reference to a class, _S_ includes a reference to the companion object
73+
of the class, if it exists, as well as the implicit scopes of all of _T_'s parent classes.
74+
- If _T_ is a reference to an object, _S_ includes _T_ itself as well as
75+
the implicit scopes of all of _T_'s parent classes.
76+
- If _T_ is a reference to an opaque type alias named _A_, _S_ includes
77+
a reference to an object _A_ defined in the same scope as the type, if it exists,
78+
as well as the implicit scope of _T_'s underlying type or bounds.
79+
- If _T_ is a reference to an an abstract type named _A_, _S_ includes
80+
a reference to an object _A_ defined in the same scope as the type, if it exists,
81+
as well as the implicit scopes of _T_'s lower and upper bound.
82+
- If _T_ is a reference to an anchor of the form _p.A_ then _S_ also includes
83+
all term references on the path _p_.
84+
- If _T_ is some other type, _S_ includes the implicit scopes of all anchors of _T_.
85+
86+
87+
5588
**4.** The treatment of ambiguity errors has changed. If an ambiguity is encountered in some recursive step of an implicit search, the ambiguity is propagated to the caller.
5689

5790
Example: Say you have the following definitions:

0 commit comments

Comments
 (0)