Skip to content

Commit

Permalink
Emit all classes as public to avoid object deserialization issues
Browse files Browse the repository at this point in the history
This aligns our behavior with Scala 2 and fixes the issue encountered in
typelevel/cats-effect#2360 (comment).

Alternatively, we could change ModuleSerializationProxy upstream to call
`setAccessible(true)` on the MODULE$ field, but this wouldn't work if the object
in question is inside a Java 9+ module.
  • Loading branch information
smarter committed Mar 14, 2022
1 parent 30ce9a5 commit 2f717a4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/backend/jvm/BTypesFromSymbols.scala
Expand Up @@ -296,7 +296,11 @@ class BTypesFromSymbols[I <: DottyBackendInterface](val int: I) extends BTypes {
*/
final def javaFlags(sym: Symbol): Int = {

val privateFlag = sym.is(Private) || (sym.isPrimaryConstructor && sym.owner.isTopLevelModuleClass)
// Classes are always emitted as public. This matches the behavior of Scala 2
// and is necessary for object deserialization to work properly, otherwise
// ModuleSerializationProxy may fail with an accessiblity error (see
// tests/run/serialize.scala).
val privateFlag = !sym.isClass && (sym.is(Private) || (sym.isPrimaryConstructor && sym.owner.isTopLevelModuleClass))

val finalFlag = sym.is(Final) && !toDenot(sym).isClassConstructor && !sym.is(Mutable) && !sym.enclosingClass.is(Trait)

Expand Down
12 changes: 4 additions & 8 deletions tests/run/i4404a.check
@@ -1,8 +1,4 @@
false
false
false
true
false
true
true
true
public class Car
public final class Volvo
public final class Car$
public static final class Car$$anon$1
12 changes: 5 additions & 7 deletions tests/run/i4404a.scala
Expand Up @@ -8,12 +8,10 @@ object Car {
}

object Test {
def main(args: Array[String]) = {
List(new Car, new Volvo, Car, Car.car)
.map(_.getClass.getModifiers)
.foreach { m =>
println(Modifier.isPrivate(m))
println(Modifier.isFinal(m))
}
def main(args: Array[String]): Unit = {
val l = List(new Car, new Volvo, Car, Car.car)
.map(_.getClass)
.map(cls => s"${Modifier.toString(cls.getModifiers)} $cls")
println(l.mkString("\n"))
}
}
2 changes: 1 addition & 1 deletion tests/run/i4404b.check
@@ -1,2 +1,2 @@
true
false
true
15 changes: 15 additions & 0 deletions tests/run/serialize.scala
@@ -1,3 +1,14 @@
package a {
object Outer extends Serializable {
private object Inner extends Serializable

val inner: AnyRef = Inner
}
class Bar extends Serializable {
val x: AnyRef = Outer.inner
}
}

object Test {
def serializeDeserialize[T <: AnyRef](obj: T): T = {
import java.io.*
Expand Down Expand Up @@ -26,5 +37,9 @@ object Test {

val baz = serializeDeserialize(Baz)
assert(baz ne Baz)

val bar = new a.Bar
val bar1 = serializeDeserialize(bar)
assert(bar.x eq bar1.x)
}
}

0 comments on commit 2f717a4

Please sign in to comment.