Skip to content

Different behaviour in VM and generated code for ref string #7795

@PMunch

Description

@PMunch

I was trying to create a more efficient string slice type for some parsing I'm doing. I though I had got it to work, but when I ran in in the VM (in a macro) it ended up with different results than when it's run normally:

static:
  var
    a: ref string
    b: ref string
  new a
  a[] = "Hello world"
  b = a
  echo a[]
  echo b[]
  b[5] = 'c'
  echo a[]
  echo b[]
  # Output:
  # Hello world
  # Hello world
  # Hello world
  # Hellocworld

var
  d: ref string
  e: ref string
new d
d[] = "Hello world"
e = d
echo d[]
echo e[]
e[5] = 'c'
echo d[]
echo e[]
# Output:
# Hello world
# Hello world
# Hellocworld
# Hellocworld

Notice how the code in both the static block and below it is identical save for the variable names. But the output is different (second one is expected behaviour were modifying the string through the reference modifies the string for the other reference as well).

Metadata

Metadata

Assignees

No one assigned

    Labels

    VMsee also `const` label

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions