@@ -8,9 +8,20 @@ import org.junit.runners.JUnit4
88import scala .tools .nsc .backend .jvm .BTypes .InternalName
99import scala .tools .nsc .backend .jvm .opt .InlineSourceMatcherTest ._
1010import scala .tools .nsc .backend .jvm .opt .InlinerHeuristics ._
11+ import scala .tools .testing .BytecodeTesting
12+ import scala .tools .testing .BytecodeTesting ._
1113
1214@ RunWith (classOf [JUnit4 ])
13- class InlineSourceMatcherTest {
15+ class InlineSourceMatcherTest extends BytecodeTesting {
16+ import compiler ._
17+
18+ override def compilerArgs = " -opt:l:inline -opt-warnings"
19+ def setInlineFrom (s : String ): Unit = {
20+ global.settings.optInlineFrom.value = s
21+ // the setting is read once per run
22+ global.perRunCaches.clearAll()
23+ }
24+
1425 case class E (regex : String , negated : Boolean = false , terminal : Boolean = true )
1526
1627 def check (pat : String , expect : E * ): InlineSourceMatcher = {
@@ -158,6 +169,63 @@ class InlineSourceMatcherTest {
158169 m.d(" a/C" )
159170 }
160171 }
172+
173+ @ Test
174+ def inlineFromSameClass (): Unit = {
175+ val code =
176+ """ class C {
177+ | @inline final def f = 1
178+ | def t = f
179+ |}
180+ """ .stripMargin
181+
182+ def n (): Unit = assertInvoke(getMethod(compileClass(code), " t" ), " C" , " f" )
183+ def y (): Unit = assertNoInvoke(getMethod(compileClass(code), " t" ))
184+
185+ setInlineFrom(" " ); n()
186+ setInlineFrom(" C" ); y()
187+ setInlineFrom(" **:!**.C" ); n()
188+ setInlineFrom(" **:!**.C:C" ); y()
189+ }
190+
191+ @ Test
192+ def inlineFromPackages (): Unit = {
193+ val code =
194+ """ package a { class C {
195+ | object D { @inline def f = 1 }
196+ | @inline final def f = 2
197+ |}}
198+ |package b { class E { import a._
199+ | def t1(c: C) = c.f
200+ | def t2(c: C) = c.D.f
201+ |}}
202+ """ .stripMargin
203+
204+ {
205+ setInlineFrom(" " )
206+ val List (_, _, e) = compileClasses(code)
207+ assertInvoke(getMethod(e, " t1" ), " a/C" , " f" )
208+ assertInvoke(getMethod(e, " t2" ), " a/C$D$" , " f" )
209+ }
210+ {
211+ setInlineFrom(" a.C" )
212+ val List (_, _, e) = compileClasses(code)
213+ assertNoInvoke(getMethod(e, " t1" ))
214+ assertInvoke(getMethod(e, " t2" ), " a/C$D$" , " f" )
215+ }
216+ {
217+ setInlineFrom(" a.C*" )
218+ val List (_, _, e) = compileClasses(code)
219+ assertNoInvoke(getMethod(e, " t1" ))
220+ assertDoesNotInvoke(getMethod(e, " t2" ), " f" ) // t2 still has an invocation to the getter `D`
221+ }
222+ {
223+ setInlineFrom(" a.C*:!a.C*$" )
224+ val List (_, _, e) = compileClasses(code)
225+ assertNoInvoke(getMethod(e, " t1" ))
226+ assertInvoke(getMethod(e, " t2" ), " a/C$D$" , " f" )
227+ }
228+ }
161229}
162230
163231object InlineSourceMatcherTest {
0 commit comments