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

Ignore private constructors #778

Merged
merged 10 commits into from
Dec 11, 2023

Conversation

jeroentervoorde
Copy link
Contributor

I've added a classPrivate flag in the same fashion as the scopedPrivate flag and got it value from the pickled flags.
This works for normal classes with private constructors as demonstrated by the test but I could use some input if there are other constructs that would require additional unit tests.

Case classes work as well but they need the -Xsource:3 option and private unapply (as documented here) to pass.
I could add a test for that, either by making it a nok test or by adding some way to enable the -Xsource:3 option for that specific test.

I also enabled the doMethodOverloads in MimaUnpicker for constructors as well which was commented out with a todo.
A side effect is that private[...] constructors are now also ignored (using the existing isScopedPrivate flag) but I'm not actually sure this is the wanted behaviour. If not I propose to add a doConstructorOverloads that only checks the classPrivate flag and either keep doMethodOverloads as is or also include the check for classPrivate is done in the PR.

References #738

This enables the doMethodOverloads in MimaUnpicker for constructors as well which was commented out with a todo.
@jeroentervoorde jeroentervoorde changed the title Private constructors Ignore private constructors Aug 9, 2023
…on field on the case class.

This also fixed binary compatibility of mima-core.
@jeroentervoorde jeroentervoorde marked this pull request as draft August 10, 2023 14:22
@jeroentervoorde
Copy link
Contributor Author

jeroentervoorde commented Aug 11, 2023

The build failed due to binary compatibility problems caused by an additional field in DefDef in the TastyUnpickler.

I don't really see a way to avoid this. I've added the problems to the mima filters for now.
Not sure if DefDef is/should be part of the public api.

@jeroentervoorde jeroentervoorde marked this pull request as ready for review August 17, 2023 09:46
@SethTisue
Copy link
Collaborator

SethTisue commented Sep 10, 2023

I'm sorry we haven't found a reviewer for this yet. I know it's discouraging to work on something and then get weeks of silence. Sadly, MiMa is without a primary owner at present. It has had a series of them in the past, but they've moved on to other things.

If I can't find someone more knowledgeable than myself to review it before too much longer, I'll take a look and presumably optimistically-merge it before too much longer. (But not this week, busy with Scala Days.)

Copy link
Contributor

@julienrf julienrf left a comment

Choose a reason for hiding this comment

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

Thank you @jeroentervoorde for the pull request!

I am not familiar with the codebase but the approach looks good to me, although I am not sure about the changes in TastyUnpickler.scala. It would be good to get feedback from someone more experienced with the project.

I agree that adding a test with a case class and -Xsource:3 would be amazing!

Comment on lines 105 to 130
def copyClassPrivate(tree: Tree, pkgInfo: PackageInfo): Unit = new ClassTraverser(pkgInfo) {
override def forEachClass(clsDef: ClsDef, cls: ClassInfo): Unit = {
}

override def traverseTemplate(tmpl: Template): Unit = {
super.traverseTemplate(tmpl)
doMethods(tmpl)
}

def doMethods(tmpl: Template) = {
val clazz = currentClass
tmpl.meths.iterator
.toSeq.groupBy(_.name).foreach { case (name, pickleMethods) =>
doMethodOverloads(clazz, name, pickleMethods)
}
}

def doMethodOverloads(clazz: ClassInfo, name: Name, pickleMethods: Seq[DefDef]) = {
val bytecodeMethods = clazz.methods.get(name.source).filter(!_.isBridge).toList
if (pickleMethods.size == bytecodeMethods.size && pickleMethods.exists(_.classPrivate)) {
bytecodeMethods.zip(pickleMethods).foreach { case (bytecodeMeth, pickleMeth) =>
bytecodeMeth.classPrivate = pickleMeth.classPrivate
}
}
}
}.traverse(tree)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please explain why it is not possible to do the job in copyPrivateWithin? I am a bit lost.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I looked into it a bit more and actually was possible. The classPrivate flags are now also copied for ValDefs but I guess that's ok.

To make this work:
- Added option to skip tests (using skip-${binaryVersion}.txt)
- Added support for scala-options-${binaryVersion}.txt) which can be used to set -Xsource:3 (or other scalac options)
@jeroentervoorde
Copy link
Contributor Author

I did add a test for the private constructor change. I had to add some new functionality to the test runner though:

  • Tests can now be skipped by adding a "skip-${version}.txt" file.
  • Scalac options can be set using a "scalac-options-${version}.txt" file.

I also noticed that i did not get a warning for the unapply method in scala 3 as the signature doesn't actually change anymore (at least in my test). Is this always the case with scala 3?

@julienrf
Copy link
Contributor

I also noticed that i did not get a warning for the unapply method in scala 3 as the signature doesn't actually change anymore (at least in my test). Is this always the case with scala 3?

Yes. I think weird things could happen at run-time, but the type signature is the same.

@julienrf
Copy link
Contributor

julienrf commented Dec 7, 2023

@dwijnand would you be interested in reviewing this PR?

Copy link
Collaborator

@dwijnand dwijnand left a comment

Choose a reason for hiding this comment

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

LGTM, thank you @jeroentervoorde !

@dwijnand dwijnand merged commit 68f6dc6 into lightbend-labs:main Dec 11, 2023
11 checks passed
daddykotex added a commit to daddykotex/docs.scala-lang that referenced this pull request Jan 23, 2024
The comment is outdated as the issue was fixed in MiMa: lightbend-labs/mima#778
@SethTisue SethTisue mentioned this pull request Jun 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants