Skip to content

Fix #9509: Reveal further arguments in extension method applications #9542

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 2 commits into from
Aug 14, 2020
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
25 changes: 14 additions & 11 deletions compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2086,20 +2086,23 @@ trait Applications extends Compatibility {
* where <type-args> comes from `pt` if it is a (possibly ignored) PolyProto.
*/
def extMethodApply(methodRef: untpd.Tree, receiver: Tree, pt: Type)(using Context): Tree = {
/** Integrate the type arguments from `currentPt` into `methodRef`, and produce
* a matching expected type.
* If `currentPt` is ignored, the new expected type will be ignored too.
/** Integrate the type arguments (if any) from `currentPt` into `tree`, and produce
* an expected type that hides the appropriate amount of information through IgnoreProto.
*/
def integrateTypeArgs(currentPt: Type, wasIgnored: Boolean = false): (untpd.Tree, Type) = currentPt match {
case IgnoredProto(ignored) =>
integrateTypeArgs(ignored, wasIgnored = true)
def normalizePt(tree: untpd.Tree, currentPt: Type): (untpd.Tree, Type) = currentPt match
// Always reveal expected arguments to guide inference (needed for i9509.scala)
case IgnoredProto(ignored: FunOrPolyProto) =>
normalizePt(tree, ignored)
// Always hide expected member to allow for chained extensions (needed for i6900.scala)
case _: SelectionProto =>
(tree, IgnoredProto(currentPt))
case PolyProto(targs, restpe) =>
val core = untpd.TypeApply(methodRef, targs.map(untpd.TypedSplice(_)))
(core, if (wasIgnored) IgnoredProto(restpe) else restpe)
val tree1 = untpd.TypeApply(tree, targs.map(untpd.TypedSplice(_)))
normalizePt(tree1, restpe)
case _ =>
(methodRef, pt)
}
val (core, pt1) = integrateTypeArgs(pt)
(tree, currentPt)

val (core, pt1) = normalizePt(methodRef, pt)
val app = withMode(Mode.SynthesizeExtMethodReceiver) {
typed(untpd.Apply(core, untpd.TypedSplice(receiver) :: Nil), pt1, ctx.typerState.ownedVars)
}
Expand Down
11 changes: 11 additions & 0 deletions tests/pos/i9509.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
enum AList[+A] {
case Cons[X](head: X, tail: AList[X]) extends AList[X]
case Nil
}

object AList {
extension [A](l: AList[A]) def sum(using numeric: Numeric[A]): A = l match {
case Cons(x, xs) => numeric.plus(x, xs.sum(using numeric))
case Nil => numeric.zero
}
}