Skip to content

Singleton inference failure when used in same file #11982

@valencik

Description

@valencik

Compiler version

3.0.0-RC2

Minimized code

The following compiles fine on it's own:

package tuplefun
object Unpair {

  def pair[A, B](using a: ValueOf[A], b: ValueOf[B]): Tuple2[A, B] =
    (a.value, b.value)

  def unpair[X <: Tuple2[?, ?]](
      using a: ValueOf[Tuple.Head[X]],
            b: ValueOf[Tuple.Head[Tuple.Tail[X]]]
  ): Tuple2[Tuple.Head[X], Tuple.Head[Tuple.Tail[X]]] =
    type AA = Tuple.Head[X]
    type BB = Tuple.Head[Tuple.Tail[X]]
    pair[AA, BB](using a, b)
}

However, attempts to use unpair within the same file fail to compile, see the following scastie for an example:
https://scastie.scala-lang.org/wa8mgjM9TPOY2trROxRWXg

Everything compiles and works if we move UnpairApp, shown in above scastie link and below, to a new file.

object UnpairApp {
  import Unpair._

  type Tshape = ("msg", 42)

  // the following won't compile when in the same file as Unpair
  val p1: ("msg", 42) = unpair[Tshape] // error: No singleton value available for Any

  @main def pairHello: Unit =
    assert(p1 == ("msg", 42))
    println(p1)
}

Additionally if one loads the console in sbt and imports unpair things work:

scala> import tuplefun.Unpair.unpair

scala> type T = ("msg", 42)
// defined alias type T = ("msg", 42)

scala> unpair[T]                   
val res0: ("msg", 42) = (msg,42)

Expectation

UnpairApp and/or usage of unpair should compile whether defined in the same file or a separate file from Unpair

What are you doing?

If curious, the motivation that lead me here was trying to call a function of the form def func[A, B] when what I have is a type alias type T = Tuple2[A, B] and I do not want to spell out the types again.

Metadata

Metadata

Labels

area:match-typesitype:bugitype:soundnessSoundness bug (it lets us compile code that crashes at runtime with a ClassCastException)

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions