Skip to content

Commit e921fdd

Browse files
committed
Support @binaryAPI on private constructors
1 parent c3258eb commit e921fdd

File tree

4 files changed

+8
-35
lines changed

4 files changed

+8
-35
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ object Checking {
530530
if sym.is(Enum) then fail(em"@binaryAPI cannot be used on enum definitions.")
531531
else if sym.isType && !sym.is(Module) && !(sym.is(Given) || sym.companionModule.is(Given)) then fail(em"@binaryAPI cannot be used on ${sym.showKind} definitions")
532532
else if !sym.owner.isClass && !(sym.is(Param) && sym.owner.isConstructor) then fail(em"@binaryAPI cannot be used on local definitions.")
533-
else if sym.is(Private) then fail(em"@binaryAPI cannot be used on private definitions.\n\nCould the definition `private[${sym.owner.name}]` or `protected` instead or use `@binaryAPIAccessor` instead of `@binaryAPI`.")
533+
else if sym.is(Private) && !sym.isConstructor then fail(em"@binaryAPI cannot be used on private definitions.\n\nCould the definition `private[${sym.owner.name}]` or `protected` instead or use `@binaryAPIAccessor` instead of `@binaryAPI`.")
534534
if sym.hasAnnotation(defn.BinaryAPIAccessorAnnot) then
535535
if sym.is(Enum) then fail(em"@binaryAPIAccessor cannot be used on enum definitions.")
536536
else if sym.isConstructor then fail(em"@binaryAPIAccessor cannot be used on constructors.")

tests/neg/binaryAPI.check

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,6 @@
2222
19 | @binaryAPI case B(a: Int) // error
2323
| ^
2424
| @binaryAPI cannot be used on enum definitions.
25-
-- Error: tests/neg/binaryAPI.scala:22:9 -------------------------------------------------------------------------------
26-
22 |class Foo @binaryAPI private (x: Int): // error
27-
| ^
28-
| @binaryAPI cannot be used on private definitions.
29-
|
30-
| Could the definition `private[Foo]` or `protected` instead or use `@binaryAPIAccessor` instead of `@binaryAPI`.
31-
-- Error: tests/neg/binaryAPI.scala:23:25 ------------------------------------------------------------------------------
32-
23 | @binaryAPI private def this(x: Int, y: Int) = this(x + y) // error
33-
| ^
34-
| @binaryAPI cannot be used on private definitions.
35-
|
36-
| Could the definition `private[Foo]` or `protected` instead or use `@binaryAPIAccessor` instead of `@binaryAPI`.
37-
-- Error: tests/neg/binaryAPI.scala:25:9 -------------------------------------------------------------------------------
38-
25 |class Bar @binaryAPI private[this] (x: Int): // error
39-
| ^
40-
| @binaryAPI cannot be used on private definitions.
41-
|
42-
| Could the definition `private[Bar]` or `protected` instead or use `@binaryAPIAccessor` instead of `@binaryAPI`.
43-
-- Error: tests/neg/binaryAPI.scala:26:31 ------------------------------------------------------------------------------
44-
26 | @binaryAPI private[this] def this(x: Int, y: Int) = this(x + y) // error
45-
| ^
46-
| @binaryAPI cannot be used on private definitions.
47-
|
48-
| Could the definition `private[Bar]` or `protected` instead or use `@binaryAPIAccessor` instead of `@binaryAPI`.
4925
-- Error: tests/neg/binaryAPI.scala:5:16 -------------------------------------------------------------------------------
5026
5 |@binaryAPI type A // error
5127
| ^

tests/neg/binaryAPI.scala

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,3 @@ def f(@binaryAPI x: Int) = 3 // error
1717
enum Enum2:
1818
@binaryAPI case A // error
1919
@binaryAPI case B(a: Int) // error
20-
21-
22-
class Foo @binaryAPI private (x: Int): // error
23-
@binaryAPI private def this(x: Int, y: Int) = this(x + y) // error
24-
25-
class Bar @binaryAPI private[this] (x: Int): // error
26-
@binaryAPI private[this] def this(x: Int, y: Int) = this(x + y) // error

tests/pos/binaryAPI.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,15 @@ package constructors {
128128
class Foo @binaryAPI private[constructors] (x: Int):
129129
@binaryAPI private[constructors] def this(x: Int, y: Int) = this(x + y)
130130

131+
class Bar @binaryAPI(x: Int):
132+
@binaryAPI private def this(x: Int, y: Int) = this(x + y)
133+
inline def bar: Bar = new Bar(x, x)
134+
131135
inline def newFoo(x: Int) = new Foo(x)
132136
inline def newFoo(x: Int, y: Int) = new Foo(x, y)
133137
}
134138

135139
def testConstructors =
136-
import constructors.*
137-
val f = newFoo(1)
138-
val g = newFoo(1, 2)
140+
val f = constructors.newFoo(1)
141+
val g = constructors.newFoo(1, 2)
142+
val h = new constructors.Bar(1).bar

0 commit comments

Comments
 (0)