Skip to content

Conversation

@afloram
Copy link
Contributor

@afloram afloram commented Aug 8, 2025

This is a new check that reports an error if a Struct, Union, or Exception (ast.Struct) exceeds a specified maximum depth.

The base depth is 1 (to leave 0 as not having a specified depth from the config). If a struct has a field whose type is also a struct, the depth of the parent struct is now 2.

It can be configured with:

  • max: The maximum allowed depth. This is used for every node mentioned above. However, a node can specify its own allowed depth with a maxDepth annotation.
  • allow_cycles:
    • true: when we find a cycle, we stop the traversal and the depth up to that point is simply another candidate for the max depth of the current node that is being checked.
    • false: errors are reported for cycles. It can also be thought of as cycles having infinite depth.

Note that lists, maps (key and value separately), and sets, also count as adding 1 unit of depth. That said, it'd be very simple to adjust this, and we could even make it configurable, not only with whether they should count or not, but by how much.

There are many things that could probably be improved, with one of the big ones being the fact that right now I'm modifying things outside of the check for purposes really only needed for this check. Tried some stuff like extracting code to have different functions for different use-cases, but wasn't liking the direction that was taking.

Caching the Parse results brings the time from ~18s to ~1.8s in my setup when running just this check with a max depth of 99999 and and allowing cycles (to explore as much as possible without returning early).

Tests are missing because I wanted to first discuss this with you @jparise. Though to be fair they could have helped make this more understandable.

While working on this I encountered a bunch of errors with edge-cases, and I wouldn't be surprised if there are more. Tests could also help with that.

@afloram
Copy link
Contributor Author

afloram commented Aug 8, 2025

Another thing could be adding the typedef and intermediate non-struct depth jumps to the path to make the error messages clearer.

@afloram afloram changed the title [WIP] Add depth check Add depth check Sep 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant