From a641aebfcb601a5769c650e09670f5bd0349b6a8 Mon Sep 17 00:00:00 2001 From: Hamza Remmal Date: Tue, 4 Feb 2025 22:24:59 +0100 Subject: [PATCH] chore: do not enter scala 2 extension methods and let dotty generate them --- .../dotc/core/unpickleScala2/Scala2Unpickler.scala | 10 +++++++++- .../dotty/tools/dotc/transform/ExtensionMethods.scala | 10 ++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala b/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala index 3b91312740d1..c5681abf20fd 100644 --- a/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala +++ b/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala @@ -535,7 +535,15 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas true) && // We discard the private val representing a case accessor. We only enter the case accessor def. // We do need to load these symbols to read properly unpickle the annotations on the symbol (see sbt-test/scala2-compat/i19421). - !flags.isAllOf(CaseAccessor | PrivateLocal, butNot = Method) + !flags.isAllOf(CaseAccessor | PrivateLocal, butNot = Method) && + // We don't enter Value Classes' extension methods from pickles, but we do + // generate in the ExtensionMethods phase, at the same time as we do for Scala 3 + // Same trick is used by tasty-query (see + //https://github.com/scalacenter/tasty-query/blob/fdefadcabb2f21d5c4b71f728b81c68f6fddcc0f/tasty-query/shared/src/main/scala/tastyquery/reader/pickles/PickleReader.scala#L261-L273 + //) + // This trick is also useful when reading the Scala 2 Standard library from tasty, since + // the extension methods will not be present, and it avoid having to distinguish between Scala2 pickles and Scala2 tasty (stdlib) + !(owner.is(ModuleClass) && sym.name.endsWith("$extension")) if (canEnter) owner.asClass.enter(sym, symScope(owner)) diff --git a/compiler/src/dotty/tools/dotc/transform/ExtensionMethods.scala b/compiler/src/dotty/tools/dotc/transform/ExtensionMethods.scala index f0d1c687df8e..cd78e6da36d7 100644 --- a/compiler/src/dotty/tools/dotc/transform/ExtensionMethods.scala +++ b/compiler/src/dotty/tools/dotc/transform/ExtensionMethods.scala @@ -74,12 +74,10 @@ class ExtensionMethods extends MiniPhase with DenotTransformer with FullParamete sym.validFor = thisPhase.validFor } - // Create extension methods, except if the class comes from Scala 2 - // because it adds extension methods before pickling. - if !valueClass.is(Scala2x, butNot = Scala2Tasty) then - for (decl <- valueClass.classInfo.decls) - if isMethodWithExtension(decl) then - enterInModuleClass(createExtensionMethod(decl, moduleClassSym.symbol)) + // Create extension methods + for (decl <- valueClass.classInfo.decls) + if isMethodWithExtension(decl) then + enterInModuleClass(createExtensionMethod(decl, moduleClassSym.symbol)) // Create synthetic methods to cast values between the underlying type // and the ErasedValueType. These methods are removed in ElimErasedValueType.