aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authormlugg <mlugg@mlugg.co.uk>2023-03-05 12:39:32 +0000
committerAndrew Kelley <andrew@ziglang.org>2023-04-12 12:06:19 -0400
commitccf670c2b04ebeb9db43eb9f5c47c6cf03e4b1d0 (patch)
tree3f7899bf6231500782af185ffe701ee66793e226 /lib/std
parent602029bb2fb78048e46136784e717b57b8de8f2c (diff)
downloadzig-ccf670c2b04ebeb9db43eb9f5c47c6cf03e4b1d0.tar.gz
zig-ccf670c2b04ebeb9db43eb9f5c47c6cf03e4b1d0.zip
Zir: implement explicit block_comptime instruction
Resolves: #7056
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/Build/Cache.zig4
-rw-r--r--lib/std/crypto/25519/ed25519.zig2
-rw-r--r--lib/std/enums.zig8
-rw-r--r--lib/std/math.zig2
-rw-r--r--lib/std/meta.zig12
-rw-r--r--lib/std/meta/trait.zig14
-rw-r--r--lib/std/net/test.zig2
-rw-r--r--lib/std/unicode.zig6
-rw-r--r--lib/std/zig/system/linux.zig8
9 files changed, 30 insertions, 28 deletions
diff --git a/lib/std/Build/Cache.zig b/lib/std/Build/Cache.zig
index b25e349168..f8c83451cb 100644
--- a/lib/std/Build/Cache.zig
+++ b/lib/std/Build/Cache.zig
@@ -374,7 +374,7 @@ pub const Manifest = struct {
self.failed_file_index = null;
const ext = ".txt";
- var manifest_file_path: [self.hex_digest.len + ext.len]u8 = undefined;
+ var manifest_file_path: [hex_digest_len + ext.len]u8 = undefined;
var bin_digest: BinDigest = undefined;
self.hash.hasher.final(&bin_digest);
@@ -389,7 +389,7 @@ pub const Manifest = struct {
self.hash.hasher.update(&bin_digest);
mem.copy(u8, &manifest_file_path, &self.hex_digest);
- manifest_file_path[self.hex_digest.len..][0..ext.len].* = ext.*;
+ manifest_file_path[hex_digest_len..][0..ext.len].* = ext.*;
if (self.files.items.len == 0) {
// If there are no file inputs, we check if the manifest file exists instead of
diff --git a/lib/std/crypto/25519/ed25519.zig b/lib/std/crypto/25519/ed25519.zig
index 5d85fd5224..66dcf3705b 100644
--- a/lib/std/crypto/25519/ed25519.zig
+++ b/lib/std/crypto/25519/ed25519.zig
@@ -622,7 +622,7 @@ test "ed25519 test vectors" {
},
};
for (entries) |entry| {
- var msg: [entry.msg_hex.len / 2]u8 = undefined;
+ var msg: [64 / 2]u8 = undefined;
_ = try fmt.hexToBytes(&msg, entry.msg_hex);
var public_key_bytes: [32]u8 = undefined;
_ = try fmt.hexToBytes(&public_key_bytes, entry.public_key_hex);
diff --git a/lib/std/enums.zig b/lib/std/enums.zig
index 8ef097e909..c8c999d4b1 100644
--- a/lib/std/enums.zig
+++ b/lib/std/enums.zig
@@ -177,9 +177,9 @@ test "std.enums.directEnumArrayDefault slice" {
/// Cast an enum literal, value, or string to the enum value of type E
/// with the same name.
pub fn nameCast(comptime E: type, comptime value: anytype) E {
- comptime {
+ return comptime blk: {
const V = @TypeOf(value);
- if (V == E) return value;
+ if (V == E) break :blk value;
var name: ?[]const u8 = switch (@typeInfo(V)) {
.EnumLiteral, .Enum => @tagName(value),
.Pointer => if (std.meta.trait.isZigString(V)) value else null,
@@ -187,12 +187,12 @@ pub fn nameCast(comptime E: type, comptime value: anytype) E {
};
if (name) |n| {
if (@hasField(E, n)) {
- return @field(E, n);
+ break :blk @field(E, n);
}
@compileError("Enum " ++ @typeName(E) ++ " has no field named " ++ n);
}
@compileError("Cannot cast from " ++ @typeName(@TypeOf(value)) ++ " to " ++ @typeName(E));
- }
+ };
}
test "std.enums.nameCast" {
diff --git a/lib/std/math.zig b/lib/std/math.zig
index 4d8550a8bc..65643d92d7 100644
--- a/lib/std/math.zig
+++ b/lib/std/math.zig
@@ -877,7 +877,7 @@ fn testDivFloor() !void {
/// zero.
pub fn divCeil(comptime T: type, numerator: T, denominator: T) !T {
@setRuntimeSafety(false);
- if (comptime std.meta.trait.isNumber(T) and denominator == 0) return error.DivisionByZero;
+ if ((comptime std.meta.trait.isNumber(T)) and denominator == 0) return error.DivisionByZero;
const info = @typeInfo(T);
switch (info) {
.ComptimeFloat, .Float => return @ceil(numerator / denominator),
diff --git a/lib/std/meta.zig b/lib/std/meta.zig
index 07f42075b4..3aca23b267 100644
--- a/lib/std/meta.zig
+++ b/lib/std/meta.zig
@@ -549,14 +549,14 @@ test "std.meta.FieldType" {
}
pub fn fieldNames(comptime T: type) *const [fields(T).len][]const u8 {
- comptime {
+ return comptime blk: {
const fieldInfos = fields(T);
var names: [fieldInfos.len][]const u8 = undefined;
for (fieldInfos, 0..) |field, i| {
names[i] = field.name;
}
- return &names;
- }
+ break :blk &names;
+ };
}
test "std.meta.fieldNames" {
@@ -590,14 +590,14 @@ test "std.meta.fieldNames" {
/// Given an enum or error set type, returns a pointer to an array containing all tags for that
/// enum or error set.
pub fn tags(comptime T: type) *const [fields(T).len]T {
- comptime {
+ return comptime blk: {
const fieldInfos = fields(T);
var res: [fieldInfos.len]T = undefined;
for (fieldInfos, 0..) |field, i| {
res[i] = @field(T, field.name);
}
- return &res;
- }
+ break :blk &res;
+ };
}
test "std.meta.tags" {
diff --git a/lib/std/meta/trait.zig b/lib/std/meta/trait.zig
index 843ada7f56..36f0a8924d 100644
--- a/lib/std/meta/trait.zig
+++ b/lib/std/meta/trait.zig
@@ -400,18 +400,18 @@ test "isTuple" {
/// *const u8, ?[]const u8, ?*const [N]u8.
/// ```
pub fn isZigString(comptime T: type) bool {
- comptime {
+ return comptime blk: {
// Only pointer types can be strings, no optionals
const info = @typeInfo(T);
- if (info != .Pointer) return false;
+ if (info != .Pointer) break :blk false;
const ptr = &info.Pointer;
// Check for CV qualifiers that would prevent coerction to []const u8
- if (ptr.is_volatile or ptr.is_allowzero) return false;
+ if (ptr.is_volatile or ptr.is_allowzero) break :blk false;
// If it's already a slice, simple check.
if (ptr.size == .Slice) {
- return ptr.child == u8;
+ break :blk ptr.child == u8;
}
// Otherwise check if it's an array type that coerces to slice.
@@ -419,12 +419,12 @@ pub fn isZigString(comptime T: type) bool {
const child = @typeInfo(ptr.child);
if (child == .Array) {
const arr = &child.Array;
- return arr.child == u8;
+ break :blk arr.child == u8;
}
}
- return false;
- }
+ break :blk false;
+ };
}
test "isZigString" {
diff --git a/lib/std/net/test.zig b/lib/std/net/test.zig
index 30a63d9e3d..c4afc0e4e3 100644
--- a/lib/std/net/test.zig
+++ b/lib/std/net/test.zig
@@ -99,7 +99,7 @@ test "parse and render UNIX addresses" {
const fmt_addr = std.fmt.bufPrint(buffer[0..], "{}", .{addr}) catch unreachable;
try std.testing.expectEqualSlices(u8, "/tmp/testpath", fmt_addr);
- const too_long = [_]u8{'a'} ** (addr.un.path.len + 1);
+ const too_long = [_]u8{'a'} ** 200;
try testing.expectError(error.NameTooLong, net.Address.initUnix(too_long[0..]));
}
diff --git a/lib/std/unicode.zig b/lib/std/unicode.zig
index 7ac641e948..378b216ef3 100644
--- a/lib/std/unicode.zig
+++ b/lib/std/unicode.zig
@@ -774,13 +774,13 @@ test "utf8ToUtf16LeWithNull" {
/// Converts a UTF-8 string literal into a UTF-16LE string literal.
pub fn utf8ToUtf16LeStringLiteral(comptime utf8: []const u8) *const [calcUtf16LeLen(utf8) catch unreachable:0]u16 {
- comptime {
+ return comptime blk: {
const len: usize = calcUtf16LeLen(utf8) catch |err| @compileError(err);
var utf16le: [len:0]u16 = [_:0]u16{0} ** len;
const utf16le_len = utf8ToUtf16Le(&utf16le, utf8[0..]) catch |err| @compileError(err);
assert(len == utf16le_len);
- return &utf16le;
- }
+ break :blk &utf16le;
+ };
}
const CalcUtf16LeLenError = Utf8DecodeError || error{Utf8InvalidStartByte};
diff --git a/lib/std/zig/system/linux.zig b/lib/std/zig/system/linux.zig
index ee07daebe9..f2f89f8317 100644
--- a/lib/std/zig/system/linux.zig
+++ b/lib/std/zig/system/linux.zig
@@ -147,7 +147,9 @@ test "cpuinfo: PowerPC" {
}
const ArmCpuinfoImpl = struct {
- cores: [4]CoreInfo = undefined,
+ const num_cores = 4;
+
+ cores: [num_cores]CoreInfo = undefined,
core_no: usize = 0,
have_fields: usize = 0,
@@ -162,7 +164,7 @@ const ArmCpuinfoImpl = struct {
const cpu_models = @import("arm.zig").cpu_models;
fn addOne(self: *ArmCpuinfoImpl) void {
- if (self.have_fields == 4 and self.core_no < self.cores.len) {
+ if (self.have_fields == 4 and self.core_no < num_cores) {
if (self.core_no > 0) {
// Deduplicate the core info.
for (self.cores[0..self.core_no]) |it| {
@@ -222,7 +224,7 @@ const ArmCpuinfoImpl = struct {
else => false,
};
- var known_models: [self.cores.len]?*const Target.Cpu.Model = undefined;
+ var known_models: [num_cores]?*const Target.Cpu.Model = undefined;
for (self.cores[0..self.core_no], 0..) |core, i| {
known_models[i] = cpu_models.isKnown(.{
.architecture = core.architecture,