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

hasUniqueRepresentation incorrectly returns false for packed structs #17592

Closed
matklad opened this issue Oct 18, 2023 · 0 comments · Fixed by #20143
Closed

hasUniqueRepresentation incorrectly returns false for packed structs #17592

matklad opened this issue Oct 18, 2023 · 0 comments · Fixed by #20143
Labels
bug Observed behavior contradicts documented or intended behavior standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@matklad
Copy link
Contributor

matklad commented Oct 18, 2023

Zig Version

0.11.0

Steps to Reproduce and Observed Behavior

const std = @import("std");
const assert = std.debug.assert;

const S = packed struct(u8) {
    @"0": bool,
    @"1": bool,
    @"2": bool,
    @"3": bool,
    @"4": bool,
    @"5": bool,
    @"6": bool,
    @"7": bool,
};

comptime {
    // This fails, but it should pass, `S` is an `u8` in disguise.
    assert(std.meta.trait.hasUniqueRepresentation(S));
}

Expected Behavior

hasUniqueRepresentation returns true for S.

It seems that packed structs should be specially handled over here:

zig/lib/std/meta/trait.zig

Lines 563 to 573 in aeadcb3

.Struct => |info| {
var sum_size = @as(usize, 0);
inline for (info.fields) |field| {
const FieldType = field.type;
if (comptime !hasUniqueRepresentation(FieldType)) return false;
sum_size += @sizeOf(FieldType);
}
return @sizeOf(T) == sum_size;
},

@matklad matklad added the bug Observed behavior contradicts documented or intended behavior label Oct 18, 2023
@Vexu Vexu added the standard library This issue involves writing Zig code for the standard library. label Oct 18, 2023
@Vexu Vexu added this to the 0.13.0 milestone Oct 18, 2023
cb22 added a commit to tigerbeetle/tigerbeetle that referenced this issue Oct 23, 2023
@Vexu Vexu modified the milestones: 0.14.0, 0.13.0 Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants