Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support sizeof and alignof inside macros for stable types #15497

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

HertzDevil
Copy link
Contributor

This PR enables the use of sizeof and alignof in macro expressions when the type's size and alignment are known to be stable, i.e. their values cannot be changed by adding new code. The idea was raised in this forum thread and possibly in other GitHub issues as well. With this PR you can write compile-time checks like:

{% unless sizeof(Void*) == sizeof(LibC::SizeT) %}
  {% raise "we don't allow the x86 Real Mode here" %}
{% end %}

All types are stable, except for the following:

  • Non-primitive structs, e.g. Bytes
  • ReferenceStorage instances
  • Modules, e.g. GC (not to be confused with GC.class, all metaclasses are stable)
  • Uninstantiated generic types, e.g. Array
  • Annotations, e.g. x = uninitialized Flags (these "types" are ill-behaved anyway)
  • StaticArray, Tuple, NamedTuple, and Union instances containing any unstable constituent types

typeof is disallowed, because we obviously cannot do any kind of non-macro semantic analysis inside macros.

Although the non-macro sizeof and alignof support constants, their macro counterparts do not, even when constant evaluates to a simple compile-time literal (A = 1; {{ sizeof(A) }}).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant