Skip to content

'invalid type ... in this context' when function, recursive ref and case types are used together #3167

@zielmicha

Description

@zielmicha

The following code produces f.nim(17, 17) Error: invalid type: 'ArgsDef' in this context: 'proc (name: string, valueType: int): ArgDef'

type
  ParserThunk[T] = (proc(): T)

  ArgDefType* = enum
    adtValue
    adtMoreArgs

  ArgDef* = ref object
    case typ*: ArgDefType
    of adtValue:
      valueType: int
    of adtMoreArgs:
      args*: ParserThunk[ArgsDef]

  ArgsDef* = ParserThunk[seq[ArgDef]]

proc valueArgDef(name: string, valueType: int): ArgDef =
  new(result)
  result.typ = adtValue
  result.valueType = valueType

However, this compiles (ArgsDef is 'inlined'):

type
  ParserThunk[T] = (proc(): T)

  ArgDefType* = enum
    adtValue
    adtMoreArgs

  ArgDef* = ref object
    case typ*: ArgDefType
    of adtValue:
      valueType: int
    of adtMoreArgs:
      args*: ParserThunk[ParserThunk[seq[ArgDef]]]

proc valueArgDef(name: string, valueType: int): ArgDef =
  new(result)
  result.typ = adtValue
  result.valueType = valueType

Other simplification, such as removing case type make this code compile. I've also managed to produce longer variations of this which crash the compiler.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions