File tree Expand file tree Collapse file tree 7 files changed +77
-9
lines changed
compiler/src/dotty/tools/dotc
docs/_docs/reference/other-new-features Expand file tree Collapse file tree 7 files changed +77
-9
lines changed Original file line number Diff line number Diff 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)
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -4176,9 +4176,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
41764176 readapt(tree.appliedToNone) // insert () to primary constructors
41774177 else
41784178 errorTree(tree, em " Missing arguments for $methodStr" )
4179- case _ => tryInsertApplyOrImplicit(tree, pt, locked) {
4180- errorTree (tree, MethodDoesNotTakeParameters (tree))
4181- }
4179+ case _ =>
4180+ tryInsertApplyOrImplicit (tree, pt, locked) :
4181+ errorTree(tree, MethodDoesNotTakeParameters (tree))
41824182 }
41834183
41844184 def adaptNoArgsImplicitMethod (wtp : MethodType ): Tree = {
Original file line number Diff line number Diff line change @@ -39,8 +39,11 @@ The precise rules are as follows:
3939 - the class has a companion object (which might have been generated in step 1), and
4040 - that companion object does not already define a member named ` apply ` .
4141
42- Each generated ` apply ` method forwards to one constructor of the class. It has the
43- same type and value parameters as the constructor.
42+ Each generated ` apply ` method forwards to one constructor of the class.
43+ It has the same type and value parameters as the constructor,
44+ as well as the same access restriction as the class.
45+ If the class is ` protected ` , then either the companion object must be ` protected `
46+ or the ` apply ` method will be made ` protected ` .
4447
4548Constructor proxy companions cannot be used as values by themselves. A proxy companion object must
4649be selected with ` apply ` (or be applied to arguments, in which case the ` apply ` is implicitly
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
10+
11+ package p:
12+ private [p] class C (i : Int ) // ctor proxy gets privateWithin of class
13+ private [p] class D (i : Int )
14+ object D
15+
16+ package q:
17+ def f () = p.C (42 ) // error
18+ def g () = p.D (42 ) // error
Original file line number Diff line number Diff line change 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 ()
You can’t perform that action at this time.
0 commit comments