Skip to content

Commit

Permalink
Cleanup for experimental SIP-62 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
KacperFKorban committed Jul 22, 2024
1 parent 9c3e454 commit 6ef7d8e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
42 changes: 41 additions & 1 deletion compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1804,7 +1804,7 @@ object desugar {
/** Create tree for for-comprehension `<for (enums) do body>` or
* `<for (enums) yield body>` where mapName and flatMapName are chosen
* corresponding to whether this is a for-do or a for-yield.
* The creation performs the following rewrite rules:
* If betterFors are enabled, the creation performs the following rewrite rules:
*
* 1.
*
Expand Down Expand Up @@ -1872,6 +1872,46 @@ object desugar {
* (Where empty for-comprehensions are excluded by the parser)
*
* If the aliases are not followed by a guard, otherwise an error.
*
* With betterFors disabled, the translation is as follows:
*
* 1.
*
* for (P <- G) E ==> G.foreach (P => E)
*
* Here and in the following (P => E) is interpreted as the function (P => E)
* if P is a variable pattern and as the partial function { case P => E } otherwise.
*
* 2.
*
* for (P <- G) yield E ==> G.map (P => E)
*
* 3.
*
* for (P_1 <- G_1; P_2 <- G_2; ...) ...
* ==>
* G_1.flatMap (P_1 => for (P_2 <- G_2; ...) ...)
*
* 4.
*
* for (P <- G; E; ...) ...
* =>
* for (P <- G.filter (P => E); ...) ...
*
* 5. For any N:
*
* for (P_1 <- G; P_2 = E_2; val P_N = E_N; ...)
* ==>
* for (TupleN(P_1, P_2, ... P_N) <-
* for (x_1 @ P_1 <- G) yield {
* val x_2 @ P_2 = E_2
* ...
* val x_N & P_N = E_N
* TupleN(x_1, ..., x_N)
* } ...)
*
* If any of the P_i are variable patterns, the corresponding `x_i @ P_i` is not generated
* and the variable constituting P_i is used instead of x_i
*
* @param mapName The name to be used for maps (either map or foreach)
* @param flatMapName The name to be used for flatMaps (either flatMap or foreach)
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/config/Feature.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ object Feature:
(into, "Allow into modifier on parameter types"),
(namedTuples, "Allow named tuples"),
(modularity, "Enable experimental modularity features"),
(betterMatchTypeExtractors, "Enable better match type extractors")
(betterMatchTypeExtractors, "Enable better match type extractors"),
(betterFors, "Enable improvements in `for` comprehensions")
)

// legacy language features from Scala 2 that are no longer supported.
Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/runtime/stdLibPatches/language.scala
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ object language:

/** Experimental support for improvements in `for` comprehensions
*
* @see [[https://dotty.epfl.ch/docs/reference/experimental/better-fors]]
* @see [[https://github.com/scala/improvement-proposals/pull/79]]
*/
@compileTimeOnly("`betterFors` can only be used at compile time in import statements")
object betterFors
Expand Down
3 changes: 2 additions & 1 deletion project/MiMaFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ object MiMaFilters {
val ForwardsBreakingChanges: Map[String, Seq[ProblemFilter]] = Map(
// Additions that require a new minor version of the library
Build.mimaPreviousDottyVersion -> Seq(

ProblemFilters.exclude[MissingFieldProblem]("scala.runtime.stdLibPatches.language#experimental.betterFors"),
ProblemFilters.exclude[MissingClassProblem]("scala.runtime.stdLibPatches.language$experimental$betterFors$"),
),

// Additions since last LTS
Expand Down
1 change: 1 addition & 0 deletions tests/run/fors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import annotation.tailrec

@scala.annotation.experimental
object Test extends App {
val xs = List(1, 2, 3)
val ys = List(Symbol("a"), Symbol("b"), Symbol("c"))
Expand Down

0 comments on commit 6ef7d8e

Please sign in to comment.