Skip to content

Commit 28fa050

Browse files
authored
Constructor companion gets privateWithin (#22627)
Follow-up to #22560 Fixes #22626 When creating a companion for a constructor proxy, the companion is `protected` if the class is. Now it also receives the `privateWithin` boundary of the class, for `protected[p]`.
2 parents 595c5d7 + eea481f commit 28fa050

File tree

9 files changed

+99
-8
lines changed

9 files changed

+99
-8
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ object NamerOps:
183183
cls.owner, cls.name.toTermName,
184184
flags, flags,
185185
constructorCompanionCompleter(cls),
186-
coord = cls.coord,
187-
compUnitInfo = cls.compUnitInfo)
186+
cls.privateWithin,
187+
cls.coord,
188+
cls.compUnitInfo)
188189
companion.moduleClass.registerCompanion(cls)
189190
cls.registerCompanion(companion.moduleClass)
190191
companion

tests/neg/i18545.check

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
1-
-- [E173] Reference Error: tests/neg/i18545.scala:13:20 ----------------------------------------------------------------
2-
13 | def test: IOLocal.IOLocalImpl[Int] = // error
1+
-- [E173] Reference Error: tests/neg/i18545.scala:16:20 ----------------------------------------------------------------
2+
16 | def test: IOLocal.IOLocalImpl[Int] = // error
33
| ^^^^^^^^^^^^^^^^^^^
44
|class IOLocalImpl cannot be accessed as a member of iolib.IOLocal.type from the top-level definitions in package tests.
55
| private[IOLocal] class IOLocalImpl can only be accessed from object IOLocal in package iolib.
6-
-- [E173] Reference Error: tests/neg/i18545.scala:14:24 ----------------------------------------------------------------
7-
14 | IOLocal.IOLocalImpl.apply(42) // error
6+
-- [E173] Reference Error: tests/neg/i18545.scala:17:24 ----------------------------------------------------------------
7+
17 | IOLocal.IOLocalImpl.apply(42) // error
88
| ^^^^^^^^^^^^^^^^^^^^^^^^^
99
|method apply cannot be accessed as a member of iolib.IOLocal.IOLocalImpl.type from the top-level definitions in package tests.
1010
| private[IOLocal] method apply can only be accessed from object IOLocal in package iolib.
11-
-- [E050] Type Error: tests/neg/i18545.scala:15:22 ---------------------------------------------------------------------
12-
15 | def test2 = IOLocal.IOLocalImpl(42) // error
11+
-- [E050] Type Error: tests/neg/i18545.scala:18:22 ---------------------------------------------------------------------
12+
18 | def test2 = IOLocal.IOLocalImpl(42) // error
1313
| ^^^^^^^^^^^^^^^^^^^
1414
| object IOLocalImpl in object IOLocal does not take parameters
1515
|
1616
| 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

+5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ package iolib:
77
def apply[A](default: A): IO[IOLocal[A]] = IO(new IOLocalImpl(default))
88

99
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]
1013

1114
package tests:
1215
import iolib.IOLocal
1316
def test: IOLocal.IOLocalImpl[Int] = // error
1417
IOLocal.IOLocalImpl.apply(42) // error
1518
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)