Open
Description
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)
wherebaseType(p, C) = q.C[U_1,...,U_n]
as follows:
- If
T
is a reference to the ith class type parameter of some classD
:
- If
baseType(p, D) = r.D[W_1,...,W_m]
is defined, thenW_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.