Nim icon indicating copy to clipboard operation
Nim copied to clipboard

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

Open zielmicha opened this issue 10 years ago • 4 comments

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.

zielmicha avatar Aug 01 '15 18:08 zielmicha