Skip to content
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

Emit all classes as public to avoid object deserialization issues #14686

Merged
merged 1 commit into from
Mar 21, 2022
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
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/backend/jvm/BTypesFromSymbols.scala
Original file line number Diff line number Diff line change
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 and https://github.com/typelevel/cats-effect/pull/2360).
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
7 changes: 6 additions & 1 deletion project/MiMaFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ object MiMaFilters {
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.deriving.Mirror.fromTuple"),

// Private to the compiler - needed for forward binary compatibility
ProblemFilters.exclude[MissingClassProblem]("scala.annotation.since")
ProblemFilters.exclude[MissingClassProblem]("scala.annotation.since"),

// Private inner classes, but we emit all classes as public in Java bytecode
ProblemFilters.exclude[InaccessibleClassProblem]("scala.quoted.FromExpr$PrimitiveFromExpr"),
ProblemFilters.exclude[InaccessibleClassProblem]("scala.quoted.Type$ValueOf$"),
ProblemFilters.exclude[InaccessibleClassProblem]("scala.reflect.Selectable$DefaultSelectable"),
)
}
12 changes: 4 additions & 8 deletions tests/run/i4404a.check
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
true
false
true
15 changes: 15 additions & 0 deletions tests/run/serialize.scala
Original file line number Diff line number Diff line change
@@ -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)
}
}