Skip to content

Commit c91d5bf

Browse files
committed
Get rid of JavaMethodType
This is possible thanks to the previous commit, in all remaining places where we called `isJavaMethod` we can rely on contextual informations to obtain the same information, so we can completely get rid of it which is a nice simplification. Also get rid of ElimRepeated#transformTypeOfTree which turned out to be a no-op: the denotation transformer already transforms method types and there's nothing to do on top of that.
1 parent 0705749 commit c91d5bf

File tree

13 files changed

+22
-68
lines changed

13 files changed

+22
-68
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ class Definitions {
122122
}
123123
val resParamRef = enterTypeParam(cls, paramNamePrefix ++ "R", Covariant, decls).typeRef
124124
val methodType = MethodType.companion(
125-
isJava = false,
126125
isContextual = name.isContextFunction,
127126
isImplicit = false,
128127
isErased = name.isErasedFunction)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ object NamerOps:
3939
val (isContextual, isImplicit, isErased) =
4040
if params.isEmpty then (false, false, false)
4141
else (params.head.is(Given), params.head.is(Implicit), params.head.is(Erased))
42-
val make = MethodType.companion(isJava = isJava, isContextual = isContextual, isImplicit = isImplicit, isErased = isErased)
42+
val make = MethodType.companion(isContextual = isContextual, isImplicit = isImplicit, isErased = isErased)
4343
if isJava then
4444
for param <- params do
4545
if param.info.isDirectRef(defn.ObjectClass) then param.info = defn.AnyType

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,7 @@ object TypeErasure {
249249
!classify(tp).derivesFrom(defn.ObjectClass) &&
250250
!tp.symbol.is(JavaDefined)
251251
case tp: TypeParamRef =>
252-
!classify(tp).derivesFrom(defn.ObjectClass) &&
253-
!tp.binder.resultType.isJavaMethod
252+
!classify(tp).derivesFrom(defn.ObjectClass)
254253
case tp: TypeAlias => isUnboundedGeneric(tp.alias)
255254
case tp: TypeBounds =>
256255
val upper = classify(tp.hi)
@@ -474,7 +473,7 @@ class TypeErasure(isJava: Boolean, semiEraseVCs: Boolean, isConstructor: Boolean
474473
TypeComparer.orType(this(tp1), this(tp2), isErased = true)
475474
case tp: MethodType =>
476475
def paramErasure(tpToErase: Type) =
477-
erasureFn(tp.isJavaMethod, semiEraseVCs, isConstructor, wildcardOK)(tpToErase)
476+
erasureFn(isJava, semiEraseVCs, isConstructor, wildcardOK)(tpToErase)
478477
val (names, formals0) = if (tp.isErasedMethod) (Nil, Nil) else (tp.paramNames, tp.paramInfos)
479478
val formals = formals0.mapConserve(paramErasure)
480479
eraseResult(tp.resultType) match {

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

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ object Types {
7777
* +- GroundType -+- AndType
7878
* +- OrType
7979
* +- MethodOrPoly ---+-- PolyType
80-
* +-- MethodType ---+- ImplicitMethodType
81-
* +- ContextualMethodType
82-
* | +- JavaMethodType
80+
* | +-- MethodType
8381
* +- ClassInfo
8482
* |
8583
* +- NoType
@@ -400,9 +398,6 @@ object Types {
400398
/** Is this an alias TypeBounds? */
401399
final def isTypeAlias: Boolean = this.isInstanceOf[TypeAlias]
402400

403-
/** Is this a MethodType which is from Java? */
404-
def isJavaMethod: Boolean = false
405-
406401
/** Is this a Method or PolyType which has implicit or contextual parameters? */
407402
def isImplicitMethod: Boolean = false
408403

@@ -1725,35 +1720,22 @@ object Types {
17251720
* @param dropLast The number of trailing parameters that should be dropped
17261721
* when forming the function type.
17271722
*/
1728-
def toFunctionType(dropLast: Int = 0)(using Context): Type = this match {
1723+
def toFunctionType(isJava: Boolean, dropLast: Int = 0)(using Context): Type = this match {
17291724
case mt: MethodType if !mt.isParamDependent =>
17301725
val formals1 = if (dropLast == 0) mt.paramInfos else mt.paramInfos dropRight dropLast
17311726
val isContextual = mt.isContextualMethod && !ctx.erasedTypes
17321727
val isErased = mt.isErasedMethod && !ctx.erasedTypes
17331728
val result1 = mt.nonDependentResultApprox match {
1734-
case res: MethodType => res.toFunctionType()
1729+
case res: MethodType => res.toFunctionType(isJava)
17351730
case res => res
17361731
}
17371732
val funType = defn.FunctionOf(
1738-
formals1 mapConserve (_.translateFromRepeated(toArray = mt.isJavaMethod)),
1733+
formals1 mapConserve (_.translateFromRepeated(toArray = isJava)),
17391734
result1, isContextual, isErased)
17401735
if (mt.isResultDependent) RefinedType(funType, nme.apply, mt)
17411736
else funType
17421737
}
17431738

1744-
final def dropJavaMethod(using Context): Type = this match
1745-
case pt: PolyType => pt.derivedLambdaType(resType = pt.resType.dropJavaMethod)
1746-
1747-
case mt: MethodType =>
1748-
if mt.isJavaMethod then
1749-
MethodType.apply(mt.paramNames, mt.paramInfos, mt.resType.dropJavaMethod)
1750-
else
1751-
mt.derivedLambdaType(resType = mt.resType.dropJavaMethod)
1752-
1753-
case _ => this
1754-
1755-
end dropJavaMethod
1756-
17571739
/** The signature of this type. This is by default NotAMethod,
17581740
* but is overridden for PolyTypes, MethodTypes, and TermRef types.
17591741
* (the reason why we deviate from the "final-method-with-pattern-match-in-base-class"
@@ -3558,7 +3540,6 @@ object Types {
35583540

35593541
def companion: MethodTypeCompanion
35603542

3561-
final override def isJavaMethod: Boolean = companion eq JavaMethodType
35623543
final override def isImplicitMethod: Boolean =
35633544
companion.eq(ImplicitMethodType) ||
35643545
companion.eq(ErasedImplicitMethodType) ||
@@ -3654,21 +3635,14 @@ object Types {
36543635
}
36553636

36563637
object MethodType extends MethodTypeCompanion("MethodType") {
3657-
def companion(isJava: Boolean = false, isContextual: Boolean = false, isImplicit: Boolean = false, isErased: Boolean = false): MethodTypeCompanion =
3658-
if (isJava) {
3659-
assert(!isImplicit)
3660-
assert(!isErased)
3661-
assert(!isContextual)
3662-
JavaMethodType
3663-
}
3664-
else if (isContextual)
3638+
def companion(isContextual: Boolean = false, isImplicit: Boolean = false, isErased: Boolean = false): MethodTypeCompanion =
3639+
if (isContextual)
36653640
if (isErased) ErasedContextualMethodType else ContextualMethodType
36663641
else if (isImplicit)
36673642
if (isErased) ErasedImplicitMethodType else ImplicitMethodType
36683643
else
36693644
if (isErased) ErasedMethodType else MethodType
36703645
}
3671-
object JavaMethodType extends MethodTypeCompanion("JavaMethodType")
36723646
object ErasedMethodType extends MethodTypeCompanion("ErasedMethodType")
36733647
object ContextualMethodType extends MethodTypeCompanion("ContextualMethodType")
36743648
object ErasedContextualMethodType extends MethodTypeCompanion("ErasedContextualMethodType")

compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ class ClassfileParser(
447447

448448
index += 1
449449
val restype = sig2type(tparams, skiptvs = false)
450-
JavaMethodType(paramnames.toList, paramtypes.toList, restype)
450+
MethodType(paramnames.toList, paramtypes.toList, restype)
451451
case 'T' =>
452452
val n = subName(';'.==).toTypeName
453453
index += 1

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

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
3131
override def changesMembers: Boolean = true // the phase adds vararg forwarders
3232

3333
def transformInfo(tp: Type, sym: Symbol)(using Context): Type =
34-
elimRepeated(tp)
34+
elimRepeated(tp, sym.is(JavaDefined))
3535

3636
/** Create forwarder symbols for the methods that are annotated
3737
* with `@varargs` or that override java varargs.
@@ -88,15 +88,14 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
8888
(sym.allOverriddenSymbols.exists(s => s.is(JavaDefined) || hasVarargsAnnotation(s)))
8989

9090
/** Eliminate repeated parameters from method types. */
91-
private def elimRepeated(tp: Type)(using Context): Type = tp.stripTypeVar match
91+
private def elimRepeated(tp: Type, isJava: Boolean)(using Context): Type = tp.stripTypeVar match
9292
case tp @ MethodTpe(paramNames, paramTypes, resultType) =>
93-
val resultType1 = elimRepeated(resultType)
93+
val resultType1 = elimRepeated(resultType, isJava)
9494
val paramTypes1 =
9595
val lastIdx = paramTypes.length - 1
9696
if lastIdx >= 0 then
9797
val last = paramTypes(lastIdx)
9898
if last.isRepeatedParam then
99-
val isJava = tp.isJavaMethod
10099
// We need to be careful when handling Java repeated parameters
101100
// of the form `Object...` or `T...` where `T` is unbounded:
102101
// in both cases, `Object` will have been translated to `FromJavaObject`
@@ -117,22 +116,10 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
117116
else paramTypes
118117
tp.derivedLambdaType(paramNames, paramTypes1, resultType1)
119118
case tp: PolyType =>
120-
tp.derivedLambdaType(tp.paramNames, tp.paramInfos, elimRepeated(tp.resultType))
119+
tp.derivedLambdaType(tp.paramNames, tp.paramInfos, elimRepeated(tp.resultType, isJava))
121120
case tp =>
122121
tp
123122

124-
def transformTypeOfTree(tree: Tree)(using Context): Tree =
125-
tree.withType(elimRepeated(tree.tpe))
126-
127-
override def transformTypeApply(tree: TypeApply)(using Context): Tree =
128-
transformTypeOfTree(tree)
129-
130-
override def transformIdent(tree: Ident)(using Context): Tree =
131-
transformTypeOfTree(tree)
132-
133-
override def transformSelect(tree: Select)(using Context): Tree =
134-
transformTypeOfTree(tree)
135-
136123
override def transformApply(tree: Apply)(using Context): Tree =
137124
val args = tree.args.mapConserve {
138125
case arg: Typed if isWildcardStarArg(arg) =>
@@ -147,7 +134,7 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
147134
arg.expr
148135
case arg => arg
149136
}
150-
transformTypeOfTree(cpy.Apply(tree)(tree.fun, args))
137+
cpy.Apply(tree)(tree.fun, args)
151138

152139
/** Convert sequence argument to Java array */
153140
private def seqToArray(tree: Tree)(using Context): Tree = tree match

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ class PickleQuotes extends MacroTransform {
377377
(tree: Tree) => {
378378
def newCapture = {
379379
val tpw = tree.tpe.widen match {
380-
case tpw: MethodicType => tpw.toFunctionType()
380+
case tpw: MethodicType => tpw.toFunctionType(isJava = false)
381381
case tpw => tpw
382382
}
383383
assert(tpw.isInstanceOf[ValueType])

compiler/src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ trait Applications extends Compatibility {
628628
false
629629
case argtpe =>
630630
def SAMargOK = formal match {
631-
case SAMType(sam) => argtpe <:< sam.toFunctionType()
631+
case SAMType(sam) => argtpe <:< sam.toFunctionType(isJava = formal.classSymbol.is(JavaDefined))
632632
case _ => false
633633
}
634634
isCompatible(argtpe, formal) || ctx.mode.is(Mode.ImplicitsEnabled) && SAMargOK

compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,7 @@ object ErrorReporting {
118118

119119
/** A subtype log explaining why `found` does not conform to `expected` */
120120
def whyNoMatchStr(found: Type, expected: Type): String = {
121-
val found1 = found.dropJavaMethod
122-
val expected1 = expected.dropJavaMethod
123-
if ((found1 eq found) != (expected eq expected1) && (found1 <:< expected1))
124-
i"""
125-
|(Note that Scala's and Java's representation of this type differs)"""
126-
else if (ctx.settings.explainTypes.value)
121+
if (ctx.settings.explainTypes.value)
127122
i"""
128123
|${ctx.typerState.constraint}
129124
|${TypeComparer.explained(_.isSubType(found, expected))}"""

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,7 @@ class Namer { typer: Typer =>
10491049
if sym.isStableMember && sym.isPublic && !refersToPrivate(path.tpe) then
10501050
(StableRealizable, ExprType(path.tpe.select(sym)))
10511051
else
1052-
(EmptyFlags, mbr.info.ensureMethodic.dropJavaMethod)
1052+
(EmptyFlags, mbr.info.ensureMethodic)
10531053
var mbrFlags = Exported | Method | Final | maybeStable | sym.flags & RetainedExportFlags
10541054
if sym.is(ExtensionMethod) then mbrFlags |= ExtensionMethod
10551055
val forwarderName = checkNoConflict(alias, isPrivate = false, span)

compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ trait TypeAssigner {
375375

376376
def assignType(tree: untpd.Closure, meth: Tree, target: Tree)(using Context): Closure =
377377
tree.withType(
378-
if (target.isEmpty) meth.tpe.widen.toFunctionType(tree.env.length)
378+
if (target.isEmpty) meth.tpe.widen.toFunctionType(isJava = meth.symbol.is(JavaDefined), tree.env.length)
379379
else target.tpe)
380380

381381
def assignType(tree: untpd.CaseDef, pat: Tree, body: Tree)(using Context): CaseDef = {

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3603,7 +3603,7 @@ class Typer extends Namer
36033603
if defn.isFunctionType(wtp) && !defn.isFunctionType(pt) =>
36043604
pt match {
36053605
case SAMType(sam)
3606-
if wtp <:< sam.toFunctionType() =>
3606+
if wtp <:< sam.toFunctionType(isJava = pt.classSymbol.is(JavaDefined)) =>
36073607
// was ... && isFullyDefined(pt, ForceDegree.flipBottom)
36083608
// but this prevents case blocks from implementing polymorphic partial functions,
36093609
// since we do not know the result parameter a priori. Have to wait until the

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
385385
def etaExpand(owner: Symbol): Term = self.tpe.widen match {
386386
case mtpe: Types.MethodType if !mtpe.isParamDependent =>
387387
val closureResType = mtpe.resType match {
388-
case t: Types.MethodType => t.toFunctionType()
388+
case t: Types.MethodType => t.toFunctionType(isJava = self.symbol.is(JavaDefined))
389389
case t => t
390390
}
391391
val closureTpe = Types.MethodType(mtpe.paramNames, mtpe.paramInfos, closureResType)

0 commit comments

Comments
 (0)