Scala constructor cannot be called from Java

77 views
Skip to first unread message

Matthieu Leclercq

unread,
Mar 3, 2015, 11:23:39 AM3/3/15
to scala-i...@googlegroups.com
Hi,

I'm having a problem that looks like a bug in the scala compiler (tested with 2.11.5).

Given the following scala code:

trait W

trait T1
trait T2
extends T1

object O1 {
    type t
= T1 with T2
}

class C1[w<:W](o: O1.t)

class C2 extends T1 with T2

And the given Java code:

public class Main {

   
public static void main(String[] args) {
       
new C1<>(new C2());
   
}
}

Execution results in the following exception:

Exception in thread "main" java.lang.NoSuchMethodError: C1.<init>(LT1;)V
    at
Main.main(Main.java:4)

(Note that calling the constructor similarly from Scala does not cause this exception)

It seems that the declaration of the C1 <init> method in the bytecode is not correct. it looks like:

  // access flags 0x1
 
// signature (LT1;)V
 
// declaration: void <init>(T1)
 
public <init>(LT2;)V

And in the bytecode compiled from the Main java class, the call to the constructor is compiled as:

INVOKESPECIAL C1.<init> (LT1;)V

Here the java code tries to call an <init> method that takes a LT1 parameter while the actual <init> method takes a LT2 param. It looks like there is a discrepancy between the method prototype and the generic signature info.

This looks similar to https://issues.scala-lang.org/browse/SI-7007 but seems to happen when the constructor is called from Java.

Should I open a Jira ?

Thanks,
Matthieu.

Jason Zaugg

unread,
Mar 4, 2015, 11:14:28 PM3/4/15
to scala-i...@googlegroups.com
On Wed, Mar 4, 2015 at 2:23 AM, Matthieu Leclercq <matthieul...@gmail.com> wrote:
Hi,

I'm having a problem that looks like a bug in the scala compiler (tested with 2.11.5).

... 
This looks similar to https://issues.scala-lang.org/browse/SI-7007 but seems to happen when the constructor is called from Java.

Should I open a Jira ?

Hi Matthieu,

Thanks for the report and test case.

I've lodged SI-9200 on your behalf to track this issue, and submitted patch #4374 to fix it.

-jason

Reply all
Reply to author
Forward
0 new messages