Skip to content

sema: do checked cast when resolving aggregate size #23516

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

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

Conversation

dweiller
Copy link
Contributor

@dweiller dweiller commented Apr 9, 2025

Fixes #17664, fixes #23511.

When setting the layout for a struct or union, we cannot do an unchecked @intCast as it is possible the sum of field sizes is greater than std.math.maxInt(u32). This PR adds a new compile error when the resolved size of a union or struct would be greater than std.math.maxInt(u32) to tell the user that their union or struct is too large.

I haven't looked into the reason why, but the compiler seems fine to have tuple structs with sizes up to std.math.maxInt(usize). This feels somewhat inconsistent on 64 bit machines, but this PR is not aiming to change anything about what sizes for aggregate types are supported.

Feel free to bikeshed the wording of the new compile error, it currently looks like:

test/cases/compile_errors/aggregate_too_large.zig:5:11: error: struct layout requires unsupported size: maximum is 4294967295, got 4294967296
const T = struct {
          ^~~~~~

@alexrp alexrp requested a review from mlugg April 9, 2025 08:36
@alexrp alexrp added this to the 0.14.1 milestone Apr 9, 2025
@dweiller dweiller force-pushed the struct-size-check branch from b72cee8 to 676fe0f Compare April 9, 2025 11:03
@dweiller dweiller force-pushed the struct-size-check branch from 676fe0f to 8f4ad16 Compare April 9, 2025 14: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.

@sizeOf() reports incorrect size of structs over 4 GiB crash with struct size exceeding u32
2 participants