Skip to content

sizeof default in type section fails with concept #12720

@mratsim

Description

@mratsim

In a similar vein to #12714 and #12636

const CacheLineSize = 128

type
  Enqueueable = concept x, type T
    x is ptr
    x.next is Atomic[pointer]

  MyChannel*[T: Enqueueable] = object
    pad: array[CacheLineSize - sizeof(default(T)[]), byte]
    dummy: typeof(default(T)[])
.../bug_sizeof.nim(9, 46) template/generic instantiation of `default` from here
../.choosenim/toolchains/nim-1.0.2/lib/system.nim(1818, 16) Error: invalid type: 'T: Enqueueable' in this context: 'proc (T: type T: Enqueueable): T: Enqueueable{.noSideEffect.}' for proc

Unfortunately there is no workaround since typedesc are now NimNode in macros (nim-lang/RFCs#148 and #11400) and there is no way to convert in NimNode back into a typedesc (#6785)

Otherwise somehing like this would probably work

macro sizeofDeref(T: typedesc): untyped =
  let x = default(T)[]
  result = newLit sizeof(x)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions