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

Option(null.asInstanceOf[Boolean]) returns None instead of Some(false) #7397

Closed
scabug opened this issue Apr 20, 2013 · 8 comments
Closed

Option(null.asInstanceOf[Boolean]) returns None instead of Some(false) #7397

scabug opened this issue Apr 20, 2013 · 8 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Apr 20, 2013

Consider this:

scala> Option(null.asInstanceOf[Boolean])

The correct result should be:

res0: Option[Boolean] = Some(false)

Instead, the result is:

res0: Option[Boolean] = None

Mailing list thread: https://groups.google.com/d/topic/scala-language/2QH1WXQ2hM8/discussion

Pull request: scala/scala#2422

@scabug
Copy link
Author

scabug commented Apr 20, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7397?orig=1
Reporter: @soc
Affected Versions: 2.8.1, 2.9.3, 2.10.1, 2.11.0-M2
Attachments:

  • 7397.diff (created on Aug 15, 2013 11:02:55 PM UTC, 1980 bytes)

@scabug
Copy link
Author

scabug commented Apr 21, 2013

@paulp said:

Nightmarishly, it turns out specialization depends on the current behavior. Say
you have a specialized class like this:

class Klass[@specialized(Long) A]( val a: A )

The Long-specialized subclass has to call the generic Klass constructor with
some kind of argument. It currently passes null, which is passed through
untouched where it lies unseen because the field is inaccessible, as all means
of accessing it have been overridden to point to the specialized field in
Klass$mcJ$sp.

After I teach erasure that null.asInstanceOf[Long] means it's a Long, the lesson
is taken too well: no longer is the null passed through untouched, because it's
a Long. And since the eventual destination is a generic field, it is boxed into
a java.lang.Long before being placed there.

Spelled in diff form:

 diff --git a/Klass$mcJ$sp.class b/Klass$mcJ$sp.class
 index 6af6c5282c..2d7f69c300 100644
 --- a/Klass$mcJ$sp.class
 +++ b/Klass$mcJ$sp.class
 @@ -34,7 +34,7 @@ public class Klass$mcJ$sp extends Klass<java.lang.Object> {
      Code:
          : lload_1
          : putfield      #                  // Field a$mcJ$sp:J
 -        : aconst_null
 -        : invokespecial #                  // Method Klass."<init>":(Ljava/lang/Object;)V
 +        : invokestatic  #                  // Method scala/runtime/BoxesRunTime.boxToLong:(J)Ljava/lang/Long;
 +       : invokespecial #                  // Method Klass."<init>":(Ljava/lang/Object;)V
         : return
  }

@scabug
Copy link
Author

scabug commented Apr 21, 2013

@paulp said:
Throwing back to Unassigned because I've learned my lesson about pressing on when I find out something like that.

@scabug
Copy link
Author

scabug commented Apr 21, 2013

@soc said:
From my POV, correctness should win over performance, but I agree that this gets very ugly.

@scabug
Copy link
Author

scabug commented May 20, 2013

@JamesIry said:
2.10.2 is about to be cut. Kicking down the road and un-assigning to foster work stealing.

@scabug
Copy link
Author

scabug commented Aug 15, 2013

@paulp said:
I attached my patch in case it will save anyone else time.

@scabug
Copy link
Author

scabug commented May 18, 2016

@lrytz said:
scala/scala#5176

@scabug scabug closed this as completed May 19, 2016
@scabug
Copy link
Author

scabug commented May 20, 2016

@soc said:
Wow, you are amazing! Thanks a lot! This has bugging me for a long time already!

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

No branches or pull requests

2 participants