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
}
const print = @import("std").debug.print;
var x: [3]u16 = .{ 1, 2, 9 };
const sx: []u16 = &x;
fn f(comptime s: []const u16) void {
print("{}\n", .{ s.*[2] }); // this line works
const arr = s.*; // // error: genSetReg called with a value larger than dst_reg (when without the next line)
print("{}\n", .{ arr[2] }); // error: type 'u16' does not support indexing
}
pub fn main() void {
f(sx);
}
I think this is the problem. Change it to const s = &x; and the result is as expected:
*[2]bool
false
bool
[2]bool
That would turn s into a pointer to an array (*[2]bool), but OP is experimenting with 0.17.0’s new slice-to-array-pointer conversion. A slice with comptime known length should be able to coerce to an array pointer now.