scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
230 stars 21 forks source link

Failing to infer type of based on type member refinement of type parameter #12797

Open eejbyfeldt opened 1 year ago

eejbyfeldt commented 1 year ago

Reproduction steps

Scala version: 2.13 and 2.12 but works in scala 3

trait Base {
  type M
}

final class Impl extends Base {
  type M = Int
}

object Test {
  def toRep[T, S <: Base { type M = T }](s: S): T = ???

  toRep(new Impl)
}

Problem

Trying to compile the code above it fails with

refinement_inference.scala:12: error: inferred type arguments [Nothing,Impl] do not conform to method toRep's type parameter bounds [T,S <: Base{type M = T}]
  toRep(new Impl)
  ^
refinement_inference.scala:12: error: type mismatch;
 found   : Impl
 required: S
  toRep(new Impl)
        ^
two errors found

so the compiler failed to infer the type of T correctly.

More info

Changing the def of toRep slightly so

def toRep[T, S <: Base](s: S { type M = T }): T = ???

makes it infer the correct type and the code compiles.