From 7be5bfbb547f9d9980138f9602b927d210f85294 Mon Sep 17 00:00:00 2001 From: Hamza Remmal Date: Thu, 6 Feb 2025 10:57:58 +0100 Subject: [PATCH] chore: recover scala 2's semantics for AnyVal under Ycompile-scala2-library --- .../dotc/transform/SyntheticMembers.scala | 8 +++++- .../tools/dotc/printing/PrintingTest.scala | 3 +++ ...Scala2LibraryBootstrappedMiMaFilters.scala | 17 +++++++++++-- tests/printing/posttyper/i22533.check | 25 +++++++++++++++++++ tests/printing/posttyper/i22533.flags | 1 + tests/printing/posttyper/i22533.scala | 7 ++++++ 6 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 tests/printing/posttyper/i22533.check create mode 100644 tests/printing/posttyper/i22533.flags create mode 100644 tests/printing/posttyper/i22533.scala diff --git a/compiler/src/dotty/tools/dotc/transform/SyntheticMembers.scala b/compiler/src/dotty/tools/dotc/transform/SyntheticMembers.scala index 45606b0dbef5..376e43b3982d 100644 --- a/compiler/src/dotty/tools/dotc/transform/SyntheticMembers.scala +++ b/compiler/src/dotty/tools/dotc/transform/SyntheticMembers.scala @@ -78,7 +78,13 @@ class SyntheticMembers(thisPhase: DenotTransformer) { private def existingDef(sym: Symbol, clazz: ClassSymbol)(using Context): Symbol = val existing = sym.matchingMember(clazz.thisType) - if existing != sym && !existing.is(Deferred) then existing else NoSymbol + if ctx.settings.YcompileScala2Library.value && clazz.isValueClass && (sym == defn.Any_equals || sym == defn.Any_hashCode) then + NoSymbol + else if existing != sym && !existing.is(Deferred) then + existing + else + NoSymbol + end existingDef private def synthesizeDef(sym: TermSymbol, rhsFn: List[List[Tree]] => Context ?=> Tree)(using Context): Tree = DefDef(sym, rhsFn(_)(using ctx.withOwner(sym))).withSpan(ctx.owner.span.focus) diff --git a/compiler/test/dotty/tools/dotc/printing/PrintingTest.scala b/compiler/test/dotty/tools/dotc/printing/PrintingTest.scala index 8a80a6978bdb..15522d61e31f 100644 --- a/compiler/test/dotty/tools/dotc/printing/PrintingTest.scala +++ b/compiler/test/dotty/tools/dotc/printing/PrintingTest.scala @@ -69,6 +69,9 @@ class PrintingTest { @Test def printing: Unit = testIn("tests/printing", "typer") + @Test + def posttyper: Unit = testIn("tests/printing/posttyper", "posttyper") + @Test def untypedPrinting: Unit = testIn("tests/printing/untyped", "parser") diff --git a/project/Scala2LibraryBootstrappedMiMaFilters.scala b/project/Scala2LibraryBootstrappedMiMaFilters.scala index 102a2a50e9d4..65d24bcd96ca 100644 --- a/project/Scala2LibraryBootstrappedMiMaFilters.scala +++ b/project/Scala2LibraryBootstrappedMiMaFilters.scala @@ -18,8 +18,21 @@ object Scala2LibraryBootstrappedMiMaFilters { // trait $init$ ProblemFilters.exclude[DirectMissingMethodProblem]("scala.*.$init$"), - // Value class extension methods - ProblemFilters.exclude[DirectMissingMethodProblem]("scala.*$extension"), + // Issue: https://github.com/scala/scala3/issues/22495 + ProblemFilters.exclude[DirectMissingMethodProblem]("scala.collection.ArrayOps.scala$collection$ArrayOps$$elemTag$extension"), + ProblemFilters.exclude[DirectMissingMethodProblem]("scala.collection.ArrayOps.iterateUntilEmpty$extension"), + ProblemFilters.exclude[DirectMissingMethodProblem]("scala.collection.StringOps.isLineBreak$extension"), + ProblemFilters.exclude[DirectMissingMethodProblem]("scala.collection.StringOps.isLineBreak2$extension"), + ProblemFilters.exclude[DirectMissingMethodProblem]("scala.collection.StringOps.linesSeparated$extension"), + ProblemFilters.exclude[DirectMissingMethodProblem]("scala.collection.StringOps.escape$extension"), + ProblemFilters.exclude[DirectMissingMethodProblem]("scala.collection.StringOps.toBooleanImpl$extension"), + ProblemFilters.exclude[DirectMissingMethodProblem]("scala.collection.StringOps.unwrapArg$extension"), + ProblemFilters.exclude[DirectMissingMethodProblem]("scala.collection.StringOps.iterateUntilEmpty$extension"), + ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.Tuple2Zipped.coll1$extension"), + ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.Tuple2Zipped.coll2$extension"), + ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.Tuple3Zipped.coll1$extension"), + ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.Tuple3Zipped.coll2$extension"), + ProblemFilters.exclude[DirectMissingMethodProblem]("scala.runtime.Tuple3Zipped.coll3$extension"), // Companion module class ProblemFilters.exclude[FinalClassProblem]("scala.*$"), diff --git a/tests/printing/posttyper/i22533.check b/tests/printing/posttyper/i22533.check new file mode 100644 index 000000000000..33c023d94a74 --- /dev/null +++ b/tests/printing/posttyper/i22533.check @@ -0,0 +1,25 @@ +[[syntax trees at end of posttyper]] // tests/printing/posttyper/i22533.scala +package { + @SourceFile("tests/printing/posttyper/i22533.scala") trait A() extends Any { + override def equals(x: Any): Boolean = ??? + override def hashCode(): Int = ??? + } + @SourceFile("tests/printing/posttyper/i22533.scala") final class Foo(u: Int) + extends AnyVal(), A { + override def hashCode(): Int = Foo.this.u.hashCode() + override def equals(x$0: Any): Boolean = + x$0 match + { + case x$0 @ _:Foo @unchecked => this.u.==(x$0.u) + case _ => false + } + private[this] val u: Int + } + final lazy module val Foo: Foo = new Foo() + @SourceFile("tests/printing/posttyper/i22533.scala") final module class Foo() + extends AnyRef() { this: Foo.type => + private def writeReplace(): AnyRef = + new scala.runtime.ModuleSerializationProxy(classOf[Foo.type]) + } +} + diff --git a/tests/printing/posttyper/i22533.flags b/tests/printing/posttyper/i22533.flags new file mode 100644 index 000000000000..21379f85d52a --- /dev/null +++ b/tests/printing/posttyper/i22533.flags @@ -0,0 +1 @@ +-Ycompile-scala2-library diff --git a/tests/printing/posttyper/i22533.scala b/tests/printing/posttyper/i22533.scala new file mode 100644 index 000000000000..07e9e1c4c011 --- /dev/null +++ b/tests/printing/posttyper/i22533.scala @@ -0,0 +1,7 @@ +//> using options -Ycompile-scala2-library + +trait A extends Any: + override def equals(x: Any): Boolean = ??? + override def hashCode(): Int = ??? + +class Foo(u: Int) extends AnyVal, A \ No newline at end of file