-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Erase fields for Null/Nothing val and var
Getters for Null fields return trivially `null`. Getter for Nothing will `throw null`. This is only be reachable if the field is accesed in the costructor before the field is initialized. In this case a `NullPointerException` is thrown as before.
- Loading branch information
1 parent
16756e4
commit 38c486c
Showing
15 changed files
with
193 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
0 | ||
1 | ||
foo | ||
??? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
try { | ||
println("0") | ||
val f = new Foo | ||
println("1") | ||
println(f.foo) | ||
println("2") | ||
println(f.foo) | ||
} catch { | ||
case e: NotImplementedError => println("???") | ||
} | ||
|
||
// TODO: Erase | ||
// Currently not erasing fields for lazy vals | ||
assert(classOf[Foo].getDeclaredFields.exists(_.getName.startsWith("foo")), "Field foo erased. Optimized accidentally?") | ||
} | ||
|
||
|
||
} | ||
|
||
class Foo { | ||
lazy val foo: Nothing = { | ||
println("foo") | ||
??? | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
0 | ||
foo | ||
??? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
try { | ||
println("0") | ||
val f = new Foo | ||
println("1") | ||
println(f.foo) | ||
} catch { | ||
case e: NotImplementedError => println("???") | ||
} | ||
|
||
assert(!classOf[Foo].getDeclaredFields.exists(_.getName.startsWith("foo")), "field foo not erased") | ||
} | ||
|
||
|
||
} | ||
|
||
class Foo { | ||
val foo: Nothing = { | ||
println("foo") | ||
??? | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
0 | ||
foo | ||
??? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
try { | ||
println("0") | ||
val f = new Foo | ||
println("1") | ||
f.foo | ||
f.foo | ||
f.foo = { | ||
println("foo3") | ||
??? | ||
} | ||
println(f.foo) | ||
} catch { | ||
case e: NotImplementedError => println("???") | ||
} | ||
|
||
assert(!classOf[Foo].getDeclaredFields.exists(_.getName.startsWith("foo")), "field foo not erased") | ||
} | ||
|
||
|
||
} | ||
|
||
class Foo { | ||
var foo: Nothing = { | ||
println("foo") | ||
??? | ||
} | ||
|
||
foo = { | ||
println("foo2") | ||
??? | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
1 | ||
foo | ||
null | ||
2 | ||
null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
object Test { | ||
|
||
def main(args: Array[String]): Unit = { | ||
val f = new Foo | ||
println(1) | ||
println(f.foo) | ||
println(2) | ||
println(f.foo) | ||
|
||
// TODO: Erase | ||
// Currently not erasing fields for lazy vals | ||
assert(f.getClass.getDeclaredFields.exists(_.getName.startsWith("foo")), "Field foo erased. Optimized accidentally?") | ||
} | ||
|
||
|
||
} | ||
|
||
class Foo { | ||
lazy val foo: Null = { | ||
println("foo") | ||
null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
foo | ||
null | ||
null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
val f = new Foo | ||
println(f.foo) | ||
println(f.foo) | ||
assert(!f.getClass.getDeclaredFields.exists(_.getName.startsWith("foo")), "field foo not erased") | ||
} | ||
} | ||
|
||
class Foo { | ||
val foo: Null = { | ||
println("foo") | ||
null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
foo | ||
foo2 | ||
null | ||
foo3 | ||
null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
val f = new Foo | ||
println(f.foo) | ||
f.foo | ||
f.foo = { | ||
println("foo3") | ||
null | ||
} | ||
println(f.foo) | ||
assert(!f.getClass.getDeclaredFields.exists(_.getName.startsWith("foo")), "field foo not erased") | ||
} | ||
} | ||
|
||
class Foo { | ||
var foo: Null = { | ||
println("foo") | ||
null | ||
} | ||
|
||
foo = { | ||
println("foo2") | ||
null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters