Skip to content

Elide conversion of receiver in DropForMap #23416

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
32 changes: 24 additions & 8 deletions compiler/src/dotty/tools/dotc/transform/localopt/DropForMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package dotty.tools.dotc
package transform.localopt

import dotty.tools.dotc.ast.tpd.*
import dotty.tools.dotc.core.Decorators.*
import dotty.tools.dotc.core.Contexts.*
import dotty.tools.dotc.core.Decorators.*
import dotty.tools.dotc.core.Flags.*
import dotty.tools.dotc.core.StdNames.*
import dotty.tools.dotc.core.Symbols.*
import dotty.tools.dotc.core.Types.*
Expand All @@ -25,14 +26,20 @@ class DropForMap extends MiniPhase:
override def description: String = DropForMap.description

override def transformApply(tree: Apply)(using Context): Tree =
if !tree.hasAttachment(desugar.TrailingForMap) then tree
else tree match
case aply @ Apply(MapCall(f), List(Lambda(List(param), body)))
if f.tpe =:= aply.tpe => // make sure that the type of the expression won't change
f // drop the map call
tree.removeAttachment(desugar.TrailingForMap) match
case Some(_) =>
tree match
case aply @ Apply(MapCall(f), List(Lambda(List(param), body))) =>
if f.tpe =:= aply.tpe then // make sure that the type of the expression won't change
return f // drop the map call
else
f match
case Converted(r) if r.tpe =:= aply.tpe =>
return r // drop the map call and the conversion
case _ =>
case _ =>
tree.removeAttachment(desugar.TrailingForMap)
tree
case _ =>
tree

private object Lambda:
def unapply(tree: Tree)(using Context): Option[(List[ValDef], Tree)] =
Expand All @@ -49,6 +56,15 @@ class DropForMap extends MiniPhase:
case TypeApply(fn, _) => unapply(fn)
case _ => None

private object Converted:
def unapply(tree: Tree)(using Context): Option[Tree] = tree match
case Apply(fn @ Apply(_, _), _) => unapply(fn)
case Apply(fn, r :: Nil)
if fn.symbol.is(Implicit) || fn.symbol.name == nme.apply && fn.symbol.owner.derivesFrom(defn.ConversionClass)
=> Some(r)
case TypeApply(fn, _) => unapply(fn)
case _ => None

object DropForMap:
val name: String = "dropForMap"
val description: String = "Drop unused trailing map calls in for comprehensions"
54 changes: 54 additions & 0 deletions tests/run/i23409.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

//> using options -preview

// dropForMap should be aware of conversions to receiver

import language.implicitConversions

trait Func[F[_]]:
def map[A, B](fa: F[A])(f: A => B): F[B]

object Func:
trait Ops[F[_], A]:
type T <: Func[F]
def t: T
def fa: F[A]
def map[B](f: A => B): F[B] = t.map[A, B](fa)(f)

object OldStyle:
implicit def cv[F[_], A](fa0: F[A])(using Func[F]): Ops[F, A] { type T = Func[F] } =
new Ops[F, A]:
type T = Func[F]
def t: T = summon[Func[F]]
def fa = fa0

object NewStyle:
given [F[_], A] => Func[F] => Conversion[F[A], Ops[F, A] { type T = Func[F] }]:
def apply(fa0: F[A]): Ops[F, A] { type T = Func[F] } =
new Ops[F, A]:
type T = Func[F]
def t: T = summon[Func[F]]
def fa = fa0
end Func

def works =
for i <- List(42) yield i

class C[A]
object C:
given Func[C]:
def map[A, B](fa: C[A])(f: A => B): C[B] = ??? // must be elided

def implicitlyConverted() = println:
import Func.OldStyle.given
//C().map(x => x) --> C()
for x <- C() yield x

def usingConversion() = println:
import Func.NewStyle.given
//C().map(x => x) --> C()
for x <- C() yield x

@main def Test =
implicitlyConverted()
usingConversion()
Loading