Skip to content
This repository was archived by the owner on Nov 26, 2025. It is now read-only.
This repository was archived by the owner on Nov 26, 2025. It is now read-only.

sizeof returns comptime int #225

@MasonRemaley

Description

@MasonRemaley

Right now, sizeof(T) is translated to @sizeOf(T). However @sizeOf(T) returns comptime_int whereas in C's sizeof returns size_t.

This discrepancy often won't matter, but it can cause translation to fail if the result gets fed into @bitCast. alignof has the same issue.

I believe the fix is to translate sizeof(T) to @as(usize, @sizeOf(T)) though I have to make sure this doesn't break implicit coercions that C expects to be able to do or something.

The change would probably look something like this, once #204 merges I can test to see if this works and PR it:

MacroTranslator.zig

// Also needs to be applied to `alignof`
.keyword_sizeof => {
    mt.i += 1;
    const operand = if (mt.eat(.l_paren)) blk: {
        const inner = (try mt.parseCTypeName(scope, false)).?;
        try mt.expect(.r_paren);
        break :blk inner;
    } else try mt.parseCUnaryExpr(scope);

    const size = try mt.t.createHelperCallNode(.sizeof, &.{operand});
    const size_t = try ZigTag.type.create(mt.t.arena, "size_t");
    return mt.t.createHelperCallNode(.cast, &.{ size_t, size });
},

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions