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

Host Pointer Size Not Exposed At Comptime #14568

Closed
nsmryan opened this issue Feb 5, 2023 · 2 comments
Closed

Host Pointer Size Not Exposed At Comptime #14568

nsmryan opened this issue Feb 5, 2023 · 2 comments
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.

Comments

@nsmryan
Copy link

nsmryan commented Feb 5, 2023

Zig Version

0.10.0-dev.4707+209a0d2a8

Steps to Reproduce and Observed Behavior

Pointers to fields within a packed struct have a field bit-width in bits and a 'pointer host size' (size of the backing integer in bytes) attached to them that are not part of normal pointers.

This information can be provided when creating a pointer through the 'align' keyword, as in the following program which creates a pointer to a field in a packed struct and prints out the information about the pointer that is available through '@typeinfo':

const std = @import("std");

const S = packed struct {
    a: u4,
    b: u4,
};

pub fn main() void {
    var s: S = S{ .a = 1, .b = 2 };

    // First entry: alignment in bytes.
    // Second entry: size of the field itself in bits.
    // Third entry: size of the backing integer in bytes.
    const ptr: *align(1:2:1) u2 = @ptrCast(*align(1:2:1) u2, &s.b);

    std.debug.print("@typeInfo(ptr) = {}\n", .{@typeInfo(@TypeOf(ptr))});
}

However, once the pointer has been created there is no way to retrieve this information. It is not exposed through std.builtin.Type.

I believe that field width could be determined from std.builtin.Type.Pointer.child using '@bitSizeOf' (perhaps I'm missing a detail there), but the 'host pointer size' is not available.

Without this information, I cannot extract and store this information to later re-synthesize the pointer if it points within a packed struct.

Expected Behavior

I expect to be able to retrieve all information about a pointer using '@typeinfo' including the 'host pointer size'.

More generally: I expect that a pointer can be mapped to a std.builtin.Type structure and back without loss of information.

@nsmryan nsmryan added the bug Observed behavior contradicts documented or intended behavior label Feb 5, 2023
@Vexu Vexu added enhancement Solving this issue will likely involve adding new logic or components to the codebase. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. and removed bug Observed behavior contradicts documented or intended behavior labels Feb 5, 2023
@Vexu Vexu added this to the 0.12.0 milestone Feb 5, 2023
@rohlem
Copy link
Contributor

rohlem commented Feb 5, 2023

Duplicate of #6846 iiuc. (This one has more text, so I guess we can close the old one if you want.)

@nsmryan
Copy link
Author

nsmryan commented Feb 5, 2023

I didn't see that issue. It does seem like the same concept, although I appreciate the point that it makes that perhaps Pointer.alignment is where the information is missing.

#6846 has a later milestone currently, so I favor #14568 :)

@Vexu Vexu closed this as completed Feb 5, 2023
@Vexu Vexu removed this from the 0.12.0 milestone Feb 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

3 participants