@@ -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
@@ -2251,49 +2252,53 @@ object desugar {
22512252 case (gen : GenFrom ) :: (rest @ (GenFrom (_, _, _) :: _)) =>
22522253 val cont = makeFor(mapName, flatMapName, rest, body)
22532254 Apply (rhsSelect(gen, flatMapName), makeLambda(gen, cont))
2254- case (gen : GenFrom ) :: rest
2255- if sourceVersion.enablesBetterFors
2256- && rest.dropWhile(_.isInstanceOf [GenAlias ]).headOption.forall(e => e.isInstanceOf [GenFrom ]) // possible aliases followed by a generator or end of for
2257- && ! rest.takeWhile(_.isInstanceOf [GenAlias ]).exists(a => isNestedGivenPattern(a.asInstanceOf [GenAlias ].pat)) =>
2258- val cont = makeFor(mapName, flatMapName, rest, body)
2259- val selectName =
2260- if rest.exists(_.isInstanceOf [GenFrom ]) then flatMapName
2261- else mapName
2262- val aply = Apply (rhsSelect(gen, selectName), makeLambda(gen, cont))
2263- markTrailingMap(aply, gen, selectName)
2264- aply
22652255 case (gen : GenFrom ) :: (rest @ GenAlias (_, _) :: _) =>
2266- val (valeqs, rest1) = rest.span(_.isInstanceOf [GenAlias ])
2267- val pats = valeqs map { case GenAlias (pat, _) => pat }
2268- val rhss = valeqs map { case GenAlias (_, rhs) => rhs }
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- }
2277- val rhs1 = makeFor(nme.map, nme.flatMap, GenFrom (defpat0, gen.expr, gen.checkMode) :: Nil , Block (pdefs, makeTuple(id0 :: ids).withAttachment(ForArtifact , ())))
2278- val allpats = gen.pat :: pats
2279- val vfrom1 = GenFrom (makeTuple(allpats), rhs1, GenCheckMode .Ignore )
2280- makeFor(mapName, flatMapName, vfrom1 :: rest1, body)
2256+ val (valeqs, suffix) = rest.span(_.isInstanceOf [GenAlias ])
2257+ // possible aliases followed by a generator or end of for, when betterFors.
2258+ // exclude value definitions with a given pattern (given T = x)
2259+ val better = sourceVersion.enablesBetterFors
2260+ && suffix.headOption.forall(_.isInstanceOf [GenFrom ])
2261+ && ! valeqs.exists(a => isNestedGivenPattern(a.asInstanceOf [GenAlias ].pat))
2262+ if better then
2263+ val cont = makeFor(mapName, flatMapName, enums = rest, body)
2264+ val selectName =
2265+ if suffix.exists(_.isInstanceOf [GenFrom ]) then flatMapName
2266+ else mapName
2267+ Apply (rhsSelect(gen, selectName), makeLambda(gen, cont))
2268+ .tap(markTrailingMap(_, gen, selectName))
2269+ else
2270+ val (pats, rhss) = valeqs.map { case GenAlias (pat, rhs) => (pat, rhs) }.unzip
2271+ val (defpat0, id0) = makeIdPat(gen.pat)
2272+ val (defpats, ids) = pats.map(makeIdPat).unzip
2273+ val pdefs = valeqs.lazyZip(defpats).lazyZip(rhss).map: (valeq, defpat, rhs) =>
2274+ val mods = defpat match
2275+ case defTree : DefTree => defTree.mods
2276+ case _ => Modifiers ()
2277+ makePatDef(valeq, mods, defpat, rhs)
2278+ val rhs1 =
2279+ val enums = GenFrom (defpat0, gen.expr, gen.checkMode) :: Nil
2280+ val body = Block (pdefs, makeTuple(id0 :: ids).withAttachment(ForArtifact , ()))
2281+ makeFor(nme.map, nme.flatMap, enums, body)
2282+ val allpats = gen.pat :: pats
2283+ val vfrom1 = GenFrom (makeTuple(allpats), rhs1, GenCheckMode .Ignore )
2284+ makeFor(mapName, flatMapName, enums = vfrom1 :: suffix, body)
2285+ end if
22812286 case (gen : GenFrom ) :: test :: rest =>
2282- val filtered = Apply (rhsSelect(gen, nme.withFilter), makeLambda(gen, test))
2283- val genFrom = GenFrom (gen.pat, filtered, if sourceVersion.enablesBetterFors then GenCheckMode .Filtered else GenCheckMode .Ignore )
2287+ val genFrom =
2288+ val filtered = Apply (rhsSelect(gen, nme.withFilter), makeLambda(gen, test))
2289+ val mode = if sourceVersion.enablesBetterFors then GenCheckMode .Filtered else GenCheckMode .Ignore
2290+ GenFrom (gen.pat, filtered, mode)
22842291 makeFor(mapName, flatMapName, genFrom :: rest, body)
2285- case GenAlias (_, _) :: _ if sourceVersion.enablesBetterFors =>
2286- val (valeqs, rest) = enums.span(_.isInstanceOf [GenAlias ])
2287- val pats = valeqs.map { case GenAlias (pat, _) => pat }
2288- val rhss = valeqs.map { case GenAlias (_, rhs) => rhs }
2292+ case enums @ GenAlias (_, _) :: _ if sourceVersion.enablesBetterFors =>
2293+ val (valeqs, suffix) = enums.span(_.isInstanceOf [GenAlias ])
2294+ val (pats, rhss) = valeqs.map { case GenAlias (pat, rhs) => (pat, rhs) }.unzip
22892295 val (defpats, ids) = pats.map(makeIdPat).unzip
2290- val pdefs = valeqs.lazyZip(defpats).lazyZip(rhss).map { (valeq, defpat, rhs) =>
2296+ val pdefs = valeqs.lazyZip(defpats).lazyZip(rhss).map: (valeq, defpat, rhs) =>
22912297 val mods = defpat match
22922298 case defTree : DefTree => defTree.mods
22932299 case _ => Modifiers ()
22942300 makePatDef(valeq, mods, defpat, rhs)
2295- }
2296- Block (pdefs, makeFor(mapName, flatMapName, rest, body))
2301+ Block (pdefs, makeFor(mapName, flatMapName, enums = suffix, body))
22972302 case _ =>
22982303 EmptyTree // may happen for erroneous input
22992304 }
0 commit comments