Open exelotl opened 3 years ago
Attempting to use offsetOf on a type fails when used within a procedure inside a static block or assigned to a const.
offsetOf
type GoodboySave = object checksum: uint32 header: array[8, char] version: uint8 saveCount: uint8 savePoint: uint16 shards: uint32 friendCount: uint8 proc getSaveSize: int = ## Get size in bytes without checksum const ofs = GoodboySave.offsetof(header) # Error here const len = GoodboySave.sizeof - ofs len echo getSaveSize()
system.nim(607, 13) Error: cannot evaluate at compile time: tmp`gensym0
20
It works fine when calling offsetOf at the top level:
const ofs = GoodboySave.offsetof(header) # Ok proc getSaveSize: int = ## Get size in bytes without checksum const len = GoodboySave.sizeof - ofs len
Tested from Nim 1.4.2 to 1.4.6 RC1
$ nim -v Nim Compiler Version 1.4.4 [Linux: amd64] Compiled at 2021-02-23 Copyright (c) 2006-2020 by Andreas Rumpf
This seems to be a consequence of #10828 etc, moving it to a proc or even doing (proc (): auto = GoodboySave.offsetof(header))() is a workaround
(proc (): auto = GoodboySave.offsetof(header))()
Attempting to use
offsetOf
on a type fails when used within a procedure inside a static block or assigned to a const.Example
Current Output
Expected Output
Additional Information
It works fine when calling
offsetOf
at the top level:Tested from Nim 1.4.2 to 1.4.6 RC1