I think it depends on what you mean with better, I think it may be the more convenient solution, but using align(1)
everywhere effectively would turn of alignment checks and then the code just requires to be used with actual alignments that work on the target platform. Basically telling “trust me bro” to the compiler and then possibly not making sure of the right alignment anywhere.
This topic is named preventing absence of @alignCast
, if everything is declared with align(1)
then you never enforce an @alignCast
going from *anyopaque
to a function pointer, so it seems like the opposite of what the topic was about.
It seems similar to using []const u8
to refer to strings where you know that they happen to be zero terminated and then passing the .ptr
to C functions, it works, but it means that you are removing guard rails and some day it may cause some foot-shooting to you or somebody else.
Still there may be situations where removing guard rails like this makes sense, but it probably would be nice if it was only done in “restricted areas”, where you have alternative measures that somehow bring back some of the lost checking through alternative tools / asserts / tests / build-steps / linters etc.
Using align(4)
everywhere may be very inconvenient but it would ensure that it always works because it would prevent absence of @alignCast
when going from *anyopaque
to function pointer.
When something is align(4)
it also can be used as align(1)
because higher alignments are compatible with lower alignments, but going from lower to higher alignment always requires a check (if there isn’t some code that ensures that the higher alignment is the actual alignment).
I think over-aligning the functions on x86 to align(4)
would be the solution that keeps the type system checks for alignment enabled and making it compatible with ARM, setting it to align(1)
would disable alignment checks and rely on “lets hope every part just happens to do the right thing for the architecture we are on”, basically asking for align(1)
, but hoping to always get align(4) where it is needed.
I also think that the actual solution may be none of the two.
If we want code to run on both x86 and ARM the best thing to do may be to actually test the code on both platforms, through those tests we would discover cases where an @alignCast
is missing, without forcing any non-default alignments.