From b09c317ae7c6ff60ba195848ba61d725a730594d Mon Sep 17 00:00:00 2001 From: Techatrix <19954306+Techatrix@users.noreply.github.com> Date: Tue, 24 Jan 2023 22:07:19 +0100 Subject: [PATCH] bug hunting --- src/ComptimeInterpreter.zig | 18 ++++++++++++------ src/DocumentStore.zig | 4 ++++ src/Server.zig | 2 +- src/analyser/InternPool.zig | 6 ++++-- src/analyser/completions.zig | 11 ++++++----- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/ComptimeInterpreter.zig b/src/ComptimeInterpreter.zig index 794de32..49cc24e 100644 --- a/src/ComptimeInterpreter.zig +++ b/src/ComptimeInterpreter.zig @@ -46,15 +46,19 @@ pub fn recordError( comptime fmt: []const u8, args: anytype, ) error{OutOfMemory}!void { - try interpreter.errors.put(interpreter.allocator, node_idx, .{ + const message = try std.fmt.allocPrint(interpreter.allocator, fmt, args); + errdefer interpreter.allocator.free(message); + const previous = try interpreter.errors.fetchPut(interpreter.allocator, node_idx, .{ .code = code, - .message = try std.fmt.allocPrint(interpreter.allocator, fmt, args), + .message = message, }); + if (previous != null) interpreter.allocator.free(message); } pub fn deinit(interpreter: *ComptimeInterpreter) void { - var err_it = interpreter.errors.iterator(); - while (err_it.next()) |entry| interpreter.allocator.free(entry.value_ptr.message); + for (interpreter.errors.values()) |err| { + interpreter.allocator.free(err.message); + } interpreter.errors.deinit(interpreter.allocator); interpreter.ip.deinit(interpreter.allocator); @@ -302,8 +306,8 @@ pub fn interpret( const decl = ast.varDecl(tree, node_idx).?; - const type_value = if (decl.ast.type_node != 0) try ((try interpreter.interpret(decl.ast.type_node, namespace, .{})).getValue()) else null; - const init_value = if (decl.ast.init_node != 0) try ((try interpreter.interpret(decl.ast.init_node, namespace, .{})).getValue()) else null; + const type_value = if (decl.ast.type_node != 0) (try interpreter.interpret(decl.ast.type_node, namespace, .{})).maybeGetValue() else null; + const init_value = if (decl.ast.init_node != 0) (try interpreter.interpret(decl.ast.init_node, namespace, .{})).maybeGetValue() else null; if (type_value == null and init_value == null) return InterpretResult{ .nothing = {} }; @@ -470,6 +474,8 @@ pub fn interpret( const can_have_fields: bool = switch (inner_lhs) { .simple => |simple| switch (simple) { .type => blk: { + if (irv.val == .none) break :blk true; + const ty_key = interpreter.ip.indexToKey(irv.val); if (interpreter.huntItDown(ty_key.getNamespace(), field_name, options)) |decl| { return InterpretResult{ .value = Value{ diff --git a/src/DocumentStore.zig b/src/DocumentStore.zig index a94fbca..27903a4 100644 --- a/src/DocumentStore.zig +++ b/src/DocumentStore.zig @@ -68,6 +68,10 @@ pub const Handle = struct { associated_build_file: ?Uri = null, pub fn deinit(self: *Handle, allocator: std.mem.Allocator) void { + if (self.interpreter) |interpreter| { + interpreter.deinit(); + allocator.destroy(interpreter); + } self.document_scope.deinit(allocator); self.tree.deinit(allocator); allocator.free(self.text); diff --git a/src/Server.zig b/src/Server.zig index 82c0d71..d5a08b8 100644 --- a/src/Server.zig +++ b/src/Server.zig @@ -361,7 +361,7 @@ fn generateDiagnostics(server: *Server, handle: DocumentStore.Handle) error{OutO var err_it = int.errors.iterator(); while (err_it.next()) |err| { - try diagnostics.append(allocator, .{ + diagnostics.appendAssumeCapacity(.{ .range = offsets.nodeToRange(tree, err.key_ptr.*, server.offset_encoding), .severity = .Error, .code = .{ .string = err.value_ptr.code }, diff --git a/src/analyser/InternPool.zig b/src/analyser/InternPool.zig index fdc93e4..07fb94e 100644 --- a/src/analyser/InternPool.zig +++ b/src/analyser/InternPool.zig @@ -321,7 +321,8 @@ pub const Key = union(enum) { .@"anyframe", .null_type, .undefined_type, - .enum_literal_type, => true, + .enum_literal_type, + => true, .undefined_value, .void_value, @@ -344,7 +345,8 @@ pub const Key = union(enum) { .union_type, .tuple_type, .vector_type, - .anyframe_type, => true, + .anyframe_type, + => true, .int_u64_value, .int_i64_value, diff --git a/src/analyser/completions.zig b/src/analyser/completions.zig index 207d12c..14e0eee 100644 --- a/src/analyser/completions.zig +++ b/src/analyser/completions.zig @@ -10,7 +10,7 @@ pub fn dotCompletions( ip: *InternPool, ty: InternPool.Index, val: InternPool.Index, - node: ?Ast.Node.Index + node: ?Ast.Node.Index, ) error{OutOfMemory}!void { _ = node; @@ -40,7 +40,7 @@ pub fn dotCompletions( } }, .union_type => {}, // TODO - .enum_type => |enum_info|{ + .enum_type => |enum_info| { for (enum_info.fields) |field| { const field_name = ip.indexToKey(field.name).bytes; try completions.append(arena, .{ @@ -70,7 +70,7 @@ pub fn dotCompletions( .kind = .Field, .detail = "usize", }); - } else if(ip.indexToKey(pointer_info.elem_type) == .array_type) { + } else if (ip.indexToKey(pointer_info.elem_type) == .array_type) { try completions.append(arena, .{ .label = "len", .kind = .Field, @@ -127,7 +127,7 @@ pub fn dotCompletions( } }, .tuple_type => |tuple_info| { - for (tuple_info.types) |tuple_ty,i| { + for (tuple_info.types) |tuple_ty, i| { try completions.append(arena, .{ .label = try std.fmt.allocPrint(arena, "{d}", .{i}), .kind = .Field, @@ -140,7 +140,8 @@ pub fn dotCompletions( .error_union_type, .function_type, .vector_type, - .anyframe_type => {}, + .anyframe_type, + => {}, .int_u64_value, .int_i64_value,