-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Don't generate fields for BoxedUnit val and var #2400
Don't generate fields for BoxedUnit val and var #2400
Conversation
This is intended as a way to not generate fields for phantom fields, which by this time are erased to BoxedUnit fields.
Nice idea :-) |
What happens if you extend a class where |
@smarter, superclass will have abstract getter and no actual field, so it should work fine. |
Getters for Null fields return trivially `null`. Getter for Nothing will `throw null`. This is only reachable if the field is accesed in the costructor before the field is initialized. In this case a `NullPointerException` is thrown as before.
38c486c
to
ca8477b
Compare
If that happens before the backend, it will be virtually impossible for the Scala.js to correctly implement its interop with JS. In a Scala.js-defined JS class, such a field must exist and be visible from JavaScript with the value |
You should also exclude |
@adriaanm I would assume that it is only necessary for |
@sjrd I will try to put it in it's own miniphase just before backend. |
@nicolassstucki Don't we want phantom fields to also be optimized out in other backends? |
If you have the following Scala.js class: @ScalaJSDefined
class Foo extends js.Object {
val x: Unit = ()
} Then the following must be true to respect the Scala.js interop spec: val foo: Foo = new Foo
foo.hasOwnProperty("x") And also from JS, if you have a foo.hasOwnProperty("x") |
It's not about the value they return, it's about memory model and reordering of operations in JVM. I'd propose to include a check that the field is indeed not a |
@@ -161,6 +161,12 @@ class FirstTransform extends MiniPhaseTransform with InfoTransformer with Annota | |||
} else ddef | |||
} | |||
|
|||
override def transformValDef(vdef: tpd.ValDef)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = { | |||
if (vdef.symbol.hasAnnotation(defn.VolatileAnnot) && vdef.tpt.tpe.isPhantom) | |||
ctx.error("Phantom fields can not be @volitile", vdef.pos) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: volitile -> volatile
tests/run/phantom-var-2.scala
Outdated
class Foo { | ||
import Boo._ | ||
|
||
var foo = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it makes sense to allow phantom var
. var
implies runtime semantics that just make no sense for phantoms.
7f0c087
to
bd7d21d
Compare
bd7d21d
to
c30b2db
Compare
This is intended as a way to not generate fields
for phantom fields, which by this time are erased
to BoxedUnit fields.