Skip to content

Case class with protected constructor has a public apply #14187

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

Closed
dwijnand opened this issue Dec 29, 2021 · 4 comments · Fixed by #14266
Closed

Case class with protected constructor has a public apply #14187

dwijnand opened this issue Dec 29, 2021 · 4 comments · Fixed by #14266
Assignees
Milestone

Comments

@dwijnand
Copy link
Member

Compiler version

3.1.0

Minimized code

sealed case class Foo protected (i: Int, j: Int)

final class Bar(n: Int) extends Foo(n, n)

class Other:
  def foo  = Foo(1, 2)
  def foo2 = Foo.apply(1, 2)

Output

$ scalac -3.1.0 foo.scala
$

Expectation

I shouldn't be able to construct a case class with a protected constructor in an Other class.

Notes

Looks like this was changed in #5830. I'd argue a protected constructor should mean everything a private one means with the addition that it can have subclasses because they can access the constructor. Meaning it shouldn't have an apply method (because it makes no sense in an object and may even will impact overload resolution) and therefore it shouldn't mixin its FunctionX either.

@dwijnand dwijnand self-assigned this Dec 29, 2021
@dwijnand
Copy link
Member Author

dwijnand commented Dec 29, 2021

For a real world case, I was playing around with removing RealTypeBounds by making TypeBounds non-abstract but with a protected constructor so AliasingBounds can still extend it. But the current implementation made TypeBounds' synthetic apply (which doesn't use unique) publicly accessible.

@joroKr21
Copy link
Member

Isn't it too late to change that? I.e. wouldn't it be binary incompatible? On the other hand if I'm not mistaken Scala 2.13 applies the same modifiers to apply and copy as well. If I said private I didn't mean to allow sneaky developers to work around my validations by using apply or copy.

@dwijnand dwijnand linked a pull request Jan 13, 2022 that will close this issue
@SethTisue
Copy link
Member

if I'm not mistaken Scala 2.13 applies the same modifiers to apply and copy as well

yes, but only under -Xsource:3: scala/scala#7702

@joroKr21
Copy link
Member

yes, but only under -Xsource:3: scala/scala#7702

I'm sure there's a joke somewhere there about predicting the future 😄

@Kordyjan Kordyjan added this to the 3.1.3 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants