Skip to content
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

java-defined static overloaded polymorphic method is hosed #7209

Closed
scabug opened this issue Mar 3, 2013 · 6 comments
Closed

java-defined static overloaded polymorphic method is hosed #7209

scabug opened this issue Mar 3, 2013 · 6 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Mar 3, 2013

Ignoring the fact that I have to give the type argument explicitly and annotate the type in great detail, it's safely a bug that it works once and then blows up from that point forward. (How do we even have bugs like that, what is hanging onto such state?)

package j;

import java.util.*;

public class J {
  public static <E> List<E> copyOf(Iterable<? extends E> elements) { return null; }
  public static <E> List<E> copyOf(Collection<? extends E> elements) { return null; }
  public static <E> List<E> copyOf(Iterator<? extends E> elements) { return null; }
  public static <E> List<E> copyOf(E[] elements) { return null; }
}
/***

scala> j.J.copyOf[String](new java.util.ArrayList[String]: java.util.Collection[_ <: String])
res0: java.util.List[String] = null

scala> j.J.copyOf[String](new java.util.ArrayList[String]: java.util.Collection[_ <: String])
<console>:34: error: type mismatch;
 found   : java.util.ArrayList[?0] where type ?0
 required: java.util.Collection[_ <: String]
              j.J.copyOf[String](new java.util.ArrayList[String]: java.util.Collection[_ <: String])
                                 ^
***/
@scabug
Copy link
Author

scabug commented Mar 3, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7209?orig=1
Reporter: @paulp
See #7482

@scabug
Copy link
Author

scabug commented Mar 3, 2013

Juha Heljoranta (p52mm3x) said:
Minimized:

class S {
  def s[T](x: Traversable[_ <: T]): Seq[T] = ???
  def s[T](x: Iterable[_ <: T]): Seq[T] = ???
  s[Int](Vector(1)) // ok
  s(Vector(1)) // fail
}
<console>:11: error: ambiguous reference to overloaded definition,
both method s in class S of type [T](x: Iterable[_ <: T])Seq[T]
and  method s in class S of type [T](x: Traversable[_ <: T])Seq[T]
match argument types (scala.collection.immutable.Vector[Int])
         s(Vector(1)) // fail
         ^

@scabug
Copy link
Author

scabug commented Jun 26, 2013

@retronym said (edited on Jun 26, 2013 9:29:14 AM UTC):
I suspect the state we were hanging onto was the root context, fixed in #7319.

UDPATE:

Wrong! This was #7482, which progressed in v2.11.0-M3~14.

@scabug
Copy link
Author

scabug commented Jun 26, 2013

@retronym said (edited on Jun 26, 2013 9:49:55 AM UTC):
In Juha's excellent and tiny test case:

# s[Int](Vector(1)) // okay
% qbin/scalac sandbox/test.scala
isAsSpecific((x: Iterable[_ <: Int])Seq[Int], (x: Traversable[_ <: Int])Seq[Int]): true
isAsSpecific((x: Traversable[_ <: Int])Seq[Int], (x: Iterable[_ <: Int])Seq[Int]): false
isAsSpecific((x: Iterable[_ <: Int])Seq[Int], (x: Traversable[_ <: Int])Seq[Int]): true
isAsSpecific((x: Traversable[_ <: Int])Seq[Int], (x: Iterable[_ <: Int])Seq[Int]): false

# s(Vector(1)) // fail
% qbin/scalac sandbox/test.scala
isAsSpecific([T](x: Iterable[_ <: T])Seq[T], [T](x: Traversable[_ <: T])Seq[T]): false
isAsSpecific([T](x: Traversable[_ <: T])Seq[T], [T](x: Iterable[_ <: T])Seq[T]): false
isAsSpecific([T](x: Traversable[_ <: T])Seq[T], [T](x: Iterable[_ <: T])Seq[T]): false
isAsSpecific([T](x: Iterable[_ <: T])Seq[T], [T](x: Traversable[_ <: T])Seq[T]): false
isAsSpecific([T](x: Iterable[_ <: T])Seq[T], [T](x: Traversable[_ <: T])Seq[T]): false
isAsSpecific([T](x: Traversable[_ <: T])Seq[T], [T](x: Iterable[_ <: T])Seq[T]): false
isAsSpecific([T](x: Iterable[_ <: T])Seq[T], [T](x: Traversable[_ <: T])Seq[T]): false
isAsSpecific([T](x: Traversable[_ <: T])Seq[T], [T](x: Iterable[_ <: T])Seq[T]): false
isAsSpecific([T](x: Traversable[_ <: T])Seq[T], [T](x: Iterable[_ <: T])Seq[T]): false
isAsSpecific([T](x: Iterable[_ <: T])Seq[T], [T](x: Traversable[_ <: T])Seq[T]): false
isAsSpecific([T](x: Iterable[_ <: T])Seq[T], [T](x: Traversable[_ <: T])Seq[T]): false
isAsSpecific([T](x: Traversable[_ <: T])Seq[T], [T](x: Iterable[_ <: T])Seq[T]): false
sandbox/test.scala:5: error: ambiguous reference to overloaded definition,
both method s in class S of type [T](x: Iterable[_ <: T])Seq[T]
and  method s in class S of type [T](x: Traversable[_ <: T])Seq[T]
match argument types (scala.collection.immutable.Vector[Int])

@scabug
Copy link
Author

scabug commented Jun 26, 2013

@retronym said:
I think this is all in line with the spec.

If an identifier or selection e references several members of a class, the context of the reference is used to identify a unique member. The way this is done depends on whether or not e is used as a function. Let A be the set of members referenced by e.
Assume first that e appears as a function in an application, as in e(e1, ..., em).

...
A polymorphic method of type [a1 >: L1 <: U1, ..., an >: Ln <: Un]T is as specific as some other member of type S if T is as specific as S under the assumption that for i = 1, . . . , n each ai is an abstract type name bounded frombelowbyLi and from above by Ui.
...

Assume next that e appears as a function in a type application, as in e[targs]. Then all alternatives in A which take the same number of type parameters as there are type arguments in targs are chosen. It is an error if no such alternative exists. If there are several such alternatives, overloading resolution is applied again to the whole expression e[targs].

Without explicit type arguments, the compiler chooses between [T](x: Traversable[_ <: T])Seq[T] and [U](x: Iterable[_ <: U])Seq[U]. I've changed the type parameter names to distinguish them. They are unrelated types, so neither function has parameters of types that are applicable to the other.

@scabug
Copy link
Author

scabug commented Jun 26, 2013

@retronym said:
Closing as a duplicate of #7482 for the REPL corruption.

I'm treating the overload resolution part as not-a-bug.

@scabug scabug closed this as completed Jun 26, 2013
@scabug scabug added this to the 2.11.0-M3 milestone Apr 7, 2017
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

2 participants