Skip to content

Commit ac34982

Browse files
som-snytttgodzik
authored andcommitted
Missing ctors from class path are private
[Cherry-picked da86c2c]
1 parent 3b1284b commit ac34982

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
@@ -64,18 +64,22 @@ object Scala2Unpickler {
6464
denot.info = PolyType.fromParams(denot.owner.typeParams, denot.info)
6565
}
6666

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

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

114118
val scalacCompanion = denot.classSymbol.scalacLinkedClass
115119

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)