Skip to content

Commit 855f572

Browse files
authored
Merge pull request #12184 from dotty-staging/fix-12170
Detect given bindings in for generators
2 parents 9f46b28 + 54e9eef commit 855f572

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,14 +1554,21 @@ object desugar {
15541554
}
15551555
}
15561556

1557+
/** Is `pat` of the form `x`, `x T`, or `given T`? when used as the lhs of a generator,
1558+
* these are all considered irrefutable.
1559+
*/
1560+
def isVarBinding(pat: Tree): Boolean = pat match
1561+
case pat @ Bind(_, pat1) if pat.mods.is(Given) => isVarBinding(pat1)
1562+
case IdPattern(_) => true
1563+
case _ => false
1564+
15571565
def needsNoFilter(gen: GenFrom): Boolean =
15581566
if (gen.checkMode == GenCheckMode.FilterAlways) // pattern was prefixed by `case`
15591567
false
1560-
else (
1561-
gen.checkMode != GenCheckMode.FilterNow ||
1562-
IdPattern.unapply(gen.pat).isDefined ||
1563-
isIrrefutable(gen.pat, gen.expr)
1564-
)
1568+
else
1569+
gen.checkMode != GenCheckMode.FilterNow
1570+
|| isVarBinding(gen.pat)
1571+
|| isIrrefutable(gen.pat, gen.expr)
15651572

15661573
/** rhs.name with a pattern filter on rhs unless `pat` is irrefutable when
15671574
* matched against `rhs`.

tests/run/i12170.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import scala.compiletime.error
2+
3+
object BadFilters:
4+
inline def withFilter(f: Int => Boolean): BadFilters.type = error("Unexpected withFilter call")
5+
def foreach(f: Int => Unit): Unit = f(42)
6+
7+
@main def Test =
8+
for
9+
x: Int <- BadFilters
10+
do println(x)
11+
for
12+
given Int <- BadFilters
13+
do println(summon[Int])

0 commit comments

Comments
 (0)