Skip to content

Type override in traits/classes with self-type #12161

Open
@scalauser12

Description

@scalauser12

reproduction steps

using Scala 2.13.3

  trait A {
    self: AnyRef =>
    type T = Nothing
    def test: T = throw new NotImplementedError
  }

  trait B extends A {
    self: AnyRef =>
    override type T = String
    override def test: T = "test"
  }

  class C extends B {
    self: AnyRef =>
  }

  val c: A = new C

problem

This code compiles despite type safety having been compromised. (It does not compile without self-types.) At runtime c.test throws java.lang.ClassCastException: java.lang.String cannot be cast to scala.runtime.Nothing$.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions