Skip to content

withFilter required for irrefutable for comprehension pattern #15579

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

Closed
Swoorup opened this issue Jul 4, 2022 · 9 comments · Fixed by #15593
Closed

withFilter required for irrefutable for comprehension pattern #15579

Swoorup opened this issue Jul 4, 2022 · 9 comments · Fixed by #15593
Assignees
Labels
area:pattern-matching itype:bug regression This worked in a previous version but doesn't anymore
Milestone

Comments

@Swoorup
Copy link

Swoorup commented Jul 4, 2022

Tuple decomposition seems to have regressed in some cases. The following works fine under 3.1.3

Compiler version

3.2.0-RC1

Minimized code

//> using scala "3.2.0-RC1"
//> using option "-source:future"
//> using option "-Ykind-projector"
//> using lib "org.typelevel::cats-effect::3.3.13"

import cats.Monad
import cats.effect.Temporal
import cats.syntax.all.*

trait ProjectionStore[F[_], State, Output]:
  def loadRecent: F[(Int, Option[State])]

def test[F[_]: Temporal](store: ProjectionStore[F, Int, Int]) = 
  for (x, y) <- store.loadRecent
  yield println(s"x: $x, y: $y")

Output

[error] ./t.scala:14:17: value withFilter is not a member of F[(Int, Option[Int])]
[error]
[error] where:    F is a type in method test with bounds <: [_] =>> Any
[error]   for (x, y) <- store.loadRecent
[error]                 ^^^^^^^^^^^^^^^^
Error compiling project (Scala 3.2.0-RC1, JVM)
Compilation failed

Expectation

Compilation passes

@Swoorup Swoorup added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jul 4, 2022
@Swoorup Swoorup changed the title Tuple decomposition (some) in for-comprehension broken in 3.2.0-RC1 Tuple decomposition (some) in for-comprehension regressed in 3.2.0-RC1 Jul 4, 2022
@armanbilge
Copy link
Contributor

armanbilge commented Jul 4, 2022

Minification:

//> using scala "3.nightly"
//> using option "-source:future"

trait Foo[A]:
  def map[B](f: A => B): Foo[B] = ???

def baz: Foo[(Int, String)] = ???

@main def main =
  for (x, y) <- baz
  yield ()
Compiling project (Scala 3.2.1-RC1-bin-20220703-cde3d13-NIGHTLY, JVM)
[error] ./test.scala:10:17: value withFilter is not a member of Foo[(Int, String)]
[error]   for (x, y) <- baz
[error]                 ^^^
Error compiling project (Scala 3.2.1-RC1-bin-20220703-cde3d13-NIGHTLY, JVM)
Compilation failed

@bishabosha bishabosha added area:pattern-matching and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Jul 4, 2022
@bishabosha
Copy link
Member

Minification:

//> using scala "3.nightly"
//> using option "-source:future"

trait Foo[A]:
  def map[B](f: A => B): Foo[B] = ???

def baz: Foo[(Int, String)] = ???

@main def main =
  for (x, y) <- baz
  yield ()
Compiling project (Scala 3.2.1-RC1-bin-20220703-cde3d13-NIGHTLY, JVM)
[error] ./test.scala:10:17: value withFilter is not a member of Foo[(Int, String)]
[error]   for (x, y) <- baz
[error]                 ^^^
Error compiling project (Scala 3.2.1-RC1-bin-20220703-cde3d13-NIGHTLY, JVM)
Compilation failed

This code has failed the same way since 3.0.0

@armanbilge
Copy link
Contributor

armanbilge commented Jul 4, 2022

Are you sure?
This one compiles for me.

//> using scala "3.1.3"
//> using option "-source:future"

trait Foo[A]:
  def map[B](f: A => B): Foo[B] = ???

def baz: Foo[(Int, String)] = ???

@main def main =
  for (x, y) <- baz
  yield ()
$ scala-cli compile test.scala 
Compiling project (Scala 3.1.3, JVM)
Compiled project (Scala 3.1.3, JVM)

Apologies if I did something dumb here :)

Edit: even works in 3.0.0.

$ scala-cli compile test.scala 
Downloading Scala 3.0.0 compiler
Compiling project (Scala 3.0.0, JVM)
Compiled project (Scala 3.0.0, JVM)

@bishabosha
Copy link
Member

bishabosha commented Jul 4, 2022

when I tried in the repl:

~/workspace/scripts » scala
Welcome to Scala 3.1.3 (17.0.2, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
                                                                                                                             
scala> trait Foo[A]:
     |   def map[B](f: A => B): Foo[B] = ???
     | 
     | def baz: Foo[(Int, String)] = ???
     | 
     | @main def main =
     |   for (x, y) <- baz
     |   yield ()
     | 
-- [E008] Not Found Error: -----------------------------------------------------
7 |  for (x, y) <- baz
  |                ^^^
  |                value withFilter is not a member of Foo[(Int, String)]
1 error found
                                                                                                                             
scala> 

Edit: ok sorry, I did not see you used the -source:future option:

@bishabosha
Copy link
Member

so it seems that (x, y) is not considered irrefutable anymore?

@bishabosha
Copy link
Member

bishabosha commented Jul 4, 2022

same issue with a non-tuple pattern:

//> using scala "3.2.0-RC1"
trait Foo[A]:
  def map[B](f: A => B): Foo[B] = ???

final case class Bar[A, B](a: A, b: B)

def baz: Foo[Bar[Int, String]] = ???

@main def main =
  for Bar(x, y) <- baz
  yield ()
-- [E008] Not Found Error: -----------------------------------------------------
9 |  for Bar(x, y) <- baz
  |                   ^^^
  |                 value withFilter is not a member of Foo[Bar[Int, String]]

@prolativ
Copy link
Contributor

prolativ commented Jul 4, 2022

Should we classify this issue as a regression then? On the one hand this stopped working, on the other the semantics of -source:future is theoretically different when one changes the minor version but still this should not break in this case.

@prolativ prolativ changed the title Tuple decomposition (some) in for-comprehension regressed in 3.2.0-RC1 withFilter required for irrefutable for comprehension pattern Jul 4, 2022
@bishabosha
Copy link
Member

I think the issue is that these patterns should not be considered refutable

@griggt griggt added the regression This worked in a previous version but doesn't anymore label Jul 5, 2022
griggt added a commit to griggt/dotty that referenced this issue Jul 5, 2022
@griggt
Copy link
Contributor

griggt commented Jul 5, 2022

😳

PR: #15593

Thanks for testing the RC and catching this!

Kordyjan pushed a commit to dotty-staging/dotty that referenced this issue Jul 6, 2022
bishabosha pushed a commit to dotty-staging/dotty that referenced this issue Oct 18, 2022
@Kordyjan Kordyjan added this to the 3.2.1 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:pattern-matching itype:bug regression This worked in a previous version but doesn't anymore
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants