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
20 changes: 15 additions & 5 deletions lib/std/Progress.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,9 @@ fn computeRedraw(serialized_buffer: *Serialized.Buffer) struct { []u8, usize } {
i += progress_pulsing.len;
} else {
const percent = completed_items * 100 / estimated_total;
i += (std.fmt.bufPrint(buf[i..], @"progress_normal {d}", .{percent}) catch &.{}).len;
if (std.fmt.bufPrint(buf[i..], @"progress_normal {d}", .{percent})) |b| {
i += b.len;
} else |_| {}
}
},
.success => {
Expand All @@ -1265,7 +1267,9 @@ fn computeRedraw(serialized_buffer: *Serialized.Buffer) struct { []u8, usize } {
i += progress_pulsing_error.len;
} else {
const percent = completed_items * 100 / estimated_total;
i += (std.fmt.bufPrint(buf[i..], @"progress_error {d}", .{percent}) catch &.{}).len;
if (std.fmt.bufPrint(buf[i..], @"progress_error {d}", .{percent})) |b| {
i += b.len;
} else |_| {}
}
},
}
Expand Down Expand Up @@ -1364,12 +1368,18 @@ fn computeNode(
if (!is_empty_root) {
if (name.len != 0 or estimated_total > 0) {
if (estimated_total > 0) {
i += (std.fmt.bufPrint(buf[i..], "[{d}/{d}] ", .{ completed_items, estimated_total }) catch &.{}).len;
if (std.fmt.bufPrint(buf[i..], "[{d}/{d}] ", .{ completed_items, estimated_total })) |b| {
i += b.len;
} else |_| {}
} else if (completed_items != 0) {
i += (std.fmt.bufPrint(buf[i..], "[{d}] ", .{completed_items}) catch &.{}).len;
if (std.fmt.bufPrint(buf[i..], "[{d}] ", .{completed_items})) |b| {
i += b.len;
} else |_| {}
}
if (name.len != 0) {
i += (std.fmt.bufPrint(buf[i..], "{s}", .{name}) catch &.{}).len;
if (std.fmt.bufPrint(buf[i..], "{s}", .{name})) |b| {
i += b.len;
} else |_| {}
}
}

Expand Down
5 changes: 3 additions & 2 deletions lib/std/Target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,7 @@ pub const Cpu = struct {
pub const Index = std.math.Log2Int(std.meta.Int(.unsigned, usize_count * @bitSizeOf(usize)));
pub const ShiftInt = std.math.Log2Int(usize);

pub const empty = Set{ .ints = [1]usize{0} ** usize_count };
pub const empty: Set = .{ .ints = @splat(0) };

pub fn isEmpty(set: Set) bool {
return for (set.ints) |x| {
Expand Down Expand Up @@ -1268,7 +1268,8 @@ pub const Cpu = struct {
return struct {
/// Populates only the feature bits specified.
pub fn featureSet(features: []const F) Set {
var x = Set.empty;
if (@sizeOf(F) == 0) return .empty;
var x: Set = .empty;
for (features) |feature| {
x.addFeature(@intFromEnum(feature));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/std/crypto/chacha20.zig
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ fn ChaChaVecImpl(comptime rounds_nb: usize, comptime degree: comptime_int) type
}

fn hashToBytes(comptime dm: usize, out: *[64 * dm]u8, x: BlockVec) void {
for (0..dm) |d| {
inline for (0..dm) |d| {
for (0..4) |i| {
mem.writeInt(u32, out[64 * d + 16 * i + 0 ..][0..4], x[i][0 + 4 * d], .little);
mem.writeInt(u32, out[64 * d + 16 * i + 4 ..][0..4], x[i][1 + 4 * d], .little);
Expand Down
26 changes: 22 additions & 4 deletions lib/std/json/static.zig
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ pub fn innerParse(
switch (try source.peekNextTokenType()) {
.array_begin => {
// Typical array.
return internalParseArray(T, arrayInfo.child, arrayInfo.len, allocator, source, options);
return internalParseArray(T, arrayInfo.child, allocator, source, options);
},
.string => {
if (arrayInfo.child != u8) return error.UnexpectedToken;
Expand Down Expand Up @@ -443,7 +443,7 @@ pub fn innerParse(
.vector => |vecInfo| {
switch (try source.peekNextTokenType()) {
.array_begin => {
return internalParseArray(T, vecInfo.child, vecInfo.len, allocator, source, options);
return internalParseVector(T, vecInfo.child, vecInfo.len, allocator, source, options);
},
else => return error.UnexpectedToken,
}
Expand Down Expand Up @@ -517,6 +517,25 @@ pub fn innerParse(
}

fn internalParseArray(
comptime T: type,
comptime Child: type,
allocator: Allocator,
source: anytype,
options: ParseOptions,
) !T {
assert(.array_begin == try source.next());

var r: T = undefined;
for (&r) |*elem| {
elem.* = try innerParse(Child, allocator, source, options);
}

if (.array_end != try source.next()) return error.UnexpectedToken;

return r;
}

fn internalParseVector(
comptime T: type,
comptime Child: type,
comptime len: comptime_int,
Expand All @@ -527,8 +546,7 @@ fn internalParseArray(
assert(.array_begin == try source.next());

var r: T = undefined;
var i: usize = 0;
while (i < len) : (i += 1) {
inline for (0..len) |i| {
r[i] = try innerParse(Child, allocator, source, options);
}

Expand Down
5 changes: 2 additions & 3 deletions lib/std/meta.zig
Original file line number Diff line number Diff line change
Expand Up @@ -743,9 +743,8 @@ pub fn eql(a: anytype, b: @TypeOf(a)) bool {
return true;
},
.vector => |info| {
var i: usize = 0;
while (i < info.len) : (i += 1) {
if (!eql(a[i], b[i])) return false;
inline for (0..info.len) |i| {
if (a[i] != b[i]) return false;
}
return true;
},
Expand Down
8 changes: 3 additions & 5 deletions lib/std/testing.zig
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,8 @@ fn expectEqualInner(comptime T: type, expected: T, actual: T) !void {
.array => |array| try expectEqualSlices(array.child, &expected, &actual),

.vector => |info| {
var i: usize = 0;
while (i < info.len) : (i += 1) {
if (!std.meta.eql(expected[i], actual[i])) {
inline for (0..info.len) |i| {
if (expected[i] != actual[i]) {
print("index {d} incorrect. expected {any}, found {any}\n", .{
i, expected[i], actual[i],
});
Expand Down Expand Up @@ -828,8 +827,7 @@ fn expectEqualDeepInner(comptime T: type, expected: T, actual: T) error{TestExpe
print("Vector len not the same, expected {d}, found {d}\n", .{ info.len, @typeInfo(@TypeOf(actual)).vector.len });
return error.TestExpectedEqual;
}
var i: usize = 0;
while (i < info.len) : (i += 1) {
inline for (0..info.len) |i| {
expectEqualDeep(expected[i], actual[i]) catch |e| {
print("index {d} incorrect. expected {any}, found {any}\n", .{
i, expected[i], actual[i],
Expand Down
73 changes: 23 additions & 50 deletions lib/std/zig/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2728,12 +2728,9 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
.elem_ptr,
.elem_val,
.elem_ptr_node,
.elem_val_node,
.elem_val_imm,
.field_ptr,
.field_val,
.field_ptr_named,
.field_val_named,
.func,
.func_inferred,
.func_fancy,
Expand Down Expand Up @@ -6156,37 +6153,28 @@ fn fieldAccess(
scope: *Scope,
ri: ResultInfo,
node: Ast.Node.Index,
) InnerError!Zir.Inst.Ref {
switch (ri.rl) {
.ref, .ref_coerced_ty => return addFieldAccess(.field_ptr, gz, scope, .{ .rl = .ref }, node),
else => {
const access = try addFieldAccess(.field_val, gz, scope, .{ .rl = .none }, node);
return rvalue(gz, ri, access, node);
},
}
}

fn addFieldAccess(
tag: Zir.Inst.Tag,
gz: *GenZir,
scope: *Scope,
lhs_ri: ResultInfo,
node: Ast.Node.Index,
) InnerError!Zir.Inst.Ref {
const astgen = gz.astgen;
const tree = astgen.tree;

const object_node, const field_ident = tree.nodeData(node).node_and_token;
const str_index = try astgen.identAsString(field_ident);
const lhs = try expr(gz, scope, lhs_ri, object_node);
const lhs = try expr(gz, scope, .{ .rl = .ref }, object_node);

const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);
try emitDbgStmt(gz, cursor);

return gz.addPlNode(tag, node, Zir.Inst.Field{
const ref = try gz.addPlNode(.field_ptr, node, Zir.Inst.Field{
.lhs = lhs,
.field_name_start = str_index,
});
switch (ri.rl) {
.ref, .ref_coerced_ty => return ref,
else => {
const loaded = try gz.addUnNode(.load, ref, node);
return rvalue(gz, ri, loaded, node);
},
}
}

fn arrayAccess(
Expand All @@ -6196,28 +6184,17 @@ fn arrayAccess(
node: Ast.Node.Index,
) InnerError!Zir.Inst.Ref {
const tree = gz.astgen.tree;
const lhs_node, const rhs_node = tree.nodeData(node).node_and_node;
const lhs = try expr(gz, scope, .{ .rl = .ref }, lhs_node);
const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);
const rhs = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, rhs_node);
try emitDbgStmt(gz, cursor);
const ref = try gz.addPlNode(.elem_ptr_node, node, Zir.Inst.Bin{ .lhs = lhs, .rhs = rhs });
switch (ri.rl) {
.ref, .ref_coerced_ty => {
const lhs_node, const rhs_node = tree.nodeData(node).node_and_node;
const lhs = try expr(gz, scope, .{ .rl = .ref }, lhs_node);

const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);

const rhs = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, rhs_node);
try emitDbgStmt(gz, cursor);

return gz.addPlNode(.elem_ptr_node, node, Zir.Inst.Bin{ .lhs = lhs, .rhs = rhs });
},
.ref, .ref_coerced_ty => return ref,
else => {
const lhs_node, const rhs_node = tree.nodeData(node).node_and_node;
const lhs = try expr(gz, scope, .{ .rl = .none }, lhs_node);

const cursor = maybeAdvanceSourceCursorToMainToken(gz, node);

const rhs = try expr(gz, scope, .{ .rl = .{ .coerced_ty = .usize_type } }, rhs_node);
try emitDbgStmt(gz, cursor);

return rvalue(gz, ri, try gz.addPlNode(.elem_val_node, node, Zir.Inst.Bin{ .lhs = lhs, .rhs = rhs }), node);
const loaded = try gz.addUnNode(.load, ref, node);
return rvalue(gz, ri, loaded, node);
},
}
}
Expand Down Expand Up @@ -9286,17 +9263,13 @@ fn builtinCall(
return rvalue(gz, ri, result, node);
},
.field => {
if (ri.rl == .ref or ri.rl == .ref_coerced_ty) {
return gz.addPlNode(.field_ptr_named, node, Zir.Inst.FieldNamed{
.lhs = try expr(gz, scope, .{ .rl = .ref }, params[0]),
.field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1], .field_name),
});
}
const result = try gz.addPlNode(.field_val_named, node, Zir.Inst.FieldNamed{
.lhs = try expr(gz, scope, .{ .rl = .none }, params[0]),
const ref = try gz.addPlNode(.field_ptr_named, node, Zir.Inst.FieldNamed{
.lhs = try expr(gz, scope, .{ .rl = .ref }, params[0]),
.field_name = try comptimeExpr(gz, scope, .{ .rl = .{ .coerced_ty = .slice_const_u8_type } }, params[1], .field_name),
});
return rvalue(gz, ri, result, node);
if (ri.rl == .ref or ri.rl == .ref_coerced_ty) return ref;
const loaded = try gz.addUnNode(.load, ref, node);
return rvalue(gz, ri, loaded, node);
},
.FieldType => {
const ty_inst = try typeExpr(gz, scope, params[0]);
Expand Down
26 changes: 2 additions & 24 deletions lib/std/zig/Zir.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Zig Intermediate Representation.
//!
//! Astgen.zig converts AST nodes to these untyped IR instructions. Next,
//! `std.zig.AstGen` converts AST nodes to these untyped IR instructions. Next,
//! Sema.zig processes these into AIR.
//! The minimum amount of information needed to represent a list of ZIR instructions.
//! Once this structure is completed, it can be used to generate AIR, followed by
Expand Down Expand Up @@ -442,8 +442,7 @@ pub const Inst = struct {
elem_ptr,
/// Given an array, slice, or pointer, returns the element at the provided index.
/// Uses the `pl_node` union field. AST node is a[b] syntax. Payload is `Bin`.
elem_val_node,
/// Same as `elem_val_node` but used only for for loop.
/// Used only for for loop.
/// Uses the `pl_node` union field. AST node is the condition of a for loop.
/// Payload is `Bin`.
/// No OOB safety check is emitted.
Expand Down Expand Up @@ -472,19 +471,10 @@ pub const Inst = struct {
/// to the named field. The field name is stored in string_bytes. Used by a.b syntax.
/// Uses `pl_node` field. The AST node is the a.b syntax. Payload is Field.
field_ptr,
/// Given a struct or object that contains virtual fields, returns the named field.
/// The field name is stored in string_bytes. Used by a.b syntax.
/// This instruction also accepts a pointer.
/// Uses `pl_node` field. The AST node is the a.b syntax. Payload is Field.
field_val,
/// Given a pointer to a struct or object that contains virtual fields, returns a pointer
/// to the named field. The field name is a comptime instruction. Used by @field.
/// Uses `pl_node` field. The AST node is the builtin call. Payload is FieldNamed.
field_ptr_named,
/// Given a struct or object that contains virtual fields, returns the named field.
/// The field name is a comptime instruction. Used by @field.
/// Uses `pl_node` field. The AST node is the builtin call. Payload is FieldNamed.
field_val_named,
/// Returns a function type, or a function instance, depending on whether
/// the body_len is 0. Calling convention is auto.
/// Uses the `pl_node` union field. `payload_index` points to a `Func`.
Expand Down Expand Up @@ -1138,16 +1128,13 @@ pub const Inst = struct {
.elem_ptr,
.elem_val,
.elem_ptr_node,
.elem_val_node,
.elem_val_imm,
.ensure_result_used,
.ensure_result_non_error,
.ensure_err_union_payload_void,
.@"export",
.field_ptr,
.field_val,
.field_ptr_named,
.field_val_named,
.func,
.func_inferred,
.func_fancy,
Expand Down Expand Up @@ -1432,12 +1419,9 @@ pub const Inst = struct {
.elem_ptr,
.elem_val,
.elem_ptr_node,
.elem_val_node,
.elem_val_imm,
.field_ptr,
.field_val,
.field_ptr_named,
.field_val_named,
.func,
.func_inferred,
.func_fancy,
Expand Down Expand Up @@ -1679,7 +1663,6 @@ pub const Inst = struct {
.elem_ptr = .pl_node,
.elem_ptr_node = .pl_node,
.elem_val = .pl_node,
.elem_val_node = .pl_node,
.elem_val_imm = .elem_val_imm,
.ensure_result_used = .un_node,
.ensure_result_non_error = .un_node,
Expand All @@ -1688,9 +1671,7 @@ pub const Inst = struct {
.error_value = .str_tok,
.@"export" = .pl_node,
.field_ptr = .pl_node,
.field_val = .pl_node,
.field_ptr_named = .pl_node,
.field_val_named = .pl_node,
.func = .pl_node,
.func_inferred = .pl_node,
.func_fancy = .pl_node,
Expand Down Expand Up @@ -4215,7 +4196,6 @@ fn findTrackableInner(
.div,
.elem_ptr_node,
.elem_ptr,
.elem_val_node,
.elem_val,
.elem_val_imm,
.ensure_result_used,
Expand All @@ -4225,9 +4205,7 @@ fn findTrackableInner(
.error_value,
.@"export",
.field_ptr,
.field_val,
.field_ptr_named,
.field_val_named,
.import,
.int,
.int_big,
Expand Down
2 changes: 1 addition & 1 deletion lib/std/zon/Serializer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ pub fn valueArbitraryDepth(self: *Serializer, val: anytype, options: ValueOption
var container = try self.beginTuple(
.{ .whitespace_style = .{ .fields = vector.len } },
);
for (0..vector.len) |i| {
inline for (0..vector.len) |i| {
try container.fieldArbitraryDepth(val[i], options);
}
try container.end();
Expand Down
Loading
Loading