Skip to content

Not accurate type inference in for-comprhension #15216

Closed
@WojciechMazur

Description

@WojciechMazur

Type inferred inside for-comprehension is a union Any | X where X is expected

Compiler version

3.1.2
Works with 3.1.1
Fails in 3.1.3-RC3
Fails in 3.2.0-RC1-bin-20220517-e5abec0-NIGHTLY

Minimized code

Based on https://github.com/vigoo/clipp/blob/8def1b439039fc8ddc74f55d8f58a337d11f919c/clipp-core/src/test/scala/io/github/vigoo/clipp/UsageInfoSpecs.scala#L94-L108

//> using scala "3.1.2"
// //> using scala "3.1.1" // Last working stable version

sealed abstract class Free[S[_], A] {
  final def map[B](f: A => B): Free[S, B] = ???
  final def flatMap[B](f: A => Free[S, B]): Free[S, B] = new Free[S, B] {}
}

trait Parameter[T]
def namedDouble(name: String): Free[Parameter, Double] = ???

type Double2 = (Double, Double)
type Double3 = (Double, Double, Double)
val spec: Free[Parameter, Either[Double3, Double2]] = for {
  result <-
    if (???) {
      for {
        x <- namedDouble("X")
        y <- namedDouble("Y")
        z <- namedDouble("Z")
      } yield Left((x, y, z))
    } else {
      for {
        x <- namedDouble("X")
        y <- namedDouble("Y")
      } yield Right((x, y))
    }
} yield result

Output

[error] ./test.scala:28:9: Found:    (result : Either[Any | (Double, Double, Double), (Double, Double) | Any])
[error] Required: Either[Double3, Double2]
[error] } yield result
[error]         ^^^^^^

Expectation

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions