Skip to content

Allowing to override an abstract type member with equality may be unsound in some cases #11130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mario-bucev opened this issue Jan 15, 2021 · 1 comment · Fixed by #11133
Closed
Milestone

Comments

@mario-bucev
Copy link
Contributor

mario-bucev commented Jan 15, 2021

Minimized code

@main def test: Unit = {
  class Foo
  class Bar extends Foo

  trait S[-A] {
    type T >: A
  }
  trait PFoo extends S[Foo]
  trait PBar extends S[Bar] {
    override type T = Bar
  }
  class PFooBar extends PBar with PFoo {
    override type T >: Bar
  }

  def patmat[A](s: S[A]): s.T = s match {
    case p: (PFoo & s.type) => (new Foo): p.T
  }
  
  // ClassCastException: Foo cannot be cast to class Bar
  val x: Bar = patmat(new PFooBar: PBar)
}

Output

Click to expand
java.lang.ClassCastException: main$package$Foo$1 cannot be cast to main$package$Bar$1
	at main$package$.test(main.scala:21)
	at test.main(main.scala:1)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at sbt.Run.invokeMain(Run.scala:115)
	at sbt.Run.execute$1(Run.scala:79)
	at sbt.Run.$anonfun$runWithLoader$4(Run.scala:92)
	at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
	at sbt.util.InterfaceUtil$$anon$1.get(InterfaceUtil.scala:10)
	at sbt.TrapExit$App.run(TrapExit.scala:257)
	at java.lang.Thread.run(Thread.java:748)

Expectation

The code should be rejected, but to me, it is not clear where the problem lies. I think the issue is allowing PBar to assign Bar to T. I also find it rather strange that PFooBar#T is allowed to be a supertype of Bar, it should be rejected and we should instead have PFooBar#T >: Bar | Foo (= Foo)

@Jasper-M
Copy link
Contributor

Jasper-M commented Jan 15, 2021

Scala 2 complains about PFooBar:

error: incompatible type in overriding
type T >: Foo (defined in trait S)
  with override type T = Bar (defined in trait PBar);
 found   : Bar
 required:  >: Foo

odersky added a commit to dotty-staging/dotty that referenced this issue Jan 15, 2021
In a situation like
```
class A { type T = B }
class B extends A { override type T }
```

We only checked that `T` in `A` correctly overrides `T` in `B`, since `T` in `A`
is taken to be the overriding member, since it is abstract. But we also have to check
the reverse.

This replaces a more specialized treatment where we checked that final members
in `Object` could not be overridden by abstract members in traits. I removed a bunch
of tests that all tested variations of that other check, and changed the check
file for one representative.

Fixes scala#11130
@Kordyjan Kordyjan added this to the 3.0.0 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants