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

AnyRef specialization broken when traits, abstract classes and classes are involved #6043

Open
scabug opened this issue Jul 7, 2012 · 5 comments

Comments

@scabug
Copy link

scabug commented Jul 7, 2012

The summary (title) of this issue is fairly involved but I couldn't find better one. I think it's the best to present code at this point:

trait Foo[@specialized(AnyRef, Int) A, @specialized(Boolean) B] {
  def apply(x: A): Boolean
}
abstract class Inter extends Foo[String, Boolean]
class Baz extends Inter {
  def apply(x: String) = false
}

We are interested in apply method of Baz class. Let's see the javap output:

Compiled from "check-both.scala"
public class Baz extends Inter{
    public boolean apply(java.lang.String);
    public boolean apply(java.lang.Object);
    public Baz();
}

we see here that specialized overrides are missing so default specialized implementations will be used (from Inter class) that forward to Foo$class and thus box.

This problem is caused by the fact that AnyRef specialization is a bit more tricky than specialization on primitive types. When we specialize on AnyRef and then we check if given type argument for specialized type parameter is valid we have to use subtyping relationship instead of equality. The relevant piece of code is here:
https://github.com/scala/scala/blob/39f01d4f48e59c2037a3af759eb6d55d0da50e70/src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala#L162

@scabug
Copy link
Author

scabug commented Jul 7, 2012

@scabug
Copy link
Author

scabug commented Jul 7, 2012

@paulp said:
"This problem is caused by the fact that AnyRef specialization is a bit more tricky than specialization on primitive types. When we specialize on AnyRef and then we check if given type argument for specialized type parameter is valid we have to use subtyping relationship instead of equality."

Are you sure of this assessment? This looks more to me like a case of comparing types in the wrong environment. In Baz, A=String, and def apply(x: String): Boolean exists. When looking for specialized overrides involving A, (String)Boolean is exactly the method signature it ought to be looking for. The fact that it doesn't override it says to me it's looking for the wrong signature, not that it needs to apply a different test.

Parameter types of method overrides must match exactly, which also makes me skeptical that a subtype test is what's needed.

@scabug
Copy link
Author

scabug commented Jul 11, 2012

@adriaanm said:
Please re-assign to Vlad if that's more appropriate.

@scabug
Copy link
Author

scabug commented Jul 11, 2012

@axel22 said:
This is my pending fix for that one:

axel22/scala-github@830ba25

but I still have to test it, and right now I have my hands full with @static.

@scabug
Copy link
Author

scabug commented Aug 14, 2012

@non said (edited on Aug 14, 2012 4:37:49 AM UTC):
One comment on this bug: it's well-known that specialization doesn't work with class-inheritance (it's described in the paper and also in some of the docs). Thus, I don't think this example would ever work.

If you change Inter into a trait, you'll see specialization occuring:

public class Baz extends java.lang.Object implements Inter{
    public boolean apply$mcI$sp(int);
    public boolean apply(java.lang.String);
    public boolean apply(java.lang.Object);
    public Baz();
}

That said, AnyRef specialization is currently turned off, so the only specialization available is apply$mcI$sp but I think the point still stands.

I only checked this bug because I'm trying to go through and close old specialization bugs that might now be fixed in master.

@scabug scabug added this to the Backlog milestone Apr 7, 2017
@scala scala deleted a comment from scabug Jun 15, 2023
@scala scala deleted a comment from scabug Jun 15, 2023
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

1 participant