Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit generic signature for static forwarders to nullary methods #12710

Merged
merged 2 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/backend/jvm/BCodeHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import dotty.tools.dotc.core.Types
import dotty.tools.dotc.core.Types._
import dotty.tools.dotc.core.TypeErasure
import dotty.tools.dotc.transform.GenericSignatures
import dotty.tools.dotc.transform.ElimErasedValueType
import dotty.tools.io.AbstractFile
import dotty.tools.dotc.report

Expand Down Expand Up @@ -926,7 +927,7 @@ trait BCodeHelpers extends BCodeIdiomatic with BytecodeWriters {
// (one that doesn't erase to the actual signature). See run/t3452b for a test case.

val memberTpe = atPhase(erasurePhase) { moduleClass.denot.thisType.memberInfo(sym) }
val erasedMemberType = TypeErasure.fullErasure(memberTpe)
val erasedMemberType = ElimErasedValueType.elimEVT(TypeErasure.transformInfo(sym, memberTpe))
if (erasedMemberType =:= sym.denot.info)
getGenericSignatureHelper(sym, moduleClass, memberTpe).orNull
else null
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/TypeErasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst
// See doc comment for ElimByName for speculation how we could improve this.
else
MethodType(Nil, Nil,
eraseResult(sym.info.finalResultType.translateFromRepeated(toArray = sourceLanguage.isJava)))
eraseResult(rt.translateFromRepeated(toArray = sourceLanguage.isJava)))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was needed for test run/t3452g

The backend calls TypeErasure.transformInfo(<Symbol TraversableLike.tail>, <Type ExprType(AbstractTrav[String])>).

Erasure should, I assume, continue to work on AbstractTrav[String] (which is rt here). Calling sym.info.finalResultType gives Repr which erases differently.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently that was intentionally changed in 76af6c5

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests passed...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but at the very least we need to carefully check if this breaks binary compatibility.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very true. How could I do that? Compile the community build twice and do a jardiff?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran dotty.communitybuild.CommunityBuildTestA twice; once with this PR, once with the line here reverted. This resulted in 14081 identical classfiles.

I also added a check to see when the old and new erasure are different and it only happened when called from the backend:

+          val a = eraseResult(rt.translateFromRepeated(toArray = sourceLanguage.isJava))
+          val b = eraseResult(sym.info.finalResultType.translateFromRepeated(toArray = sourceLanguage.isJava))
+          if (!(a =:= b) && !(new Exception).getStackTrace.exists(_.toString.contains("getStaticForwarderGenericSignature"))) then
+            throw new Exception(s"[ERASURE]\n- $a\n- $b\n- $tp\n- ${sym.showFullName}")
           MethodType(Nil, Nil,
-            eraseResult(rt.translateFromRepeated(toArray = sourceLanguage.isJava)))
+            a)

I ran the sbt test and CommunityBuildTestA with this in place and did't see the exception showing up.

@smarter any other ideas?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I've run a similar test on the whole CI and it also didn't show any situation where the two types differed, this makes sense since the tp passed to eraseInfo should always be the symbol info at that point (it's only called from transformInfo which is only called from Erasure#transform, which always uses the symbol info except for java.lang.Object which is treated specially), so LGTM 🎇

case tp1: PolyType =>
eraseResult(tp1.resultType) match
case rt: MethodType => rt
Expand Down
23 changes: 12 additions & 11 deletions compiler/src/dotty/tools/dotc/transform/ElimErasedValueType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ import NameKinds.SuperAccessorName

object ElimErasedValueType {
val name: String = "elimErasedValueType"

def elimEVT(tp: Type)(using Context): Type = tp match {
case ErasedValueType(_, underlying) =>
elimEVT(underlying)
case tp: MethodType =>
val paramTypes = tp.paramInfos.mapConserve(elimEVT)
val retType = elimEVT(tp.resultType)
tp.derivedLambdaType(tp.paramNames, paramTypes, retType)
case _ =>
tp
}
}

/** This phase erases ErasedValueType to their underlying type.
Expand All @@ -25,6 +36,7 @@ object ElimErasedValueType {
class ElimErasedValueType extends MiniPhase with InfoTransformer { thisPhase =>

import tpd._
import ElimErasedValueType.elimEVT

override def phaseName: String = ElimErasedValueType.name

Expand All @@ -48,17 +60,6 @@ class ElimErasedValueType extends MiniPhase with InfoTransformer { thisPhase =>
elimEVT(tp)
}

def elimEVT(tp: Type)(using Context): Type = tp match {
case ErasedValueType(_, underlying) =>
elimEVT(underlying)
case tp: MethodType =>
val paramTypes = tp.paramInfos.mapConserve(elimEVT)
val retType = elimEVT(tp.resultType)
tp.derivedLambdaType(tp.paramNames, paramTypes, retType)
case _ =>
tp
}

def transformTypeOfTree(tree: Tree)(using Context): Tree =
tree.withType(elimEVT(tree.tpe))

Expand Down
7 changes: 7 additions & 0 deletions tests/pos/i10347/A_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
trait L[+T] { def head: T }
class K(val s: String) extends AnyVal
object A {
def foo: L[String] = ???
def bar: L[K] = ???
def baz(k: K): L[String] = ???
}
5 changes: 5 additions & 0 deletions tests/pos/i10347/C_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class C_2 {
String hi = A.foo().head();
String hy = A.bar().head();
String hj = A.baz("").head();
}