google / emboss

Emboss is a tool for generating code that reads and writes binary data structures.
Apache License 2.0
71 stars 21 forks source link

`requires` on a field cannot reference other fields #124

Open fsareshwala opened 6 months ago

fsareshwala commented 6 months ago

Suppose I had the following Emboss definition. Such a definition creates a requirement bound on the value of bar while also exposing max_value virtual field programmatically via Foo::max_value(). It allows developers to keep the minimum or maximum bounds defined in one place and referred to elsewhere, including their code.

struct Foo:
  let max_value = 128
  0 [+1] UInt bar
  [requires: 0 <= this <= max_value]

However, this is currently not possible. According to the language reference, "for [requires] on a field, other fields may not be referenced."

There is a workaround:

struct Foo:
  [requires: 0 <= bar <= max_value]
  let max_value = 128

  0 [+1] UInt bar

While this accomplishes the same goal, I would say it's not as good as being able to refer to other fields from a [requires] on a field itself. If I had multiple of these requirement blocks, it could get pretty unwieldy to have them all at the top instead of closer to where they semantically matter.

fsareshwala commented 6 months ago

Note that #126 makes the workaround currently a bit weak. If we can fix #126, the workaround will at least be a bit stronger.