Skip to content

feat: add type size/alignment hover information#3175

Closed
ShadowCurse wants to merge 3 commits intozigtools:masterfrom
ShadowCurse:add_size_align
Closed

feat: add type size/alignment hover information#3175
ShadowCurse wants to merge 3 commits intozigtools:masterfrom
ShadowCurse:add_size_align

Conversation

@ShadowCurse
Copy link
Copy Markdown

Add a basic version of size/alignment calculations for types and print these values as a part of the hover tool tip.
There are 2 main paths to get this info:

  1. InternPool can calculate size/alignment for types it contain, but it does not contain many things
  2. AST path can calculate sizes as well, but has some nuances

In general, this machinery seems to function pretty well from my testing. There are some limitations though:

  • anything requiring comptime does not show any info. It also recursively prevents parent types from showing any info as well
  • extern structs cannot have proper size calculations since AST can mess up the field ordering

Maybe extern can be solved if someone more knowledgeable about internals can take a look at the problem.

Fixes: #1677

@ShadowCurse ShadowCurse force-pushed the add_size_align branch 2 times, most recently from 0cdb45e to 5d32009 Compare April 16, 2026 00:31
Add utility functions for calculation of size and alignment of a given
type. These will be used in the following commit to provide additional
hover information.

Signed-off-by: ShadowCurse <shadowcurse@noreply.codeberg.org>
Add functions for size/alignment retrieval for types. The implementation
tries to get size/align info from InternPool if it can, but since not
all types exist there (as far as I can see, only primitive types do),
there is an alternative calculation path using the AST directly. This
seems to work pretty well, but has limitations that  for `extern` types
it is not possible to correctly calculate the size since the order of
fields may be unstable. Additionally, manually `align(..)`ed fields also
do not work in this version.

Next commit will use these to add size/alignment info to the hover
tooltips.

Signed-off-by: ShadowCurse <shadowcurse@noreply.codeberg.org>
Wire up functions from previous commits to display basic size/alignment
information for types.

Current limitations:
- types which require comptime evaluation do not show anything
  - if such type is a field, the parent will not show anything as well
- `extern` types do not calculate size correctly due to AST not having
  stable field ordering

Signed-off-by: ShadowCurse <shadowcurse@noreply.codeberg.org>
@Techatrix
Copy link
Copy Markdown
Member

I appreciate your work on this issue, however I do not believe that a good solution to #1677 is reimplement the size calculation logic of the Zig compiler in ZLS at this time. Especially when dealing with properties like size and alignment, you would like to put your trust into tools to always tell you the correct answer. But this implementation is too buggy and maintenance heavy that I'd rather wait until we can query the compiler to get reliable results from the source of truth. There is no way around this anyway since some types like auto structs have implementation defined size. The compiler is free to reorder fields, add padding or insert "invisible" fields however it may wish. Any standalone implementation of this in ZLS would be an approximation at best.

@Techatrix Techatrix closed this Apr 21, 2026
@ShadowCurse
Copy link
Copy Markdown
Author

I see your points and do agree with them. This compiler query feature you mentioned seems like the exact solution we need here indeed.
Anyway, I appreciate you taking a look at this.

@ShadowCurse ShadowCurse deleted the add_size_align branch April 21, 2026 21:17
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.

show size/alignment/padding in tooltip for type annotations

2 participants