@@ -18,6 +18,7 @@ import reporting.*
1818import printing .Formatting .hl
1919import config .Printers
2020import parsing .Parsers
21+ import dotty .tools .dotc .util .chaining .*
2122
2223import scala .annotation .{unchecked as _ , * }, internal .sharable
2324
@@ -2249,49 +2250,53 @@ object desugar {
22492250 case (gen : GenFrom ) :: (rest @ (GenFrom (_, _, _) :: _)) =>
22502251 val cont = makeFor(mapName, flatMapName, rest, body)
22512252 Apply (rhsSelect(gen, flatMapName), makeLambda(gen, cont))
2252- case (gen : GenFrom ) :: rest
2253- if sourceVersion.enablesBetterFors
2254- && rest.dropWhile(_.isInstanceOf [GenAlias ]).headOption.forall(e => e.isInstanceOf [GenFrom ]) // possible aliases followed by a generator or end of for
2255- && ! rest.takeWhile(_.isInstanceOf [GenAlias ]).exists(a => isNestedGivenPattern(a.asInstanceOf [GenAlias ].pat)) =>
2256- val cont = makeFor(mapName, flatMapName, rest, body)
2257- val selectName =
2258- if rest.exists(_.isInstanceOf [GenFrom ]) then flatMapName
2259- else mapName
2260- val aply = Apply (rhsSelect(gen, selectName), makeLambda(gen, cont))
2261- markTrailingMap(aply, gen, selectName)
2262- aply
22632253 case (gen : GenFrom ) :: (rest @ GenAlias (_, _) :: _) =>
2264- val (valeqs, rest1) = rest.span(_.isInstanceOf [GenAlias ])
2265- val pats = valeqs map { case GenAlias (pat, _) => pat }
2266- val rhss = valeqs map { case GenAlias (_, rhs) => rhs }
2267- val (defpat0, id0) = makeIdPat(gen.pat)
2268- val (defpats, ids) = (pats map makeIdPat).unzip
2269- val pdefs = valeqs.lazyZip(defpats).lazyZip(rhss).map { (valeq, defpat, rhs) =>
2270- val mods = defpat match
2271- case defTree : DefTree => defTree.mods
2272- case _ => Modifiers ()
2273- makePatDef(valeq, mods, defpat, rhs)
2274- }
2275- val rhs1 = makeFor(nme.map, nme.flatMap, GenFrom (defpat0, gen.expr, gen.checkMode) :: Nil , Block (pdefs, makeTuple(id0 :: ids).withAttachment(ForArtifact , ())))
2276- val allpats = gen.pat :: pats
2277- val vfrom1 = GenFrom (makeTuple(allpats), rhs1, GenCheckMode .Ignore )
2278- makeFor(mapName, flatMapName, vfrom1 :: rest1, body)
2254+ val (valeqs, suffix) = rest.span(_.isInstanceOf [GenAlias ])
2255+ // possible aliases followed by a generator or end of for, when betterFors.
2256+ // exclude value definitions with a given pattern (given T = x)
2257+ val better = sourceVersion.enablesBetterFors
2258+ && suffix.headOption.forall(_.isInstanceOf [GenFrom ])
2259+ && ! valeqs.exists(a => isNestedGivenPattern(a.asInstanceOf [GenAlias ].pat))
2260+ if better then
2261+ val cont = makeFor(mapName, flatMapName, enums = rest, body)
2262+ val selectName =
2263+ if suffix.exists(_.isInstanceOf [GenFrom ]) then flatMapName
2264+ else mapName
2265+ Apply (rhsSelect(gen, selectName), makeLambda(gen, cont))
2266+ .tap(markTrailingMap(_, gen, selectName))
2267+ else
2268+ val (pats, rhss) = valeqs.map { case GenAlias (pat, rhs) => (pat, rhs) }.unzip
2269+ val (defpat0, id0) = makeIdPat(gen.pat)
2270+ val (defpats, ids) = pats.map(makeIdPat).unzip
2271+ val pdefs = valeqs.lazyZip(defpats).lazyZip(rhss).map: (valeq, defpat, rhs) =>
2272+ val mods = defpat match
2273+ case defTree : DefTree => defTree.mods
2274+ case _ => Modifiers ()
2275+ makePatDef(valeq, mods, defpat, rhs)
2276+ val rhs1 =
2277+ val enums = GenFrom (defpat0, gen.expr, gen.checkMode) :: Nil
2278+ val body = Block (pdefs, makeTuple(id0 :: ids).withAttachment(ForArtifact , ()))
2279+ makeFor(nme.map, nme.flatMap, enums, body)
2280+ val allpats = gen.pat :: pats
2281+ val vfrom1 = GenFrom (makeTuple(allpats), rhs1, GenCheckMode .Ignore )
2282+ makeFor(mapName, flatMapName, enums = vfrom1 :: suffix, body)
2283+ end if
22792284 case (gen : GenFrom ) :: test :: rest =>
2280- val filtered = Apply (rhsSelect(gen, nme.withFilter), makeLambda(gen, test))
2281- val genFrom = GenFrom (gen.pat, filtered, if sourceVersion.enablesBetterFors then GenCheckMode .Filtered else GenCheckMode .Ignore )
2285+ val genFrom =
2286+ val filtered = Apply (rhsSelect(gen, nme.withFilter), makeLambda(gen, test))
2287+ val mode = if sourceVersion.enablesBetterFors then GenCheckMode .Filtered else GenCheckMode .Ignore
2288+ GenFrom (gen.pat, filtered, mode)
22822289 makeFor(mapName, flatMapName, genFrom :: rest, body)
2283- case GenAlias (_, _) :: _ if sourceVersion.enablesBetterFors =>
2284- val (valeqs, rest) = enums.span(_.isInstanceOf [GenAlias ])
2285- val pats = valeqs.map { case GenAlias (pat, _) => pat }
2286- val rhss = valeqs.map { case GenAlias (_, rhs) => rhs }
2290+ case enums @ GenAlias (_, _) :: _ if sourceVersion.enablesBetterFors =>
2291+ val (valeqs, suffix) = enums.span(_.isInstanceOf [GenAlias ])
2292+ val (pats, rhss) = valeqs.map { case GenAlias (pat, rhs) => (pat, rhs) }.unzip
22872293 val (defpats, ids) = pats.map(makeIdPat).unzip
2288- val pdefs = valeqs.lazyZip(defpats).lazyZip(rhss).map { (valeq, defpat, rhs) =>
2294+ val pdefs = valeqs.lazyZip(defpats).lazyZip(rhss).map: (valeq, defpat, rhs) =>
22892295 val mods = defpat match
22902296 case defTree : DefTree => defTree.mods
22912297 case _ => Modifiers ()
22922298 makePatDef(valeq, mods, defpat, rhs)
2293- }
2294- Block (pdefs, makeFor(mapName, flatMapName, rest, body))
2299+ Block (pdefs, makeFor(mapName, flatMapName, enums = suffix, body))
22952300 case _ =>
22962301 EmptyTree // may happen for erroneous input
22972302 }
0 commit comments