aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorVeikka Tuominen <git@vexu.eu>2022-05-25 17:39:55 +0300
committerVeikka Tuominen <git@vexu.eu>2022-05-25 17:39:55 +0300
commitb0e8bf15f5017cf101eb31f74dd264eaf136045f (patch)
tree3fc4201f24b8672ff7e2414ebd1fde8f24cdcebb /test
parent71e2a56e3ef7aba10cc0648aab786973cf8416bc (diff)
downloadzig-b0e8bf15f5017cf101eb31f74dd264eaf136045f.tar.gz
zig-b0e8bf15f5017cf101eb31f74dd264eaf136045f.zip
Sema: add error for dereferencing comptime value at runtime
Diffstat (limited to 'test')
-rw-r--r--test/cases/compile_errors/dereference_anyopaque.zig50
1 files changed, 50 insertions, 0 deletions
diff --git a/test/cases/compile_errors/dereference_anyopaque.zig b/test/cases/compile_errors/dereference_anyopaque.zig
new file mode 100644
index 0000000000..44636b0851
--- /dev/null
+++ b/test/cases/compile_errors/dereference_anyopaque.zig
@@ -0,0 +1,50 @@
+const std = @import("std");
+
+const Error = error{Something};
+
+fn next() Error!void {
+ return;
+}
+
+fn parse(comptime T: type, allocator: std.mem.Allocator) !void {
+ parseFree(T, undefined, allocator);
+ _ = (try next()) != null;
+}
+
+fn parseFree(comptime T: type, value: T, allocator: std.mem.Allocator) void {
+ switch (@typeInfo(T)) {
+ .Struct => |structInfo| {
+ inline for (structInfo.fields) |field| {
+ if (!field.is_comptime)
+ parseFree(field.field_type, undefined, allocator);
+ }
+ },
+ .Pointer => |ptrInfo| {
+ switch (ptrInfo.size) {
+ .One => {
+ parseFree(ptrInfo.child, value.*, allocator);
+ },
+ .Slice => {
+ for (value) |v|
+ parseFree(ptrInfo.child, v, allocator);
+ },
+ else => unreachable,
+ }
+ },
+ else => unreachable,
+ }
+}
+
+pub export fn entry() void {
+ const allocator = std.testing.allocator_instance.allocator();
+ _ = parse(std.StringArrayHashMap(bool), allocator) catch return;
+}
+
+// error
+// backend=llvm
+//
+// :11:22: error: comparison of 'void' with null
+// :25:51: error: unable to resolve comptime value
+// :25:51: error: unable to resolve comptime value
+// :25:51: error: unable to resolve comptime value
+// :25:51: error: unable to resolve comptime value