Skip to content

Commit da86c2c

Browse files
committed
Missing ctors from class path are private
1 parent 0ba8a31 commit da86c2c

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,22 @@ object Scala2Unpickler {
6363
denot.info = PolyType.fromParams(denot.owner.typeParams, denot.info)
6464
}
6565

66-
def ensureConstructor(cls: ClassSymbol, clsDenot: ClassDenotation, scope: Scope)(using Context): Unit = {
67-
if (scope.lookup(nme.CONSTRUCTOR) == NoSymbol) {
68-
val constr = newDefaultConstructor(cls)
66+
def ensureConstructor(cls: ClassSymbol, clsDenot: ClassDenotation, scope: Scope)(using Context): Unit =
67+
doEnsureConstructor(cls, clsDenot, scope, fromScala2 = true)
68+
69+
private def doEnsureConstructor(cls: ClassSymbol, clsDenot: ClassDenotation, scope: Scope, fromScala2: Boolean)
70+
(using Context): Unit =
71+
if scope.lookup(nme.CONSTRUCTOR) == NoSymbol then
72+
val constr =
73+
if fromScala2 || cls.isAllOf(Trait | JavaDefined) then newDefaultConstructor(cls)
74+
else newConstructor(cls, Private, paramNames = Nil, paramTypes = Nil)
6975
// Scala 2 traits have a constructor iff they have initialization code
7076
// In dotc we represent that as !StableRealizable, which is also owner.is(NoInits)
7177
if clsDenot.flagsUNSAFE.is(Trait) then
7278
constr.setFlag(StableRealizable)
7379
clsDenot.setFlag(NoInits)
7480
addConstructorTypeParams(constr)
7581
cls.enter(constr, scope)
76-
}
77-
}
7882

7983
def setClassInfo(denot: ClassDenotation, info: Type, fromScala2: Boolean, selfInfo: Type = NoType)(using Context): Unit = {
8084
val cls = denot.classSymbol
@@ -108,7 +112,7 @@ object Scala2Unpickler {
108112
if (tsym.exists) tsym.setFlag(TypeParam)
109113
else denot.enter(tparam, decls)
110114
}
111-
if (!denot.flagsUNSAFE.isAllOf(JavaModule)) ensureConstructor(cls, denot, decls)
115+
if (!denot.flagsUNSAFE.isAllOf(JavaModule)) doEnsureConstructor(cls, denot, decls, fromScala2)
112116

113117
val scalacCompanion = denot.classSymbol.scalacLinkedClass
114118

tests/neg/i15144.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//> using options --release 11
2+
// test: -jvm 17+
3+
// Must be tested where release target < current JVM.
4+
// Maybe a bug that ct.sym is not used if release == JVM version.
5+
6+
import java.time.Instant
7+
8+
class C:
9+
def f: Instant = new Instant // error
10+
def g: Instant = Instant() // error
11+
def p: P = new P // error
12+
13+
class P private ()
14+
15+
@main def test() = println:
16+
C().f

0 commit comments

Comments
 (0)