Skip to content

Commit 6a4c1fa

Browse files
authored
Merge pull request #15403 from albertpchen/fix-given-within-for-comprehension
Preserve modifiers when desugaring for-comps
2 parents 9614fb9 + cfc86c1 commit 6a4c1fa

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,10 +1577,10 @@ object desugar {
15771577
* also replaced by Binds with fresh names.
15781578
*/
15791579
def makeIdPat(pat: Tree): (Tree, Ident) = pat match {
1580-
case Bind(name, pat1) =>
1580+
case bind @ Bind(name, pat1) =>
15811581
if name == nme.WILDCARD then
15821582
val name = UniqueName.fresh()
1583-
(cpy.Bind(pat)(name, pat1), Ident(name))
1583+
(cpy.Bind(pat)(name, pat1).withMods(bind.mods), Ident(name))
15841584
else (pat, Ident(name))
15851585
case id: Ident if isVarPattern(id) && id.name != nme.WILDCARD => (id, id)
15861586
case Typed(id: Ident, _) if isVarPattern(id) && id.name != nme.WILDCARD => (pat, id)
@@ -1679,7 +1679,12 @@ object desugar {
16791679
val rhss = valeqs map { case GenAlias(_, rhs) => rhs }
16801680
val (defpat0, id0) = makeIdPat(gen.pat)
16811681
val (defpats, ids) = (pats map makeIdPat).unzip
1682-
val pdefs = valeqs.lazyZip(defpats).lazyZip(rhss).map(makePatDef(_, Modifiers(), _, _))
1682+
val pdefs = valeqs.lazyZip(defpats).lazyZip(rhss).map { (valeq, defpat, rhs) =>
1683+
val mods = defpat match
1684+
case defTree: DefTree => defTree.mods
1685+
case _ => Modifiers()
1686+
makePatDef(valeq, mods, defpat, rhs)
1687+
}
16831688
val rhs1 = makeFor(nme.map, nme.flatMap, GenFrom(defpat0, gen.expr, gen.checkMode) :: Nil, Block(pdefs, makeTuple(id0 :: ids)))
16841689
val allpats = gen.pat :: pats
16851690
val vfrom1 = GenFrom(makeTuple(allpats), rhs1, GenCheckMode.Ignore)

tests/run/fors.check

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,8 @@ hello world
4444
hello world
4545
hello/1~2 hello/3~4 /1~2 /3~4 world/1~2 world/3~4
4646
(2,1) (4,3)
47+
48+
testGivens
49+
123
50+
456
51+
0

tests/run/fors.scala

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ object Test extends App {
7474

7575
// arrays
7676
for (x <- ar) print(x + " "); println()
77-
7877
}
7978

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

111+
def testGivens(): Unit = {
112+
println("\ntestGivens")
113+
114+
// bound given that is summoned in subsequent bind
115+
for
116+
a <- List(123)
117+
given Int = a
118+
b = summon[Int]
119+
do
120+
println(b)
121+
122+
// generated given that is summoned in subsequent bind
123+
for
124+
given Int <- List(456)
125+
x = summon[Int]
126+
do
127+
println(x)
128+
129+
// pick the correct given
130+
for
131+
a <- List(789)
132+
given Int = a
133+
given Int <- List(0)
134+
x = summon[Int]
135+
do
136+
println(x)
137+
}
138+
112139
////////////////////////////////////////////////////
113140

114141
testOld()
115142
testNew()
116143
testFiltering()
144+
testGivens()
117145
}

0 commit comments

Comments
 (0)