@@ -33,91 +33,89 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
33
33
34
34
override def changesMembers : Boolean = true // the phase adds vararg bridges
35
35
36
- def transformInfo (tp : Type , sym : Symbol )(implicit ctx : Context ): Type =
36
+ def transformInfo (tp : Type , sym : Symbol )(using Context ): Type =
37
37
elimRepeated(tp)
38
38
39
- override def transform (ref : SingleDenotation )(implicit ctx : Context ): SingleDenotation =
40
- super .transform(ref) match {
39
+ override def transform (ref : SingleDenotation )(using Context ): SingleDenotation =
40
+ super .transform(ref) match
41
41
case ref1 : SymDenotation if (ref1 ne ref) && overridesJava(ref1.symbol) =>
42
42
// This method won't override the corresponding Java method at the end of this phase,
43
43
// only the bridge added by `addVarArgsBridge` will.
44
44
ref1.copySymDenotation(initFlags = ref1.flags &~ Override )
45
45
case ref1 =>
46
46
ref1
47
- }
48
47
49
- override def mayChange (sym : Symbol )(implicit ctx : Context ): Boolean = sym.is(Method )
48
+ override def mayChange (sym : Symbol )(using Context ): Boolean = sym.is(Method )
50
49
51
- private def overridesJava (sym : Symbol )(implicit ctx : Context ) = sym.allOverriddenSymbols.exists(_.is(JavaDefined ))
50
+ private def overridesJava (sym : Symbol )(using Context ) = sym.allOverriddenSymbols.exists(_.is(JavaDefined ))
52
51
53
52
private def hasVargsAnnotation (sym : Symbol )(using ctx : Context ) = sym.hasAnnotation(varargsAnnot())
54
53
55
- private def elimRepeated (tp : Type )(implicit ctx : Context ): Type = tp.stripTypeVar match {
54
+ /** Eliminate repeated parameters from method types. */
55
+ private def elimRepeated (tp : Type )(using Context ): Type = tp.stripTypeVar match
56
56
case tp @ MethodTpe (paramNames, paramTypes, resultType) =>
57
57
val resultType1 = elimRepeated(resultType)
58
58
val paramTypes1 =
59
- if ( paramTypes.nonEmpty && paramTypes.last.isRepeatedParam) {
59
+ if paramTypes.nonEmpty && paramTypes.last.isRepeatedParam then
60
60
val last = paramTypes.last.translateFromRepeated(toArray = tp.isJavaMethod)
61
61
paramTypes.init :+ last
62
- }
63
62
else paramTypes
64
63
tp.derivedLambdaType(paramNames, paramTypes1, resultType1)
65
64
case tp : PolyType =>
66
65
tp.derivedLambdaType(tp.paramNames, tp.paramInfos, elimRepeated(tp.resultType))
67
66
case tp =>
68
67
tp
69
- }
70
68
71
- def transformTypeOfTree (tree : Tree )(implicit ctx : Context ): Tree =
69
+ def transformTypeOfTree (tree : Tree )(using Context ): Tree =
72
70
tree.withType(elimRepeated(tree.tpe))
73
71
74
- override def transformIdent (tree : Ident )(implicit ctx : Context ): Tree =
72
+ override def transformIdent (tree : Ident )(using Context ): Tree =
75
73
transformTypeOfTree(tree)
76
74
77
- override def transformSelect (tree : Select )(implicit ctx : Context ): Tree =
75
+ override def transformSelect (tree : Select )(using Context ): Tree =
78
76
transformTypeOfTree(tree)
79
77
80
- override def transformApply (tree : Apply )(implicit ctx : Context ): Tree = {
78
+ override def transformApply (tree : Apply )(using Context ): Tree =
81
79
val args = tree.args.mapConserve {
82
80
case arg : Typed if isWildcardStarArg(arg) =>
83
81
val isJavaDefined = tree.fun.symbol.is(JavaDefined )
84
82
val tpe = arg.expr.tpe
85
- if ( isJavaDefined && tpe.derivesFrom(defn.SeqClass ))
83
+ if isJavaDefined && tpe.derivesFrom(defn.SeqClass ) then
86
84
seqToArray(arg.expr)
87
- else if ( ! isJavaDefined && tpe.derivesFrom(defn.ArrayClass ) )
85
+ else if ! isJavaDefined && tpe.derivesFrom(defn.ArrayClass )
88
86
arrayToSeq(arg.expr)
89
87
else
90
88
arg.expr
91
89
case arg => arg
92
90
}
93
91
transformTypeOfTree(cpy.Apply (tree)(tree.fun, args))
94
- }
95
92
96
93
/** Convert sequence argument to Java array */
97
- private def seqToArray (tree : Tree )(implicit ctx : Context ): Tree = tree match {
94
+ private def seqToArray (tree : Tree )(using Context ): Tree = tree match
98
95
case SeqLiteral (elems, elemtpt) =>
99
96
JavaSeqLiteral (elems, elemtpt)
100
97
case _ =>
101
98
val elemType = tree.tpe.elemType
102
99
var elemClass = erasure(elemType).classSymbol
103
- if (defn.NotRuntimeClasses .contains(elemClass)) elemClass = defn.ObjectClass
100
+ if defn.NotRuntimeClasses .contains(elemClass) then
101
+ elemClass = defn.ObjectClass
102
+ end if
104
103
ref(defn.DottyArraysModule )
105
104
.select(nme.seqToArray)
106
105
.appliedToType(elemType)
107
106
.appliedTo(tree, clsOf(elemClass.typeRef))
108
- }
109
107
110
108
/** Convert Java array argument to Scala Seq */
111
- private def arrayToSeq (tree : Tree )(implicit ctx : Context ): Tree =
109
+ private def arrayToSeq (tree : Tree )(using Context ): Tree =
112
110
tpd.wrapArray(tree, tree.tpe.elemType)
113
111
114
- override def transformTypeApply (tree : TypeApply )(implicit ctx : Context ): Tree =
112
+ override def transformTypeApply (tree : TypeApply )(using Context ): Tree =
115
113
transformTypeOfTree(tree)
116
114
117
115
/** If method overrides a Java varargs method or is annotated with @varargs, add a varargs bridge.
118
116
* Also transform trees inside method annotation.
119
117
*/
120
- override def transformDefDef (tree : DefDef )(implicit ctx : Context ): Tree =
118
+ override def transformDefDef (tree : DefDef )(using ctx : Context ): Tree =
121
119
ctx.atPhase(thisPhase) {
122
120
val isOverride = overridesJava(tree.symbol)
123
121
val hasAnnotation = hasVargsAnnotation(tree.symbol)
@@ -133,23 +131,25 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
133
131
/** Add a Java varargs bridge
134
132
* @param ddef the original method definition which is assumed to override
135
133
* a Java varargs method JM up to this phase.
136
- * @param addFlag the flag to add to the method definition
134
+ * @param addFlag the flag to add to the method symbol
137
135
138
136
* @return a thicket consisting of `ddef` and a varargs bridge method
139
137
* which forwards java varargs to `ddef`. It retains all the
140
138
* flags of `ddef` except `Private`.
141
139
*
142
- * A bridge is necessary because the following hold
140
+ * A bridge is necessary because the following hold:
143
141
* - the varargs in `ddef` will change from `RepeatedParam[T]` to `Seq[T]` after this phase
144
142
* - _but_ the callers of `ddef` expect its varargs to be changed to `Array[? <: T]`
145
143
* The solution is to add a "bridge" method that converts its argument from `Array[? <: T]` to `Seq[T]` and
146
144
* forwards it to `ddef`.
147
145
*/
148
- private def addVarArgsBridge (ddef : DefDef , addFlag : Flag )(implicit ctx : Context ): Tree = {
146
+ private def addVarArgsBridge (ddef : DefDef , addFlag : Flag )(using Context ): Tree =
149
147
val original = ddef.symbol.asTerm
150
148
val bridge = original.copy(
151
- flags = (ddef.symbol.flags | addFlag) &~ Private ,
152
- info = toJavaVarArgs(ddef.symbol.info)).enteredAfter(thisPhase).asTerm
149
+ flags = (ddef.symbol.flags | addFlag) &~ Private ,
150
+ info = toJavaVarArgs(ddef.symbol.info)
151
+ ).enteredAfter(thisPhase).asTerm
152
+
153
153
val bridgeDef = polyDefDef(bridge, trefs => vrefss => {
154
154
val (vrefs :+ varArgRef) :: vrefss1 = vrefss
155
155
// Can't call `.argTypes` here because the underlying array type is of the
@@ -162,15 +162,14 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
162
162
})
163
163
164
164
Thicket (ddef, bridgeDef)
165
- }
166
165
167
166
/** Convert type from Scala to Java varargs method */
168
- private def toJavaVarArgs (tp : Type )(implicit ctx : Context ): Type = tp match {
167
+ private def toJavaVarArgs (tp : Type )(using Context ): Type = tp match
169
168
case tp : PolyType =>
170
169
tp.derivedLambdaType(tp.paramNames, tp.paramInfos, toJavaVarArgs(tp.resultType))
171
170
case tp : MethodType =>
172
171
val inits :+ last = tp.paramInfos
173
172
val last1 = last.translateFromRepeated(toArray = true )
174
173
tp.derivedLambdaType(tp.paramNames, inits :+ last1, tp.resultType)
175
- }
174
+
176
175
}
0 commit comments