Skip to content

[Bug] String interpolation can rewrite local variables when making function calls inside string #1944

@willemneal

Description

@willemneal

Here is a minimal example.

And code copied:

class Obj  {
    _obj: Map<string, Obj>;

    constructor() {
      this._obj = new Map();
    }

    toString(): string {
      const keys = this._obj.keys();
      const objs: string[] = new Array<string>(keys.length);
      // log(this.keys)
      for (let i: i32 = 0; i < keys.length; i++) {
        const key = keys[i];
        const keyStr = `"${key}"`;
        const value = this._obj.get(key);
        const valStr = value.toString();
        const str = `"${key}":${value.toString()}`;
        objs[i] = str;
      }

      return `{${objs.join(",")}}`;
    }
}

const obj = new Obj();
const child = new Obj();
child._obj.set("isChild", new Obj())
obj._obj.set("child", child)


export function interpolate(): string {
  return obj.toString()
}

Running the example returns:

{"isChild":{"isChild":{}}}

But should be

{"child":{"isChild":{}}}

Changing value.toString() to valStr fixes the issue.

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