From ac7703f65f7acc9137e28ded97659fbaadea4e66 Mon Sep 17 00:00:00 2001
From: Andrew Kelley
Builtin functions are provided by the compiler and are prefixed with
+ Deprecated. Use {#link|@Type#}.
+ \n");
+ if (last_columns) |n| {
+ try toc.print("
\n", n);
+ } else {
+ try toc.write("
\n");
+ }
} else {
last_action = Action.Open;
}
+ last_columns = columns;
try toc.writeByteNTimes(' ', 4 + header_stack_size * 4);
try toc.print("
@.
The {#syntax#}comptime{#endsyntax#} keyword on a parameter means that the parameter must be known
@@ -7232,6 +7232,9 @@ fn add(a: i32, b: i32) i32 { return a + b; }
This function returns an integer type with the given signness and bit count. The maximum
bit count for an integer type is {#syntax#}65535{#endsyntax#}.
{#syntax#}@Type(comptime info: @import("builtin").TypeInfo) type{#endsyntax#}
+ + This function is the inverse of {#link|@typeInfo#}. It reifies type information + into a {#syntax#}type{#endsyntax#}. +
++ It is available for the following types: +
++ For these types it is a + TODO in the compiler to implement: +
++ For these types, {#syntax#}@Type{#endsyntax#} is not available. + There is an open proposal to allow unions and structs. +
+{#syntax#}@typeId(comptime T: type) @import("builtin").TypeId{#endsyntax#}
diff --git a/src/all_types.hpp b/src/all_types.hpp index 4253a346f8..03559ccf12 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -54,6 +54,14 @@ enum PtrLen { PtrLenC, }; +// This one corresponds to the builtin.zig enum. +enum BuiltinPtrSize { + BuiltinPtrSizeOne, + BuiltinPtrSizeMany, + BuiltinPtrSizeSlice, + BuiltinPtrSizeC, +}; + enum UndefAllowed { UndefOk, UndefBad, diff --git a/src/codegen.cpp b/src/codegen.cpp index d7d4b44437..13fb0d625d 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -8161,20 +8161,25 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { " };\n" " };\n" "};\n\n"); - assert(ContainerLayoutAuto == 0); - assert(ContainerLayoutExtern == 1); - assert(ContainerLayoutPacked == 2); + static_assert(ContainerLayoutAuto == 0, ""); + static_assert(ContainerLayoutExtern == 1, ""); + static_assert(ContainerLayoutPacked == 2, ""); - assert(CallingConventionUnspecified == 0); - assert(CallingConventionC == 1); - assert(CallingConventionCold == 2); - assert(CallingConventionNaked == 3); - assert(CallingConventionStdcall == 4); - assert(CallingConventionAsync == 5); + static_assert(CallingConventionUnspecified == 0, ""); + static_assert(CallingConventionC == 1, ""); + static_assert(CallingConventionCold == 2, ""); + static_assert(CallingConventionNaked == 3, ""); + static_assert(CallingConventionStdcall == 4, ""); + static_assert(CallingConventionAsync == 5, ""); - assert(FnInlineAuto == 0); - assert(FnInlineAlways == 1); - assert(FnInlineNever == 2); + static_assert(FnInlineAuto == 0, ""); + static_assert(FnInlineAlways == 1, ""); + static_assert(FnInlineNever == 2, ""); + + static_assert(BuiltinPtrSizeOne == 0, ""); + static_assert(BuiltinPtrSizeMany == 1, ""); + static_assert(BuiltinPtrSizeSlice == 2, ""); + static_assert(BuiltinPtrSizeC == 3, ""); } { buf_appendf(contents, diff --git a/src/ir.cpp b/src/ir.cpp index 953467c185..9547b1ec61 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -20115,25 +20115,26 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr return ErrorNone; } -static uint32_t ptr_len_to_size_enum_index(PtrLen ptr_len) { +static BuiltinPtrSize ptr_len_to_size_enum_index(PtrLen ptr_len) { switch (ptr_len) { case PtrLenSingle: - return 0; + return BuiltinPtrSizeOne; case PtrLenUnknown: - return 1; + return BuiltinPtrSizeMany; case PtrLenC: - return 3; + return BuiltinPtrSizeC; } zig_unreachable(); } -static PtrLen size_enum_index_to_ptr_len(uint32_t size_enum_index) { +static PtrLen size_enum_index_to_ptr_len(BuiltinPtrSize size_enum_index) { switch (size_enum_index) { - case 0: + case BuiltinPtrSizeOne: return PtrLenSingle; - case 1: + case BuiltinPtrSizeMany: + case BuiltinPtrSizeSlice: return PtrLenUnknown; - case 3: + case BuiltinPtrSizeC: return PtrLenC; } zig_unreachable(); @@ -20142,10 +20143,10 @@ static PtrLen size_enum_index_to_ptr_len(uint32_t size_enum_index) { static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_type_entry) { Error err; ZigType *attrs_type; - uint32_t size_enum_index; + BuiltinPtrSize size_enum_index; if (is_slice(ptr_type_entry)) { attrs_type = ptr_type_entry->data.structure.fields[slice_ptr_index].type_entry; - size_enum_index = 2; + size_enum_index = BuiltinPtrSizeSlice; } else if (ptr_type_entry->id == ZigTypeIdPointer) { attrs_type = ptr_type_entry; size_enum_index = ptr_len_to_size_enum_index(ptr_type_entry->data.pointer.ptr_len); @@ -20892,21 +20893,16 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInstruction *instruction, Zi assert(payload->type == type_info_pointer_type); ConstExprValue *size_value = get_const_field(ira, payload, "size", 0); assert(size_value->type == ir_type_info_get_type(ira, "Size", type_info_pointer_type)); - uint32_t size_enum_index = bigint_as_u32(&size_value->data.x_enum_tag); - PtrLen ptr_len; - if (size_enum_index == 2) { - ptr_len = PtrLenUnknown; // TODO: is this right? - } else { - ptr_len = size_enum_index_to_ptr_len(size_enum_index); - } + BuiltinPtrSize size_enum_index = (BuiltinPtrSize)bigint_as_u32(&size_value->data.x_enum_tag); + PtrLen ptr_len = size_enum_index_to_ptr_len(size_enum_index); ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, get_const_field_meta_type(ira, payload, "child", 4), get_const_field_bool(ira, payload, "is_const", 1), get_const_field_bool(ira, payload, "is_volatile", 2), ptr_len, bigint_as_u32(get_const_field_lit_int(ira, payload, "alignment", 3)), - 0, // bit_offset_in_host??? - 0, // host_int_bytes??? + 0, // bit_offset_in_host + 0, // host_int_bytes get_const_field_bool(ira, payload, "is_allowzero", 5) ); if (size_enum_index != 2) @@ -20932,7 +20928,7 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInstruction *instruction, Zi case ZigTypeIdVector: case ZigTypeIdEnumLiteral: ir_add_error(ira, instruction, buf_sprintf( - "TODO implement @Type forr 'TypeInfo.%s': see https://github.com/ziglang/zig/issues/2907\n", type_id_name(tagTypeId))); + "TODO implement @Type for 'TypeInfo.%s': see https://github.com/ziglang/zig/issues/2907", type_id_name(tagTypeId))); return nullptr; case ZigTypeIdUnion: case ZigTypeIdFn: @@ -20940,7 +20936,7 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInstruction *instruction, Zi case ZigTypeIdArgTuple: case ZigTypeIdStruct: ir_add_error(ira, instruction, buf_sprintf( - "@Type not availble for 'TypeInfo.%s'\n", type_id_name(tagTypeId))); + "@Type not availble for 'TypeInfo.%s'", type_id_name(tagTypeId))); return nullptr; } zig_unreachable(); diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 1438a59539..38fca5754d 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -2,6 +2,15 @@ const tests = @import("tests.zig"); const builtin = @import("builtin"); pub fn addCases(cases: *tests.CompileErrorContext) void { + cases.add( + "attempt to create 17 bit float type", + \\const builtin = @import("builtin"); + \\comptime { + \\ _ = @Type(builtin.TypeInfo { .Float = builtin.TypeInfo.Float { .bits = 17 } }); + \\} + , + "tmp.zig:3:32: error: 17-bit float unsupported", + ); cases.add( "wrong type for @Type", @@ -45,15 +54,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:2:15: error: @Type not availble for 'TypeInfo.Struct'", ); - cases.add( - "array not implemented for @Type", - \\export fn entry() void { - \\ _ = @Type(@typeInfo(enum{x})); - \\} - , - "tmp.zig:2:15: error: TODO implement @Type forr 'TypeInfo.Enum': see https://github.com/ziglang/zig/issues/2907", - ); - cases.add( "wrong type for result ptr to @asyncCall", \\export fn entry() void { diff --git a/test/stage1/behavior/type.zig b/test/stage1/behavior/type.zig index 318e07fbdf..5b2998f088 100644 --- a/test/stage1/behavior/type.zig +++ b/test/stage1/behavior/type.zig @@ -38,8 +38,6 @@ test "Type.Int" { testing.expect(u64 == @Type(TypeInfo { .Int = TypeInfo.Int { .is_signed = false, .bits = 64 } })); testing.expect(i64 == @Type(TypeInfo { .Int = TypeInfo.Int { .is_signed = true, .bits = 64 } })); testTypes([_]type {u8,u32,i64}); - // TODO: should this work? - //testing.expect(u1 == @Type(TypeInfo.Int { .is_signed = false, .bits = 1 } )); } test "Type.Float" { @@ -48,8 +46,6 @@ test "Type.Float" { testing.expect(f64 == @Type(TypeInfo { .Float = TypeInfo.Float { .bits = 64 } })); testing.expect(f128 == @Type(TypeInfo { .Float = TypeInfo.Float { .bits = 128 } })); testTypes([_]type {f16, f32, f64, f128}); - // error: 17-bit float unsupported - //testing.expect(f16 == @Type(TypeInfo { .Float = TypeInfo.Float { .bits = 17 } })); } test "Type.Pointer" {