Skip to content

Commit 16145c2

Browse files
committed
WIP: implement explicit block_comptime ZIR instruction
Resolves: #7056
1 parent 12b74b2 commit 16145c2

File tree

18 files changed

+234
-304
lines changed

18 files changed

+234
-304
lines changed

lib/std/Build/Cache.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ pub const Manifest = struct {
360360
self.failed_file_index = null;
361361

362362
const ext = ".txt";
363-
var manifest_file_path: [self.hex_digest.len + ext.len]u8 = undefined;
363+
var manifest_file_path: [hex_digest_len + ext.len]u8 = undefined;
364364

365365
var bin_digest: BinDigest = undefined;
366366
self.hash.hasher.final(&bin_digest);
@@ -375,7 +375,7 @@ pub const Manifest = struct {
375375
self.hash.hasher.update(&bin_digest);
376376

377377
mem.copy(u8, &manifest_file_path, &self.hex_digest);
378-
manifest_file_path[self.hex_digest.len..][0..ext.len].* = ext.*;
378+
manifest_file_path[hex_digest_len..][0..ext.len].* = ext.*;
379379

380380
if (self.files.items.len == 0) {
381381
// If there are no file inputs, we check if the manifest file exists instead of

lib/std/crypto/25519/ed25519.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ test "ed25519 test vectors" {
622622
},
623623
};
624624
for (entries) |entry| {
625-
var msg: [entry.msg_hex.len / 2]u8 = undefined;
625+
var msg: [64 / 2]u8 = undefined;
626626
_ = try fmt.hexToBytes(&msg, entry.msg_hex);
627627
var public_key_bytes: [32]u8 = undefined;
628628
_ = try fmt.hexToBytes(&public_key_bytes, entry.public_key_hex);

lib/std/enums.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,22 +177,22 @@ test "std.enums.directEnumArrayDefault slice" {
177177
/// Cast an enum literal, value, or string to the enum value of type E
178178
/// with the same name.
179179
pub fn nameCast(comptime E: type, comptime value: anytype) E {
180-
comptime {
180+
return comptime blk: {
181181
const V = @TypeOf(value);
182-
if (V == E) return value;
182+
if (V == E) break :blk value;
183183
var name: ?[]const u8 = switch (@typeInfo(V)) {
184184
.EnumLiteral, .Enum => @tagName(value),
185185
.Pointer => if (std.meta.trait.isZigString(V)) value else null,
186186
else => null,
187187
};
188188
if (name) |n| {
189189
if (@hasField(E, n)) {
190-
return @field(E, n);
190+
break :blk @field(E, n);
191191
}
192192
@compileError("Enum " ++ @typeName(E) ++ " has no field named " ++ n);
193193
}
194194
@compileError("Cannot cast from " ++ @typeName(@TypeOf(value)) ++ " to " ++ @typeName(E));
195-
}
195+
};
196196
}
197197

198198
test "std.enums.nameCast" {

lib/std/math.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ fn testDivFloor() !void {
876876
/// zero.
877877
pub fn divCeil(comptime T: type, numerator: T, denominator: T) !T {
878878
@setRuntimeSafety(false);
879-
if (comptime std.meta.trait.isNumber(T) and denominator == 0) return error.DivisionByZero;
879+
if ((comptime std.meta.trait.isNumber(T)) and denominator == 0) return error.DivisionByZero;
880880
const info = @typeInfo(T);
881881
switch (info) {
882882
.ComptimeFloat, .Float => return @ceil(numerator / denominator),

lib/std/meta.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -549,14 +549,14 @@ test "std.meta.FieldType" {
549549
}
550550

551551
pub fn fieldNames(comptime T: type) *const [fields(T).len][]const u8 {
552-
comptime {
552+
return comptime blk: {
553553
const fieldInfos = fields(T);
554554
var names: [fieldInfos.len][]const u8 = undefined;
555555
for (fieldInfos, 0..) |field, i| {
556556
names[i] = field.name;
557557
}
558-
return &names;
559-
}
558+
break :blk &names;
559+
};
560560
}
561561

562562
test "std.meta.fieldNames" {
@@ -590,14 +590,14 @@ test "std.meta.fieldNames" {
590590
/// Given an enum or error set type, returns a pointer to an array containing all tags for that
591591
/// enum or error set.
592592
pub fn tags(comptime T: type) *const [fields(T).len]T {
593-
comptime {
593+
return comptime blk: {
594594
const fieldInfos = fields(T);
595595
var res: [fieldInfos.len]T = undefined;
596596
for (fieldInfos, 0..) |field, i| {
597597
res[i] = @field(T, field.name);
598598
}
599-
return &res;
600-
}
599+
break :blk &res;
600+
};
601601
}
602602

603603
test "std.meta.tags" {

lib/std/meta/trait.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -400,31 +400,31 @@ test "isTuple" {
400400
/// *const u8, ?[]const u8, ?*const [N]u8.
401401
/// ```
402402
pub fn isZigString(comptime T: type) bool {
403-
comptime {
403+
return comptime blk: {
404404
// Only pointer types can be strings, no optionals
405405
const info = @typeInfo(T);
406-
if (info != .Pointer) return false;
406+
if (info != .Pointer) break :blk false;
407407

408408
const ptr = &info.Pointer;
409409
// Check for CV qualifiers that would prevent coerction to []const u8
410-
if (ptr.is_volatile or ptr.is_allowzero) return false;
410+
if (ptr.is_volatile or ptr.is_allowzero) break :blk false;
411411

412412
// If it's already a slice, simple check.
413413
if (ptr.size == .Slice) {
414-
return ptr.child == u8;
414+
break :blk ptr.child == u8;
415415
}
416416

417417
// Otherwise check if it's an array type that coerces to slice.
418418
if (ptr.size == .One) {
419419
const child = @typeInfo(ptr.child);
420420
if (child == .Array) {
421421
const arr = &child.Array;
422-
return arr.child == u8;
422+
break :blk arr.child == u8;
423423
}
424424
}
425425

426-
return false;
427-
}
426+
break :blk false;
427+
};
428428
}
429429

430430
test "isZigString" {

lib/std/net/test.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ test "parse and render UNIX addresses" {
9999
const fmt_addr = std.fmt.bufPrint(buffer[0..], "{}", .{addr}) catch unreachable;
100100
try std.testing.expectEqualSlices(u8, "/tmp/testpath", fmt_addr);
101101

102-
const too_long = [_]u8{'a'} ** (addr.un.path.len + 1);
102+
const too_long = [_]u8{'a'} ** 200;
103103
try testing.expectError(error.NameTooLong, net.Address.initUnix(too_long[0..]));
104104
}
105105

lib/std/unicode.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -774,13 +774,13 @@ test "utf8ToUtf16LeWithNull" {
774774

775775
/// Converts a UTF-8 string literal into a UTF-16LE string literal.
776776
pub fn utf8ToUtf16LeStringLiteral(comptime utf8: []const u8) *const [calcUtf16LeLen(utf8) catch unreachable:0]u16 {
777-
comptime {
777+
return comptime blk: {
778778
const len: usize = calcUtf16LeLen(utf8) catch |err| @compileError(err);
779779
var utf16le: [len:0]u16 = [_:0]u16{0} ** len;
780780
const utf16le_len = utf8ToUtf16Le(&utf16le, utf8[0..]) catch |err| @compileError(err);
781781
assert(len == utf16le_len);
782-
return &utf16le;
783-
}
782+
break :blk &utf16le;
783+
};
784784
}
785785

786786
const CalcUtf16LeLenError = Utf8DecodeError || error{Utf8InvalidStartByte};

lib/std/zig/system/linux.zig

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ test "cpuinfo: PowerPC" {
147147
}
148148

149149
const ArmCpuinfoImpl = struct {
150-
cores: [4]CoreInfo = undefined,
150+
const num_cores = 4;
151+
152+
cores: [num_cores]CoreInfo = undefined,
151153
core_no: usize = 0,
152154
have_fields: usize = 0,
153155

@@ -162,7 +164,7 @@ const ArmCpuinfoImpl = struct {
162164
const cpu_models = @import("arm.zig").cpu_models;
163165

164166
fn addOne(self: *ArmCpuinfoImpl) void {
165-
if (self.have_fields == 4 and self.core_no < self.cores.len) {
167+
if (self.have_fields == 4 and self.core_no < num_cores) {
166168
if (self.core_no > 0) {
167169
// Deduplicate the core info.
168170
for (self.cores[0..self.core_no]) |it| {
@@ -222,7 +224,7 @@ const ArmCpuinfoImpl = struct {
222224
else => false,
223225
};
224226

225-
var known_models: [self.cores.len]?*const Target.Cpu.Model = undefined;
227+
var known_models: [num_cores]?*const Target.Cpu.Model = undefined;
226228
for (self.cores[0..self.core_no], 0..) |core, i| {
227229
known_models[i] = cpu_models.isKnown(.{
228230
.architecture = core.architecture,

0 commit comments

Comments
 (0)