Skip to content

Wrong variable assignment in constructors #11212

@ingarabr

Description

@ingarabr

@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;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions