Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/compiler/aro/aro/Preprocessor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2407,7 +2407,7 @@ pub fn expandedSlice(pp: *const Preprocessor, tok: anytype) []const u8 {

/// Concat two tokens and add the result to pp.generated
fn pasteTokens(pp: *Preprocessor, lhs_toks: *ExpandBuf, rhs_toks: []const TokenWithExpansionLocs) Error!void {
const lhs = while (lhs_toks.popOrNull()) |lhs| {
const lhs = while (lhs_toks.pop()) |lhs| {
if ((pp.comp.langopts.preserve_comments_in_macros and lhs.id == .comment) or
(lhs.id != .macro_ws and lhs.id != .comment))
break lhs;
Expand Down Expand Up @@ -2634,7 +2634,7 @@ fn defineFn(pp: *Preprocessor, tokenizer: *Tokenizer, macro_name: RawToken, l_pa
tok = tokenizer.nextNoWS();
if (tok.id == .ellipsis) {
try pp.err(tok, .gnu_va_macro);
gnu_var_args = params.pop();
gnu_var_args = params.pop().?;
const r_paren = tokenizer.nextNoWS();
if (r_paren.id != .r_paren) {
try pp.err(r_paren, .missing_paren_param_list);
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/aro/aro/pragmas/gcc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fn diagnosticHandler(self: *GCC, pp: *Preprocessor, start_idx: TokenIndex) Pragm
try pp.comp.diagnostics.set(str[2..], new_kind);
},
.push => try self.options_stack.append(pp.comp.gpa, pp.comp.diagnostics.options),
.pop => pp.comp.diagnostics.options = self.options_stack.popOrNull() orelse self.original_options,
.pop => pp.comp.diagnostics.options = self.options_stack.pop() orelse self.original_options,
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/aro/aro/pragmas/pack.zig
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ fn pop(pack: *Pack, p: *Parser, maybe_label: ?[]const u8) void {
}
}
} else {
const prev = pack.stack.popOrNull() orelse {
const prev = pack.stack.pop() orelse {
p.pragma_pack = 2;
return;
};
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/build_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ fn printErrorMessages(b: *std.Build, failing_step: *Step, run: *const Run) !void
// Now, `step_stack` has the subtree that we want to print, in reverse order.
try ttyconf.setColor(stderr, .dim);
var indent: usize = 0;
while (step_stack.popOrNull()) |s| : (indent += 1) {
while (step_stack.pop()) |s| : (indent += 1) {
if (indent > 0) {
try stderr.writer().writeByteNTimes(' ', (indent - 1) * 3);
try printChildNodePrefix(stderr, ttyconf);
Expand Down
2 changes: 1 addition & 1 deletion lib/docs/wasm/markdown.zig
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ fn mainImpl() !void {
var line_buf = std.ArrayList(u8).init(gpa);
defer line_buf.deinit();
while (stdin_buf.reader().streamUntilDelimiter(line_buf.writer(), '\n', null)) {
if (line_buf.getLastOrNull() == '\r') _ = line_buf.pop();
if (line_buf.getLastOrNull() == '\r') _ = line_buf.pop().?;
try parser.feedLine(line_buf.items);
line_buf.clearRetainingCapacity();
} else |err| switch (err) {
Expand Down
2 changes: 1 addition & 1 deletion lib/docs/wasm/markdown/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ fn isThematicBreak(line: []const u8) bool {
}

fn closeLastBlock(p: *Parser) !void {
const b = p.pending_blocks.pop();
const b = p.pending_blocks.pop().?;
const node = switch (b.tag) {
.list => list: {
assert(b.string_start == p.scratch_string.items.len);
Expand Down
10 changes: 5 additions & 5 deletions lib/std/Build/Cache.zig
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ pub const Manifest = struct {
break :f file;
}
const gop = try self.files.getOrPutAdapted(gpa, prefixed_path, FilesAdapter{});
errdefer _ = self.files.pop();
errdefer _ = self.files.pop().?;
if (!gop.found_existing) {
gop.key_ptr.* = .{
.prefixed_path = .{
Expand Down Expand Up @@ -634,7 +634,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);
}

Expand Down Expand Up @@ -737,7 +737,7 @@ pub const Manifest = struct {
errdefer gpa.free(prefixed_path.sub_path);

const gop = try self.files.getOrPutAdapted(gpa, prefixed_path, FilesAdapter{});
errdefer _ = self.files.pop();
errdefer _ = self.files.pop().?;

if (gop.found_existing) {
gpa.free(prefixed_path.sub_path);
Expand Down Expand Up @@ -773,7 +773,7 @@ pub const Manifest = struct {
errdefer gpa.free(prefixed_path.sub_path);

const gop = try self.files.getOrPutAdapted(gpa, prefixed_path, FilesAdapter{});
errdefer _ = self.files.pop();
errdefer _ = self.files.pop().?;

if (gop.found_existing) {
gpa.free(prefixed_path.sub_path);
Expand Down Expand Up @@ -809,7 +809,7 @@ pub const Manifest = struct {
errdefer gpa.free(prefixed_path.sub_path);

const gop = try self.files.getOrPutAdapted(gpa, prefixed_path, FilesAdapter{});
errdefer _ = self.files.pop();
errdefer _ = self.files.pop().?;

if (gop.found_existing) {
gpa.free(prefixed_path.sub_path);
Expand Down
2 changes: 1 addition & 1 deletion lib/std/Build/Step/ConfigHeader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ fn expand_variables_cmake(
// no open bracket, preserve as a literal
break :blk;
}
const open_pos = var_stack.pop();
const open_pos = var_stack.pop().?;
if (source_offset == open_pos.source) {
source_offset += open_var.len;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/std/Build/Step/Options.zig
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ fn printType(self: *Options, out: anytype, comptime T: type, value: T, indent: u
if (value) |inner| {
try printType(self, out, @TypeOf(inner), inner, indent + 4, null);
// Pop the '\n' and ',' chars
_ = self.contents.pop();
_ = self.contents.pop();
_ = self.contents.pop().?;
_ = self.contents.pop().?;
} else {
try out.writeAll("null");
}
Expand Down
48 changes: 8 additions & 40 deletions lib/std/array_hash_map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -466,15 +466,10 @@ pub fn ArrayHashMap(
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);
}
};
}
Expand Down Expand Up @@ -1403,13 +1398,15 @@ pub fn ArrayHashMapUnmanaged(
self.entries.shrinkAndFree(allocator, new_len);
}

/// Removes the last inserted `Entry` in the hash map and returns it.
pub fn pop(self: *Self) KV {
/// Removes the last inserted `Entry` in the hash map and returns it if count is nonzero.
/// 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();

Expand All @@ -1423,17 +1420,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,
Expand Down Expand Up @@ -2373,25 +2359,7 @@ test "pop" {
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" {
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 (map.popOrNull()) |pop| {
while (map.pop()) |pop| {
try testing.expect(pop.key == i - 1 and pop.value == i - 1);
i -= 1;
}
Expand Down
46 changes: 16 additions & 30 deletions lib/std/array_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
/// Asserts that the list is not empty.
/// Asserts that the index is in bounds.
pub fn swapRemove(self: *Self, i: usize) T {
if (self.items.len - 1 == i) return self.pop();
if (self.items.len - 1 == i) return self.pop().?;

const old_item = self.items[i];
self.items[i] = self.pop();
self.items[i] = self.pop().?;
return old_item;
}

Expand Down Expand Up @@ -538,21 +538,14 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
return self.items[prev_len..][0..n];
}

/// Remove and return the last element from the list.
/// Invalidates element pointers to the removed element.
/// Asserts that the list is not empty.
pub fn pop(self: *Self) T {
const val = self.items[self.items.len - 1];
self.items.len -= 1;
return val;
}

/// Remove and return the last element from the list, or
/// return `null` if list is empty.
/// Invalidates element pointers to the removed element, if any.
pub fn popOrNull(self: *Self) ?T {
pub fn pop(self: *Self) ?T {
if (self.items.len == 0) return null;
return self.pop();
const val = self.items[self.items.len - 1];
self.items.len -= 1;
return val;
}

/// Returns a slice of all the items plus the extra capacity, whose memory
Expand Down Expand Up @@ -875,10 +868,10 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
/// Asserts that the list is not empty.
/// Asserts that the index is in bounds.
pub fn swapRemove(self: *Self, i: usize) T {
if (self.items.len - 1 == i) return self.pop();
if (self.items.len - 1 == i) return self.pop().?;

const old_item = self.items[i];
self.items[i] = self.pop();
self.items[i] = self.pop().?;
return old_item;
}

Expand Down Expand Up @@ -1167,22 +1160,15 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
}

/// Remove and return the last element from the list.
/// If the list is empty, returns `null`.
/// Invalidates pointers to last element.
/// Asserts that the list is not empty.
pub fn pop(self: *Self) T {
pub fn pop(self: *Self) ?T {
if (self.items.len == 0) return null;
const val = self.items[self.items.len - 1];
self.items.len -= 1;
return val;
}

/// Remove and return the last element from the list.
/// If the list is empty, returns `null`.
/// Invalidates pointers to last element.
pub fn popOrNull(self: *Self) ?T {
if (self.items.len == 0) return null;
return self.pop();
}

/// Returns a slice of all the items plus the extra capacity, whose memory
/// contents are `undefined`.
pub fn allocatedSlice(self: Self) Slice {
Expand Down Expand Up @@ -2161,7 +2147,7 @@ test "ArrayList(u0)" {
try testing.expectEqual(count, 3);
}

test "ArrayList(?u32).popOrNull()" {
test "ArrayList(?u32).pop()" {
const a = testing.allocator;

var list = ArrayList(?u32).init(a);
Expand All @@ -2172,10 +2158,10 @@ test "ArrayList(?u32).popOrNull()" {
try list.append(2);
try testing.expectEqual(list.items.len, 3);

try testing.expect(list.popOrNull().? == @as(u32, 2));
try testing.expect(list.popOrNull().? == @as(u32, 1));
try testing.expect(list.popOrNull().? == null);
try testing.expect(list.popOrNull() == null);
try testing.expect(list.pop().? == @as(u32, 2));
try testing.expect(list.pop().? == @as(u32, 1));
try testing.expect(list.pop().? == null);
try testing.expect(list.pop() == null);
}

test "ArrayList(u32).getLast()" {
Expand Down
25 changes: 10 additions & 15 deletions lib/std/bounded_array.zig
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,15 @@ pub fn BoundedArrayAligned(
return self.slice()[prev_len..][0..n];
}

/// Remove and return the last element from the slice.
/// Asserts the slice has at least one item.
pub fn pop(self: *Self) T {
/// Remove and return the last element from the slice, or
/// return `null` if the slice is empty.
pub fn pop(self: *Self) ?T {
if (self.len == 0) return null;
const item = self.get(self.len - 1);
self.len -= 1;
return item;
}

/// Remove and return the last element from the slice, or
/// return `null` if the slice is empty.
pub fn popOrNull(self: *Self) ?T {
return if (self.len == 0) null else self.pop();
}

/// Return a slice of only the extra capacity after items.
/// This can be useful for writing directly into it.
/// Note that such an operation must be followed up with a
Expand Down Expand Up @@ -218,7 +213,7 @@ pub fn BoundedArrayAligned(
/// This operation is O(N).
pub fn orderedRemove(self: *Self, i: usize) T {
const newlen = self.len - 1;
if (newlen == i) return self.pop();
if (newlen == i) return self.pop().?;
const old_item = self.get(i);
for (self.slice()[i..newlen], 0..) |*b, j| b.* = self.get(i + 1 + j);
self.set(newlen, undefined);
Expand All @@ -230,9 +225,9 @@ pub fn BoundedArrayAligned(
/// The empty slot is filled from the end of the slice.
/// This operation is O(1).
pub fn swapRemove(self: *Self, i: usize) T {
if (self.len - 1 == i) return self.pop();
if (self.len - 1 == i) return self.pop().?;
const old_item = self.get(i);
self.set(i, self.pop());
self.set(i, self.pop().?);
return old_item;
}

Expand Down Expand Up @@ -328,8 +323,8 @@ test BoundedArray {
try testing.expectEqual(a.pop(), 0xff);

try a.resize(1);
try testing.expectEqual(a.popOrNull(), 0);
try testing.expectEqual(a.popOrNull(), null);
try testing.expectEqual(a.pop(), 0);
try testing.expectEqual(a.pop(), null);
var unused = a.unusedCapacitySlice();
@memset(unused[0..8], 2);
unused[8] = 3;
Expand Down Expand Up @@ -382,7 +377,7 @@ test BoundedArray {
try testing.expectEqual(swapped, 0xdd);
try testing.expectEqual(a.get(0), 0xee);

while (a.popOrNull()) |_| {}
while (a.pop()) |_| {}
const w = a.writer();
const s = "hello, this is a test string";
try w.writeAll(s);
Expand Down
2 changes: 1 addition & 1 deletion lib/std/dwarf/call_frame.zig
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ pub const VirtualMachine = struct {
self.current_row.copy_on_write = true;
},
.restore_state => {
const restored_columns = self.stack.popOrNull() orelse return error.InvalidOperation;
const restored_columns = self.stack.pop() orelse return error.InvalidOperation;
self.columns.shrinkRetainingCapacity(self.columns.items.len - self.current_row.columns.len);
try self.columns.ensureUnusedCapacity(allocator, restored_columns.len);

Expand Down
Loading