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

Can't use offsetOf on object that ends with an UncheckedArray #11320

MortimerSnerd opened this issue May 24, 2019 · 1 comment


Copy link

commented May 24, 2019

Get an error trying to use offsetOf on an object that has an UncheckedArray as the
last member.


  Payload = object
    something: int
    vals: UncheckedArray[int]

proc go() = 
  echo "Offset " & $offsetOf(Payload, vals)

Compile results

test.nim(7, 29) template/generic instantiation of `offsetOf` from here
../home/pkelley/src/Nim/lib/system.nim(882, 9) Error: invalid type: 'UncheckedArray[int]' in this context: 'Payload' for var

It looks like system.offsetOf is instantiating the object as a var, and that's what's causing it to fail.

Additional Information

Nim Compiler Version 0.19.9 [Linux: amd64]
Compiled at 2019-05-21
Copyright (c) 2006-2019 by Andreas Rumpf

git hash: bab5e30972743d9dc343c9c4d3ad06f3645e5741
active boot switches: -d:release

This comment has been minimized.

Copy link

commented May 26, 2019

I tried to see if the macros API is any better here as a workaround, and it seems like it is, but less that I though though. Here is an example.

import macros

macro useoffsetof(arg: typed) =
  var typeImpl = arg.getTypeImpl

  # get rid of peskey typedesc
  while typeImpl.kind == nnkBracketEXpr and typeImpl[0].eqIdent("typeDesc"):
    typeImpl = typeImpl[1].getTypeImpl

  for identDefs in typeImpl[2]:
    let sym = identDefs[0]
    echo sym, ": ", getOffset(sym)

  Payload = object
    something1, something2: int
    vals: UncheckedArray[int]



something1: 0
something2: 8
vals: -3

The output is correct until you hit the UncheckedArray, which returns -3. This -3 is an internal magic number that represents in this context "unknown offset". This i of course wrong, because the offset is known by the compiler. But at least you can get the offsets of all the other fields in this object.

@mratsim mratsim added the VM label May 27, 2019

@krux02 krux02 self-assigned this May 28, 2019

krux02 added a commit to krux02/Nim that referenced this issue Jun 19, 2019

krux02 added a commit to krux02/Nim that referenced this issue Jun 20, 2019

krux02 added a commit to krux02/Nim that referenced this issue Jul 2, 2019

@Araq Araq closed this in 20d0ef8 Jul 3, 2019

narimiran added a commit that referenced this issue Jul 8, 2019

[backport] fix #11320 (#11538)
* [backport] fix #11320
* fix test for 32 bit test

(cherry picked from commit 20d0ef8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
3 participants
You can’t perform that action at this time.