diff --git a/lib/std/Build/Cache.zig b/lib/std/Build/Cache.zig index 13c08f93a1e6..24edee37a838 100644 --- a/lib/std/Build/Cache.zig +++ b/lib/std/Build/Cache.zig @@ -775,7 +775,7 @@ pub const Manifest = struct { // Remove files not in the initial hash. while (self.files.count() != input_file_count) { - var file = self.files.pop(); + var file = self.files.pop().?; file.key.deinit(self.cache.gpa); } diff --git a/lib/std/array_hash_map.zig b/lib/std/array_hash_map.zig index 22c736d0a5c0..85e73d6dd1e5 100644 --- a/lib/std/array_hash_map.zig +++ b/lib/std/array_hash_map.zig @@ -485,15 +485,10 @@ pub fn ArrayHashMapWithAllocator( return self.unmanaged.shrinkAndFreeContext(self.allocator, new_len, self.ctx); } - /// Removes the last inserted `Entry` in the hash map and returns it. - pub fn pop(self: *Self) KV { - return self.unmanaged.popContext(self.ctx); - } - /// Removes the last inserted `Entry` in the hash map and returns it if count is nonzero. /// Otherwise returns null. - pub fn popOrNull(self: *Self) ?KV { - return self.unmanaged.popOrNullContext(self.ctx); + pub fn pop(self: *Self) ?KV { + return self.unmanaged.popContext(self.ctx); } }; } @@ -1468,12 +1463,14 @@ pub fn ArrayHashMapUnmanaged( } /// Removes the last inserted `Entry` in the hash map and returns it. - pub fn pop(self: *Self) KV { + /// Otherwise returns null. + pub fn pop(self: *Self) ?KV { if (@sizeOf(ByIndexContext) != 0) @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call popContext instead."); return self.popContext(undefined); } - pub fn popContext(self: *Self, ctx: Context) KV { + pub fn popContext(self: *Self, ctx: Context) ?KV { + if (self.entries.len == 0) return null; self.pointer_stability.lock(); defer self.pointer_stability.unlock(); @@ -1487,17 +1484,6 @@ pub fn ArrayHashMapUnmanaged( }; } - /// Removes the last inserted `Entry` in the hash map and returns it if count is nonzero. - /// Otherwise returns null. - pub fn popOrNull(self: *Self) ?KV { - if (@sizeOf(ByIndexContext) != 0) - @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call popContext instead."); - return self.popOrNullContext(undefined); - } - pub fn popOrNullContext(self: *Self, ctx: Context) ?KV { - return if (self.entries.len == 0) null else self.popContext(ctx); - } - fn fetchRemoveByKey( self: *Self, key: anytype, @@ -2425,25 +2411,7 @@ test "shrink" { } } -test "pop" { - var map = AutoArrayHashMap(i32, i32).init(std.testing.allocator); - defer map.deinit(); - - // Insert just enough entries so that the map expands. Afterwards, - // pop all entries out of the map. - - var i: i32 = 0; - while (i < 9) : (i += 1) { - try testing.expect((try map.fetchPut(i, i)) == null); - } - - while (i > 0) : (i -= 1) { - const pop = map.pop(); - try testing.expect(pop.key == i - 1 and pop.value == i - 1); - } -} - -test "popOrNull" { +test "pop()" { var map = AutoArrayHashMap(i32, i32).init(std.testing.allocator); defer map.deinit(); @@ -2455,7 +2423,7 @@ test "popOrNull" { try testing.expect((try map.fetchPut(i, i)) == null); } - while (map.popOrNull()) |pop| { + while (map.pop()) |pop| { try testing.expect(pop.key == i - 1 and pop.value == i - 1); i -= 1; } diff --git a/src/Zcu.zig b/src/Zcu.zig index c870a04adffc..7291f0f38286 100644 --- a/src/Zcu.zig +++ b/src/Zcu.zig @@ -3797,7 +3797,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv } while (true) { - if (type_queue.popOrNull()) |kv| { + if (type_queue.pop()) |kv| { const ty = kv.key; const referencer = kv.value; try checked_types.putNoClobber(gpa, ty, {}); @@ -3920,7 +3920,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv } continue; } - if (unit_queue.popOrNull()) |kv| { + if (unit_queue.pop()) |kv| { const unit = kv.key; try result.putNoClobber(gpa, unit, kv.value); diff --git a/src/Zcu/PerThread.zig b/src/Zcu/PerThread.zig index da031609a847..f1f69d107a27 100644 --- a/src/Zcu/PerThread.zig +++ b/src/Zcu/PerThread.zig @@ -2089,7 +2089,7 @@ pub fn embedFile( errdefer gpa.free(resolved_path); const gop = try zcu.embed_table.getOrPut(gpa, resolved_path); - errdefer assert(std.mem.eql(u8, zcu.embed_table.pop().key, resolved_path)); + errdefer assert(std.mem.eql(u8, zcu.embed_table.pop().?.key, resolved_path)); if (gop.found_existing) { gpa.free(resolved_path); // we're not using this key @@ -2112,7 +2112,7 @@ pub fn embedFile( errdefer gpa.free(resolved_path); const gop = try zcu.embed_table.getOrPut(gpa, resolved_path); - errdefer assert(std.mem.eql(u8, zcu.embed_table.pop().key, resolved_path)); + errdefer assert(std.mem.eql(u8, zcu.embed_table.pop().?.key, resolved_path)); if (gop.found_existing) { gpa.free(resolved_path); // we're not using this key diff --git a/src/codegen/c.zig b/src/codegen/c.zig index b9731716f799..b7824e5311ca 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -438,7 +438,7 @@ pub const Function = struct { fn allocAlignedLocal(f: *Function, inst: ?Air.Inst.Index, local_type: LocalType) !CValue { const result: CValue = result: { if (f.free_locals_map.getPtr(local_type)) |locals_list| { - if (locals_list.popOrNull()) |local_entry| { + if (locals_list.pop()) |local_entry| { break :result .{ .new_local = local_entry.key }; } } diff --git a/src/link/Coff.zig b/src/link/Coff.zig index 3b239246ca54..67fa95075e94 100644 --- a/src/link/Coff.zig +++ b/src/link/Coff.zig @@ -2300,7 +2300,7 @@ fn flushModuleInner(coff: *Coff, arena: Allocator, tid: Zcu.PerThread.Id) !void } } - while (coff.unresolved.popOrNull()) |entry| { + while (coff.unresolved.pop()) |entry| { assert(entry.value); const global = coff.globals.items[entry.key]; const sym = coff.getSymbol(global);