Skip to content

Commit f282614

Browse files
committed
Move out of Defn, revert unneeded
1 parent 6c015ca commit f282614

File tree

7 files changed

+60
-63
lines changed

7 files changed

+60
-63
lines changed

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -736,22 +736,6 @@ class Definitions {
736736
@tu lazy val MethodHandlesLookupClass: ClassSymbol = requiredClass("java.lang.invoke.MethodHandles.Lookup")
737737
@tu lazy val VarHandleClass: ClassSymbol = requiredClass("java.lang.invoke.VarHandle")
738738

739-
// Java language spec: https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-15.12.3
740-
// Scala 2 spec: https://scala-lang.org/files/archive/spec/2.13/06-expressions.html#signature-polymorphic-methods
741-
def isPolymorphicSignature(sym: Symbol) = sym.is(JavaDefined) && {
742-
val owner = sym.maybeOwner
743-
(owner == MethodHandleClass || owner == VarHandleClass)
744-
&& sym.hasAnnotation(NativeAnnot)
745-
&& sym.paramSymss.match
746-
case List(List(p)) => p.info.isRepeatedParam
747-
case _ => false
748-
}
749-
750-
def wasPolymorphicSignature(sym: Symbol) =
751-
val owner = sym.maybeOwner
752-
(owner == MethodHandleClass || owner == VarHandleClass)
753-
&& isPolymorphicSignature(owner.info.member(sym.name).symbol)
754-
755739
@tu lazy val StringBuilderClass: ClassSymbol = requiredClass("scala.collection.mutable.StringBuilder")
756740
@tu lazy val MatchErrorClass : ClassSymbol = requiredClass("scala.MatchError")
757741
@tu lazy val ConversionClass : ClassSymbol = requiredClass("scala.Conversion").typeRef.symbol.asClass

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,26 @@ object SymDenotations {
960960

961961
def isSkolem: Boolean = name == nme.SKOLEM
962962

963+
// Java language spec: https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-15.12.3
964+
// Scala 2 spec: https://scala-lang.org/files/archive/spec/2.13/06-expressions.html#signature-polymorphic-methods
965+
def isSignaturePolymorphic(using Context): Boolean =
966+
containsSignaturePolymorphic
967+
&& is(JavaDefined)
968+
&& hasAnnotation(defn.NativeAnnot)
969+
&& atPhase(typerPhase)(symbol.denot).paramSymss.match
970+
case List(List(p)) => p.info.isRepeatedParam
971+
case _ => false
972+
973+
def containsSignaturePolymorphic(using Context): Boolean =
974+
maybeOwner == defn.MethodHandleClass
975+
|| maybeOwner == defn.VarHandleClass
976+
977+
def originalSignaturePolymorphic(using Context): Denotation =
978+
if containsSignaturePolymorphic && !isSignaturePolymorphic then
979+
val d = owner.info.member(name)
980+
if d.symbol.isSignaturePolymorphic then d else NoDenotation
981+
else NoDenotation
982+
963983
def isInlineMethod(using Context): Boolean =
964984
isAllOf(InlineMethod, butNot = Accessor)
965985

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,18 +1240,17 @@ class TreeUnpickler(reader: TastyReader,
12401240
val rdr = fork
12411241
val start = rdr.reader.currentAddr
12421242
if rdr.reader.readByte() == APPLY then
1243-
val end = rdr.reader.readEnd()
1244-
val fn = rdr.readTerm()
1245-
if defn.isPolymorphicSignature(fn.symbol) then
1246-
skipTree() // expr
1247-
skipTree() // tpt
1243+
val end = rdr.reader.readEnd()
1244+
val fn = rdr.readTerm()
1245+
if fn.symbol.isSignaturePolymorphic then
12481246
val args = rdr.reader.until(end)(rdr.readTerm())
1249-
val tpt = rdr.readTpt()
1247+
skipTree() // expr
1248+
val tpt = readTpt()
12501249
val info = MethodType(args.map(_.tpe.widen), tpt.tpe)
1251-
val fun2 = fn.withType(fn.symbol.copy(info = info).termRef)
1252-
val app = Apply(fun2, args)
1253-
rdr.setSpan(start, app)
1254-
Typed(app, tpt)
1250+
val sym2 = fn.symbol.copy(info = info) // still not entered (like simpleApply)
1251+
val fun2 = fn.withType(sym2.termRef)
1252+
val app = Apply(fun2, args)
1253+
Typed(setSpan(start, app), tpt)
12551254
else Typed(readTerm(), readTpt())
12561255
else Typed(readTerm(), readTpt())
12571256
case ASSIGN =>

compiler/src/dotty/tools/dotc/semanticdb/SemanticSymbolBuilder.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ class SemanticSymbolBuilder:
7474
def addOwner(owner: Symbol): Unit =
7575
if !owner.isRoot then addSymName(b, owner)
7676

77-
def addOverloadIdx(sym: Symbol): Unit =
77+
def addOverloadIdx(initSym: Symbol): Unit =
78+
// revert from the compiler-generated overload of the signature polymorphic method
79+
val sym = initSym.originalSignaturePolymorphic.symbol.orElse(initSym)
7880
val decls =
7981
val decls0 = sym.owner.info.decls.lookupAll(sym.name)
8082
if sym.owner.isAllOf(JavaModule) then
@@ -90,10 +92,7 @@ class SemanticSymbolBuilder:
9092
case _ =>
9193
end find
9294
val sig = sym.signature
93-
// the polymorphic signature methods (invoke/invokeExact) are never overloaded
94-
// we changed the signature at the call site, which means they won't match,
95-
// but we still shouldn't add any overload index in this case.
96-
find(_.signature == sig || defn.wasPolymorphicSignature(sym))
95+
find(_.signature == sig)
9796

9897
def addDescriptor(sym: Symbol): Unit =
9998
if sym.is(ModuleClass) then

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
204204
return transformSelect(cpy.Select(tree)(qual.select(pobj).withSpan(qual.span), tree.name), targs)
205205
case _ =>
206206
}
207-
val tree0 = super.transform(tree)
208-
val tree1 = if defn.wasPolymorphicSignature(tree.symbol) then tree0.withType(tree.tpe) else tree0
207+
val tree1 = super.transform(tree)
209208
constToLiteral(tree1) match {
210209
case _: Literal => tree1
211210
case _ => superAcc.transformSelect(tree1, targs)

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,9 @@ abstract class Recheck extends Phase, SymTransformer:
261261
mt.instantiate(argTypes)
262262

263263
def recheckApply(tree: Apply, pt: Type)(using Context): Type =
264-
val wasPolymorphicSignature = tree.fun.tpe match
265-
case funRef: TermRef => defn.wasPolymorphicSignature(funRef.symbol)
266-
case _ => false
267-
if wasPolymorphicSignature then tree.fun.tpe.widenTermRefExpr.finalResultType
268-
else
269-
val funtpe = recheck(tree.fun)
270-
funtpe.widen match
264+
// reuse the tree's type if the function was a signature polymorphic method, instead of rechecking it
265+
val funtpe = if tree.fun.symbol.originalSignaturePolymorphic.exists then tree.fun.tpe else recheck(tree.fun)
266+
funtpe.widen match
271267
case fntpe: MethodType =>
272268
assert(fntpe.paramInfos.hasSameLengthAs(tree.args))
273269
val formals =

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -936,30 +936,30 @@ trait Applications extends Compatibility {
936936
/** Type application where arguments come from prototype, and no implicits are inserted */
937937
def simpleApply(fun1: Tree, proto: FunProto)(using Context): Tree =
938938
methPart(fun1).tpe match {
939+
case funRef: TermRef if funRef.symbol.isSignaturePolymorphic =>
940+
// synthesize a method type based on the types at the call site.
941+
// one can imagine the original signature-polymorphic method as
942+
// being infinitely overloaded, with each individual overload only
943+
// being brought into existence as needed
944+
val originalResultType = funRef.symbol.info.resultType.stripNull
945+
val expectedResultType = AvoidWildcardsMap()(proto.deepenProto.resultType)
946+
val resultType =
947+
if !originalResultType.isRef(defn.ObjectClass) then originalResultType
948+
else if isFullyDefined(expectedResultType, ForceDegree.all) then expectedResultType
949+
else expectedResultType match
950+
case SelectionProto(nme.asInstanceOf_, PolyProto(_, resTp), _, _) => resTp
951+
case _ => defn.ObjectType
952+
val info = MethodType(proto.typedArgs().map(_.tpe.widen), resultType)
953+
val sym2 = funRef.symbol.copy(info = info) // not entered, to avoid overload resolution problems
954+
val fun2 = fun1.withType(sym2.termRef)
955+
val app = simpleApply(fun2, proto)
956+
Typed(app, TypeTree(resultType))
939957
case funRef: TermRef =>
940-
if defn.isPolymorphicSignature(funRef.symbol) then
941-
val originalResultType = funRef.symbol.info.resultType.stripNull
942-
val expectedResultType = AvoidWildcardsMap()(proto.deepenProto.resultType)
943-
val resultType =
944-
if !originalResultType.isRef(defn.ObjectClass) then originalResultType
945-
else if isFullyDefined(expectedResultType, ForceDegree.all) then expectedResultType
946-
else expectedResultType match
947-
case SelectionProto(nme.asInstanceOf_, PolyProto(_, resTp), _, _) => resTp
948-
case _ => defn.ObjectType
949-
// synthesize a method type based on the types at the call site.
950-
// one can imagine the original signature-polymorphic method as
951-
// being infinitely overloaded, with each individual overload only
952-
// being brought into existence as needed
953-
val info = MethodType(proto.typedArgs().map(_.tpe.widen), resultType)
954-
val fun2 = fun1.withType(funRef.symbol.copy(info = info).termRef)
955-
val app = simpleApply(fun2, proto)
956-
Typed(app, TypeTree(resultType))
957-
else
958-
val app = ApplyTo(tree, fun1, funRef, proto, pt)
959-
convertNewGenericArray(
960-
widenEnumCase(
961-
postProcessByNameArgs(funRef, app).computeNullable(),
962-
pt))
958+
val app = ApplyTo(tree, fun1, funRef, proto, pt)
959+
convertNewGenericArray(
960+
widenEnumCase(
961+
postProcessByNameArgs(funRef, app).computeNullable(),
962+
pt))
963963
case _ =>
964964
handleUnexpectedFunType(tree, fun1)
965965
}

0 commit comments

Comments
 (0)