bug icon indicating copy to clipboard operation
bug copied to clipboard

crash as local trait's reference to sibling local is mixed in to subclass with same reference

Open hrhino opened this issue 6 years ago • 4 comments

[nix-shell:/code/scala/sandbox]$ cat Test.scala 
class Q {{
  object F { def apply(): Int = 1 }
  trait L { def x = F() }
  class X() extends L { def y = F() }

  println(new X())
}}

[nix-shell:/code/scala/sandbox]$ scala -d . Test.scala

[nix-shell:/code/scala/sandbox]$ javap -p Q\$X\$1.class 
Compiled from "Test.scala"
public class Q$X$1 implements Q$L$1 {
  private final Q $outer;
  private final scala.runtime.LazyRef F$module$1;
  private final scala.runtime.LazyRef F$module$1;
  public void Q$L$1$_setter_$F$module$1_$eq(scala.runtime.LazyRef);
  public scala.runtime.LazyRef F$module$1();
  public int x();
  public int y();
  public Q Q$L$$$outer();
  public Q$X$1(Q, scala.runtime.LazyRef);
}

hrhino avatar Oct 23 '19 11:10 hrhino

Ref: https://gitter.im/scala/scala?at=5db028439825bd6bacb2971b

hrhino avatar Oct 23 '19 11:10 hrhino

OK, it's even easier than I thought:

class Test {{
  val a = new AnyRef
  trait L { def x = a }
  class X() extends L { def y = a }
  new X()
}}

hrhino avatar Oct 24 '19 10:10 hrhino

@hrhino Any idea how I can work around this at least for a moment?

lukasz-gosiewski avatar Oct 25 '19 09:10 lukasz-gosiewski

In your case, you can wrap your code in an object and then evaluate the object. I'm not sure why you need the braces in worksheet mode. Alternatively, you could declare the classes/traits outside of the braces so they wouldn't need to be lambdalifted.

hrhino avatar Oct 25 '19 10:10 hrhino