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

ZON #20271

Merged
merged 2 commits into from
Feb 3, 2025
Merged

ZON #20271

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
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ pub fn build(b: *std.Build) !void {
const optimization_modes = chosen_opt_modes_buf[0..chosen_mode_index];

const fmt_include_paths = &.{ "lib", "src", "test", "tools", "build.zig", "build.zig.zon" };
const fmt_exclude_paths = &.{"test/cases"};
const fmt_exclude_paths = &.{ "test/cases", "test/behavior/zon" };
MasonRemaley marked this conversation as resolved.
Show resolved Hide resolved
const do_fmt = b.addFmt(.{
.paths = fmt_include_paths,
.exclude_paths = fmt_exclude_paths,
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/aro/aro/Value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ pub fn toInt(v: Value, comptime T: type, comp: *const Compilation) ?T {
if (comp.interner.get(v.ref()) != .int) return null;
var space: BigIntSpace = undefined;
const big_int = v.toBigInt(&space, comp);
return big_int.to(T) catch null;
return big_int.toInt(T) catch null;
}

const ComplexOp = enum {
Expand Down
4 changes: 2 additions & 2 deletions lib/compiler/aro/backend/Interner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -628,13 +628,13 @@ pub fn put(i: *Interner, gpa: Allocator, key: Key) !Ref {
if (data.fitsInTwosComp(.unsigned, 32)) {
i.items.appendAssumeCapacity(.{
.tag = .u32,
.data = data.to(u32) catch unreachable,
.data = data.toInt(u32) catch unreachable,
});
break :int;
} else if (data.fitsInTwosComp(.signed, 32)) {
i.items.appendAssumeCapacity(.{
.tag = .i32,
.data = @bitCast(data.to(i32) catch unreachable),
.data = @bitCast(data.toInt(i32) catch unreachable),
});
break :int;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/std/heap/general_purpose_allocator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,8 @@ test "shrink large object to large object" {
}

test "shrink large object to large object with larger alignment" {
if (!builtin.link_libc and builtin.os.tag == .wasi) return error.SkipZigTest; // https://github.com/ziglang/zig/issues/22731

var gpa = GeneralPurposeAllocator(test_config){};
defer std.testing.expect(gpa.deinit() == .ok) catch @panic("leak");
const allocator = gpa.allocator();
Expand Down Expand Up @@ -1307,6 +1309,8 @@ test "non-page-allocator backing allocator" {
}

test "realloc large object to larger alignment" {
if (!builtin.link_libc and builtin.os.tag == .wasi) return error.SkipZigTest; // https://github.com/ziglang/zig/issues/22731

var gpa = GeneralPurposeAllocator(test_config){};
defer std.testing.expect(gpa.deinit() == .ok) catch @panic("leak");
const allocator = gpa.allocator();
Expand Down
42 changes: 36 additions & 6 deletions lib/std/math/big/int.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2175,10 +2175,13 @@ pub const Const = struct {
TargetTooSmall,
};

/// Convert self to type T.
/// Deprecated; use `toInt`.
pub const to = toInt;

/// Convert self to integer type T.
///
/// Returns an error if self cannot be narrowed into the requested type without truncation.
pub fn to(self: Const, comptime T: type) ConvertError!T {
pub fn toInt(self: Const, comptime T: type) ConvertError!T {
switch (@typeInfo(T)) {
.int => |info| {
// Make sure -0 is handled correctly.
Expand Down Expand Up @@ -2216,7 +2219,26 @@ pub const Const = struct {
}
}
},
else => @compileError("cannot convert Const to type " ++ @typeName(T)),
else => @compileError("expected int type, found '" ++ @typeName(T) ++ "'"),
}
}

/// Convert self to float type T.
pub fn toFloat(self: Const, comptime T: type) T {
if (self.limbs.len == 0) return 0;

const base = std.math.maxInt(std.math.big.Limb) + 1;
var result: f128 = 0;
var i: usize = self.limbs.len;
while (i != 0) {
i -= 1;
const limb: f128 = @floatFromInt(self.limbs[i]);
result = @mulAdd(f128, base, result, limb);
}
if (self.positive) {
return @floatCast(result);
} else {
return @floatCast(-result);
}
}

Expand Down Expand Up @@ -2775,11 +2797,19 @@ pub const Managed = struct {

pub const ConvertError = Const.ConvertError;

/// Convert self to type T.
/// Deprecated; use `toInt`.
pub const to = toInt;

/// Convert self to integer type T.
///
/// Returns an error if self cannot be narrowed into the requested type without truncation.
pub fn to(self: Managed, comptime T: type) ConvertError!T {
return self.toConst().to(T);
pub fn toInt(self: Managed, comptime T: type) ConvertError!T {
return self.toConst().toInt(T);
}

/// Convert self to float type T.
pub fn toFloat(self: Managed, comptime T: type) T {
return self.toConst().toFloat(T);
}

/// Set self from the string representation `value`.
Expand Down
Loading
Loading