Skip to content

Comptime ptrs incorrectly compare as unequal, if derived in different ways #11278

@topolarity

Description

@topolarity

Zig Version

0.10.0-dev.1534+1cf134632

Steps to Reproduce

test {
    try testPtrEql(); // passes
    try comptime testPtrEql(); // fails
}

fn testPtrEql() !void {
    const S = extern struct {
        a: f32,
        b: [4]u8,
        c: f32,
    };

    var s = S{ .a = 1.5, .b = [_]u8{1, 2, 3, 4}, .c = 2.5};
    var ptr1: *const u8 = &s.b[0];
    var ptr2: *const u8 = &@as(*const [@sizeOf(S)] u8, @ptrCast(&s))[4];
    _ = &ptr1;
    _ = &ptr2;

    // These two pointers are derived in different ways, but
    // they should still be equal
    try expect(ptr1 == ptr2);
}

This can also be done in any number of other ways. For example, by creating a [*]T pointing to one field and constructing an elem ptr that happens to line up with another field.

Expected Behavior

Test should pass.

Actual Behavior

Fails in both stage1 and stage2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions