Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Emit missing
POP
in backend when accessing static fields
Since 2.13 we emit fields in objects as static. The backend thus has some support for static fields. ASTs that access a static field are usually limited to object fields and only occur from within the module itself (`GETSTATIC` in the getter method, `PUTSTATIC` in `clinit`). In this case the qualifier of the field access AST is always a pure expression and therefore nothing is emitted (`genLoadQualUnlessElidable`). Compiler plugins can however generate ASTs where static fields are accessed with an impure qualifier. This triggers a bug in the backend where `genLoadQualUnlessElidable` loads the qualifier reference onto the stack without consuming it. This leads to either a crash in the backend or at runtime. The test case is a macro that imitates such a scenario. Before the fix it crashes at runtime ("Bad type on operand stack").
- Loading branch information
Showing
4 changed files
with
51 additions
and
3 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,3 @@ | ||
42 | ||
hai | ||
42 |
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,21 @@ | ||
import scala.language.experimental.macros | ||
import scala.reflect.macros.blackbox.Context | ||
|
||
object A { | ||
def foo(x: Int): Int = macro foo_impl | ||
|
||
def foo_impl(c: Context)(x: c.Expr[Int]): c.Tree = { | ||
val g = c.universe.asInstanceOf[scala.tools.nsc.Global] | ||
import g._ | ||
import scala.tools.nsc.symtab.Flags._ | ||
|
||
val t = x.tree.asInstanceOf[Tree] match { | ||
case s @ Select(_, n) if n.toString == "f2" => | ||
val field = s.symbol.accessed | ||
field.setFlag(STATIC).resetFlag(PRIVATE | LOCAL) | ||
s.setSymbol(field) | ||
} | ||
|
||
t.asInstanceOf[c.Tree] | ||
} | ||
} |
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 @@ | ||
import scala.tools.partest.BytecodeTest | ||
import scala.tools.testkit.ASMConverters._ | ||
import scala.tools.asm.Opcodes._ | ||
|
||
object Test extends BytecodeTest { | ||
val f2 = 42 | ||
|
||
def getT: Test.type = { | ||
println("hai") | ||
Test | ||
} | ||
|
||
def show(): Unit = { | ||
println(A.foo(Test.f2)) | ||
println(A.foo(getT.f2)) | ||
|
||
val ins = instructionsFromMethod(getMethod(loadClassNode("Test$"), "show")) | ||
val gs = ins.count { | ||
case Field(GETSTATIC, "Test$", "f2", _) => true | ||
case _ => false | ||
} | ||
assert(gs == 2) | ||
} | ||
} |