Sema: more runtime indexing comptime value checks

This commit is contained in:
Veikka Tuominen
2022-07-01 21:08:31 +03:00
parent 1569b9c165
commit c07c2d68c7
12 changed files with 98 additions and 88 deletions

View File

@@ -0,0 +1,16 @@
const Foo = struct {
name: []const u8,
T: type,
};
export fn entry() void {
const xx: [2]Foo = .{ .{ .name = "", .T = u8 }, .{ .name = "", .T = u8 } };
for (xx) |f| { _ = f;}
}
// error
// backend=stage2
// target=native
//
// :7:10: error: values of type '[2]tmp.Foo' must be comptime known, but index value is runtime known
// :3:8: note: struct requires comptime because of this field
// :3:8: note: types are not available at runtime

View File

@@ -0,0 +1,20 @@
export fn entry() void {
foo(452) catch |err| switch (err) {
error.Foo ... error.Bar => {},
else => {},
};
}
fn foo(x: i32) !void {
switch (x) {
0 ... 10 => return error.Foo,
11 ... 20 => return error.Bar,
else => {},
}
}
// error
// backend=llvm
// target=native
//
// :2:34: error: ranges not allowed when switching on type '@typeInfo(@typeInfo(@TypeOf(tmp.foo)).Fn.return_type.?).ErrorUnion.error_set'
// :3:19: note: range here

View File

@@ -7,7 +7,7 @@ comptime {
}
// error
// backend=stage1
// backend=stage2
// target=native
//
// tmp.zig:5:26: error: attempt to read 4 bytes from [4]u8 at index 1 which is 3 bytes
// :5:26: error: dereference of '*const u24' exceeds bounds of containing decl of type '[4]u8'

View File

@@ -0,0 +1,20 @@
const Struct = struct {
a: u32,
};
fn getIndex() usize {
return 2;
}
export fn entry() void {
const index = getIndex();
const field = @typeInfo(Struct).Struct.fields[index];
_ = field;
}
// error
// backend=stage2
// target=native
//
// :9:51: error: values of type '[]const builtin.Type.StructField' must be comptime known, but index value is runtime known
// :287:21: note: struct requires comptime because of this field
// :287:21: note: types are not available at runtime
// :290:20: note: struct requires comptime because of this field

View File

@@ -1,14 +0,0 @@
const Foo = struct {
name: []const u8,
T: type,
};
export fn entry() void {
const xx: [2]Foo = undefined;
for (xx) |f| { _ = f;}
}
// error
// backend=stage1
// target=native
//
// tmp.zig:7:5: error: values of type 'Foo' must be comptime known, but index value is runtime known

View File

@@ -1,19 +0,0 @@
export fn entry() void {
try foo(452) catch |err| switch (err) {
error.A ... error.B => {},
else => {},
};
}
fn foo(x: i32) !void {
switch (x) {
0 ... 10 => return error.Foo,
11 ... 20 => return error.Bar,
else => {},
}
}
// error
// backend=stage1
// target=native
//
// tmp.zig:3:17: error: operator not allowed for errors

View File

@@ -1,17 +0,0 @@
const Struct = struct {
a: u32,
};
fn getIndex() usize {
return 2;
}
export fn entry() void {
const index = getIndex();
const field = @typeInfo(Struct).Struct.fields[index];
_ = field;
}
// error
// backend=stage1
// target=native
//
// tmp.zig:9:51: error: values of type 'std.builtin.Type.StructField' must be comptime known, but index value is runtime known