Skip to content

Commit fddab10

Browse files
committed
Constructor proxy or companion is as protected as class
Require the constructor companion to be protected if the class is; or create it protected. Otherwise, create protected proxies.
1 parent 4518ce5 commit fddab10

File tree

6 files changed

+63
-7
lines changed

6 files changed

+63
-7
lines changed

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,11 @@ object NamerOps:
149149
*/
150150
def addConstructorApplies(scope: MutableScope, cls: ClassSymbol, modcls: ClassSymbol)(using Context): scope.type =
151151
def proxy(constr: Symbol): Symbol =
152+
var flags = ApplyProxyFlags | (constr.flagsUNSAFE & AccessFlags)
153+
if cls.is(Protected) && !modcls.is(Protected) then flags |= Protected
152154
newSymbol(
153155
modcls, nme.apply,
154-
ApplyProxyFlags | (constr.flagsUNSAFE & AccessFlags),
156+
flags,
155157
ApplyProxyCompleter(constr),
156158
cls.privateWithin,
157159
constr.coord)
@@ -175,9 +177,11 @@ object NamerOps:
175177

176178
/** A new symbol that is the constructor companion for class `cls` */
177179
def classConstructorCompanion(cls: ClassSymbol)(using Context): TermSymbol =
180+
var flags = ConstructorCompanionFlags
181+
if cls.is(Protected) then flags |= Protected
178182
val companion = newModuleSymbol(
179183
cls.owner, cls.name.toTermName,
180-
ConstructorCompanionFlags, ConstructorCompanionFlags,
184+
flags, flags,
181185
constructorCompanionCompleter(cls),
182186
coord = cls.coord,
183187
compUnitInfo = cls.compUnitInfo)

compiler/src/dotty/tools/dotc/typer/Applications.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -1198,9 +1198,8 @@ trait Applications extends Compatibility {
11981198
//
11991199
// summonFrom {
12001200
// case given A[t] =>
1201-
// summonFrom
1201+
// summonFrom:
12021202
// case given `t` => ...
1203-
// }
12041203
// }
12051204
//
12061205
// the second `summonFrom` should expand only once the first `summonFrom` is

compiler/src/dotty/tools/dotc/typer/Typer.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -4173,9 +4173,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
41734173
readapt(tree.appliedToNone) // insert () to primary constructors
41744174
else
41754175
errorTree(tree, em"Missing arguments for $methodStr")
4176-
case _ => tryInsertApplyOrImplicit(tree, pt, locked) {
4177-
errorTree(tree, MethodDoesNotTakeParameters(tree))
4178-
}
4176+
case _ =>
4177+
tryInsertApplyOrImplicit(tree, pt, locked):
4178+
errorTree(tree, MethodDoesNotTakeParameters(tree))
41794179
}
41804180

41814181
def adaptNoArgsImplicitMethod(wtp: MethodType): Tree = {

tests/neg/i22560.scala

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
class A:
3+
protected class B
4+
5+
// This fails to compile, as expected
6+
val x = A().B() // error
7+
8+
object C:
9+
protected val p = "protected"
10+
protected def getString() = "Hello!"
11+
protected class D:
12+
def d = D() // ok
13+
14+
// This fails to compile
15+
// val y = C.p
16+
17+
// And this also fails to compile
18+
// val z = C.getString()
19+
20+
// However, this compiles just fine.
21+
val alpha = C.D() // error
22+
val beta = new C.D() // error

tests/neg/i22560b.scala

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
class Enumeration:
3+
protected class Val(i: Int):
4+
def this() = this(42)
5+
object Val
6+
7+
class Test extends Enumeration:
8+
val Hearts = Val(27) // error
9+
val Diamonds = Val() // error

tests/pos/i22560.scala

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
package companionless:
3+
4+
class Enumeration:
5+
protected class Val(i: Int):
6+
def this() = this(42)
7+
8+
class Test extends Enumeration:
9+
val Hearts = Val(27)
10+
val Diamonds = Val()
11+
12+
13+
package companioned:
14+
15+
class Enumeration:
16+
protected class Val(i: Int):
17+
def this() = this(42)
18+
protected object Val
19+
20+
class Test extends Enumeration:
21+
val Hearts = Val(27)
22+
val Diamonds = Val()

0 commit comments

Comments
 (0)