fix @typeInfo not setting a field to comptime

This commit is contained in:
Andrew Kelley 2018-05-15 14:11:41 -04:00
parent 04bca58a3a
commit 74b10c08d1
2 changed files with 171 additions and 135 deletions

View File

@ -16549,6 +16549,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
{ {
size_t byte_offset = LLVMOffsetOfElement(ira->codegen->target_data_ref, type_entry->type_ref, struct_field->gen_index); size_t byte_offset = LLVMOffsetOfElement(ira->codegen->target_data_ref, type_entry->type_ref, struct_field->gen_index);
inner_fields[1].data.x_maybe = create_const_vals(1); inner_fields[1].data.x_maybe = create_const_vals(1);
inner_fields[1].data.x_maybe->special = ConstValSpecialStatic;
inner_fields[1].data.x_maybe->type = ira->codegen->builtin_types.entry_usize; inner_fields[1].data.x_maybe->type = ira->codegen->builtin_types.entry_usize;
bigint_init_unsigned(&inner_fields[1].data.x_maybe->data.x_bigint, byte_offset); bigint_init_unsigned(&inner_fields[1].data.x_maybe->data.x_bigint, byte_offset);
} }

View File

@ -4,16 +4,23 @@ const TypeInfo = @import("builtin").TypeInfo;
const TypeId = @import("builtin").TypeId; const TypeId = @import("builtin").TypeId;
test "type info: tag type, void info" { test "type info: tag type, void info" {
comptime { testBasic();
comptime testBasic();
}
fn testBasic() void {
assert(@TagType(TypeInfo) == TypeId); assert(@TagType(TypeInfo) == TypeId);
const void_info = @typeInfo(void); const void_info = @typeInfo(void);
assert(TypeId(void_info) == TypeId.Void); assert(TypeId(void_info) == TypeId.Void);
assert(void_info.Void == {}); assert(void_info.Void == {});
} }
}
test "type info: integer, floating point type info" { test "type info: integer, floating point type info" {
comptime { testIntFloat();
comptime testIntFloat();
}
fn testIntFloat() void {
const u8_info = @typeInfo(u8); const u8_info = @typeInfo(u8);
assert(TypeId(u8_info) == TypeId.Int); assert(TypeId(u8_info) == TypeId.Int);
assert(!u8_info.Int.is_signed); assert(!u8_info.Int.is_signed);
@ -23,10 +30,13 @@ test "type info: integer, floating point type info" {
assert(TypeId(f64_info) == TypeId.Float); assert(TypeId(f64_info) == TypeId.Float);
assert(f64_info.Float.bits == 64); assert(f64_info.Float.bits == 64);
} }
}
test "type info: pointer type info" { test "type info: pointer type info" {
comptime { testPointer();
comptime testPointer();
}
fn testPointer() void {
const u32_ptr_info = @typeInfo(&u32); const u32_ptr_info = @typeInfo(&u32);
assert(TypeId(u32_ptr_info) == TypeId.Pointer); assert(TypeId(u32_ptr_info) == TypeId.Pointer);
assert(u32_ptr_info.Pointer.is_const == false); assert(u32_ptr_info.Pointer.is_const == false);
@ -34,10 +44,13 @@ test "type info: pointer type info" {
assert(u32_ptr_info.Pointer.alignment == 4); assert(u32_ptr_info.Pointer.alignment == 4);
assert(u32_ptr_info.Pointer.child == u32); assert(u32_ptr_info.Pointer.child == u32);
} }
}
test "type info: slice type info" { test "type info: slice type info" {
comptime { testSlice();
comptime testSlice();
}
fn testSlice() void {
const u32_slice_info = @typeInfo([]u32); const u32_slice_info = @typeInfo([]u32);
assert(TypeId(u32_slice_info) == TypeId.Slice); assert(TypeId(u32_slice_info) == TypeId.Slice);
assert(u32_slice_info.Slice.is_const == false); assert(u32_slice_info.Slice.is_const == false);
@ -45,27 +58,36 @@ test "type info: slice type info" {
assert(u32_slice_info.Slice.alignment == 4); assert(u32_slice_info.Slice.alignment == 4);
assert(u32_slice_info.Slice.child == u32); assert(u32_slice_info.Slice.child == u32);
} }
}
test "type info: array type info" { test "type info: array type info" {
comptime { testArray();
comptime testArray();
}
fn testArray() void {
const arr_info = @typeInfo([42]bool); const arr_info = @typeInfo([42]bool);
assert(TypeId(arr_info) == TypeId.Array); assert(TypeId(arr_info) == TypeId.Array);
assert(arr_info.Array.len == 42); assert(arr_info.Array.len == 42);
assert(arr_info.Array.child == bool); assert(arr_info.Array.child == bool);
} }
}
test "type info: nullable type info" { test "type info: nullable type info" {
comptime { testNullable();
comptime testNullable();
}
fn testNullable() void {
const null_info = @typeInfo(?void); const null_info = @typeInfo(?void);
assert(TypeId(null_info) == TypeId.Nullable); assert(TypeId(null_info) == TypeId.Nullable);
assert(null_info.Nullable.child == void); assert(null_info.Nullable.child == void);
} }
}
test "type info: promise info" { test "type info: promise info" {
comptime { testPromise();
comptime testPromise();
}
fn testPromise() void {
const null_promise_info = @typeInfo(promise); const null_promise_info = @typeInfo(promise);
assert(TypeId(null_promise_info) == TypeId.Promise); assert(TypeId(null_promise_info) == TypeId.Promise);
assert(null_promise_info.Promise.child == @typeOf(undefined)); assert(null_promise_info.Promise.child == @typeOf(undefined));
@ -75,10 +97,12 @@ test "type info: promise info" {
assert(promise_info.Promise.child == usize); assert(promise_info.Promise.child == usize);
} }
test "type info: error set, error union info" {
testErrorSet();
comptime testErrorSet();
} }
test "type info: error set, error union info" { fn testErrorSet() void {
comptime {
const TestErrorSet = error { const TestErrorSet = error {
First, First,
Second, Second,
@ -96,10 +120,13 @@ test "type info: error set, error union info" {
assert(error_union_info.ErrorUnion.error_set == TestErrorSet); assert(error_union_info.ErrorUnion.error_set == TestErrorSet);
assert(error_union_info.ErrorUnion.payload == usize); assert(error_union_info.ErrorUnion.payload == usize);
} }
}
test "type info: enum info" { test "type info: enum info" {
comptime { testEnum();
comptime testEnum();
}
fn testEnum() void {
const Os = @import("builtin").Os; const Os = @import("builtin").Os;
const os_info = @typeInfo(Os); const os_info = @typeInfo(Os);
@ -111,10 +138,13 @@ test "type info: enum info" {
assert(os_info.Enum.tag_type == u5); assert(os_info.Enum.tag_type == u5);
assert(os_info.Enum.defs.len == 0); assert(os_info.Enum.defs.len == 0);
} }
}
test "type info: union info" { test "type info: union info" {
comptime { testUnion();
comptime testUnion();
}
fn testUnion() void {
const typeinfo_info = @typeInfo(TypeInfo); const typeinfo_info = @typeInfo(TypeInfo);
assert(TypeId(typeinfo_info) == TypeId.Union); assert(TypeId(typeinfo_info) == TypeId.Union);
assert(typeinfo_info.Union.layout == TypeInfo.ContainerLayout.Auto); assert(typeinfo_info.Union.layout == TypeInfo.ContainerLayout.Auto);
@ -148,10 +178,13 @@ test "type info: union info" {
assert(extern_union_info.Union.fields[0].enum_field == null); assert(extern_union_info.Union.fields[0].enum_field == null);
assert(extern_union_info.Union.fields[0].field_type == &c_void); assert(extern_union_info.Union.fields[0].field_type == &c_void);
} }
}
test "type info: struct info" { test "type info: struct info" {
comptime { testStruct();
comptime testStruct();
}
fn testStruct() void {
const struct_info = @typeInfo(TestStruct); const struct_info = @typeInfo(TestStruct);
assert(TypeId(struct_info) == TypeId.Struct); assert(TypeId(struct_info) == TypeId.Struct);
assert(struct_info.Struct.layout == TypeInfo.ContainerLayout.Packed); assert(struct_info.Struct.layout == TypeInfo.ContainerLayout.Packed);
@ -165,7 +198,6 @@ test "type info: struct info" {
assert(struct_info.Struct.defs[0].data.Fn.return_type == void); assert(struct_info.Struct.defs[0].data.Fn.return_type == void);
assert(struct_info.Struct.defs[0].data.Fn.fn_type == fn(&const TestStruct)void); assert(struct_info.Struct.defs[0].data.Fn.fn_type == fn(&const TestStruct)void);
} }
}
const TestStruct = packed struct { const TestStruct = packed struct {
const Self = this; const Self = this;
@ -178,7 +210,11 @@ const TestStruct = packed struct {
}; };
test "type info: function type info" { test "type info: function type info" {
comptime { testFunction();
comptime testFunction();
}
fn testFunction() void {
const fn_info = @typeInfo(@typeOf(foo)); const fn_info = @typeInfo(@typeOf(foo));
assert(TypeId(fn_info) == TypeId.Fn); assert(TypeId(fn_info) == TypeId.Fn);
assert(fn_info.Fn.calling_convention == TypeInfo.CallingConvention.Unspecified); assert(fn_info.Fn.calling_convention == TypeInfo.CallingConvention.Unspecified);
@ -193,7 +229,6 @@ test "type info: function type info" {
assert(TypeId(bound_fn_info) == TypeId.BoundFn); assert(TypeId(bound_fn_info) == TypeId.BoundFn);
assert(bound_fn_info.BoundFn.args[0].arg_type == &const TestStruct); assert(bound_fn_info.BoundFn.args[0].arg_type == &const TestStruct);
} }
}
fn foo(comptime a: usize, b: bool, args: ...) usize { fn foo(comptime a: usize, b: bool, args: ...) usize {
return 0; return 0;