Skip to content

Include P in the implicit scope of P.this.type #17088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,8 @@ trait ImplicitRunInfo:
traverse(t.prefix)
case t: ThisType if t.cls.is(Module) && t.cls.isStaticOwner =>
traverse(t.cls.sourceModule.termRef)
case t: ThisType =>
traverse(t.tref)
case t: ConstantType =>
traverse(t.underlying)
case t: TypeParamRef =>
Expand Down Expand Up @@ -743,6 +745,7 @@ trait ImplicitRunInfo:
* - If `T` is a singleton reference, the anchors of its underlying type, plus,
* if `T` is of the form `(P#x).type`, the anchors of `P`.
* - If `T` is the this-type of a static object, the anchors of a term reference to that object.
* - If `T` is some other this-type `P.this.type`, the anchors of `P`.
* - If `T` is some other type, the union of the anchors of each constituent type of `T`.
*
* The _implicit scope_ of a type `tp` is the smallest set S of term references (i.e. TermRefs)
Expand Down
3 changes: 2 additions & 1 deletion docs/_docs/reference/changed-features/implicit-resolution.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ Opaque type aliases count as anchors only outside the scope where their alias is
1. If _T_ is a reference to a type parameter, the union of the anchors of both of its bounds.
1. If _T_ is a singleton reference, the anchors of its underlying type, plus,
if _T_ is of the form _(P#x).type_, the anchors of _P_.
1. If _T_ is the this-type _o.this_ of a static object _o_, the anchors of a term reference _o.type_ to that object.
1. If _T_ is the this-type _o.this_ of a static object _o_, the anchors of a term reference _o.type_ to that object,
1. If _T_ is some other this-type _P.this.type_, the anchors of _P_.
1. If _T_ is some other type, the union of the anchors of each constituent type of _T_.

**Definition:** The _implicit scope_ of a type _T_ is the smallest set _S_ of term references such that
Expand Down
6 changes: 6 additions & 0 deletions tests/pos/this-implicit-scope.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Foo[+T]
class Elem:
def one(a: Elem, x: Foo[a.type]): Int = x.ext
def two(x: Foo[Elem.this.type]): Int = x.ext
object Elem:
extension (x: Foo[Elem]) def ext: Int = 1