Skip to content

Method in trait not a legal implementation in inheriting class (V2) #4839

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
allanrenucci opened this issue Jul 25, 2018 · 5 comments
Closed

Comments

@allanrenucci
Copy link
Contributor

allanrenucci commented Jul 25, 2018

package collection

trait Map[K, +V] extends MapOps[K, V, Map]

trait MapOps[K, +V, +CC[_, _]] {
  def collect[K2, V2](pf: PartialFunction[(K, V), (K2, V2)]): CC[K2, V2] = ???
}

trait SortedMapOps[K, +V, +CC[X, Y] <: Map[X, Y] with SortedMapOps[X, Y, CC]]
  extends MapOps[K, V, Map]

trait StrictOptimizedMapOps[K, +V, +CC[_, _]] extends MapOps[K, V, CC] {

  override def collect[K2, V2](pf: PartialFunction[(K, V), (K2, V2)]): CC[K2, V2] = ???
}

trait StrictOptimizedSortedMapOps[K, +V, +CC[X, Y] <: Map[X, Y] with SortedMapOps[X, Y, CC]]
  extends SortedMapOps[K, V, CC] with StrictOptimizedMapOps[K, V, Map]


package immutable {
  trait Map[K, +V] extends collection.Map[K, V] with MapOps[K, V, Map]

  trait MapOps[K, +V, +CC[X, +Y] <: MapOps[X, Y, CC]] extends collection.MapOps[K, V, CC]

  trait SortedMapOps[K, +V, +CC[X, +Y] <: Map[X, Y] with SortedMapOps[X, Y, CC]]
    extends MapOps[K, V, Map] with collection.SortedMapOps[K, V, CC]

  class TreeMap[K, +V] extends Map[K, V] with SortedMapOps[K, V, TreeMap]
    with StrictOptimizedSortedMapOps[K, V, TreeMap]
}
-- Error: tests/allan/Test.scala:86:8 ------------------------------------------
86 |  class TreeMap[K, +V] extends Map[K, V] with SortedMapOps[K, V, TreeMap]
   |        ^
   |method collect in trait StrictOptimizedMapOps is not a legal implementation of `collect` in class TreeMap
   |  its type             [K2, V2](pf: PartialFunction[(K, V), (K2, V2)]): collection.Map[K2, V2]
   |  does not conform to  [K2, V2]
   |  (pf: PartialFunction[(K, V), (K2, V2)]): collection.immutable.Map[K2, V2]
one error found
@allanrenucci
Copy link
Contributor Author

I am not sure it is a bug in Dotty. This can be fixed with:

trait StrictOptimizedSortedMapOps[K, +V, +CC[X, Y] <: Map[X, Y] with SortedMapOps[X, Y, CC]]
-  extends SortedMapOps[K, V, CC] with StrictOptimizedMapOps[K, V, Map]
+  extends SortedMapOps[K, V, CC] with StrictOptimizedMapOps[K, V, CC]

cc/ @julienrf

@odersky
Copy link
Contributor

odersky commented Jul 25, 2018

I have the impression this is a bug in the library, not in dotty. @allanrenucci can you do a PR against the library?

@Blaisorblade
Copy link
Contributor

Blaisorblade commented Jul 25, 2018

It'd be also a bug in Scalac, no?
EDIT: I confirmed that TreeMap.collect forwards to StrictOptimizedSortedMapOps.collect, which is not sound, because StrictOptimizedSortedMapOps.collect gives a collection.Map instead of a collection.immutable.Map. But Scalac doesn't notice because the forwarder is generated after erasure, when the return types are Object everywhere.

allanrenucci added a commit to dotty-staging/scala that referenced this issue Jul 26, 2018
smarter added a commit to dotty-staging/scala that referenced this issue Jul 26, 2018
Introduce an extra type parameter "UnsortedCC" to pass to
StrictOptimizedMapOps.

See scala/scala3#4839.
@smarter
Copy link
Member

smarter commented Jul 26, 2018

Opened scala/bug#11042 to track a related soundness bug in scalac.

allanrenucci added a commit to dotty-staging/scala that referenced this issue Jul 26, 2018
Inherit `StrictOptimizedMapOps` to refine return type of map, flatmap,
concat and collect.

See scala/scala3#4839
@Blaisorblade
Copy link
Contributor

Closing as the bug was not in Dotty and the library was fixed — @allanrenucci correct me if I'm wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants