kaitai-io / kaitai_struct

Kaitai Struct: declarative language to generate binary data parsers in C++ / C# / Go / Java / JavaScript / Lua / Nim / Perl / PHP / Python / Ruby
https://kaitai.io
4.04k stars 199 forks source link

Pass `SpecsValueTypeDerive` incorrectly reports path for `enum` key #1137

Open Mingun opened 1 month ago

Mingun commented 1 month ago

Example of the current error:

# enum_unknown_inst_value.ksy: /instances/should_fail/enum:
#   error: unable to find enum 'animal', searching from enum_unknown_inst_value
#
meta:
  id: enum_unknown_inst_value
instances:
  should_fail:
    value: 1
    enum: animal
[info] - enum_unknown_inst_value *** FAILED ***
[info]   [enum_unknown_inst_value.ksy: /instances/should_fail/value:
[info]          error: unable to find enum 'animal', searching from enum_unknown_inst_value
[info]   ]
[info]     did not equal
[info]   [enum_unknown_inst_value.ksy: /instances/should_fail/enum:
[info]          error: unable to find enum 'animal', searching from enum_unknown_inst_value
[info]   ] (SimpleMatchers.scala:34)

(error reported at path .../value instead of the correct path .../enum)

This error is generated by pass SpecsValueTypeDerive which calculates the type of value instance (i. e. expression in the value field). The reason, why enum key influences this pass is because handling of this key implemented as artificial change of the value expression by wrapping it by the Ast.expr.EnumById node.

Then TypeDetector tries to calculate type of this node using ClassTypeProvider.resolveEnum which throws an instance of ExpressionError due to unknown enum. When this exception is caught, it is reported as error in value field.

Possible solution would to not use the Ast.expr.EnumById node, but handle convertion to enum somehow else. Another way is to throw different error when processing Ast.expr.EnumById or somehow else distinguish between throwned errors.