scala/bug

Wrong variable assignment in constructors

ingarabr opened this issue · 2 comments

@gafiatulin did come over a strange behaviour when constructing a class where the first argument used ticks and a special character and the second was the same up to the special character, for instance:

scala> case class A(`a-b`: Int, a: Int)
defined class A

scala> A(1, 2)
res0: A = A(1,1)

This applies to both class and case classes but not functions. From the decompiled code below you can see that it does assign the second variable to the wrong value. If the values isn't compatible you will get a runtime error.

class B(`a-b`: Int, a: Int){ override def toString = s"${`a-b`} $a"}

decompiles to:

public class B {
    private final int a$minusb;
    private final int a;

    public String toString() {
        return new StringContext((Seq)Predef$.MODULE$.wrapRefArray((Object[])new String[]{"", " ", ""})).s((Seq)Predef$.MODULE$.genericWrapArray((Object)new Object[]{BoxesRunTime.boxToInteger((int)this.a$minusb), BoxesRunTime.boxToInteger((int)this.a)}));
    }

    public B(int a$minusb, int a) {
        this.a$minusb = a$minusb;
        this.a = a$minusb;
    }
}

W/A from #10825 (comment) - just need to change an order of clashing fields:

scala> case class A(a: Int, `a-b`: Int)
defined class A

scala> A(1, 2)
res1: A = A(1,2)

I think this is a duplicate of #8831.