@@ -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
@@ -2181,49 +2182,53 @@ object desugar {
2181
2182
case (gen : GenFrom ) :: (rest @ (GenFrom (_, _, _) :: _)) =>
2182
2183
val cont = makeFor(mapName, flatMapName, rest, body)
2183
2184
Apply (rhsSelect(gen, flatMapName), makeLambda(gen, cont))
2184
- case (gen : GenFrom ) :: rest
2185
- if sourceVersion.enablesBetterFors
2186
- && rest.dropWhile(_.isInstanceOf [GenAlias ]).headOption.forall(e => e.isInstanceOf [GenFrom ]) // possible aliases followed by a generator or end of for
2187
- && ! rest.takeWhile(_.isInstanceOf [GenAlias ]).exists(a => isNestedGivenPattern(a.asInstanceOf [GenAlias ].pat)) =>
2188
- val cont = makeFor(mapName, flatMapName, rest, body)
2189
- val selectName =
2190
- if rest.exists(_.isInstanceOf [GenFrom ]) then flatMapName
2191
- else mapName
2192
- val aply = Apply (rhsSelect(gen, selectName), makeLambda(gen, cont))
2193
- markTrailingMap(aply, gen, selectName)
2194
- aply
2195
2185
case (gen : GenFrom ) :: (rest @ GenAlias (_, _) :: _) =>
2196
- val (valeqs, rest1) = rest.span(_.isInstanceOf [GenAlias ])
2197
- val pats = valeqs map { case GenAlias (pat, _) => pat }
2198
- val rhss = valeqs map { case GenAlias (_, rhs) => rhs }
2199
- val (defpat0, id0) = makeIdPat(gen.pat)
2200
- val (defpats, ids) = (pats map makeIdPat).unzip
2201
- val pdefs = valeqs.lazyZip(defpats).lazyZip(rhss).map { (valeq, defpat, rhs) =>
2202
- val mods = defpat match
2203
- case defTree : DefTree => defTree.mods
2204
- case _ => Modifiers ()
2205
- makePatDef(valeq, mods, defpat, rhs)
2206
- }
2207
- val rhs1 = makeFor(nme.map, nme.flatMap, GenFrom (defpat0, gen.expr, gen.checkMode) :: Nil , Block (pdefs, makeTuple(id0 :: ids).withAttachment(ForArtifact , ())))
2208
- val allpats = gen.pat :: pats
2209
- val vfrom1 = GenFrom (makeTuple(allpats), rhs1, GenCheckMode .Ignore )
2210
- makeFor(mapName, flatMapName, vfrom1 :: rest1, body)
2186
+ val (valeqs, suffix) = rest.span(_.isInstanceOf [GenAlias ])
2187
+ // possible aliases followed by a generator or end of for, when betterFors.
2188
+ // exclude value definitions with a given pattern (given T = x)
2189
+ val better = sourceVersion.enablesBetterFors
2190
+ && suffix.headOption.forall(_.isInstanceOf [GenFrom ])
2191
+ && ! valeqs.exists(a => isNestedGivenPattern(a.asInstanceOf [GenAlias ].pat))
2192
+ if better then
2193
+ val cont = makeFor(mapName, flatMapName, enums = rest, body)
2194
+ val selectName =
2195
+ if suffix.exists(_.isInstanceOf [GenFrom ]) then flatMapName
2196
+ else mapName
2197
+ Apply (rhsSelect(gen, selectName), makeLambda(gen, cont))
2198
+ .tap(markTrailingMap(_, gen, selectName))
2199
+ else
2200
+ val (pats, rhss) = valeqs.map { case GenAlias (pat, rhs) => (pat, rhs) }.unzip
2201
+ val (defpat0, id0) = makeIdPat(gen.pat)
2202
+ val (defpats, ids) = pats.map(makeIdPat).unzip
2203
+ val pdefs = valeqs.lazyZip(defpats).lazyZip(rhss).map: (valeq, defpat, rhs) =>
2204
+ val mods = defpat match
2205
+ case defTree : DefTree => defTree.mods
2206
+ case _ => Modifiers ()
2207
+ makePatDef(valeq, mods, defpat, rhs)
2208
+ val rhs1 =
2209
+ val enums = GenFrom (defpat0, gen.expr, gen.checkMode) :: Nil
2210
+ val body = Block (pdefs, makeTuple(id0 :: ids).withAttachment(ForArtifact , ()))
2211
+ makeFor(nme.map, nme.flatMap, enums, body)
2212
+ val allpats = gen.pat :: pats
2213
+ val vfrom1 = GenFrom (makeTuple(allpats), rhs1, GenCheckMode .Ignore )
2214
+ makeFor(mapName, flatMapName, enums = vfrom1 :: suffix, body)
2215
+ end if
2211
2216
case (gen : GenFrom ) :: test :: rest =>
2212
- val filtered = Apply (rhsSelect(gen, nme.withFilter), makeLambda(gen, test))
2213
- val genFrom = GenFrom (gen.pat, filtered, if sourceVersion.enablesBetterFors then GenCheckMode .Filtered else GenCheckMode .Ignore )
2217
+ val genFrom =
2218
+ val filtered = Apply (rhsSelect(gen, nme.withFilter), makeLambda(gen, test))
2219
+ val mode = if sourceVersion.enablesBetterFors then GenCheckMode .Filtered else GenCheckMode .Ignore
2220
+ GenFrom (gen.pat, filtered, mode)
2214
2221
makeFor(mapName, flatMapName, genFrom :: rest, body)
2215
- case GenAlias (_, _) :: _ if sourceVersion.enablesBetterFors =>
2216
- val (valeqs, rest) = enums.span(_.isInstanceOf [GenAlias ])
2217
- val pats = valeqs.map { case GenAlias (pat, _) => pat }
2218
- val rhss = valeqs.map { case GenAlias (_, rhs) => rhs }
2222
+ case enums @ GenAlias (_, _) :: _ if sourceVersion.enablesBetterFors =>
2223
+ val (valeqs, suffix) = enums.span(_.isInstanceOf [GenAlias ])
2224
+ val (pats, rhss) = valeqs.map { case GenAlias (pat, rhs) => (pat, rhs) }.unzip
2219
2225
val (defpats, ids) = pats.map(makeIdPat).unzip
2220
- val pdefs = valeqs.lazyZip(defpats).lazyZip(rhss).map { (valeq, defpat, rhs) =>
2226
+ val pdefs = valeqs.lazyZip(defpats).lazyZip(rhss).map: (valeq, defpat, rhs) =>
2221
2227
val mods = defpat match
2222
2228
case defTree : DefTree => defTree.mods
2223
2229
case _ => Modifiers ()
2224
2230
makePatDef(valeq, mods, defpat, rhs)
2225
- }
2226
- Block (pdefs, makeFor(mapName, flatMapName, rest, body))
2231
+ Block (pdefs, makeFor(mapName, flatMapName, enums = suffix, body))
2227
2232
case _ =>
2228
2233
EmptyTree // may happen for erroneous input
2229
2234
}
0 commit comments