Skip to content

Commit

Permalink
Merge pull request #15403 from albertpchen/fix-given-within-for-compr…
Browse files Browse the repository at this point in the history
…ehension

Preserve modifiers when desugaring for-comps
  • Loading branch information
odersky authored Jun 10, 2022
2 parents 9614fb9 + cfc86c1 commit 6a4c1fa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
11 changes: 8 additions & 3 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1577,10 +1577,10 @@ object desugar {
* also replaced by Binds with fresh names.
*/
def makeIdPat(pat: Tree): (Tree, Ident) = pat match {
case Bind(name, pat1) =>
case bind @ Bind(name, pat1) =>
if name == nme.WILDCARD then
val name = UniqueName.fresh()
(cpy.Bind(pat)(name, pat1), Ident(name))
(cpy.Bind(pat)(name, pat1).withMods(bind.mods), Ident(name))
else (pat, Ident(name))
case id: Ident if isVarPattern(id) && id.name != nme.WILDCARD => (id, id)
case Typed(id: Ident, _) if isVarPattern(id) && id.name != nme.WILDCARD => (pat, id)
Expand Down Expand Up @@ -1679,7 +1679,12 @@ object desugar {
val rhss = valeqs map { case GenAlias(_, rhs) => rhs }
val (defpat0, id0) = makeIdPat(gen.pat)
val (defpats, ids) = (pats map makeIdPat).unzip
val pdefs = valeqs.lazyZip(defpats).lazyZip(rhss).map(makePatDef(_, Modifiers(), _, _))
val pdefs = valeqs.lazyZip(defpats).lazyZip(rhss).map { (valeq, defpat, rhs) =>
val mods = defpat match
case defTree: DefTree => defTree.mods
case _ => Modifiers()
makePatDef(valeq, mods, defpat, rhs)
}
val rhs1 = makeFor(nme.map, nme.flatMap, GenFrom(defpat0, gen.expr, gen.checkMode) :: Nil, Block(pdefs, makeTuple(id0 :: ids)))
val allpats = gen.pat :: pats
val vfrom1 = GenFrom(makeTuple(allpats), rhs1, GenCheckMode.Ignore)
Expand Down
5 changes: 5 additions & 0 deletions tests/run/fors.check
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,8 @@ hello world
hello world
hello/1~2 hello/3~4 /1~2 /3~4 world/1~2 world/3~4
(2,1) (4,3)

testGivens
123
456
0
30 changes: 29 additions & 1 deletion tests/run/fors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ object Test extends App {

// arrays
for (x <- ar) print(x + " "); println()

}

/////////////////// filtering with case ///////////////////
Expand Down Expand Up @@ -109,9 +108,38 @@ object Test extends App {
for case (x, y) <- xs do print(s"${(y, x)} "); println()
}

def testGivens(): Unit = {
println("\ntestGivens")

// bound given that is summoned in subsequent bind
for
a <- List(123)
given Int = a
b = summon[Int]
do
println(b)

// generated given that is summoned in subsequent bind
for
given Int <- List(456)
x = summon[Int]
do
println(x)

// pick the correct given
for
a <- List(789)
given Int = a
given Int <- List(0)
x = summon[Int]
do
println(x)
}

////////////////////////////////////////////////////

testOld()
testNew()
testFiltering()
testGivens()
}

0 comments on commit 6a4c1fa

Please sign in to comment.