Skip to content

Commit b61eb2e

Browse files
authored
Merge pull request #13066 from dotty-staging/fix-13043
Handle overloaded members when generating Java varargs bridges
2 parents 27e221b + abf4b45 commit b61eb2e

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

compiler/src/dotty/tools/dotc/transform/ElimRepeated.scala

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package dotty.tools.dotc
1+
package dotty.tools
2+
package dotc
23
package transform
34

45
import core._
@@ -76,16 +77,32 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
7677

7778
override def infoMayChange(sym: Symbol)(using Context): Boolean = sym.is(Method)
7879

79-
private def overridesJava(sym: Symbol)(using Context) = sym.allOverriddenSymbols.exists(_.is(JavaDefined))
80+
/** Does `sym` override a symbol defined in a Java class? One might think that
81+
* this can be expressed as
82+
*
83+
* sym.allOverriddenSymbols.exists(_.is(JavaDefined))
84+
*
85+
* but that does not work, since `allOverriddenSymbols` gets confused because the
86+
* signatures of a Java varargs method and a Scala varargs override are not the same.
87+
*/
88+
private def overridesJava(sym: Symbol)(using Context) =
89+
sym.owner.info.baseClasses.drop(1).exists { bc =>
90+
bc.is(JavaDefined) && {
91+
val other = bc.info.nonPrivateDecl(sym.name)
92+
other.hasAltWith { alt =>
93+
sym.owner.thisType.memberInfo(alt.symbol).matchesLoosely(sym.info)
94+
}
95+
}
96+
}
8097

8198
private def hasVarargsAnnotation(sym: Symbol)(using Context) = sym.hasAnnotation(defn.VarargsAnnot)
8299

83100
private def parentHasVarargsAnnotation(sym: Symbol)(using Context) = sym.allOverriddenSymbols.exists(hasVarargsAnnotation)
84101

85102
private def isVarargsMethod(sym: Symbol)(using Context) =
86-
hasVarargsAnnotation(sym) ||
87-
hasRepeatedParams(sym) &&
88-
(sym.allOverriddenSymbols.exists(s => s.is(JavaDefined) || hasVarargsAnnotation(s)))
103+
hasVarargsAnnotation(sym)
104+
|| hasRepeatedParams(sym)
105+
&& (overridesJava(sym) || sym.allOverriddenSymbols.exists(hasVarargsAnnotation))
89106

90107
/** Eliminate repeated parameters from method types. */
91108
private def elimRepeated(tp: Type, isJava: Boolean)(using Context): Type = tp.stripTypeVar match
@@ -292,6 +309,7 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
292309
report.error(s"$src produces a forwarder method that conflicts with ${conflict.showDcl}", original.srcPos)
293310
case Nil =>
294311
forwarder.enteredAfter(thisPhase)
312+
end addVarArgsForwarder
295313

296314
/** Convert type from Scala to Java varargs method */
297315
private def toJavaVarArgs(tp: Type)(using Context): Type = tp match

tests/pos/i13043/Impl.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Impl extends Intf {
2+
override def thing(x: Int) = ???
3+
override def thing(y: String*) = ???
4+
override def thing2(y: String*) = ???
5+
}

tests/pos/i13043/Intf.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
interface Intf {
2+
public void thing(int x);
3+
public void thing(String... y);
4+
public void thing2(String... y);
5+
}

0 commit comments

Comments
 (0)