const print = @import("std").debug.print;
var x: [2]bool = .{false, true}; // global runtime value
pub fn main() void {
// 's' is a comptime slice.
const s: []bool = &x;
print("{}\n", .{ @TypeOf(s) }); // []bool
print("{}\n", .{ s[0] }); // false
print("{}\n", .{ @TypeOf(s[0]) }); // bool
print("{}\n", .{ @TypeOf(s.*) }); // bool (should be '[2]bool'?)
}
And the following code doesn’t compile.
var x: [2]bool = .{false, true}; // global runtime value
pub fn main() void { // error: genSetReg called with a value larger than dst_reg
const s: []bool = &x;
const arr = s.*;
_ = arr;
}
Looks like the bug is more so in @TypeOf, seeing a dereference and returning the pointer child type without checking if the dereference is valid, silly considering only 2/4 pointer types support it.
Not sure why you expect the type of deref []T to be []T
Sorry, I meant [2]bool (instead of []bool).
Looks the problem only occurs for comptime slices derived from runtime global arrays.
const print = @import("std").debug.print;
var x: [3]u16 = .{ 1, 2, 3 };
const sx: []u16 = &x;
const y: [3]u16 = .{1, 2, 3};
const sy: []const u16 = &y;
pub fn main() void {
print("{}\n", .{ @TypeOf(sx) }); // []u16
print("{}\n", .{ @TypeOf(sy) }); // []const u16
const ay = sy.*;
print("{}\n", .{ ay[0] }); // 1
const ax = sx.*;
print("{}\n", .{ ax[0] }); // error: type 'u16' does not support indexing
}