Skip to content

@publicInBinary has now effect on secondary constructors #22630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/inlines/PrepareInlineable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ object PrepareInlineable {
def preTransform(tree: Tree)(using Context): Tree = tree match {
case tree: RefTree if needsAccessor(tree.symbol) =>
if (tree.symbol.isConstructor) {
report.error("Implementation restriction: cannot use private constructors in inline methods", tree.srcPos)
tree // TODO: create a proper accessor for the private constructor
report.error("Private constructors used in inline methods require @publicInBinary", tree.srcPos)
tree
}
else
val accessor = useAccessor(tree)
Expand Down
7 changes: 6 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/ExpandPrivate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ import ValueClasses.*
* Make private accessor in value class not-private. This is necessary to unbox
* the value class when accessing it from separate compilation units
*
* Also, make non-private any private parameter forwarders that forward to an inherited
* Make non-private any private parameter forwarders that forward to an inherited
* public or protected parameter accessor with the same name as the forwarder.
* This is necessary since private methods are not allowed to have the same name
* as inherited public ones.
*
* Also, make non-private any private constructor that is annotated with `@publicInBinary`.
* (See SIP-52)
*
* See discussion in https://github.com/scala/scala3/pull/784
* and https://github.com/scala/scala3/issues/783
*/
Expand Down Expand Up @@ -102,6 +105,8 @@ class ExpandPrivate extends MiniPhase with IdentityDenotTransformer { thisPhase
override def transformDefDef(tree: DefDef)(using Context): DefDef = {
val sym = tree.symbol
tree.rhs match {
case _ if sym.isConstructor && sym.hasPublicInBinary =>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a weird way to write an if condition, but avoids another level of indentation with an if-else expression.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SIP-39 - Uncluttering Abuse of Match

sym.ensureNotPrivate.installAfter(thisPhase)
case Apply(sel @ Select(_: Super, _), _)
if sym.isAllOf(PrivateParamAccessor) && sel.symbol.is(ParamAccessor) && sym.name == sel.symbol.name =>
sym.ensureNotPrivate.installAfter(thisPhase)
Expand Down
4 changes: 4 additions & 0 deletions tests/neg/i22498.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Error: tests/neg/i22498.scala:7:32 ----------------------------------------------------------------------------------
7 | inline def proxy: Foo = new Foo(0) // error
| ^^^
| Private constructors used in inline methods require @publicInBinary
7 changes: 7 additions & 0 deletions tests/neg/i22498.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//> using options -experimental

import scala.annotation.publicInBinary

class Foo:
private def this(x: Int) = this()
inline def proxy: Foo = new Foo(0) // error
3 changes: 3 additions & 0 deletions tests/run/i22497.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public Foo()
public Foo(int)
public Foo(java.lang.String)
13 changes: 13 additions & 0 deletions tests/run/i22497.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// scalajs: --skip

import scala.annotation.publicInBinary
import scala.annotation.experimental

@experimental
class Foo:
@publicInBinary private def this(i: Int) = this()
@publicInBinary protected def this(i: String) = this()

@experimental
@main def Test =
println(classOf[Foo].getConstructors().mkString("\n"))
10 changes: 10 additions & 0 deletions tests/run/i22498.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//> using options -experimental

import scala.annotation.publicInBinary

class Foo:
@publicInBinary private def this(x: Int) = this()
inline def proxy: Foo = new Foo(0)

@main def Test =
val x = (new Foo).proxy
Loading