diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-07-08 02:04:53 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-08 02:04:53 -0400 |
| commit | 62d27fcfb687e3ab1f10c72513e19529d8ffceed (patch) | |
| tree | 2885da99ca325959e40f417346aca63ddef4fb31 /lib/std | |
| parent | 7935e83b1d5d29cca058597ebdac6dfd012a790a (diff) | |
| parent | c2e66d9bab396a69514ec7c3c41fb0404e542f21 (diff) | |
| download | zig-62d27fcfb687e3ab1f10c72513e19529d8ffceed.tar.gz zig-62d27fcfb687e3ab1f10c72513e19529d8ffceed.zip | |
Merge pull request #9325 from ziglang/stage2-inferred-error-sets
Stage2 inferred error sets and `@panic`
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/builtin.zig | 7 | ||||
| -rw-r--r-- | lib/std/hash_map.zig | 32 |
2 files changed, 33 insertions, 6 deletions
diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig index 7bf60f5283..9d432a3a00 100644 --- a/lib/std/builtin.zig +++ b/lib/std/builtin.zig @@ -677,6 +677,13 @@ pub const panic: PanicFn = if (@hasDecl(root, "panic")) root.panic else default_ /// therefore must be kept in sync with the compiler implementation. pub fn default_panic(msg: []const u8, error_return_trace: ?*StackTrace) noreturn { @setCold(true); + // Until self-hosted catches up with stage1 language features, we have a simpler + // default panic function: + if (builtin.zig_is_stage2) { + while (true) { + @breakpoint(); + } + } if (@hasDecl(root, "os") and @hasDecl(root.os, "panic")) { root.os.panic(msg, error_return_trace); unreachable; diff --git a/lib/std/hash_map.zig b/lib/std/hash_map.zig index d64e122aaf..d6762c9d3b 100644 --- a/lib/std/hash_map.zig +++ b/lib/std/hash_map.zig @@ -483,10 +483,20 @@ pub fn HashMap( return self.unmanaged.getOrPutValueContext(self.allocator, key, value, self.ctx); } + /// Deprecated: call `ensureUnusedCapacity` or `ensureTotalCapacity`. + pub const ensureCapacity = ensureTotalCapacity; + /// Increases capacity, guaranteeing that insertions up until the /// `expected_count` will not cause an allocation, and therefore cannot fail. - pub fn ensureCapacity(self: *Self, expected_count: Size) !void { - return self.unmanaged.ensureCapacityContext(self.allocator, expected_count, self.ctx); + pub fn ensureTotalCapacity(self: *Self, expected_count: Size) !void { + return self.unmanaged.ensureTotalCapacityContext(self.allocator, expected_count, self.ctx); + } + + /// Increases capacity, guaranteeing that insertions up until + /// `additional_count` **more** items will not cause an allocation, and + /// therefore cannot fail. + pub fn ensureUnusedCapacity(self: *Self, additional_count: Size) !void { + return self.unmanaged.ensureUnusedCapacityContext(self.allocator, additional_count, self.ctx); } /// Returns the number of total elements which may be present before it is @@ -821,16 +831,26 @@ pub fn HashMapUnmanaged( return new_cap; } - pub fn ensureCapacity(self: *Self, allocator: *Allocator, new_size: Size) !void { + /// Deprecated: call `ensureUnusedCapacity` or `ensureTotalCapacity`. + pub const ensureCapacity = ensureTotalCapacity; + + pub fn ensureTotalCapacity(self: *Self, allocator: *Allocator, new_size: Size) !void { if (@sizeOf(Context) != 0) - @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call ensureCapacityContext instead."); - return ensureCapacityContext(self, allocator, new_size, undefined); + @compileError("Cannot infer context " ++ @typeName(Context) ++ ", call ensureTotalCapacityContext instead."); + return ensureTotalCapacityContext(self, allocator, new_size, undefined); } - pub fn ensureCapacityContext(self: *Self, allocator: *Allocator, new_size: Size, ctx: Context) !void { + pub fn ensureTotalCapacityContext(self: *Self, allocator: *Allocator, new_size: Size, ctx: Context) !void { if (new_size > self.size) try self.growIfNeeded(allocator, new_size - self.size, ctx); } + pub fn ensureUnusedCapacity(self: *Self, allocator: *Allocator, additional_size: Size) !void { + return ensureUnusedCapacityContext(self, allocator, additional_size, undefined); + } + pub fn ensureUnusedCapacityContext(self: *Self, allocator: *Allocator, additional_size: Size, ctx: Context) !void { + return ensureTotalCapacityContext(self, allocator, self.capacity() + additional_size, ctx); + } + pub fn clearRetainingCapacity(self: *Self) void { if (self.metadata) |_| { self.initMetadatas(); |
