Skip to content

replace @Type with individual type-creating builtins #23733

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
81 changes: 41 additions & 40 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@
{#syntax#}i7{#endsyntax#} refers to a signed 7-bit integer. The maximum allowed bit-width of an
integer type is {#syntax#}65535{#endsyntax#}.
</p>
{#see_also|Integers|Floats|void|Errors|@Type#}
{#see_also|Integers|Floats|void|Errors|@Int#}
{#header_close#}
{#header_open|Primitive Values#}
<div class="table-wrapper">
Expand Down Expand Up @@ -3707,9 +3707,9 @@ void do_a_thing(struct Foo *foo) {
<td>{#syntax#}x{#endsyntax#} is a {#syntax#}@FieldType(T, "a"){#endsyntax#}</td>
</tr>
<tr>
<th scope="row">{#syntax#}@Type(x){#endsyntax#}</th>
<th scope="row">{#syntax#}@Struct(x){#endsyntax#}</th>
<td>-</td>
<td>{#syntax#}x{#endsyntax#} is a {#syntax#}std.builtin.Type{#endsyntax#}</td>
<td>{#syntax#}x{#endsyntax#} is a {#syntax#}std.builtin.Type.Struct{#endsyntax#}</td>
</tr>
<tr>
<th scope="row">{#syntax#}@typeInfo(x){#endsyntax#}</th>
Expand Down Expand Up @@ -3823,7 +3823,7 @@ void do_a_thing(struct Foo *foo) {
<td>{#syntax#}x{#endsyntax#} has no result location (typed initializers do not propagate result locations)</td>
</tr>
<tr>
<th scope="row">{#syntax#}@Type(x){#endsyntax#}</th>
<th scope="row">{#syntax#}@Struct(x){#endsyntax#}</th>
<td>{#syntax#}ptr{#endsyntax#}</td>
<td>{#syntax#}x{#endsyntax#} has no result location</td>
</tr>
Expand Down Expand Up @@ -5748,41 +5748,48 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val
</p>
{#header_close#}

{#header_open|@Type#}
<pre>{#syntax#}@Type(comptime info: std.builtin.Type) type{#endsyntax#}</pre>
{#header_open|@Enum#}
<pre>{#syntax#}@Enum(comptime info: std.builtin.Type.Enum) type{#endsyntax#}</pre>
<p>
This function is the inverse of {#link|@typeInfo#}. It reifies type information
into a {#syntax#}type{#endsyntax#}.
This function reifies an enum type with the given information.
</p>
{#header_close#}
{#header_open|@EnumLiteral#}
<pre>{#syntax#}@EnumLiteral() type{#endsyntax#}</pre>
<p>
It is available for the following types:
This function reifies the special enum literal type.
A unique type that represents anonymous enum values that can coerce to any enum type.
</p>
{#header_close#}
{#header_open|@Int#}
<pre>{#syntax#}@Int(comptime signedness: std.builtin.Signedness, comptime bits: u16) type{#endsyntax#}</pre>
<p>
This function reifies an integer type with the given signness and bit count.
</p>
{#header_close#}
{#header_open|@Pointer#}
<pre>{#syntax#}@Pointer(comptime info: std.builtin.Type.Pointer) type{#endsyntax#}</pre>
<p>
This function reifies a pointer type with the given information.
</p>
{#header_close#}
{#header_open|@Struct#}
<pre>{#syntax#}@Struct(comptime info: std.builtin.Type.Struct) type{#endsyntax#}</pre>
<p>
This function reifies an struct type with the given information.
</p>
<ul>
<li>{#syntax#}type{#endsyntax#}</li>
<li>{#syntax#}noreturn{#endsyntax#}</li>
<li>{#syntax#}void{#endsyntax#}</li>
<li>{#syntax#}bool{#endsyntax#}</li>
<li>{#link|Integers#} - The maximum bit count for an integer type is {#syntax#}65535{#endsyntax#}.</li>
<li>{#link|Floats#}</li>
<li>{#link|Pointers#}</li>
<li>{#syntax#}comptime_int{#endsyntax#}</li>
<li>{#syntax#}comptime_float{#endsyntax#}</li>
<li>{#syntax#}@TypeOf(undefined){#endsyntax#}</li>
<li>{#syntax#}@TypeOf(null){#endsyntax#}</li>
<li>{#link|Arrays#}</li>
<li>{#link|Optionals#}</li>
<li>{#link|Error Set Type#}</li>
<li>{#link|Error Union Type#}</li>
<li>{#link|Vectors#}</li>
<li>{#link|opaque#}</li>
<li>{#syntax#}anyframe{#endsyntax#}</li>
<li>{#link|struct#}</li>
<li>{#link|enum#}</li>
<li>{#link|Enum Literals#}</li>
<li>{#link|union#}</li>
<li>{#link|Functions#}</li>
</ul>
{#header_close#}
{#header_open|@Union#}
<pre>{#syntax#}@Union(comptime info: std.builtin.Type.Union) type{#endsyntax#}</pre>
<p>
This function reifies a union type with the given information.
</p>
{#header_close#}
{#header_open|@Vector#}
<pre>{#syntax#}@Vector(len: comptime_int, Element: type) type{#endsyntax#}</pre>
<p>Creates {#link|Vectors#}.</p>
{#header_close#}

{#header_open|@typeInfo#}
<pre>{#syntax#}@typeInfo(comptime T: type) std.builtin.Type{#endsyntax#}</pre>
<p>
Expand Down Expand Up @@ -5834,12 +5841,6 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val
</p>
{#header_close#}


{#header_open|@Vector#}
<pre>{#syntax#}@Vector(len: comptime_int, Element: type) type{#endsyntax#}</pre>
<p>Creates {#link|Vectors#}.</p>
{#header_close#}

{#header_open|@volatileCast#}
<pre>{#syntax#}@volatileCast(value: anytype) DestType{#endsyntax#}</pre>
<p>
Expand Down
2 changes: 1 addition & 1 deletion doc/langref/std_options.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub const std_options: std.Options = .{

fn myLogFn(
comptime level: std.log.Level,
comptime scope: @Type(.enum_literal),
comptime scope: @TypeOf(.enum_literal),
comptime format: []const u8,
args: anytype,
) void {
Expand Down
12 changes: 5 additions & 7 deletions lib/compiler/aro/aro/Attribute.zig
Original file line number Diff line number Diff line change
Expand Up @@ -712,13 +712,11 @@ pub const Arguments = blk: {
};
}

break :blk @Type(.{
.@"union" = .{
.layout = .auto,
.tag_type = null,
.fields = &union_fields,
.decls = &.{},
},
break :blk @Union(.{
.layout = .auto,
.tag_type = null,
.fields = &union_fields,
.decls = &.{},
});
};

Expand Down
4 changes: 2 additions & 2 deletions lib/compiler/resinator/code_pages.zig
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ pub const UnsupportedCodePage = enum(u16) {

pub const CodePage = blk: {
const fields = @typeInfo(SupportedCodePage).@"enum".fields ++ @typeInfo(UnsupportedCodePage).@"enum".fields;
break :blk @Type(.{ .@"enum" = .{
break :blk @Enum(.{
.tag_type = u16,
.decls = &.{},
.fields = fields,
.is_exhaustive = true,
} });
});
};

pub fn isSupported(code_page: CodePage) bool {
Expand Down
4 changes: 2 additions & 2 deletions lib/compiler/resinator/errors.zig
Original file line number Diff line number Diff line change
Expand Up @@ -868,12 +868,12 @@ pub const ErrorDetailsWithoutCodePage = blk: {
i += 1;
}
std.debug.assert(i == fields_without_codepage.len);
break :blk @Type(.{ .@"struct" = .{
break :blk @Struct(.{
.layout = .auto,
.fields = &fields_without_codepage,
.decls = &.{},
.is_tuple = false,
} });
});
};

fn cellCount(code_page: SupportedCodePage, source: []const u8, start_index: usize, end_index: usize) usize {
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/test_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ fn mainTerminal() void {

pub fn log(
comptime message_level: std.log.Level,
comptime scope: @Type(.enum_literal),
comptime scope: @TypeOf(.enum_literal),
comptime format: []const u8,
args: anytype,
) void {
Expand Down
5 changes: 1 addition & 4 deletions lib/compiler_rt/common.zig
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,7 @@ pub fn normalize(comptime T: type, significand: *std.meta.Int(.unsigned, @typeIn
pub inline fn fneg(a: anytype) @TypeOf(a) {
const F = @TypeOf(a);
const bits = @typeInfo(F).float.bits;
const U = @Type(.{ .int = .{
.signedness = .unsigned,
.bits = bits,
} });
const U = @Int(.unsigned, bits);
const sign_bit_mask = @as(U, 1) << (bits - 1);
const negated = @as(U, @bitCast(a)) ^ sign_bit_mask;
return @bitCast(negated);
Expand Down
10 changes: 5 additions & 5 deletions lib/compiler_rt/float_from_int.zig
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ pub inline fn floatFromBigInt(comptime T: type, comptime signedness: std.builtin
switch (x.len) {
0 => return 0,
inline 1...4 => |limbs_len| return @floatFromInt(@as(
@Type(.{ .int = .{ .signedness = signedness, .bits = 32 * limbs_len } }),
@Int(signedness, 32 * limbs_len),
@bitCast(x[0..limbs_len].*),
)),
else => {},
}

// sign implicit fraction round sticky
const I = comptime @Type(.{ .int = .{
.signedness = signedness,
.bits = @as(u16, @intFromBool(signedness == .signed)) + 1 + math.floatFractionalBits(T) + 1 + 1,
} });
const I = comptime @Int(
signedness,
@as(u16, @intFromBool(signedness == .signed)) + 1 + math.floatFractionalBits(T) + 1 + 1,
);

const clrsb = clrsb: {
var clsb: usize = 0;
Expand Down
7 changes: 2 additions & 5 deletions lib/compiler_rt/int_from_float.zig
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub inline fn bigIntFromFloat(comptime signedness: std.builtin.Signedness, resul
0 => return,
inline 1...4 => |limbs_len| {
result[0..limbs_len].* = @bitCast(@as(
@Type(.{ .int = .{ .signedness = signedness, .bits = 32 * limbs_len } }),
@Int(signedness, 32 * limbs_len),
@intFromFloat(a),
));
return;
Expand All @@ -66,10 +66,7 @@ pub inline fn bigIntFromFloat(comptime signedness: std.builtin.Signedness, resul

// sign implicit fraction
const significand_bits = 1 + math.floatFractionalBits(@TypeOf(a));
const I = @Type(comptime .{ .int = .{
.signedness = signedness,
.bits = @as(u16, @intFromBool(signedness == .signed)) + significand_bits,
} });
const I = @Int(signedness, @as(u16, @intFromBool(signedness == .signed)) + significand_bits);

const parts = math.frexp(a);
const significand_bits_adjusted_to_handle_smin = @as(i32, significand_bits) +
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler_rt/memcpy.zig
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ inline fn copyFixedLength(
else if (len > @sizeOf(usize))
@Vector(len, u8)
else
@Type(.{ .int = .{ .signedness = .unsigned, .bits = len * 8 } });
@Int(.unsigned, len * 8);

const loop_count = @divExact(len, @sizeOf(T));

Expand Down
2 changes: 1 addition & 1 deletion lib/fuzzer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var log_file_writer: ?std.fs.File.Writer = null;

fn logOverride(
comptime level: std.log.Level,
comptime scope: @Type(.enum_literal),
comptime scope: @TypeOf(.enum_literal),
comptime format: []const u8,
args: anytype,
) void {
Expand Down
4 changes: 2 additions & 2 deletions lib/std/Io.zig
Original file line number Diff line number Diff line change
Expand Up @@ -858,12 +858,12 @@ pub fn PollFiles(comptime StreamEnum: type) type {
.alignment = @alignOf(fs.File),
};
}
return @Type(.{ .@"struct" = .{
return @Struct(.{
.layout = .auto,
.fields = &struct_fields,
.decls = &.{},
.is_tuple = false,
} });
});
}

test {
Expand Down
13 changes: 5 additions & 8 deletions lib/std/Io/Reader.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1172,10 +1172,10 @@ pub const TakeLeb128Error = Error || error{Overflow};
/// Read a single LEB128 value as type T, or `error.Overflow` if the value cannot fit.
pub fn takeLeb128(r: *Reader, comptime Result: type) TakeLeb128Error!Result {
const result_info = @typeInfo(Result).int;
return std.math.cast(Result, try r.takeMultipleOf7Leb128(@Type(.{ .int = .{
.signedness = result_info.signedness,
.bits = std.mem.alignForwardAnyAlign(u16, result_info.bits, 7),
} }))) orelse error.Overflow;
return std.math.cast(Result, try r.takeMultipleOf7Leb128(@Int(
result_info.signedness,
std.mem.alignForwardAnyAlign(u16, result_info.bits, 7),
))) orelse error.Overflow;
}

pub fn expandTotalCapacity(r: *Reader, allocator: Allocator, n: usize) Allocator.Error!void {
Expand Down Expand Up @@ -1231,10 +1231,7 @@ fn takeMultipleOf7Leb128(r: *Reader, comptime Result: type) TakeLeb128Error!Resu
const result_info = @typeInfo(Result).int;
comptime assert(result_info.bits % 7 == 0);
var remaining_bits: std.math.Log2IntCeil(Result) = result_info.bits;
const UnsignedResult = @Type(.{ .int = .{
.signedness = .unsigned,
.bits = result_info.bits,
} });
const UnsignedResult = @Int(.unsigned, result_info.bits);
var result: UnsignedResult = 0;
var fits = true;
while (true) {
Expand Down
22 changes: 8 additions & 14 deletions lib/std/Io/Writer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1781,7 +1781,7 @@ pub fn writeUleb128(w: *Writer, value: anytype) Error!void {
try w.writeLeb128(switch (@typeInfo(@TypeOf(value))) {
.comptime_int => @as(std.math.IntFittingRange(0, @abs(value)), value),
.int => |value_info| switch (value_info.signedness) {
.signed => @as(@Type(.{ .int = .{ .signedness = .unsigned, .bits = value_info.bits -| 1 } }), @intCast(value)),
.signed => @as(@Int(.unsigned, value_info.bits -| 1), @intCast(value)),
.unsigned => value,
},
else => comptime unreachable,
Expand All @@ -1794,7 +1794,7 @@ pub fn writeSleb128(w: *Writer, value: anytype) Error!void {
.comptime_int => @as(std.math.IntFittingRange(@min(value, -1), @max(0, value)), value),
.int => |value_info| switch (value_info.signedness) {
.signed => value,
.unsigned => @as(@Type(.{ .int = .{ .signedness = .signed, .bits = value_info.bits + 1 } }), value),
.unsigned => @as(@Int(.signed, value_info.bits + 1), value),
},
else => comptime unreachable,
});
Expand All @@ -1803,10 +1803,10 @@ pub fn writeSleb128(w: *Writer, value: anytype) Error!void {
/// Write a single integer as LEB128 to the given writer.
pub fn writeLeb128(w: *Writer, value: anytype) Error!void {
const value_info = @typeInfo(@TypeOf(value)).int;
try w.writeMultipleOf7Leb128(@as(@Type(.{ .int = .{
.signedness = value_info.signedness,
.bits = std.mem.alignForwardAnyAlign(u16, value_info.bits, 7),
} }), value));
try w.writeMultipleOf7Leb128(@as(@Int(
value_info.signedness,
std.mem.alignForwardAnyAlign(u16, value_info.bits, 7),
), value));
}

fn writeMultipleOf7Leb128(w: *Writer, value: anytype) Error!void {
Expand All @@ -1821,16 +1821,10 @@ fn writeMultipleOf7Leb128(w: *Writer, value: anytype) Error!void {
.unsigned => remaining > std.math.maxInt(u7),
};
byte.* = if (@inComptime()) @typeInfo(@TypeOf(buffer)).pointer.child{
.bits = @bitCast(@as(@Type(.{ .int = .{
.signedness = value_info.signedness,
.bits = 7,
} }), @truncate(remaining))),
.bits = @bitCast(@as(@Int(value_info.signedness, 7), @truncate(remaining))),
.more = more,
} else .{
.bits = @bitCast(@as(@Type(.{ .int = .{
.signedness = value_info.signedness,
.bits = 7,
} }), @truncate(remaining))),
.bits = @bitCast(@as(@Int(value_info.signedness, 7), @truncate(remaining))),
.more = more,
};
if (value_info.bits > 7) remaining >>= 7;
Expand Down
2 changes: 1 addition & 1 deletion lib/std/Io/fixed_buffer_stream.zig
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn Slice(comptime T: type) type {
else => @compileError("invalid type given to fixedBufferStream"),
}
new_ptr_info.size = .slice;
return @Type(.{ .pointer = new_ptr_info });
return @Pointer(new_ptr_info);
},
else => @compileError("invalid type given to fixedBufferStream"),
}
Expand Down
8 changes: 4 additions & 4 deletions lib/std/crypto/phc_encoding.zig
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ pub fn deserialize(comptime HashResult: type, str: []const u8) Error!HashResult
if (kvSplit(field)) |opt_version| {
if (mem.eql(u8, opt_version.key, version_param_name)) {
if (@hasField(HashResult, "alg_version")) {
const value_type_info = switch (@typeInfo(@TypeOf(out.alg_version))) {
.optional => |opt| @typeInfo(opt.child),
else => |t| t,
const ValueType = switch (@typeInfo(@TypeOf(out.alg_version))) {
.optional => |opt| opt.child,
else => @TypeOf(out.alg_version),
};
out.alg_version = fmt.parseUnsigned(
@Type(value_type_info),
ValueType,
opt_version.value,
10,
) catch return Error.InvalidEncoding;
Expand Down
2 changes: 1 addition & 1 deletion lib/std/crypto/tls.zig
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ pub inline fn array(
const elem_size = @divExact(@bitSizeOf(Elem), 8);
var arr: [len_size + elem_size * elems.len]u8 = undefined;
std.mem.writeInt(Len, arr[0..len_size], @intCast(elem_size * elems.len), .big);
const ElemInt = @Type(.{ .int = .{ .signedness = .unsigned, .bits = @bitSizeOf(Elem) } });
const ElemInt = @Int(.unsigned, @bitSizeOf(Elem));
for (0.., @as([elems.len]Elem, elems)) |index, elem| {
std.mem.writeInt(
ElemInt,
Expand Down
Loading
Loading