Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.ArrayList: popOrNull() -> pop() [v2] #22720

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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 @@ -2446,7 +2446,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 @@ -2676,7 +2676,7 @@ fn defineFn(pp: *Preprocessor, tokenizer: *Tokenizer, define_tok: RawToken, macr
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 @@ -1190,7 +1190,7 @@ pub fn printErrorMessages(
const ttyconf = options.ttyconf;
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/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
2 changes: 1 addition & 1 deletion lib/std/Build/Step/ConfigHeader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,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
47 changes: 16 additions & 31 deletions lib/std/array_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,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 @@ -555,23 +555,15 @@ 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 {
/// 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 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, or
/// return `null` if list is empty.
/// Invalidates element pointers to the removed element, if any.
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 @@ -897,10 +889,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 @@ -1190,22 +1182,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 @@ -2184,7 +2169,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 @@ -2195,10 +2180,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
Loading
Loading