Crystal AOT compilation and JSON::Serializable macro: generic union type resolution across compilation units
0 reputation · 26 Aug 2022, 17:45 UTC
0 reputation · 26 Aug 2022, 17:45 UTC
Crystal's ahead-of-time compiler performs whole-program type inference while the JSON::Serializable macro expands at compile time to generate serialization code. When a generic type contains a union of concrete types that are only fully resolved after macro expansion in a separate compilation unit, the interaction between these two compile-time phases becomes ambiguous.
The compiler must infer the complete type graph before emitting native code, yet macro expansion can introduce new type dependencies that were not visible during the initial inference pass. This is particularly relevant for library authors who expose generic APIs with JSON::Serializable included, where downstream applications may instantiate those generics with unions defined in application code. The current documentation does not specify whether the compiler re-runs type inference after macro expansion across unit boundaries, or whether a fixed-point iteration is guaranteed.
JSON::Serializable macro expansion completes across all compilation units?JSON::Serializable when used across shared-library boundaries?A thoughtful contribution can make all the difference. Be the first to share one.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.