Skip to content

Commit c696e7e

Browse files
committed
Minor refactor of better for desugar
1 parent 5429b1f commit c696e7e

File tree

1 file changed

+40
-35
lines changed

1 file changed

+40
-35
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import reporting.*
1818
import printing.Formatting.hl
1919
import config.Printers
2020
import parsing.Parsers
21+
import dotty.tools.dotc.util.chaining.*
2122

2223
import scala.annotation.{unchecked as _, *}, internal.sharable
2324

@@ -2234,49 +2235,53 @@ object desugar {
22342235
case (gen: GenFrom) :: (rest @ (GenFrom(_, _, _) :: _)) =>
22352236
val cont = makeFor(mapName, flatMapName, rest, body)
22362237
Apply(rhsSelect(gen, flatMapName), makeLambda(gen, cont))
2237-
case (gen: GenFrom) :: rest
2238-
if sourceVersion.enablesBetterFors
2239-
&& rest.dropWhile(_.isInstanceOf[GenAlias]).headOption.forall(e => e.isInstanceOf[GenFrom]) // possible aliases followed by a generator or end of for
2240-
&& !rest.takeWhile(_.isInstanceOf[GenAlias]).exists(a => isNestedGivenPattern(a.asInstanceOf[GenAlias].pat)) =>
2241-
val cont = makeFor(mapName, flatMapName, rest, body)
2242-
val selectName =
2243-
if rest.exists(_.isInstanceOf[GenFrom]) then flatMapName
2244-
else mapName
2245-
val aply = Apply(rhsSelect(gen, selectName), makeLambda(gen, cont))
2246-
markTrailingMap(aply, gen, selectName)
2247-
aply
22482238
case (gen: GenFrom) :: (rest @ GenAlias(_, _) :: _) =>
2249-
val (valeqs, rest1) = rest.span(_.isInstanceOf[GenAlias])
2250-
val pats = valeqs map { case GenAlias(pat, _) => pat }
2251-
val rhss = valeqs map { case GenAlias(_, rhs) => rhs }
2252-
val (defpat0, id0) = makeIdPat(gen.pat)
2253-
val (defpats, ids) = (pats map makeIdPat).unzip
2254-
val pdefs = valeqs.lazyZip(defpats).lazyZip(rhss).map { (valeq, defpat, rhs) =>
2255-
val mods = defpat match
2256-
case defTree: DefTree => defTree.mods
2257-
case _ => Modifiers()
2258-
makePatDef(valeq, mods, defpat, rhs)
2259-
}
2260-
val rhs1 = makeFor(nme.map, nme.flatMap, GenFrom(defpat0, gen.expr, gen.checkMode) :: Nil, Block(pdefs, makeTuple(id0 :: ids).withAttachment(ForArtifact, ())))
2261-
val allpats = gen.pat :: pats
2262-
val vfrom1 = GenFrom(makeTuple(allpats), rhs1, GenCheckMode.Ignore)
2263-
makeFor(mapName, flatMapName, vfrom1 :: rest1, body)
2239+
val (valeqs, suffix) = rest.span(_.isInstanceOf[GenAlias])
2240+
// possible aliases followed by a generator or end of for, when betterFors.
2241+
// exclude value definitions with a given pattern (given T = x)
2242+
val better = sourceVersion.enablesBetterFors
2243+
&& suffix.headOption.forall(_.isInstanceOf[GenFrom])
2244+
&& !valeqs.exists(a => isNestedGivenPattern(a.asInstanceOf[GenAlias].pat))
2245+
if better then
2246+
val cont = makeFor(mapName, flatMapName, enums = rest, body)
2247+
val selectName =
2248+
if suffix.exists(_.isInstanceOf[GenFrom]) then flatMapName
2249+
else mapName
2250+
Apply(rhsSelect(gen, selectName), makeLambda(gen, cont))
2251+
.tap(markTrailingMap(_, gen, selectName))
2252+
else
2253+
val (pats, rhss) = valeqs.map { case GenAlias(pat, rhs) => (pat, rhs) }.unzip
2254+
val (defpat0, id0) = makeIdPat(gen.pat)
2255+
val (defpats, ids) = pats.map(makeIdPat).unzip
2256+
val pdefs = valeqs.lazyZip(defpats).lazyZip(rhss).map: (valeq, defpat, rhs) =>
2257+
val mods = defpat match
2258+
case defTree: DefTree => defTree.mods
2259+
case _ => Modifiers()
2260+
makePatDef(valeq, mods, defpat, rhs)
2261+
val rhs1 =
2262+
val enums = GenFrom(defpat0, gen.expr, gen.checkMode) :: Nil
2263+
val body = Block(pdefs, makeTuple(id0 :: ids).withAttachment(ForArtifact, ()))
2264+
makeFor(nme.map, nme.flatMap, enums, body)
2265+
val allpats = gen.pat :: pats
2266+
val vfrom1 = GenFrom(makeTuple(allpats), rhs1, GenCheckMode.Ignore)
2267+
makeFor(mapName, flatMapName, enums = vfrom1 :: suffix, body)
2268+
end if
22642269
case (gen: GenFrom) :: test :: rest =>
2265-
val filtered = Apply(rhsSelect(gen, nme.withFilter), makeLambda(gen, test))
2266-
val genFrom = GenFrom(gen.pat, filtered, if sourceVersion.enablesBetterFors then GenCheckMode.Filtered else GenCheckMode.Ignore)
2270+
val genFrom =
2271+
val filtered = Apply(rhsSelect(gen, nme.withFilter), makeLambda(gen, test))
2272+
val mode = if sourceVersion.enablesBetterFors then GenCheckMode.Filtered else GenCheckMode.Ignore
2273+
GenFrom(gen.pat, filtered, mode)
22672274
makeFor(mapName, flatMapName, genFrom :: rest, body)
2268-
case GenAlias(_, _) :: _ if sourceVersion.enablesBetterFors =>
2269-
val (valeqs, rest) = enums.span(_.isInstanceOf[GenAlias])
2270-
val pats = valeqs.map { case GenAlias(pat, _) => pat }
2271-
val rhss = valeqs.map { case GenAlias(_, rhs) => rhs }
2275+
case enums @ GenAlias(_, _) :: _ if sourceVersion.enablesBetterFors =>
2276+
val (valeqs, suffix) = enums.span(_.isInstanceOf[GenAlias])
2277+
val (pats, rhss) = valeqs.map { case GenAlias(pat, rhs) => (pat, rhs) }.unzip
22722278
val (defpats, ids) = pats.map(makeIdPat).unzip
2273-
val pdefs = valeqs.lazyZip(defpats).lazyZip(rhss).map { (valeq, defpat, rhs) =>
2279+
val pdefs = valeqs.lazyZip(defpats).lazyZip(rhss).map: (valeq, defpat, rhs) =>
22742280
val mods = defpat match
22752281
case defTree: DefTree => defTree.mods
22762282
case _ => Modifiers()
22772283
makePatDef(valeq, mods, defpat, rhs)
2278-
}
2279-
Block(pdefs, makeFor(mapName, flatMapName, rest, body))
2284+
Block(pdefs, makeFor(mapName, flatMapName, enums = suffix, body))
22802285
case _ =>
22812286
EmptyTree //may happen for erroneous input
22822287
}

0 commit comments

Comments
 (0)