authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-05-25 17:39:55+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-05-25 17:39:55+03:00
logb0e8bf15f5017cf101eb31f74dd264eaf136045f
tree3fc4201f24b8672ff7e2414ebd1fde8f24cdcebb
parent71e2a56e3ef7aba10cc0648aab786973cf8416bc

Sema: add error for dereferencing comptime value at runtime


2 files changed, 53 insertions(+), 0 deletions(-)

src/Sema.zig+3
......@@ -20961,6 +20961,9 @@ fn analyzeLoad(
2096120961 }
2096220962 }
2096320963
20964 const valid_rt = try sema.validateRunTimeType(block, src, elem_ty, false);
20965 if (!valid_rt) return sema.failWithNeededComptime(block, src);
20966
2096420967 try sema.requireRuntimeBlock(block, src);
2096520968 return block.addTyOp(.load, elem_ty, ptr);
2096620969}
test/cases/compile_errors/dereference_anyopaque.zig created+50
......@@ -0,0 +1,50 @@
1const std = @import("std");
2
3const Error = error{Something};
4
5fn next() Error!void {
6 return;
7}
8
9fn parse(comptime T: type, allocator: std.mem.Allocator) !void {
10 parseFree(T, undefined, allocator);
11 _ = (try next()) != null;
12}
13
14fn parseFree(comptime T: type, value: T, allocator: std.mem.Allocator) void {
15 switch (@typeInfo(T)) {
16 .Struct => |structInfo| {
17 inline for (structInfo.fields) |field| {
18 if (!field.is_comptime)
19 parseFree(field.field_type, undefined, allocator);
20 }
21 },
22 .Pointer => |ptrInfo| {
23 switch (ptrInfo.size) {
24 .One => {
25 parseFree(ptrInfo.child, value.*, allocator);
26 },
27 .Slice => {
28 for (value) |v|
29 parseFree(ptrInfo.child, v, allocator);
30 },
31 else => unreachable,
32 }
33 },
34 else => unreachable,
35 }
36}
37
38pub export fn entry() void {
39 const allocator = std.testing.allocator_instance.allocator();
40 _ = parse(std.StringArrayHashMap(bool), allocator) catch return;
41}
42
43// error
44// backend=llvm
45//
46// :11:22: error: comparison of 'void' with null
47// :25:51: error: unable to resolve comptime value
48// :25:51: error: unable to resolve comptime value
49// :25:51: error: unable to resolve comptime value
50// :25:51: error: unable to resolve comptime value