Skip to content

Commit 50498d8

Browse files
Reinterpret Scala 2 case accessors xyz$access$idx (#18907)
Fixes #18884 Related to #18874
2 parents 3b974c5 + b035f76 commit 50498d8

File tree

8 files changed

+73
-14
lines changed

8 files changed

+73
-14
lines changed

Diff for: compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala

+8
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,14 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas
462462
flags &~= Method | Accessor
463463
if !flags.is(StableRealizable) then flags |= Mutable
464464

465+
// Skip case accessor `<xyz>$access$<idx>` and keep track of their name to make `<xyz>` the case accessor
466+
if flags.is(CaseAccessor) && name.toString().contains("$access$") then
467+
val accessorName = name.toString().split('$').head.toTermName // <xyz>
468+
symScope(owner) // we assume that the `<xyz>` is listed before the accessor and hence is already entered in the scope
469+
.find(decl => decl.isAllOf(ParamAccessor) && decl.name == accessorName)
470+
.setFlag(CaseAccessor)
471+
return NoSymbol // skip this member
472+
465473
name = name.adjustIfModuleClass(flags)
466474
if (flags.is(Method))
467475
name =

Diff for: compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ class DottyBytecodeTests extends DottyBytecodeTest {
10581058
TypeOp(CHECKCAST, "scala/collection/immutable/$colon$colon"),
10591059
VarOp(ASTORE, 3),
10601060
VarOp(ALOAD, 3),
1061-
Invoke(INVOKEVIRTUAL, "scala/collection/immutable/$colon$colon", "next$access$1", "()Lscala/collection/immutable/List;", false),
1061+
Invoke(INVOKEVIRTUAL, "scala/collection/immutable/$colon$colon", "next", "()Lscala/collection/immutable/List;", false),
10621062
VarOp(ASTORE, 4),
10631063
VarOp(ALOAD, 3),
10641064
Invoke(INVOKEVIRTUAL, "scala/collection/immutable/$colon$colon", "head", "()Ljava/lang/Object;", false),
@@ -1112,7 +1112,7 @@ class DottyBytecodeTests extends DottyBytecodeTest {
11121112
Invoke(INVOKESTATIC, "scala/runtime/BoxesRunTime", "unboxToInt", "(Ljava/lang/Object;)I", false),
11131113
VarOp(ISTORE, 4),
11141114
VarOp(ALOAD, 3),
1115-
Invoke(INVOKEVIRTUAL, "scala/collection/immutable/$colon$colon", "next$access$1", "()Lscala/collection/immutable/List;", false),
1115+
Invoke(INVOKEVIRTUAL, "scala/collection/immutable/$colon$colon", "next", "()Lscala/collection/immutable/List;", false),
11161116
VarOp(ASTORE, 5),
11171117
Op(ICONST_1),
11181118
VarOp(ILOAD, 4),

Diff for: compiler/test/dotty/tools/vulpix/ParallelTesting.scala

+30-9
Original file line numberDiff line numberDiff line change
@@ -610,17 +610,38 @@ trait ParallelTesting extends RunnerOrchestration { self =>
610610
.run()
611611
.mkString(JFile.pathSeparator)
612612

613-
val stdlibClasspath = artifactClasspath("org.scala-lang", "scala3-library_3")
614-
val scalacClasspath = artifactClasspath("org.scala-lang", "scala3-compiler_3")
615-
616613
val pageWidth = TestConfiguration.pageWidth - 20
617-
val flags1 = flags.copy(defaultClassPath = stdlibClasspath)
618-
.withClasspath(targetDir.getPath)
619-
.and("-d", targetDir.getPath)
620-
.and("-pagewidth", pageWidth.toString)
621614

622-
val scalacCommand = Array("java", "-cp", scalacClasspath, "dotty.tools.dotc.Main")
623-
val command = scalacCommand ++ flags1.all ++ files.map(_.getAbsolutePath)
615+
val fileArgs = files.map(_.getAbsolutePath)
616+
617+
def scala2Command(): Array[String] = {
618+
assert(!flags.options.contains("-scalajs"),
619+
"Compilation tests with Scala.js on Scala 2 are not supported.\nThis test can be skipped using the `// scalajs: --skip` tag")
620+
val stdlibClasspath = artifactClasspath("org.scala-lang", "scala-library")
621+
val scalacClasspath = artifactClasspath("org.scala-lang", "scala-compiler")
622+
val flagsArgs = flags
623+
.copy(options = Array.empty, defaultClassPath = stdlibClasspath)
624+
.withClasspath(targetDir.getPath)
625+
.and("-d", targetDir.getPath)
626+
.all
627+
val scalacCommand = Array("java", "-cp", scalacClasspath, "scala.tools.nsc.Main")
628+
scalacCommand ++ flagsArgs ++ fileArgs
629+
}
630+
631+
def scala3Command(): Array[String] = {
632+
val stdlibClasspath = artifactClasspath("org.scala-lang", "scala3-library_3")
633+
val scalacClasspath = artifactClasspath("org.scala-lang", "scala3-compiler_3")
634+
val flagsArgs = flags
635+
.copy(defaultClassPath = stdlibClasspath)
636+
.withClasspath(targetDir.getPath)
637+
.and("-d", targetDir.getPath)
638+
.and("-pagewidth", pageWidth.toString)
639+
.all
640+
val scalacCommand = Array("java", "-cp", scalacClasspath, "dotty.tools.dotc.Main")
641+
scalacCommand ++ flagsArgs ++ fileArgs
642+
}
643+
644+
val command = if compiler.startsWith("2") then scala2Command() else scala3Command()
624645
val process = Runtime.getRuntime.exec(command)
625646

626647
val reporter = mkReporter

Diff for: tests/neg/i18884.scala

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def test(xs: ::[Int]): List[Int] =
2+
xs.next$access$1 // error

Diff for: tests/run/i18884.check

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Foo1(1)
2+
Foo2(2, 3)
3+
Foo3(4, 5)
4+
Foo4(6, 7)

Diff for: tests/run/i18884/A_1_c2.13.12.scala

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// scalajs: --skip
2+
3+
package lib
4+
5+
case class Foo1(private[lib] var x: Int) {}
6+
case class Foo2(private[lib] var x: Int, private[lib] var y: Int)
7+
case class Foo3(private[lib] var x: Int, var y: Int)
8+
case class Foo4(var x: Int, private[lib] var y: Int) {
9+
val z: Int = x
10+
}

Diff for: tests/run/i18884/B_2.scala

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import lib.*
2+
3+
@main def Test: Unit =
4+
test(new Foo1(1))
5+
test(new Foo2(2, 3))
6+
test(new Foo3(4, 5))
7+
test(new Foo4(6, 7))
8+
9+
def test(any: Any): Unit =
10+
any match
11+
case Foo1(x) => println(s"Foo1($x)")
12+
case Foo2(x, y) => println(s"Foo2($x, $y)")
13+
case Foo3(x, y) => println(s"Foo3($x, $y)")
14+
case Foo4(x, y) => println(s"Foo4($x, $y)")

Diff for: tests/run/typeclass-derivation3.check

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ Cons(hd = Cons(hd = 11, tl = Cons(hd = 22, tl = Cons(hd = 33, tl = Nil))), tl =
99
Cons(hd = Left(x = 1), tl = Cons(hd = Right(x = Pair(x = 2, y = 3)), tl = Nil))
1010
Cons(hd = Left(x = 1), tl = Cons(hd = Right(x = Pair(x = 2, y = 3)), tl = Nil))
1111
true
12-
::(head = 1, next$access$1 = ::(head = 2, next$access$1 = ::(head = 3, next$access$1 = Nil())))
13-
::(head = ::(head = 1, next$access$1 = Nil()), next$access$1 = ::(head = ::(head = 2, next$access$1 = ::(head = 3, next$access$1 = Nil())), next$access$1 = Nil()))
14-
::(head = Nil(), next$access$1 = ::(head = ::(head = 1, next$access$1 = Nil()), next$access$1 = ::(head = ::(head = 2, next$access$1 = ::(head = 3, next$access$1 = Nil())), next$access$1 = Nil())))
12+
::(head = 1, next = ::(head = 2, next = ::(head = 3, next = Nil())))
13+
::(head = ::(head = 1, next = Nil()), next = ::(head = ::(head = 2, next = ::(head = 3, next = Nil())), next = Nil()))
14+
::(head = Nil(), next = ::(head = ::(head = 1, next = Nil()), next = ::(head = ::(head = 2, next = ::(head = 3, next = Nil())), next = Nil())))

0 commit comments

Comments
 (0)