Skip to content

Commit cdf38a1

Browse files
lrytzretronym
authored andcommitted
SI-9256 check companions in same compilation unit only if same run
1 parent 79fa035 commit cdf38a1

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

src/compiler/scala/tools/nsc/typechecker/Namers.scala

+1
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ trait Namers extends MethodSynthesis {
431431
&& !(module isCoDefinedWith clazz)
432432
&& module.exists
433433
&& clazz.exists
434+
&& (currentRun.compiles(clazz) == currentRun.compiles(module))
434435
)
435436
if (fails) {
436437
reporter.error(tree.pos, (

test/junit/scala/tools/nsc/backend/jvm/DirectCompileTest.scala

+12
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,16 @@ class DirectCompileTest extends BytecodeTesting {
9999
compiler.compileToBytes(a)
100100
compiler.compileToBytes(b)
101101
}
102+
103+
@Test
104+
def residentMultipleRunsNotCompanions(): Unit = {
105+
val compiler = newCompiler()
106+
val a = List(("public class A { }", "A.java"))
107+
// when checking that a class and its companion are defined in the same compilation unit, the
108+
// compiler would also emit a warning if the two symbols are defined in separate runs. this
109+
// would lead to an error message when compiling the scala class A.
110+
val b = "class A"
111+
compiler.compileToBytes("", a)
112+
compiler.compileToBytes(b)
113+
}
102114
}

test/junit/scala/tools/nsc/backend/jvm/opt/InlineWarningTest.scala

+12-12
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class InlineWarningTest extends BytecodeTesting {
9797
@Test
9898
def cannotInlinePrivateCallIntoDifferentClass(): Unit = {
9999
val code =
100-
"""class M {
100+
"""class A {
101101
| @inline final def f = {
102102
| @noinline def nested = 0
103103
| nested
@@ -106,15 +106,15 @@ class InlineWarningTest extends BytecodeTesting {
106106
| def t = f // ok
107107
|}
108108
|
109-
|class N {
110-
| def t(a: M) = a.f // not possible
109+
|class B {
110+
| def t(a: A) = a.f // not possible
111111
|}
112112
""".stripMargin
113113

114114
val warn =
115-
"""M::f()I is annotated @inline but could not be inlined:
116-
|The callee M::f()I contains the instruction INVOKESTATIC M.nested$1 ()I
117-
|that would cause an IllegalAccessError when inlined into class N""".stripMargin
115+
"""A::f()I is annotated @inline but could not be inlined:
116+
|The callee A::f()I contains the instruction INVOKESTATIC A.nested$1 ()I
117+
|that would cause an IllegalAccessError when inlined into class B""".stripMargin
118118

119119
var c = 0
120120
compileToBytes(code, allowMessage = i => { c += 1; i.msg contains warn })
@@ -124,24 +124,24 @@ class InlineWarningTest extends BytecodeTesting {
124124
@Test
125125
def dontWarnWhenNotIlnineAnnotated(): Unit = {
126126
val code =
127-
"""class M {
127+
"""class A {
128128
| final def f(t: Int => Int) = {
129129
| @noinline def nested = 0
130130
| nested + t(1)
131131
| }
132132
| def t = f(x => x + 1)
133133
|}
134134
|
135-
|class N {
136-
| def t(a: M) = a.f(x => x + 1)
135+
|class B {
136+
| def t(a: A) = a.f(x => x + 1)
137137
|}
138138
""".stripMargin
139139
compileToBytes(code, allowMessage = _ => false) // no warnings allowed
140140

141141
val warn =
142-
"""M::f(Lscala/Function1;)I could not be inlined:
143-
|The callee M::f(Lscala/Function1;)I contains the instruction INVOKESTATIC M.nested$1 ()I
144-
|that would cause an IllegalAccessError when inlined into class N""".stripMargin
142+
"""A::f(Lscala/Function1;)I could not be inlined:
143+
|The callee A::f(Lscala/Function1;)I contains the instruction INVOKESTATIC A.nested$1 ()I
144+
|that would cause an IllegalAccessError when inlined into class B""".stripMargin
145145

146146
var c = 0
147147
compilerWarnAll.compileToBytes(code, allowMessage = i => { c += 1; i.msg contains warn })

test/junit/scala/tools/nsc/backend/jvm/opt/InlinerTest.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -835,11 +835,11 @@ class InlinerTest extends BytecodeTesting {
835835
@Test
836836
def inlineInvokeSpecial(): Unit = {
837837
val code =
838-
"""class Aa {
838+
"""class A {
839839
| def f1 = 0
840840
|}
841-
|class B extends Aa {
842-
| @inline final override def f1 = 1 + super.f1 // invokespecial Aa.f1
841+
|class B extends A {
842+
| @inline final override def f1 = 1 + super.f1 // invokespecial A.f1
843843
|
844844
| private def f2m = 0 // public B$$f2m in bytecode
845845
| @inline final def f2 = f2m // invokevirtual B.B$$f2m
@@ -863,13 +863,13 @@ class InlinerTest extends BytecodeTesting {
863863

864864
val warn =
865865
"""B::f1()I is annotated @inline but could not be inlined:
866-
|The callee B::f1()I contains the instruction INVOKESPECIAL Aa.f1 ()I
866+
|The callee B::f1()I contains the instruction INVOKESPECIAL A.f1 ()I
867867
|that would cause an IllegalAccessError when inlined into class T.""".stripMargin
868868
var c = 0
869869
val List(a, b, t) = compile(code, allowMessage = i => {c += 1; i.msg contains warn})
870870
assert(c == 1, c)
871871

872-
assertInvoke(getMethod(b, "t1"), "Aa", "f1")
872+
assertInvoke(getMethod(b, "t1"), "A", "f1")
873873
assertInvoke(getMethod(b, "t2"), "B", "B$$f2m")
874874
assertInvoke(getMethod(b, "t3"), "B", "<init>")
875875
assertInvoke(getMethod(b, "t4"), "B", "<init>")

0 commit comments

Comments
 (0)