Skip to content

Commit c121bd8

Browse files
committed
Drop inlined map
1 parent 14974ed commit c121bd8

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

compiler/src/dotty/tools/dotc/inlines/Inlines.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ object Inlines:
571571

572572
// Take care that only argument bindings go into `bindings`, since positions are
573573
// different for bindings from arguments and bindings from body.
574-
val inlined = tpd.Inlined(call, bindings, expansion)
574+
val inlined = tpd.Inlined(call, bindings, expansion.withAttachmentsFrom(call))
575575

576576
if !hasOpaqueProxies then inlined
577577
else

compiler/src/dotty/tools/dotc/transform/localopt/DropForMap.scala

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import dotty.tools.dotc.ast.desugar
2020
* The latter condition is checked in [[Desugar.scala#makeFor]]
2121
*/
2222
class DropForMap extends MiniPhase:
23-
import DropForMap.*
2423

2524
override def phaseName: String = DropForMap.name
2625

@@ -39,6 +38,22 @@ class DropForMap extends MiniPhase:
3938
case _ => tree
4039
case tree => tree
4140

41+
override def transformInlined(tree: Inlined)(using Context): Tree = tree match
42+
case Inlined(call, bindings, expansion) if expansion.hasAttachment(desugar.TrailingForMap) =>
43+
call match
44+
case Unmapped(f) =>
45+
bindings.collectFirst:
46+
case vd: ValDef if f.sameTree(vd.rhs) =>
47+
expansion.find:
48+
case Inlined(Thicket(Nil), Nil, Ident(ident)) => ident == vd.name
49+
case _ => false
50+
.match
51+
case Some(ref) => cpy.Inlined(tree)(call, bindings, ref)
52+
case _ => tree
53+
.getOrElse(tree)
54+
case _ => tree
55+
case tree => tree
56+
4257
// Extracts a fun from a possibly nested Apply with lambda and arbitrary implicit args.
4358
private object Unmapped:
4459
private def loop(tree: Tree)(using Context): Option[Tree] =
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
MySome(())
2+
MySome(2)
3+
MySome((2,3))
4+
MySome((2,(3,4)))
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//> using options -preview
2+
3+
class myOptionModule(doOnMap: => Unit):
4+
sealed trait MyOption[+A]:
5+
inline def map[B](f: A => B): MyOption[B] =
6+
this match
7+
case MySome(x) =>
8+
doOnMap
9+
MySome(f(x))
10+
case MyNone => MyNone
11+
def flatMap[B](f: A => MyOption[B]): MyOption[B] =
12+
this match
13+
case MySome(x) => f(x)
14+
case MyNone => MyNone
15+
case class MySome[A](x: A) extends MyOption[A]
16+
case object MyNone extends MyOption[Nothing]
17+
object MyOption:
18+
def apply[A](x: A): MyOption[A] = MySome(x)
19+
20+
@main def Test =
21+
22+
val myOption = myOptionModule(???)
23+
24+
import myOption.*
25+
26+
def portablePrintMyOption(opt: MyOption[Any]): Unit = println:
27+
opt match
28+
case MySome(()) => "MySome(())"
29+
case opt => opt
30+
31+
val z = for {
32+
a <- MyOption(1)
33+
b <- MyOption(())
34+
} yield ()
35+
36+
portablePrintMyOption(z)
37+
38+
val z2 = for {
39+
a <- MyOption(1)
40+
b <- MyOption(2)
41+
} yield b
42+
43+
portablePrintMyOption(z2)
44+
45+
val z3 = for {
46+
a <- MyOption(1)
47+
(b, c) <- MyOption((2, 3))
48+
} yield (b, c)
49+
50+
portablePrintMyOption(z3)
51+
52+
val z4 = for {
53+
a <- MyOption(1)
54+
(b, (c, d)) <- MyOption((2, (3, 4)))
55+
} yield (b, (c, d))
56+
57+
portablePrintMyOption(z4)
58+
end Test

0 commit comments

Comments
 (0)