Skip to content
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

Case class copy and apply inherit access modifiers from constructor (under -Xsource:3) #7702

Merged
merged 1 commit into from Feb 13, 2020

Conversation

Jasper-M
Copy link
Member

@Jasper-M Jasper-M commented Jan 31, 2019

Backport from dotty:

  • If a case class constructor is private or private[foo]: the synthesized copy and apply methods will have the same access modifier.
  • If a case class constructor is protected or protected[foo]: the synthesized copy method will have the same access modifier. The synthesized apply method will remain public, because protected doesn't make sense in an object.

Obviously, if a user defines a custom copy or apply method, that one—including its access modifier—will take precedence.

See the accompanying pos and neg tests.

Only enabled under -Xsource:2.14 and by extension under -Xsource:3.

@Jasper-M

This comment has been minimized.

@Jasper-M Jasper-M changed the title [WIP] Case class copy and apply inherit access modifiers from constructor Case class copy and apply inherit access modifiers from constructor [ci: last-only] Feb 6, 2019
@Jasper-M
Copy link
Member Author

Jasper-M commented Feb 6, 2019

Added some changes to bring the PR in line with scala/scala3#5830 and fixes for corresponding dotty issues scala/scala3#5857 and scala/scala3#5859.

@Jasper-M
Copy link
Member Author

Jasper-M commented Feb 6, 2019

@adriaanm All those private constructors in pos/userdefined_apply.scala, are they there to avoid that calls to apply get transformed to calls to the constructor? In which case it would be fine to make them protected instead?

@adriaanm
Copy link
Contributor

adriaanm commented Feb 6, 2019

@adriaanm All those private constructors in pos/userdefined_apply.scala, are they there to avoid that calls to apply get transformed to calls to the constructor? In which case it would be fine to make them protected instead?

IIRC it's to control who can create an instance of a class -- if this is what you want, a private constructor is useless if your apply is public, so basically the same as what your PR offers by default :-)

@Jasper-M
Copy link
Member Author

Jasper-M commented Feb 6, 2019

@adriaanm Err... Not following 100% what you're saying. Some of those tests seem to rely specifically on the generated apply not being private.

But anyway I think this PR is ready for review and discussion on how to proceed (SIP or no SIP, -Xsource flag or no flag, ...).

Status of this PR right now:

  • private and protected, both qualified and unqualified, get inherited by the copy method from the constructor
  • (un)qualified private gets inherited by the apply method from the constructor
  • (un)qualified protected constructor results in normal public apply method, as seen in dotty
  • case classes with a (un)qualified private constructor will have a companion object that does not extend a FunctionN trait.

I reviewed all failing tests and the bugs they are defending against. In all cases the right thing to do seemed to me turning private constructors into protected. That way apply is still public and calls to apply will not get rewritten into calls to the constructor.

@eed3si9n
Copy link
Member

But anyway I think this PR is ready for review and discussion on how to proceed (SIP or no SIP, -Xsource flag or no flag, ...).

@smarter Is there a SIP for this on Dotty's change?

@smarter
Copy link
Member

smarter commented Aug 14, 2019

@smarter Is there a SIP for this on Dotty's change?

There isn't.

@tpolecat
Copy link
Contributor

I wasn't aware of this PR until just now. It would be really nice to get this into Scala 2 so we can get away from sealed abstract case class (although I admit it's probably my favorite trick).

@szeiger szeiger modified the milestones: 2.13.1, 2.13.2 Aug 30, 2019
@Jasper-M Jasper-M force-pushed the topic/7884 branch 2 times, most recently from 0125b16 to 491a536 Compare September 9, 2019 11:19
@Jasper-M
Copy link
Member Author

Jasper-M commented Sep 9, 2019

Added some tests based on @eed3si9n's comment. And rebased in the hopes of fixing MIMA failures in travis.

@Jasper-M
Copy link
Member Author

Jasper-M commented Sep 9, 2019

Ok, I get it now. MIMA complains because some apply and copy methods became private.

@Jasper-M
Copy link
Member Author

I put the change behind the -Xsource:2.14 flag, since this can't go in 2.13 anyway. That should also fix the MIMA issues.

Copy link
Member

@lrytz lrytz left a comment

Choose a reason for hiding this comment

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

This looks really good! Implementation, testing, all 👍.

I think this doesn't need a SIP.

My only concern is that it's a breaking change, but I cannot think of anything that we can do in 2.13 to aid / anticipate migration.

@Jasper-M Jasper-M changed the title Case class copy and apply inherit access modifiers from constructor [ci: last-only] Case class copy and apply inherit access modifiers from constructor Oct 10, 2019
@Jasper-M
Copy link
Member Author

Can this be considered for 2.13.2?

@lrytz
Copy link
Member

lrytz commented Oct 11, 2019

Under -Xsource:2.14 for sure. I didn't merge it so @adriaanm can take another look.

@adriaanm
Copy link
Contributor

Sorry for the delay. I will do my best to look soon. In principle it all looks good.

@adriaanm adriaanm requested review from retronym and removed request for adriaanm November 5, 2019 10:02
@adriaanm
Copy link
Contributor

adriaanm commented Nov 5, 2019

Sorry for lagging on this. I've asked Jason to take a look instead. He's been reworking this part of namers.

@SethTisue
Copy link
Member

@retronym do you intend to try to get this over the finish line for 2.13.2?

@SethTisue SethTisue modified the milestones: 2.13.2, 2.13.3 Feb 6, 2020
@lrytz
Copy link
Member

lrytz commented Feb 13, 2020

Let's merge this, it's looking good

@lrytz lrytz merged commit 11ae2a9 into scala:2.13.x Feb 13, 2020
@dwijnand dwijnand modified the milestones: 2.13.3, 2.13.2 Feb 13, 2020
@dwijnand dwijnand added the release-notes worth highlighting in next release notes label Feb 13, 2020
@SethTisue SethTisue changed the title Case class copy and apply inherit access modifiers from constructor Case class copy and apply inherit access modifiers from constructor (under -Xsource:2.14) Feb 13, 2020
@SethTisue SethTisue changed the title Case class copy and apply inherit access modifiers from constructor (under -Xsource:2.14) Case class copy and apply inherit access modifiers from constructor (under -Xsource:3) Apr 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-notes worth highlighting in next release notes
Projects
None yet
10 participants