Skip to content

Misleading error message when attempting to perform comptime dereference to pointer whose type doesn't have a well-defined layout when the real pointer value has another type #22613

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
gabeuehlein opened this issue Jan 25, 2025 · 0 comments
Labels
error message This issue points out an error message that is unhelpful and should be improved.
Milestone

Comments

@gabeuehlein
Copy link
Contributor

Zig Version

0.14.0-dev.2922+f77e1b862

Steps to Reproduce and Observed Output

Currently, the Zig compiler emits a poor-quality error message for the following code:

// a.zig
comptime {
    _ = @as(*const []const u8, @ptrCast(&"foobar")).*;
}
$ zig build-obj a.zig
a.zig:2:52: error: comptime dereference requires '[]const u8' to have a well-defined layout
    _ = @as(*const []const u8, @ptrCast(&"foobar")).*;
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
referenced by:
    root: lib/std/start.zig:3:22
    comptime: lib/std/start.zig:27:9
    2 reference(s) hidden; use '-freference-trace=4' to see all references

The real problem is that &"foobar" has a type of *const *const [6:0]u8 and that a *const [6:0]u8 can't be coerced into a []const u8 through a pointer dereference.

Expected Output

Something like the following:

a.zig:2:52: error: cannot reinterpret '*const [6:0]u8' as '[]const u8'
    _ = @as(*const []const u8, @ptrCast(&"foobar")).*;
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
a.zig:0:0: note: '*const [6:0]u8' here
    _ = @as(*const []const u8, @ptrCast(&"foobar")).*;
                                        ^~~~~~~~~
a.zig:0:0: note: casted to '*const []const u8' here
    _ = @as(*const []const u8, @ptrCast(&"foobar")).*;
                               ^~~~~~~~
referenced by:
    root: lib/std/start.zig:3:22
    comptime: lib/std/start.zig:27:9
    2 reference(s) hidden; use '-freference-trace=4' to see all references

Of course, the exact error message depends on how type punning at comptime gets implemented (see #9646 for an example). Under that proposal, I'm pretty sure this would be illegal type punning.
Alternatively, I suppose that implicit pointer-to-array to slice coercions like this could be allowed in some way, which would make the above example compile successfully, but that's a topic for another issue.

@gabeuehlein gabeuehlein added the error message This issue points out an error message that is unhelpful and should be improved. label Jan 25, 2025
@andrewrk andrewrk added this to the unplanned milestone Jan 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error message This issue points out an error message that is unhelpful and should be improved.
Projects
None yet
Development

No branches or pull requests

2 participants