Skip to content

LLVM backend fails to lower comptime-known packed struct value containing pointer #24166

Closed as duplicate of#21463
@dvmason

Description

@dvmason

Zig Version

0.15.0-dev.736+b6d904624

Steps to Reproduce and Observed Behavior

Minimum reproducible error.

Save the following as bug24166.zig and zig test bug24166.zig

const std = @import("std");
const lastPredefinedSymbol = 53;
const staticSymbols =  blk: {
    var symbolArray = [_]PointedObject{undefined} ** lastPredefinedSymbol;
    for (symbolArray[0..]) |*sym|
         initSymbol(sym);
    break :blk symbolArray;
};
fn initSymbol(sym: *PointedObject) void {
    sym.header = HeapHeader{
        .classIndex = .Symbol, .hash = 42, .format = .notIndexable, .age = .static, .length = 1 };
}
inline fn symbol_of(index: u24, _: u4) Object {
    return Object{ .ref = @ptrCast(&staticSymbols[index - 1]) };
}
// inline
fn symbol0(index: u24) Object {
    return symbol_of(index, 0);
}
inline
fn symbolX(index: u24) Object {
    return symbol_of(index, 0);
}
const symbols = struct {
    const value = symbol0(2);
};
test "make it run" {
    std.debug.print("All three of these should be the same:\n", .{});
    std.debug.print("the compile-time value:     {}\n", .{ symbols.value.raw()});
    std.debug.print("the run-time value:         {}\n", .{ symbol0(2).raw()});
    std.debug.print("not inlined run-time value: {}\n", .{ symbolX(2).raw()});
}
const Object = packed struct {
    ref: *const u64,
    fn raw(self: Object) u64 {
        return @bitCast(self);
    }
};
const PointedObject = packed struct {
    header: HeapHeader,
    data: packed union {
        int: i64,
        unsigned: u64,
    },
};
const HeapHeader = packed struct(u64) {
    classIndex: ClassIndex = .none,
    hash: u24 = 0,
    format: Format = .free,
    immutable: bool = false,
    age: Age = .nursery,
    length: u11 = 0,
    forwarded: bool = false,
};
const Age = enum(u4) {
    nursery,
    static,
};
const Format = enum(u7) {
    notIndexable, // this is an Object with no indexable values
    free,
};
const ClassIndex = enum(u16) {
    none = 0,
    Symbol,
};

Expected Behavior

The three lines should print the same value.

There are actually 2 (probably related) bugs here:

  1. the compile-time value is not initialized to the proper value
  2. the inlined run-time value is not correct - probably because it uses the same compile-time execution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    backend-llvmThe LLVM backend outputs an LLVM IR Module.bugObserved behavior contradicts documented or intended behaviormiscompilationThe compiler reports success but produces semantically incorrect code.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions