# New DSDL directive for simple compile-time checks

**URL:** <https://forum.opencyphal.org/t/new-dsdl-directive-for-simple-compile-time-checks/190>\
**Category:** Development & Maintenance\
**Created:** [October 11, 2018, 12:46pm UTC](https://forum.opencyphal.org/t/new-dsdl-directive-for-simple-compile-time-checks/190 "2018-10-11T12:46:06Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![kjetilkjeka](https://forum.opencyphal.org/user_avatar/forum.opencyphal.org/kjetilkjeka/32/43_2.png) [@kjetilkjeka](https://forum.opencyphal.org/u/kjetilkjeka)\
**Post date:** [October 19, 2018, 9:00am UTC](https://forum.opencyphal.org/t/new-dsdl-directive-for-simple-compile-time-checks/190/8 "2018-10-19T09:00:38Z")

</div>

> [@pavel.kirienko](#):
>
> We want to be sure that fields that follow variable-length entities are still byte-aligned (or otherwise aligned) at all times. Here’s an example:
> 
> ```auto
> uint7 node_id
> void1
> @assert offset % 8 == 0
> void3
> uint8[<=16] unique_id
> @assert min_offset / 8 == 2
> @assert max_offset / 8 == 18
> 
> # Making sure that the next field is always aligned
> @assert min_offset % 8 == 0
> @assert max_offset % 8 == 0
> uint16 foo
> 
> # Eventually (in a future release) we could go further and ask the compiler to
> # verify the offset against every possible permutation of the preceding
> # variable-length fields.
> @assert any_offset % 8 == 0 # I have not given much thought to this yet
> 
> @assert max_offset / 8 < 400
> 
> ```
> 
> Yes, it won’t work with variable-length fields where the element sizes are not a multiple of 8 bits, but those are rare (they don’t exist in the standard data type set, for example).

This is a big problem with min/max offset. We can only check the arrays that are incredibly hard to get wrong, to begin with. We could make `@assert aligned` work equal to your proposed `@assert any_offset % 8 == 0` without needing to reason about min/max/any offsets and mod operators.

> [@pavel.kirienko](#):
>
> I am not sure why do we need `@assert aligned data` or `end` since the same goals can be achieved by moving the assertion statement after the field it is intended to protect.
> 
> ```auto
> @assert aligned
> uint7 data0
> uint8[<=1] data1
> @assert aligned # Applies both to the above and to the below
> void3
> uint5 data2
> @assert aligned
> void2
> uint64 data3
> 
> ```
> 
> Stuff like `@assert aligned +2` doesn’t seem to be really needed, does it? Unconventional checks can always be implemented by checking against the offset variables directly, e.g. `@assert (offset - 2) % 8 == 0`

Do you take the challange to remove `end` and `data` in the following examples?

```python
void7
@assert aligned data
uint47[<=1] data

```

```python
void5
@assert aligned data # After you've looked up SomeType you will realize that the definition is not a multiple of 8 bytes
SomeType[<=7] data

```

```python
void5
@assert aligned end
SomeType[13]

```

> [@pavel.kirienko](#):
>
> If strongly desired, we could add some _sugar_ that would expand as follows:
> 
> ```auto
> @assert aligned
> # Is equivalent to:
> @assert min_offset % 8 == 0
> @assert max_offset % 8 == 0
> 
> ```

`@assert aligned` should check that it’s always aligned. Not just in 2 arbitrarily chosen cases (min/max length).

---

_[View the full topic](https://forum.opencyphal.org/t/new-dsdl-directive-for-simple-compile-time-checks/190)._
