Skip to content

Commit 7faaa24

Browse files
Adress comments
1 parent 28df04e commit 7faaa24

File tree

7 files changed

+30
-32
lines changed

7 files changed

+30
-32
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,7 @@ class Definitions {
829829
@tu lazy val FunctionalInterfaceAnnot: ClassSymbol = ctx.requiredClass("java.lang.FunctionalInterface")
830830
@tu lazy val InfixAnnot: ClassSymbol = ctx.requiredClass("scala.annotation.infix")
831831
@tu lazy val AlphaAnnot: ClassSymbol = ctx.requiredClass("scala.annotation.alpha")
832+
@tu lazy val VarargsAnnot: ClassSymbol = ctx.requiredClass("scala.annotation.varargs")
832833

833834
// A list of annotations that are commonly used to indicate that a field/method argument or return
834835
// type is not null. These annotations are used by the nullification logic in JavaNullInterop to

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ object ElimRepeated {
2626
class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
2727
import ast.tpd._
2828

29-
private val varargsAnnot = new CtxLazy(
30-
summon[Context].requiredClass("scala.annotation.varargs"))
31-
3229
override def phaseName: String = ElimRepeated.name
3330

3431
override def changesMembers: Boolean = true // the phase adds vararg bridges
@@ -49,7 +46,7 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
4946

5047
private def overridesJava(sym: Symbol)(using Context) = sym.allOverriddenSymbols.exists(_.is(JavaDefined))
5148

52-
private def hasVargsAnnotation(sym: Symbol)(using ctx: Context) = sym.hasAnnotation(varargsAnnot())
49+
private def hasVargsAnnotation(sym: Symbol)(using ctx: Context) = sym.hasAnnotation(defn.VarargsAnnot)
5350

5451
/** Eliminate repeated parameters from method types. */
5552
private def elimRepeated(tp: Type)(using Context): Type = tp.stripTypeVar match
@@ -120,17 +117,15 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
120117
val isOverride = overridesJava(tree.symbol)
121118
val hasAnnotation = hasVargsAnnotation(tree.symbol)
122119
if tree.symbol.info.isVarArgsMethod && (isOverride || hasAnnotation) then
123-
// java varargs can be overriden by synthetic methods with an Array param,
124-
// but non-overrides need the varargs bytecode flag and cannot be synthetic
120+
// non-overrides need the varargs bytecode flag and cannot be synthetic
125121
// otherwise javac refuses to call them.
126-
addVarArgsBridge(tree, if hasAnnotation then JavaVarargs else Artifact)
122+
addVarArgsBridge(tree, isOverride)
127123
else
128124
tree
129125
}
130126

131127
/** Add a Java varargs bridge
132-
* @param ddef the original method definition which is assumed to override
133-
* a Java varargs method JM up to this phase.
128+
* @param ddef the original method definition
134129
* @param addFlag the flag to add to the method symbol
135130
136131
* @return a thicket consisting of `ddef` and a varargs bridge method
@@ -143,10 +138,12 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
143138
* The solution is to add a "bridge" method that converts its argument from `Array[? <: T]` to `Seq[T]` and
144139
* forwards it to `ddef`.
145140
*/
146-
private def addVarArgsBridge(ddef: DefDef, addFlag: Flag)(using Context): Tree =
141+
private def addVarArgsBridge(ddef: DefDef, synthetic: Boolean)(using Context): Tree =
147142
val original = ddef.symbol.asTerm
143+
// For simplicity we always set the varargs flag
144+
val flags = ddef.symbol.flags | JavaVarargs &~ Private
148145
val bridge = original.copy(
149-
flags = (ddef.symbol.flags | addFlag) &~ Private,
146+
flags = if synthetic then flags | Artifact else flags,
150147
info = toJavaVarArgs(ddef.symbol.info)
151148
).enteredAfter(thisPhase).asTerm
152149

tests/pos-java-interop/i7212/C.scala

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/pos-java-interop/i7212/UseFromJava.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/pos-java-interop/i7212/UseFromScala.scala

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/run/i7212/Test.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public class Test {
2+
public static void main(String[] args) {
3+
C c = new C();
4+
c.vargs("single");
5+
c.vargs("a", "b");
6+
c.vargs(new String[]{"a", "b"});
7+
c.vargsFromScala();
8+
}
9+
}

tests/run/i7212/Varargs.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import scala.annotation._
2+
3+
class C {
4+
@varargs
5+
def vargs(args: String*): Unit = println(args)
6+
7+
def vargsFromScala(): Unit =
8+
vargs("single")
9+
vargs("a", "b")
10+
vargs(Seq("a", "b"): _*)
11+
12+
}

0 commit comments

Comments
 (0)