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

std.meta.hasUniqueRepresentation: better support packed structs #20143

Merged
merged 3 commits into from
Jun 2, 2024

Conversation

nektro
Copy link
Contributor

@nektro nektro commented Jun 1, 2024

Closes #17592

lib/std/meta.zig Outdated Show resolved Hide resolved
Co-authored-by: Matthew Lugg <mlugg@mlugg.co.uk>
@Vexu Vexu enabled auto-merge (squash) June 2, 2024 19:22
@@ -1200,6 +1200,8 @@ pub inline fn hasUniqueRepresentation(comptime T: type) bool {
.Array => |info| hasUniqueRepresentation(info.child),

.Struct => |info| {
if (info.layout == .@"packed") return @sizeOf(T) * 8 == @bitSizeOf(T);
Copy link
Sponsor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to use byte_size_in_bits over 8 here based on this comment:

zig/lib/std/mem.zig

Lines 23 to 29 in d74180c

/// The standard library currently thoroughly depends on byte size
/// being 8 bits. (see the use of u8 throughout allocation code as
/// the "byte" type.) Code which depends on this can reference this
/// declaration. If we ever try to port the standard library to a
/// non-8-bit-byte platform, this will allow us to search for things
/// which need to be updated.
pub const byte_size_in_bits = 8;

Apologies if I'm misunderstanding anything :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes but i dont think that assumption will realistically ever be broken. additionally a pass would have to be done over the entire stdlib and compiler to use that constant since its a very new addition

@Vexu Vexu merged commit db75a87 into ziglang:master Jun 2, 2024
10 checks passed
@nektro nektro deleted the patch-3 branch June 2, 2024 23:30
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.

hasUniqueRepresentation incorrectly returns false for packed structs
4 participants