@@ -2,8 +2,9 @@ package dotty.tools.dotc
2
2
package transform .localopt
3
3
4
4
import dotty .tools .dotc .ast .tpd .*
5
- import dotty .tools .dotc .core .Decorators .*
6
5
import dotty .tools .dotc .core .Contexts .*
6
+ import dotty .tools .dotc .core .Decorators .*
7
+ import dotty .tools .dotc .core .Flags .*
7
8
import dotty .tools .dotc .core .StdNames .*
8
9
import dotty .tools .dotc .core .Symbols .*
9
10
import dotty .tools .dotc .core .Types .*
@@ -25,14 +26,20 @@ class DropForMap extends MiniPhase:
25
26
override def description : String = DropForMap .description
26
27
27
28
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 _ =>
33
40
case _ =>
34
- tree.removeAttachment(desugar. TrailingForMap )
35
- tree
41
+ case _ =>
42
+ tree
36
43
37
44
private object Lambda :
38
45
def unapply (tree : Tree )(using Context ): Option [(List [ValDef ], Tree )] =
@@ -49,6 +56,15 @@ class DropForMap extends MiniPhase:
49
56
case TypeApply (fn, _) => unapply(fn)
50
57
case _ => None
51
58
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
+
52
68
object DropForMap :
53
69
val name : String = " dropForMap"
54
70
val description : String = " Drop unused trailing map calls in for comprehensions"
0 commit comments