Skip to content

Detect given bindings in for generators #12184

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 2 commits into from
Apr 25, 2021
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
17 changes: 12 additions & 5 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1554,14 +1554,21 @@ object desugar {
}
}

/** Is `pat` of the form `x`, `x T`, or `given T`? when used as the lhs of a generator,
* these are all considered irrefutable.
*/
def isVarBinding(pat: Tree): Boolean = pat match
case pat @ Bind(_, pat1) if pat.mods.is(Given) => isVarBinding(pat1)
case IdPattern(_) => true
case _ => false

def needsNoFilter(gen: GenFrom): Boolean =
if (gen.checkMode == GenCheckMode.FilterAlways) // pattern was prefixed by `case`
false
else (
gen.checkMode != GenCheckMode.FilterNow ||
IdPattern.unapply(gen.pat).isDefined ||
isIrrefutable(gen.pat, gen.expr)
)
else
gen.checkMode != GenCheckMode.FilterNow
|| isVarBinding(gen.pat)
|| isIrrefutable(gen.pat, gen.expr)

/** rhs.name with a pattern filter on rhs unless `pat` is irrefutable when
* matched against `rhs`.
Expand Down
13 changes: 13 additions & 0 deletions tests/run/i12170.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import scala.compiletime.error

object BadFilters:
inline def withFilter(f: Int => Boolean): BadFilters.type = error("Unexpected withFilter call")
def foreach(f: Int => Unit): Unit = f(42)

@main def Test =
for
x: Int <- BadFilters
do println(x)
for
given Int <- BadFilters
do println(summon[Int])