Skip to content

Stop using Scala2 classfiles for parts of the standard library - #27621

Open
SolalPirelli wants to merge 1 commit into
scala:mainfrom
dotty-staging:solal/scala2-specialization-compatibility
Open

SolalPirelli wants to merge 1 commit into
scala:mainfrom
dotty-staging:solal/scala2-specialization-compatibility

Conversation

@SolalPirelli

@SolalPirelli SolalPirelli commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #27307

After this PR, bootstrapped compilation no longer uses Scala 2 compiler outputs.

Once this is ingested for the compiler used in nonbootstrapped compilation, we'll be able to remove the corresponding infrastructure.

Have you relied on LLM-based tools in this contribution?

Somewhat, to understand both codebases. I wrote the code myself.

How was the solution tested?

Covered by MiMa + existing standard library tests.

I did not add tests specifically for this because there is no other specification than "whatever makes MiMa happy". Scala2 specialization is known to have rough edges.

*
* @return a function `f` such that `f(x1)(x2)(x3)(x4)(x5)(x6)(x7)(x8)(x9)(x10) == apply(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10)`
*/
@annotation.unspecialized def curried: T1 => T2 => T3 => T4 => T5 => T6 => T7 => T8 => T9 => T10 => R = {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I removed these annotations where they had no effect, namely in classes that aren't specialized.

@SolalPirelli
SolalPirelli force-pushed the solal/scala2-specialization-compatibility branch 3 times, most recently from ec6479c to 5c884e6 Compare November 27, 2026 10:27
val (tmap1, params1) = (params: @unchecked) match
case ValDefs(vparams) => transformDefs(vparams)
case TypeDefs(tparams) => transformDefs(tparams)
): @unchecked

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This @unchecked wasn't working anymore. I suppose it's some fallout from using the Scala 3-compiled Tuple class.

else trackedParam
if paramInfo.hasAnnotation(defn.ConsumeAnnot) then CapturingType(tp, CaptureSet.universal)
else if tp.classSymbol == symd.owner.companionClass then info.paramRefs.head // def unapply(x: C): C = x
else tp // 2.13 extractors and 0-ary extractors

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fix suggested by Oliver.

val YappToMain: Setting[Boolean] = BooleanSetting(ForkSetting, "Yapp-to-main", "Rewrite `object X extends App { ... }` to an object with an explicit `main` method. Use in conjunction with --rewrite.")

// Temporary. Do not use outside of the stdlib.
val YcompilingStdlib: Setting[Boolean] = BooleanSetting(ForkSetting, "Ycompiling-stdlib", "Temporary. Do not use. Whether we are compiling the stdlib.")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Feature.shouldBehaveAsScala2 is not enough because it's dependent on the specific compilation unit; here, we want Tuple1/2 to be modified based on whether we will ourselves add the specializations or not, but their first access may be from a compilation unit that doesn't have the language.2.13 import.

def isRechecking: Boolean =
val phaseId = ctx.phaseId
0 <= phaseId && phaseId < 64 && (ctx.base.recheckPhaseIds & (1L << phaseId)) != 0
0 <= phaseId && phaseId < 64 && ctx.base.recheckPhaseIds.contains(phaseId)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Because this adds a couple phases, the Long encoding is no longer large enough, so this is a BitSet now.

*/
private def mightBeDropped(sym: Symbol)(using Context) =
sym.is(Private, butNot = MethodOrLazy) && !sym.isAllOf(MutableParamAccessor)
sym.is(Private, butNot = MethodOrLazy) && !sym.isAllOf(MutableParamAccessor) && sym.targetName == sym.name

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Don't drop a class member if it is intended to have a target name. I needed it for the manual Unit specialization of NonLocalReturnControl (see below), but this seems to make sense anyway.

// !!!
// REVIEW: why?
coord = spanCoord(t.span.endPos)
// !!!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This and the two instances below are odd (this is the least odd), and aren't even enough to pass all pickling tests as NonLocalReturnControl.scala has before/after-pickling diffs anyway.

Please tell me what to put here... or what else to do so pickling doesn't change their coords... I've spent too much time on this.

* of class `clazz` are met.
*/
def checkOverride(member: Symbol, other: Symbol): Unit =
def isInlinedFromInlineTrait = other.owner.isAllOf(InlineTrait) && member.is(Synthetic)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It generally seems better to not depend specifically on inline traits here anyway and just relax checks for synthetic members in general.

"apply$mcIII$sp",
"apply"),
s"Apply methods were not in the expected order: $apps"
s"Apply methods were not the expected ones: $apps"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure this test has much value now. MiMa will tell us if something is wrong anyway. Should we delete it?

@@ -0,0 +1,195 @@
/*

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copied from the Scala.js repo, except for the two .asInstanceOf[Object] casts added as required by explicit nulls.

@@ -0,0 +1,95 @@
/*

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copied from the Scala.js repo, with an added nowarn on line 17.

* @constructor Create a new tuple with 1 elements.
* @param _1 Element 1 of this Tuple1
*/
@SerialVersionUID(-2392256924707456902L)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

One of the community-build projects tests that Tuple2's serialization doesn't change, so I had to add a SerialVersionUID to it anyway, and I figured I might as well add the one for Tuple1.

Comment thread project/MiMaFilters.scala
// The private `Sorting.mergeSort` function has a different implementation detail
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.util.Sorting.scala$util$Sorting$$mergeSort$default$5"),
// https://github.com/scala/scala3/pull/26100 (it was a private object anyway)
ProblemFilters.exclude[MissingClassProblem]("scala.collection.immutable.LazyList$MidEvaluation$"),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm unsure why this is only appearing now since LazyList wasn't a class we copied from Scala2.

==
andThen
apply
apply$mcDD$sp

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This check output also contains the fake $asInstanceOf$ and $isInstanceOf$ so I think having these as well is fine.

// scalajs: --skip
// (uses reflection)

class C extends Function1[Int, Int]:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

As suggested by Hamza, a test to ensure inheriting from a Function is still fine.

@SolalPirelli
SolalPirelli force-pushed the solal/scala2-specialization-compatibility branch from 5c884e6 to 0ff5047 Compare November 27, 2026 10:38
@SolalPirelli
SolalPirelli force-pushed the solal/scala2-specialization-compatibility branch from 0ff5047 to 4f99f95 Compare November 27, 2026 10:55
@SolalPirelli
SolalPirelli marked this pull request as ready for review November 27, 2026 12:45

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is an old project intended for 2.12 and 2.11. It stops compiling with this PR because the better generic signatures introduced in 3.10 are actually used in every classfile now, and that causes compile-time errors from incorrect casts. It will still work at runtime anyway. Lukas and Piotr agreed we don't need to keep it in community-build.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Java subclasses of AbstractPartialFunction fail to compile against 3.10.0: inconsistent runWith generic signature

1 participant