Skip to content

Different behavior in for comprehensions with capture checking on branch cc-experiment #15005

Open
@simhayoz

Description

@simhayoz

Compiler version

3.1.3-RC1-bin-SNAPSHOT-nonbootstrapped-git-952a829 on the cc-experiment branch with -Ycc

Minimized code

object ForComprehensionBug {
  type OptFunc[-I, +O] = Option[I] => Option[O]

  def toOptional(i: Int, optFunc: OptFunc[Int, String]): Option[String] = optFunc(Some(i))

  for {
    a <- Some(1)
    optFunction: OptFunc[Int, String] = _.map(_.toString)
  } yield toOptional(a, optFunction)
}

Output

[error] -- [E007] Type Mismatch Error: .../src/main/scala/ForComprehensionBug.scala:8:4
[error] 8 |    optFunction: OptFunc[Int, String] = _.map(_.toString)
[error]   |    ^^^^^^^^^^^
[error]   |    Found:    (optFunction : ForComprehensionBug.OptFunc[Int, String])
[error]   |    Required: {optFunction} (x$0: Int) -> ? String

Expectation

The variable should not have itself as a capability, and the code should probably behave similarly to its desugared version. See below, this version compiles correctly:

object ForComprehensionBug {
  type OptFunc[-I, +O] = Option[I] => Option[O]

  def toOptional(i: Int, optFunc: OptFunc[Int, String]): Option[String] = optFunc(Some(i))

  Some(1).flatMap(a => {
    val optFunction: OptFunc[Int, String] = _.map(_.toString)
    toOptional(a, optFunction)
  })
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions