Skip to content

Commit 9066923

Browse files
authored
Do not enter Scala 2 extension methods and let dotty "generate" them (#22519)
Scala 2 generates extension methods for Value Classes before pickling while Scala 3 generates them after. Now that we are working on compiling the Scala 2 stdlib with dotty (#22480), and therefore generating tasty files for the stdlib, we will have to distinguish between scala 2 that was unpickled and scala 2 code coming from tasty. For that, we currently have a flag, but it is not very useful apart from some very specific details where a workaround exists. This PR, removes one of 2 places where this flag is used. The [second place](https://github.com/scala/scala3/blob/aa9db1f682de634743af6d7077d5ed752430a487/compiler/src/dotty/tools/dotc/transform/YCheckPositions.scala#L51) will be removed once we inline the missing symbol in `Predef.scala` (after #22480) is merged.
2 parents f637591 + 6029f68 commit 9066923

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala

+8-1
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,14 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas
535535
true) &&
536536
// We discard the private val representing a case accessor. We only enter the case accessor def.
537537
// We do need to load these symbols to read properly unpickle the annotations on the symbol (see sbt-test/scala2-compat/i19421).
538-
!flags.isAllOf(CaseAccessor | PrivateLocal, butNot = Method)
538+
!flags.isAllOf(CaseAccessor | PrivateLocal, butNot = Method) &&
539+
// Skip entering extension methods: they will be recreated by the ExtensionMethods phase.
540+
// Same trick is used by tasty-query (see
541+
//https://github.com/scalacenter/tasty-query/blob/fdefadcabb2f21d5c4b71f728b81c68f6fddcc0f/tasty-query/shared/src/main/scala/tastyquery/reader/pickles/PickleReader.scala#L261-L273
542+
//)
543+
// This trick is also useful when reading the Scala 2 Standard library from tasty, since
544+
// the extension methods will not be present, and it avoid having to distinguish between Scala2 pickles and Scala2 tasty (stdlib)
545+
!(owner.is(ModuleClass) && sym.name.endsWith("$extension"))
539546

540547
if (canEnter)
541548
owner.asClass.enter(sym, symScope(owner))

compiler/src/dotty/tools/dotc/transform/ExtensionMethods.scala

+4-6
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,10 @@ class ExtensionMethods extends MiniPhase with DenotTransformer with FullParamete
7474
sym.validFor = thisPhase.validFor
7575
}
7676

77-
// Create extension methods, except if the class comes from Scala 2
78-
// because it adds extension methods before pickling.
79-
if !valueClass.is(Scala2x, butNot = Scala2Tasty) then
80-
for (decl <- valueClass.classInfo.decls)
81-
if isMethodWithExtension(decl) then
82-
enterInModuleClass(createExtensionMethod(decl, moduleClassSym.symbol))
77+
// Create extension methods
78+
for (decl <- valueClass.classInfo.decls)
79+
if isMethodWithExtension(decl) then
80+
enterInModuleClass(createExtensionMethod(decl, moduleClassSym.symbol))
8381

8482
// Create synthetic methods to cast values between the underlying type
8583
// and the ErasedValueType. These methods are removed in ElimErasedValueType.

0 commit comments

Comments
 (0)