Skip to content

No generic signature generated for type aliases #13103

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

Closed
hamdiallam opened this issue Jul 19, 2021 · 9 comments · Fixed by #13574
Closed

No generic signature generated for type aliases #13103

hamdiallam opened this issue Jul 19, 2021 · 9 comments · Fixed by #13574
Labels
compat:java good first issue Perfect for someone who wants to get started contributing
Milestone

Comments

@hamdiallam
Copy link

hamdiallam commented Jul 19, 2021

Compiler version: 3.0.1

Problem

Given

Scala

object Foo {
  type IntGetter = () => Int
  def getFunc(): IntGetter = () => 1
  def getFunc2(): () => Int = () => 1
}

The generic signatures generated (as seen using javap) are:

public static scala.Function0 getFunc();
public static scala.Function0<java.lang.Object> getFunc2();

Whereas in Scala 2 we get:

public static scala.Function0<java.lang.Object> getFunc();
public static scala.Function0<java.lang.Object> getFunc2();
@Kordyjan
Copy link
Contributor

I've tried to replicate your bug on the current master branch and failed. For me both methods look identical in the bytecode and have the same jvm signature (<A:Ljava/lang/Object;>()LBoxed<TA;>;). There are also no warnings when they are called from java. Can you provide the full java file that you see this problem in?

@smarter
Copy link
Member

smarter commented Jul 19, 2021

If get was defined as a val or var in a trait, this would be #6350

@hamdiallam
Copy link
Author

will work on another repro case

@hamdiallam
Copy link
Author

if you clone twitter/util and run ./sbt 'project util-core '++3.0.2-RC1' 'compile', a bunch of unchecked conversion warnings show.

I was able to get a repro working as well. I can publish the project, but I've updated the issue description with it

@smarter
Copy link
Member

smarter commented Aug 27, 2021

Thanks, the generic signatures we generate are:

public static scala.Function0 getFunc();
public static scala.Function0<java.lang.Object> getFunc2();

By contrast, Scala 2 generates:

public static scala.Function0<java.lang.Object> getFunc();
public static scala.Function0<java.lang.Object> getFunc2();

(neither compiler will emit anything referencing scala.Int since that erases to int which cannot be used as a generic type parameter, and Integer cannot be used here either for other reasons).

So the issue appears to be that we fail to generate generic signatures for type aliases.

@smarter smarter changed the title Java Unchecked Conversion warnings No generic signature generated for type aliases Aug 27, 2021
@smarter
Copy link
Member

smarter commented Aug 27, 2021

Looks like a one line fix:

diff --git compiler/src/dotty/tools/dotc/transform/GenericSignatures.scala compiler/src/dotty/tools/dotc/transform/GenericSignatures.scala
index 9392d903857..f0405f1f7e9 100644
--- compiler/src/dotty/tools/dotc/transform/GenericSignatures.scala
+++ compiler/src/dotty/tools/dotc/transform/GenericSignatures.scala
@@ -455,7 +455,7 @@ object GenericSignatures {
   private class NeedsSigCollector(using Context) extends TypeAccumulator[Boolean] {
     override def apply(x: Boolean, tp: Type): Boolean =
       if (!x)
-        tp match {
+        tp.dealias match {
           case RefinedType(parent, refinedName, refinedInfo) =>
             val sym = parent.typeSymbol
             if (sym == defn.ArrayClass) foldOver(x, refinedInfo)

I'm on vacation, anyone wants to make the PR ? :)

@dwijnand
Copy link
Member

and Integer cannot be used here either for other reasons

Why can't Integer be used? I've mentally built the model "scala.Int erases to int when it can, to Integer when it can't" (ignoring specialisation) - much like value classes.

@dwijnand dwijnand added the good first issue Perfect for someone who wants to get started contributing label Aug 29, 2021
@smarter
Copy link
Member

smarter commented Aug 29, 2021

Why can't Integer be used?

It looks like it would require extra bridges: scala/scala@e42733e (when generic signatures are available, javac uses them to derive the bytecode signatures it will emit in invoke* instructions instead of just reading the actual bytecode signature written in the classfile, this complicates our job a lot).

@SethTisue
Copy link
Member

@dwijnand ticket of record on this is scala/bug#4214

@smarter smarter linked a pull request Sep 21, 2021 that will close this issue
@Kordyjan Kordyjan added this to the 3.1.1 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compat:java good first issue Perfect for someone who wants to get started contributing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants