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

Assigning shallow string to a field makes a copy #10845

Closed
endragor opened this Issue Mar 15, 2019 · 1 comment

Comments

Projects
None yet
2 participants
@endragor
Copy link
Contributor

endragor commented Mar 15, 2019

Nim e263702

If shallow string or seq is assigned to a field, it gets copied. It's unclear what the expected behaviour is here, so at least documentation of shallow() should be improved.

Notice that this seems to be expected to work by some stdlib parts, for example:

shallow(result)

All in all, if it currently works as expected, then shallow() seems pretty useless.

Example

type
    TGenericSeq {.pure, inheritable.} = object
      len, reserved: int
      when defined(gogc):
        elemSize: int
    PGenericSeq = ptr TGenericSeq

const seqShallowFlag = low(int)

template checkShallow(str: string) =
  var s = cast[PGenericSeq](str)
  assert((s.reserved and seqShallowFlag) != 0)

type
  SomeObj = ref object
    s: string

let obj = new(SomeObj)
var str = newString(4000)
shallow(str)
obj.s = str
checkShallow(obj.s)
@Araq

This comment has been minimized.

Copy link
Member

Araq commented Mar 15, 2019

It's a valid bug.

@Araq Araq closed this in 8e5c143 Mar 15, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.