Skip to content

Commit

Permalink
Recover scala 2's semantics for AnyVal under `-Ycompile-scala2-librar…
Browse files Browse the repository at this point in the history
…y` (#22536)

Closes #22533
  • Loading branch information
hamzaremmal authored Feb 7, 2025
2 parents 7b39421 + 645e822 commit d60a914
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions compiler/test/dotty/tools/dotc/printing/PrintingTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
17 changes: 15 additions & 2 deletions project/Scala2LibraryBootstrappedMiMaFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,21 @@ object Scala2LibraryBootstrappedMiMaFilters {
ProblemFilters.exclude[FinalClassProblem]("scala.language$experimental$"),
ProblemFilters.exclude[FinalClassProblem]("scala.languageFeature$*$"),

// 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.*$"),
Expand Down
25 changes: 25 additions & 0 deletions tests/printing/posttyper/i22533.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[[syntax trees at end of posttyper]] // tests/printing/posttyper/i22533.scala
package <empty> {
@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])
}
}

1 change: 1 addition & 0 deletions tests/printing/posttyper/i22533.flags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-Ycompile-scala2-library
7 changes: 7 additions & 0 deletions tests/printing/posttyper/i22533.scala
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d60a914

Please sign in to comment.