-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Constructor proxy is restricted if class is protected (#22563)
Calling a constructor proxy should be at least as restricted as using `new`. Add `Protected` to a constructor companion if the class is protected. If the companion exists but is not protected, then make the constructor proxy protected. Explicit `new` already errored for access. Fixes #22560
- Loading branch information
Showing
7 changed files
with
77 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
class Enumeration: | ||
protected class Val(i: Int): | ||
def this() = this(42) | ||
object Val | ||
|
||
class Test extends Enumeration: | ||
val Hearts = Val(27) // error | ||
val Diamonds = Val() // error | ||
|
||
package p: | ||
private[p] class C(i: Int) // ctor proxy gets privateWithin of class | ||
private[p] class D(i: Int) | ||
object D | ||
|
||
package q: | ||
def f() = p.C(42) // error | ||
def g() = p.D(42) // error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
package companionless: | ||
|
||
class Enumeration: | ||
protected class Val(i: Int): | ||
def this() = this(42) | ||
|
||
class Test extends Enumeration: | ||
val Hearts = Val(27) | ||
val Diamonds = Val() | ||
|
||
|
||
package companioned: | ||
|
||
class Enumeration: | ||
protected class Val(i: Int): | ||
def this() = this(42) | ||
protected object Val | ||
|
||
class Test extends Enumeration: | ||
val Hearts = Val(27) | ||
val Diamonds = Val() |