Skip to content

Regression: Explicit Nulls & Flexible Types #24693

@marcinzh

Description

@marcinzh

Compiler version

3.7.4 - warning
3.7.2 - ok
3.3.7 - ok

Minimized code

//> using scala "3.7.4"
//> using option "-Yexplicit-nulls"
import scala.annotation.nowarn
import java.time.Instant

sealed trait Eff[+A]:
  def run: A = this match
    case x: Eff.Impure[A] => x()

object Eff:
  sealed abstract class Impure[+A]() extends Eff[A] with Function0[A]

  @nowarn("msg=anonymous class definition")
  inline def impure[A](inline a: => A): Eff[A] =
    new Impure[A]:
      override def apply(): A = a

  def instant: Eff[Instant] = impure(Instant.now().nn)

@main def main =
  Eff.impure:
    println("hell world")
  .run

Output

[warn] method apply exposes a flexible type in its inferred result type (java.time.Instant)?. Consider annotating the type explicitly
[warn]   def instant: Eff[Instant] = impure(Instant.now().nn)
[warn] 

Expectation

Compiles without warning.


We can make the warning go away, by applying one of the following modifications:

  1. Remove inlines
  2. Remove covariance from Eff[+A] and Impure[+A]
  3. Add explicit type parameter to the last impure call:
    def instant: Eff[Instant] = impure[Instant](Instant.now().nn)

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