zig icon indicating copy to clipboard operation
zig copied to clipboard

Compiler crashes when comptime_int literal is involved in ptrCast

Open Endeg opened this issue 1 year ago • 1 comments

Zig Version

0.13.0-dev.46+3648d7df1

Steps to Reproduce and Observed Behavior

OS: Windows

Here's two programs that reproduce this problem:

const std = @import("std");

pub fn foo(
    start: *anyopaque,
    row_ind: usize,
    value: anytype,
) void {
    // Compiler output is not available, when compiler crashes
    // @compileLog(@TypeOf(value));
    const T = @TypeOf(value);
    const typed_column: [*]T = @alignCast(@ptrCast(start));
    typed_column[row_ind] = value;
}

pub fn main() !void {
    const start: *anyopaque = undefined;
    foo(start, 32, 80);
    // No crash when type is provided
    // foo(start, 32, @as(u16, 80));
}
const std = @import("std");

pub fn main() !void {
    var data = std.mem.zeroes([128]u8);
    const typed_column: [*]comptime_int = @alignCast(@ptrCast(&data[0]));
    // No crash when type is not comptime_int
    // const typed_column: [*]u16 = @alignCast(@ptrCast(&data[0]));
    typed_column[32] = 80;
}

Building this program results in error code:

>zig build-exe src\main.zig
>echo %errorlevel%
-2147483645

Building when type info is provided:

>zig build-exe src\main.zig
>echo %errorlevel%
0

Expected Behavior

Compiler provides an error message about comptime_int not allowed in ptrCast'ing.

Endeg avatar Apr 27 '24 08:04 Endeg

Zig Version: 0.13.0-dev.49+7053eb06c

Stack Trace
thread 233764 panic: reached unreachable code
/home/david/Code/zig/src/type.zig:2228:33: 0x1abe402 in intInfo (zig)
                .simple_type => unreachable, // handled via Index enum tag above
                                ^
/home/david/Code/zig/src/codegen/llvm.zig:4225:70: 0x1b3d68b in lowerBigInt (zig)
        return o.builder.bigIntConst(try o.builder.intType(ty.intInfo(mod).bits), bigint);
                                                                     ^
/home/david/Code/zig/src/codegen/llvm.zig:3729:35: 0x18850aa in lowerValue (zig)
                return lowerBigInt(o, ty, bigint);
                                  ^
/home/david/Code/zig/src/codegen/llvm.zig:4736:42: 0x20784d7 in resolveValue (zig)
        const llvm_val = try o.lowerValue(val.toIntern());
                                         ^
/home/david/Code/zig/src/codegen/llvm.zig:4727:47: 0x2078271 in resolveInst (zig)
        const llvm_val = try self.resolveValue((try self.air.value(inst, mod)).?);
                                              ^
/home/david/Code/zig/src/codegen/llvm.zig:8869:49: 0x20b7d5d in airStore (zig)
        const src_operand = try self.resolveInst(bin_op.rhs);
                                                ^
/home/david/Code/zig/src/codegen/llvm.zig:4907:53: 0x1d27855 in genBody (zig)
                .store_safe     => try self.airStore(inst, true),
                                                    ^
/home/david/Code/zig/src/codegen/llvm.zig:1709:19: 0x1d213d6 in updateFunc (zig)
        fg.genBody(air.getMainBody()) catch |err| switch (err) {

Rexicon226 avatar Apr 27 '24 08:04 Rexicon226