1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
package scala.tools.nsc
package backend.jvm
package opt
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.junit.Test
import scala.tools.asm.Opcodes._
import org.junit.Assert._
import CodeGenTools._
import scala.tools.asm.tree.ClassNode
import scala.tools.nsc.backend.jvm.BTypes.{MethodInlineInfo, InlineInfo}
import scala.tools.partest.ASMConverters
import ASMConverters._
import scala.collection.convert.decorateAsScala._
import scala.tools.testing.ClearAfterClass
object ScalaInlineInfoTest extends ClearAfterClass.Clearable {
var compiler = newCompiler(extraArgs = "-Yopt:l:none")
def clear(): Unit = { compiler = null }
}
@RunWith(classOf[JUnit4])
class ScalaInlineInfoTest extends ClearAfterClass {
ClearAfterClass.stateToClear = ScalaInlineInfoTest
val compiler = newCompiler()
def inlineInfo(c: ClassNode): InlineInfo = c.attrs.asScala.collect({ case a: InlineInfoAttribute => a.inlineInfo }).head
@Test
def traitMembersInlineInfo(): Unit = {
val code =
"""trait T {
| def f1 = 1 // concrete method
| private def f2 = 1 // implOnly method (does not end up in the interface)
| def f3 = {
| def nest = 0 // nested method (does not end up in the interface)
| nest
| }
|
| @inline
| def f4 = super.toString // super accessor
|
| object O // module accessor (method is generated)
| def f5 = {
| object L { val x = 0 } // nested module (just flattened out)
| L.x
| }
|
| @noinline
| def f6: Int // abstract method (not in impl class)
|
| // fields
|
| val x1 = 0
| var y2 = 0
| var x3: Int
| lazy val x4 = 0
|
| final val x5 = 0
|}
""".stripMargin
val cs @ List(t, tl, to) = compileClasses(compiler)(code)
val info = inlineInfo(t)
val expect = InlineInfo (
false, // final class
None, // not a sam
Map(
// TODO SD-86: the module accessor used to be `effectivelyFinal` before nuke-impl-classes
("O()LT$O$;", MethodInlineInfo(false,false,false)),
("T$$super$toString()Ljava/lang/String;", MethodInlineInfo(false,false,false)),
("T$_setter_$x1_$eq(I)V", MethodInlineInfo(false,false,false)),
("f1()I", MethodInlineInfo(false,false,false)),
("f3()I", MethodInlineInfo(false,false,false)),
("f4()Ljava/lang/String;", MethodInlineInfo(false,true, false)),
("f5()I", MethodInlineInfo(false,false,false)),
("f6()I", MethodInlineInfo(false,false,true )),
("x1()I", MethodInlineInfo(false,false,false)),
("x3()I", MethodInlineInfo(false,false,false)),
("x3_$eq(I)V", MethodInlineInfo(false,false,false)),
("x4()I", MethodInlineInfo(false,false,false)),
("x5()I", MethodInlineInfo(true, false,false)),
("y2()I", MethodInlineInfo(false,false,false)),
("y2_$eq(I)V", MethodInlineInfo(false,false,false)),
("f2()I", MethodInlineInfo(true, false,false)),
("L$lzycompute$1(Lscala/runtime/VolatileObjectRef;)LT$L$2$;",MethodInlineInfo(true, false,false)),
// TODO SD-86: should probably be effectivelyFinal
("L$1(Lscala/runtime/VolatileObjectRef;)LT$L$2$;", MethodInlineInfo(false,false,false)),
("nest$1()I", MethodInlineInfo(true, false,false)),
("$init$()V", MethodInlineInfo(false,false,false))),
None // warning
)
assert(info == expect, info)
}
@Test
def inlineInfoSam(): Unit = {
val code =
"""trait C { // expected to be seen as sam: g(I)I
| def f = 0
| def g(x: Int): Int
| val foo = "hi"
|}
|abstract class D {
| val biz: Int
|}
|trait T { // expected to be seen as sam: h(Ljava/lang/String;)I
| def h(a: String): Int
|}
|trait E extends T { // expected to be seen as sam: h(Ljava/lang/String;)I
| def hihi(x: Int) = x
|}
|class F extends T {
| def h(a: String) = 0
|}
|trait U {
| def conc() = 10
| def nullary: Int
|}
""".stripMargin
val cs = compileClasses(compiler)(code)
val sams = cs.map(c => (c.name, inlineInfo(c).sam))
assertEquals(sams,
List(
("C",Some("g(I)I")),
("D",None),
("E",Some("h(Ljava/lang/String;)I")),
("F",None),
("T",Some("h(Ljava/lang/String;)I")),
("U",None)))
}
}
|