@@ -2,8 +2,9 @@ package dotty.tools.dotc
22package transform .localopt
33
44import dotty .tools .dotc .ast .tpd .*
5- import dotty .tools .dotc .core .Decorators .*
65import dotty .tools .dotc .core .Contexts .*
6+ import dotty .tools .dotc .core .Decorators .*
7+ import dotty .tools .dotc .core .Flags .*
78import dotty .tools .dotc .core .StdNames .*
89import dotty .tools .dotc .core .Symbols .*
910import dotty .tools .dotc .core .Types .*
@@ -25,14 +26,20 @@ class DropForMap extends MiniPhase:
2526 override def description : String = DropForMap .description
2627
2728 override def transformApply (tree : Apply )(using Context ): Tree =
28- if ! tree.hasAttachment(desugar.TrailingForMap ) then tree
29- else tree match
30- case aply @ Apply (MapCall (f), List (Lambda (List (param), body)))
31- if f.tpe =:= aply.tpe => // make sure that the type of the expression won't change
32- f // drop the map call
29+ tree.removeAttachment(desugar.TrailingForMap ) match
30+ case Some (_) =>
31+ tree match
32+ case aply @ Apply (MapCall (f), List (Lambda (List (param), body))) =>
33+ if f.tpe =:= aply.tpe then // make sure that the type of the expression won't change
34+ return f // drop the map call
35+ else
36+ f match
37+ case Converted (r) if r.tpe =:= aply.tpe =>
38+ return r // drop the map call and the conversion
39+ case _ =>
3340 case _ =>
34- tree.removeAttachment(desugar. TrailingForMap )
35- tree
41+ case _ =>
42+ tree
3643
3744 private object Lambda :
3845 def unapply (tree : Tree )(using Context ): Option [(List [ValDef ], Tree )] =
@@ -49,6 +56,15 @@ class DropForMap extends MiniPhase:
4956 case TypeApply (fn, _) => unapply(fn)
5057 case _ => None
5158
59+ private object Converted :
60+ def unapply (tree : Tree )(using Context ): Option [Tree ] = tree match
61+ case Apply (fn @ Apply (_, _), _) => unapply(fn)
62+ case Apply (fn, r :: Nil )
63+ if fn.symbol.is(Implicit ) || fn.symbol.name == nme.apply && fn.symbol.owner.derivesFrom(defn.ConversionClass )
64+ => Some (r)
65+ case TypeApply (fn, _) => unapply(fn)
66+ case _ => None
67+
5268object DropForMap :
5369 val name : String = " dropForMap"
5470 val description : String = " Drop unused trailing map calls in for comprehensions"
0 commit comments