Skip to content

Recover Scala 2's semantics for AnyVal under -Ycompile-scala2-library #22536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading