kaitai_struct icon indicating copy to clipboard operation
kaitai_struct copied to clipboard

Better compile error when the user uses `type` instead of `enum`

Open Mingun opened this issue 2 years ago • 1 comments

The following KSY should produce compilation error but it doesn't

meta:
  id: type_resolution_bug
seq:
  - id: enumeration
    type: type_resolution_bug::enum
enums:
  enum:
    0: e0
    1: e1

The enum is not a type -- it is an enum and you cannot use it in the type key

(You'll get an error in WebIDE, but it arises at the parse time, not at compile time. If you wrap seq into a nested type, WebIDE will silence

Parse error: undefined Call stack: undefined TypeError: TypeResolutionBug.Enum is not a constructor

)

Ideally the compiler should also give you a hint, that you'll probably want to use

type: <integer_type>
enum: path::to::enum

instead of

type: path::to::enum

Mingun avatar Nov 20 '21 13:11 Mingun

As I mention in https://github.com/kaitai-io/kaitai_struct/issues/1032, the bug here isn't really an enum being confused with a type. It's just that the compiler doesn't check the type path at all if it starts with the top-level type, so this compiles even though there is no type xxx:

meta:
  id: type_resolution_bug
seq:
  - id: enumeration
    type: type_resolution_bug::xxx

So from my perspective, the initial goal of this issue is now included in https://github.com/kaitai-io/kaitai_struct/issues/1032.

But trying to improve the compile error by providing a hint when the user confuses the type and enum keys is likely a good idea, albeit low priority.

generalmimon avatar May 14 '23 23:05 generalmimon