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

Wrong variable assignment in constructors #11212

Closed
ingarabr opened this issue Oct 18, 2018 · 2 comments
Closed

Wrong variable assignment in constructors #11212

ingarabr opened this issue Oct 18, 2018 · 2 comments

Comments

@ingarabr
Copy link

@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;
    }
}
@plokhotnyuk
Copy link

plokhotnyuk commented Oct 18, 2018

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)

@hrhino
Copy link

hrhino commented Oct 18, 2018

I think this is a duplicate of #8831.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants