Skip to content

Preserve modifiers when desugaring for-comps #15403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}