@@ -64,6 +64,11 @@ object desugar {
6464 */
6565 val PolyFunctionApply : Property .Key [Unit ] = Property .StickyKey ()
6666
67+ /** An attachment key to indicate that an Apply is created as a last `map`
68+ * scall in a for-comprehension.
69+ */
70+ val TrailingForMap : Property .Key [Unit ] = Property .StickyKey ()
71+
6772 /** What static check should be applied to a Match? */
6873 enum MatchCheck {
6974 case None , Exhaustive , IrrefutablePatDef , IrrefutableGenFrom
@@ -418,6 +423,7 @@ object desugar {
418423 .withMods(Modifiers (
419424 meth.mods.flags & (AccessFlags | Synthetic ) | (vparam.mods.flags & Inline ),
420425 meth.mods.privateWithin))
426+ .withSpan(vparam.rhs.span)
421427 val rest = defaultGetters(vparams :: paramss1, n + 1 )
422428 if vparam.rhs.isEmpty then rest else defaultGetter :: rest
423429 case _ :: paramss1 => // skip empty parameter lists and type parameters
@@ -1966,14 +1972,8 @@ object desugar {
19661972 *
19671973 * 3.
19681974 *
1969- * for (P <- G) yield P ==> G
1970- *
1971- * If betterFors is enabled, P is a variable or a tuple of variables and G is not a withFilter.
1972- *
19731975 * for (P <- G) yield E ==> G.map (P => E)
19741976 *
1975- * Otherwise
1976- *
19771977 * 4.
19781978 *
19791979 * for (P_1 <- G_1; P_2 <- G_2; ...) ...
@@ -2146,14 +2146,20 @@ object desugar {
21462146 case (Tuple (ts1), Tuple (ts2)) => ts1.corresponds(ts2)(deepEquals)
21472147 case _ => false
21482148
2149+ def markTrailingMap (aply : Apply , gen : GenFrom , selectName : TermName ): Unit =
2150+ if betterForsEnabled
2151+ && selectName == mapName
2152+ && gen.checkMode != GenCheckMode .Filtered // results of withFilter have the wrong type
2153+ && (deepEquals(gen.pat, body) || deepEquals(body, Tuple (Nil )))
2154+ then
2155+ aply.putAttachment(TrailingForMap , ())
2156+
21492157 enums match {
21502158 case Nil if betterForsEnabled => body
21512159 case (gen : GenFrom ) :: Nil =>
2152- if betterForsEnabled
2153- && gen.checkMode != GenCheckMode .Filtered // results of withFilter have the wrong type
2154- && deepEquals(gen.pat, body)
2155- then gen.expr // avoid a redundant map with identity
2156- else Apply (rhsSelect(gen, mapName), makeLambda(gen, body))
2160+ val aply = Apply (rhsSelect(gen, mapName), makeLambda(gen, body))
2161+ markTrailingMap(aply, gen, mapName)
2162+ aply
21572163 case (gen : GenFrom ) :: (rest @ (GenFrom (_, _, _) :: _)) =>
21582164 val cont = makeFor(mapName, flatMapName, rest, body)
21592165 Apply (rhsSelect(gen, flatMapName), makeLambda(gen, cont))
@@ -2164,7 +2170,9 @@ object desugar {
21642170 val selectName =
21652171 if rest.exists(_.isInstanceOf [GenFrom ]) then flatMapName
21662172 else mapName
2167- Apply (rhsSelect(gen, selectName), makeLambda(gen, cont))
2173+ val aply = Apply (rhsSelect(gen, selectName), makeLambda(gen, cont))
2174+ markTrailingMap(aply, gen, selectName)
2175+ aply
21682176 case (gen : GenFrom ) :: (rest @ GenAlias (_, _) :: _) =>
21692177 val (valeqs, rest1) = rest.span(_.isInstanceOf [GenAlias ])
21702178 val pats = valeqs map { case GenAlias (pat, _) => pat }
0 commit comments