@@ -18,6 +18,7 @@ import reporting.*
18
18
import printing .Formatting .hl
19
19
import config .Printers
20
20
import parsing .Parsers
21
+ import dotty .tools .dotc .util .chaining .*
21
22
22
23
import scala .annotation .{unchecked as _ , * }, internal .sharable
23
24
@@ -2234,49 +2235,53 @@ object desugar {
2234
2235
case (gen : GenFrom ) :: (rest @ (GenFrom (_, _, _) :: _)) =>
2235
2236
val cont = makeFor(mapName, flatMapName, rest, body)
2236
2237
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
2248
2238
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
2264
2269
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)
2267
2274
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
2272
2278
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) =>
2274
2280
val mods = defpat match
2275
2281
case defTree : DefTree => defTree.mods
2276
2282
case _ => Modifiers ()
2277
2283
makePatDef(valeq, mods, defpat, rhs)
2278
- }
2279
- Block (pdefs, makeFor(mapName, flatMapName, rest, body))
2284
+ Block (pdefs, makeFor(mapName, flatMapName, enums = suffix, body))
2280
2285
case _ =>
2281
2286
EmptyTree // may happen for erroneous input
2282
2287
}
0 commit comments