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

Using mem.writeInt at comptime can change the type of a structs field #1866

Closed
Hejsil opened this Issue Jan 3, 2019 · 1 comment

Comments

Projects
None yet
2 participants
@Hejsil
Copy link
Member

Hejsil commented Jan 3, 2019

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

const Bytes = struct {
    bytes: [4]u8,

    pub fn init(v: u32) Bytes {
        var res: Bytes = undefined;
        mem.writeIntLittle(u32, &res.bytes, v);

        return res;
    }
};

test "" {
    const numLittle = comptime Bytes.init(0x12345678);
    debug.assert(mem.eql(u8, []u8{ 0x78, 0x56, 0x34, 0x12 }, numLittle.bytes));
}
test.zig:18:71: error: expected type '[]const u8', found 'u32'
    debug.assert(mem.eql(u8, []u8{ 0x78, 0x56, 0x34, 0x12 }, numLittle.bytes));
                                                                      ^

@andrewrk andrewrk added this to the 0.4.0 milestone Jan 3, 2019

@andrewrk andrewrk added the bug label Jan 3, 2019

@andrewrk

This comment has been minimized.

Copy link
Member

andrewrk commented Jan 3, 2019

I'm guessing that some analyze code incorrectly used the type of the ConstExprVal rather than the type from the type system. Recently the codebase has moved toward the type of ConstExprVal being allowed to disagree with the type system type (which represents a comptime pointer cast). So this introduced new bugs. On the plus side, more advanced kinds of pointer casting are now working at comptime.

@Hejsil Hejsil referenced this issue Jan 6, 2019

Merged

Fix #1866 #1874

andrewrk added a commit that referenced this issue Jan 6, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.