Skip to content

Commit 78ddbb9

Browse files
som-snytttgodzik
authored andcommitted
Constructor companion gets privateWithin
1 parent 0b9f795 commit 78ddbb9

File tree

9 files changed

+122
-0
lines changed

9 files changed

+122
-0
lines changed

compiler/src/dotty/tools/dotc/core/NamerOps.scala

+1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ object NamerOps:
141141
cls.owner, cls.name.toTermName,
142142
flags, flags,
143143
constructorCompanionCompleter(cls),
144+
cls.privateWithin,
144145
coord = cls.coord,
145146
assocFile = cls.assocFile)
146147
companion.moduleClass.registerCompanion(cls)

tests/neg/i18545.check

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
-- [E173] Reference Error: tests/neg/i18545.scala:16:20 ----------------------------------------------------------------
2+
16 | def test: IOLocal.IOLocalImpl[Int] = // error
3+
| ^^^^^^^^^^^^^^^^^^^
4+
|class IOLocalImpl cannot be accessed as a member of iolib.IOLocal.type from the top-level definitions in package tests.
5+
| private[IOLocal] class IOLocalImpl can only be accessed from object IOLocal in package iolib.
6+
-- [E173] Reference Error: tests/neg/i18545.scala:17:24 ----------------------------------------------------------------
7+
17 | IOLocal.IOLocalImpl.apply(42) // error
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
9+
|method apply cannot be accessed as a member of iolib.IOLocal.IOLocalImpl.type from the top-level definitions in package tests.
10+
| private[IOLocal] method apply can only be accessed from object IOLocal in package iolib.
11+
-- [E050] Type Error: tests/neg/i18545.scala:18:22 ---------------------------------------------------------------------
12+
18 | def test2 = IOLocal.IOLocalImpl(42) // error
13+
| ^^^^^^^^^^^^^^^^^^^
14+
| object IOLocalImpl in object IOLocal does not take parameters
15+
|
16+
| longer explanation available when compiling with `-explain`
17+
-- [E173] Reference Error: tests/neg/i18545.scala:19:22 ----------------------------------------------------------------
18+
19 | def test3 = IOLocal.AltIOLocalImpl.apply(42) // error
19+
| ^^^^^^^^^^^^^^^^^^^^^^
20+
|object AltIOLocalImpl cannot be accessed as a member of iolib.IOLocal.type from the top-level definitions in package tests.
21+
| private[IOLocal] object AltIOLocalImpl can only be accessed from object IOLocal in package iolib.
22+
-- [E173] Reference Error: tests/neg/i18545.scala:20:22 ----------------------------------------------------------------
23+
20 | def test4 = IOLocal.AltIOLocalImpl(42) // error
24+
| ^^^^^^^^^^^^^^^^^^^^^^
25+
|object AltIOLocalImpl cannot be accessed as a member of iolib.IOLocal.type from the top-level definitions in package tests.
26+
| private[IOLocal] object AltIOLocalImpl can only be accessed from object IOLocal in package iolib.

tests/neg/i18545.scala

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package iolib:
2+
case class IO[A](value: A)
3+
4+
sealed trait IOLocal[A]
5+
6+
object IOLocal:
7+
def apply[A](default: A): IO[IOLocal[A]] = IO(new IOLocalImpl(default))
8+
9+
private[IOLocal] final class IOLocalImpl[A](default: A) extends IOLocal[A]
10+
object IOLocalImpl
11+
12+
private[IOLocal] final class AltIOLocalImpl[A](default: A) extends IOLocal[A]
13+
14+
package tests:
15+
import iolib.IOLocal
16+
def test: IOLocal.IOLocalImpl[Int] = // error
17+
IOLocal.IOLocalImpl.apply(42) // error
18+
def test2 = IOLocal.IOLocalImpl(42) // error
19+
def test3 = IOLocal.AltIOLocalImpl.apply(42) // error
20+
def test4 = IOLocal.AltIOLocalImpl(42) // error

tests/neg/i22560b.scala

+2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ package p:
1212
private[p] class C(i: Int) // ctor proxy gets privateWithin of class
1313
private[p] class D(i: Int)
1414
object D
15+
private class E(i: Int)
1516

1617
package q:
1718
def f() = p.C(42) // error
1819
def g() = p.D(42) // error
20+
def h() = p.E(42) // error

tests/neg/i22560c/client_2.scala

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
package i22560:
3+
val alpha = C.D() // error
4+
5+
class Test extends Enumeration:
6+
val Hearts = Val(27) // error
7+
val Diamonds = Val() // error
8+
9+
package q:
10+
def f() = p.C(42) // error
11+
def g() = p.D(42) // error

tests/neg/i22560c/lib_1.scala

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
package i22560:
3+
4+
object C:
5+
protected class D
6+
7+
class Enumeration:
8+
protected class Val(i: Int):
9+
def this() = this(42)
10+
object Val
11+
12+
package p:
13+
private[p] class C(i: Int) // companion gets privateWithin of class
14+
private[p] class D(i: Int) // ctor proxy gets privateWithin of class
15+
object D
16+

tests/pos/i22560.scala

+10
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,13 @@ package companioned:
2020
class Test extends Enumeration:
2121
val Hearts = Val(27)
2222
val Diamonds = Val()
23+
24+
package p:
25+
26+
package internal:
27+
28+
protected[p] class P(i : Int)
29+
private[p] class Q(i : Int)
30+
31+
def f = internal.P(42)
32+
def g = internal.Q(42)

tests/pos/i22560b/client_2.scala

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
package companionless:
3+
4+
class Test extends Enumeration:
5+
val Hearts = Val(27)
6+
val Diamonds = Val()
7+
8+
9+
package companioned:
10+
11+
class Test extends Enumeration:
12+
val Hearts = Val(27)
13+
val Diamonds = Val()
14+
15+
package p:
16+
17+
def f = internal.P(42)

tests/pos/i22560b/lib_1.scala

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
package companionless:
3+
4+
class Enumeration:
5+
protected class Val(i: Int):
6+
def this() = this(42)
7+
8+
package companioned:
9+
10+
class Enumeration:
11+
protected class Val(i: Int):
12+
def this() = this(42)
13+
protected object Val
14+
15+
package p:
16+
17+
package internal:
18+
19+
protected[p] class P(i : Int)

0 commit comments

Comments
 (0)