Skip to content

type variable resolution contradicts language spec in a case of an inner class extending an outer class #21355

Open
@symingz

Description

@symingz

Compiler version

3.4.2

Minimized code

class D[T]:
  class C[S] extends D[S]:
    def foo(x: T): T = x

@main def main(): Unit =
  val da = new D[String]
  val ea = new da.C[Int]
  println(ea.foo(10))

Output

-- [E007] Type Mismatch Error: Main.scala:8:17 ---------------------------------
8 |  println(ea.foo(10))
  |                 ^^
  |                 Found:    (10 : Int)
  |                 Required: String
  |
  | longer explanation available when compiling with `-explain`
1 error found

Expectation

The compiler's behavior is in fact reasonable, but it contradicts with Scala 3's language spec. In particular, in the chapter about types, a meta-function asSeemFrom is defined as follows:

We define asSeenFrom(T, C, p) where baseType(p, C) = q.C[U_1,...,U_n] as follows:

  • If T is a reference to the ith class type parameter of some class D:
    • If baseType(p, D) = r.D[W_1,...,W_m] is defined, then W_i
    • ...
  • ...

Applying this to the code above, to type check ea.foo(10), we compute asSeenFrom(foo(x: T):T, C, ea), which requires asSeenFrom(T, C, ea). T is a reference to the 1st type parameter of D. baseType(ea, D) = .D[Int], hence T =:= Int, and so ea.foo(10) should pass type check.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions