Skip to content

Do not generate certain interface mixin forwarders if not necessary #23674

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/MixinOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,16 @@ class MixinOps(cls: ClassSymbol, thisPhase: DenotTransformer)(using Context) {
def generateSerializationForwarder: Boolean =
(meth.name == nme.readResolve || meth.name == nme.writeReplace) && meth.info.paramNamess.flatten.isEmpty

// Even when mixinForwarderChoices option is true, we might not want to generate certain
// mixin forwarders to avoid discrepancies between java getInterfaces() and getGenericInterfaces(),
// as adding some unnecessary mixin forwarders forces us to extend the `interfaces` array
// of the generated class in its classfile
def isJavaInterfaceIndirectlyExtended =
meth.owner.isAllOf(Flags.JavaInterface) && !cls.parentSyms.contains(meth.owner)

!meth.isConstructor &&
meth.is(Method, butNot = PrivateOrAccessorOrDeferred) &&
(ctx.settings.mixinForwarderChoices.isTruthy || meth.owner.is(Scala2x) || needsDisambiguation || hasNonInterfaceDefinition ||
((ctx.settings.mixinForwarderChoices.isTruthy && !isJavaInterfaceIndirectlyExtended)|| meth.owner.is(Scala2x) || needsDisambiguation || hasNonInterfaceDefinition ||
generateJUnitForwarder || generateSerializationForwarder) &&
isInImplementingClass(meth)
Copy link
Contributor

@som-snytt som-snytt Aug 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is some formatting at https://github.com/scala/scala3/pull/23482/files which is about sealed.

}
Expand Down
4 changes: 4 additions & 0 deletions tests/run/i21177a/BaseRootJava.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public interface BaseRootJava {
default void someMethod() {
}
}
2 changes: 2 additions & 0 deletions tests/run/i21177a/GenericBaseIntermidiateJava.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public interface GenericBaseIntermidiateJava<T> extends BaseRootJava {
}
12 changes: 12 additions & 0 deletions tests/run/i21177a/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class ChildScala extends GenericBaseIntermidiateJava[Int] {
// override def someMethod() = ???
}
object Test {
def main(args: Array[String]): Unit = {
val c = classOf[ChildScala]
assert(
c.getGenericInterfaces.length == c.getInterfaces.length,
s"mismatch between ${c.getGenericInterfaces.mkString("Array(", ", ", ")")} and ${c.getInterfaces.mkString("Array(", ", ", ")")}"
)
}
}
1 change: 1 addition & 0 deletions tests/run/i21177b/A.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
interface A { default int m() { return 1; } }
11 changes: 11 additions & 0 deletions tests/run/i21177b/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
abstract class B { def m(): Int }
trait T extends B with A
class C extends T

object Test {
def main(args: Array[String]): Unit = {
val cls = classOf[C]
assert(cls.getInterfaces().length == cls.getGenericInterfaces().length)
new C().m() // lack of the mixin forwarder for m() will crash this on runtime
}
}
Loading