Skip to content

Java: incompatible types compile error when type that is reused is used in a type that is unreachable #961

Description

@Kh4n

Here is a buggy .ksy:

meta:
  id: bug
  file-extension: na
  endian: le
seq:
  - id: body
    type: type_inner
# uncomment to fix bug
#  - id: body2
#    type: type_unused
types:
  type_reused:
    seq:
      - id: count
        type: s4
  type_inner:
    seq:
      - id: file_name
        type: type_reused
  type_unused:
    seq:
      - id: source_code_loc
        type: type_reused

The compile error is:

/home/kh4n/Documents/jt-loader/target/generated-sources/kaitai/name/kh4n/jtloader/Bug.java:[110,59] incompatible types: name.kh4n.jtloader.Bug.TypeUnused cannot be converted to name.kh4n.jtloader.Bug.TypeInner

It is caused because type_reused (generated as TypeReused) has its _parent set to Bug.TypeInner:

        public TypeReused(KaitaiStream _io, Bug.TypeInner _parent, Bug _root) {
            super(_io);
            this._parent = _parent;
            this._root = _root;
            _read();
        }
...

If you uncomment this becomes:

        public TypeReused(KaitaiStream _io, KaitaiStruct _parent, Bug _root) {
            super(_io);
            this._parent = _parent;
            this._root = _root;
            _read();
        }

as it should. Manually changing the generated file also works. Not a huge issue because you can just use the type somewhere, but worth mentioning nonetheless as it is pretty common to have a type defined and then used somewhere that is currently unreachable as you are prototyping, which can be very confusing to debug.

I think it might be caused by an optimization which notices that type_unused is unreachable so it makes type_reused's parent type_inner (the only place it is technically used) but then the type_unused code is still emitted. I don't know the code base but if someone points me to possible file(s) I can try to fix the issue :). I will try my best to solve it on my own.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions