Skip to content

Commit

Permalink
Constructor proxy is protected if class is
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Feb 9, 2025
1 parent 9066923 commit 2388ac7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/core/NamerOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,11 @@ object NamerOps:
*/
def addConstructorApplies(scope: MutableScope, cls: ClassSymbol, modcls: ClassSymbol)(using Context): scope.type =
def proxy(constr: Symbol): Symbol =
var flags = ApplyProxyFlags | (constr.flagsUNSAFE & AccessFlags)
if cls.is(Protected) then flags |= Protected
newSymbol(
modcls, nme.apply,
ApplyProxyFlags | (constr.flagsUNSAFE & AccessFlags),
flags,
ApplyProxyCompleter(constr),
cls.privateWithin,
constr.coord)
Expand Down
22 changes: 22 additions & 0 deletions tests/neg/i22560.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

class A:
protected class B

// This fails to compile, as expected
val x = A().B() // error

object C:
protected val p = "protected"
protected def getString() = "Hello!"
protected class D:
def d = D() // ok

// This fails to compile
// val y = C.p

// And this also fails to compile
// val z = C.getString()

// However, this compiles just fine.
val alpha = C.D() // error
val beta = new C.D() // error

0 comments on commit 2388ac7

Please sign in to comment.