Skip to content
This repository was archived by the owner on Sep 1, 2020. It is now read-only.

Commit f5dc96b

Browse files
committed
SI-9425 Leave Companion.apply if constructor is less accessible
Calls to synthetic case class apply methods are inlined to the underlying constructor invocation in refchecks. However, this can lead to accessibility errors if the constructor is private. This commit ensures that the constructor is at least as accessible as the apply method before performing this tranform. I've manually checked that other the optimization still works in other cases: scala> class CaseApply { Some(42) } defined class CaseApply scala> :javap -c CaseApply Compiled from "<console>" public class CaseApply { public CaseApply(); Code: 0: aload_0 1: invokespecial #9 // Method java/lang/Object."<init>":()V 4: new #11 // class scala/Some 7: dup 8: bipush 42 10: invokestatic #17 // Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer; 13: invokespecial #20 // Method scala/Some."<init>":(Ljava/lang/Object;)V 16: pop 17: return }
1 parent 65fa73d commit f5dc96b

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1511,7 +1511,8 @@ abstract class RefChecks extends InfoTransform with scala.reflect.internal.trans
15111511
sym.isSourceMethod &&
15121512
sym.isCase &&
15131513
sym.name == nme.apply &&
1514-
isClassTypeAccessible(tree)
1514+
isClassTypeAccessible(tree) &&
1515+
!tree.tpe.resultType.typeSymbol.primaryConstructor.isLessAccessibleThan(tree.symbol)
15151516

15161517
if (doTransform) {
15171518
tree foreach {

test/files/run/t9425.scala

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class C { case class Foo private (x: Int); Foo.apply(0) }
2+
3+
object Test {
4+
def test(c: C) = {import c.Foo; Foo.apply(0)}
5+
def main(args: Array[String]): Unit = {
6+
test(new C)
7+
}
8+
}

0 commit comments

Comments
 (0)