Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ziglang/zig
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 9f2324389d4a
Choose a base ref
...
head repository: ziglang/zig
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 85422d7aeadc
Choose a head ref
  • 3 commits
  • 4 files changed
  • 2 contributors

Commits on Jun 19, 2018

  1. Copy the full SHA
    1392313 View commit details
  2. Added missing ?type in docs.

    alexnask committed Jun 19, 2018
    Copy the full SHA
    811539f View commit details
  3. Merge pull request #1136 from alexnask/typeinfo_improvements

    @typeinfo now uses optional types instead of @typeof(undefined)
    andrewrk authored Jun 19, 2018

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    85422d7 View commit details
Showing with 66 additions and 45 deletions.
  1. +5 −5 doc/langref.html.in
  2. +5 −5 src/codegen.cpp
  3. +48 −27 src/ir.cpp
  4. +8 −8 test/cases/type_info.zig
10 changes: 5 additions & 5 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
@@ -5755,7 +5755,7 @@ pub const TypeInfo = union(TypeId) {

pub const Union = struct {
layout: ContainerLayout,
tag_type: type,
tag_type: ?type,
fields: []UnionField,
defs: []Definition,
};
@@ -5772,20 +5772,20 @@ pub const TypeInfo = union(TypeId) {
pub const FnArg = struct {
is_generic: bool,
is_noalias: bool,
arg_type: type,
arg_type: ?type,
};

pub const Fn = struct {
calling_convention: CallingConvention,
is_generic: bool,
is_var_args: bool,
return_type: type,
async_allocator_type: type,
return_type: ?type,
async_allocator_type: ?type,
args: []FnArg,
};

pub const Promise = struct {
child: type,
child: ?type,
};

pub const Definition = struct {
10 changes: 5 additions & 5 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
@@ -6638,7 +6638,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
"\n"
" pub const Union = struct {\n"
" layout: ContainerLayout,\n"
" tag_type: type,\n"
" tag_type: ?type,\n"
" fields: []UnionField,\n"
" defs: []Definition,\n"
" };\n"
@@ -6655,20 +6655,20 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
" pub const FnArg = struct {\n"
" is_generic: bool,\n"
" is_noalias: bool,\n"
" arg_type: type,\n"
" arg_type: ?type,\n"
" };\n"
"\n"
" pub const Fn = struct {\n"
" calling_convention: CallingConvention,\n"
" is_generic: bool,\n"
" is_var_args: bool,\n"
" return_type: type,\n"
" async_allocator_type: type,\n"
" return_type: ?type,\n"
" async_allocator_type: ?type,\n"
" args: []FnArg,\n"
" };\n"
"\n"
" pub const Promise = struct {\n"
" child: type,\n"
" child: ?type,\n"
" };\n"
"\n"
" pub const Definition = struct {\n"
75 changes: 48 additions & 27 deletions src/ir.cpp
Original file line number Diff line number Diff line change
@@ -16789,16 +16789,20 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
ConstExprValue *fields = create_const_vals(1);
result->data.x_struct.fields = fields;

// @TODO ?type instead of using @typeOf(undefined) when we have no type.
// child: type
// child: ?type
ensure_field_index(result->type, "child", 0);
fields[0].special = ConstValSpecialStatic;
fields[0].type = ira->codegen->builtin_types.entry_type;
fields[0].type = get_maybe_type(ira->codegen, ira->codegen->builtin_types.entry_type);

if (type_entry->data.promise.result_type == nullptr)
fields[0].data.x_type = ira->codegen->builtin_types.entry_undef;
else
fields[0].data.x_type = type_entry->data.promise.result_type;
fields[0].data.x_optional = nullptr;
else {
ConstExprValue *child_type = create_const_vals(1);
child_type->special = ConstValSpecialStatic;
child_type->type = ira->codegen->builtin_types.entry_type;
child_type->data.x_type = type_entry->data.promise.result_type;
fields[0].data.x_optional = child_type;
}

break;
}
@@ -16939,19 +16943,23 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
fields[0].special = ConstValSpecialStatic;
fields[0].type = ir_type_info_get_type(ira, "ContainerLayout");
bigint_init_unsigned(&fields[0].data.x_enum_tag, type_entry->data.unionation.layout);
// tag_type: type
// tag_type: ?type
ensure_field_index(result->type, "tag_type", 1);
fields[1].special = ConstValSpecialStatic;
fields[1].type = ira->codegen->builtin_types.entry_type;
// @TODO ?type instead of using @typeOf(undefined) when we have no type.
fields[1].type = get_maybe_type(ira->codegen, ira->codegen->builtin_types.entry_type);

AstNode *union_decl_node = type_entry->data.unionation.decl_node;
if (union_decl_node->data.container_decl.auto_enum ||
union_decl_node->data.container_decl.init_arg_expr != nullptr)
{
fields[1].data.x_type = type_entry->data.unionation.tag_type;
ConstExprValue *tag_type = create_const_vals(1);
tag_type->special = ConstValSpecialStatic;
tag_type->type = ira->codegen->builtin_types.entry_type;
tag_type->data.x_type = type_entry->data.unionation.tag_type;
fields[1].data.x_optional = tag_type;
}
else
fields[1].data.x_type = ira->codegen->builtin_types.entry_undef;
fields[1].data.x_optional = nullptr;
// fields: []TypeInfo.UnionField
ensure_field_index(result->type, "fields", 2);

@@ -16980,7 +16988,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
inner_fields[1].special = ConstValSpecialStatic;
inner_fields[1].type = get_maybe_type(ira->codegen, type_info_enum_field_type);

if (fields[1].data.x_type == ira->codegen->builtin_types.entry_undef) {
if (fields[1].data.x_optional == nullptr) {
inner_fields[1].data.x_optional = nullptr;
} else {
inner_fields[1].data.x_optional = create_const_vals(1);
@@ -17089,8 +17097,6 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
ConstExprValue *fields = create_const_vals(6);
result->data.x_struct.fields = fields;

// @TODO Fix type = undefined with ?type

// calling_convention: TypeInfo.CallingConvention
ensure_field_index(result->type, "calling_convention", 0);
fields[0].special = ConstValSpecialStatic;
@@ -17108,22 +17114,32 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
fields[2].special = ConstValSpecialStatic;
fields[2].type = ira->codegen->builtin_types.entry_bool;
fields[2].data.x_bool = type_entry->data.fn.fn_type_id.is_var_args;
// return_type: type
// return_type: ?type
ensure_field_index(result->type, "return_type", 3);
fields[3].special = ConstValSpecialStatic;
fields[3].type = ira->codegen->builtin_types.entry_type;
fields[3].type = get_maybe_type(ira->codegen, ira->codegen->builtin_types.entry_type);
if (type_entry->data.fn.fn_type_id.return_type == nullptr)
fields[3].data.x_type = ira->codegen->builtin_types.entry_undef;
else
fields[3].data.x_type = type_entry->data.fn.fn_type_id.return_type;
fields[3].data.x_optional = nullptr;
else {
ConstExprValue *return_type = create_const_vals(1);
return_type->special = ConstValSpecialStatic;
return_type->type = ira->codegen->builtin_types.entry_type;
return_type->data.x_type = type_entry->data.fn.fn_type_id.return_type;
fields[3].data.x_optional = return_type;
}
// async_allocator_type: type
ensure_field_index(result->type, "async_allocator_type", 4);
fields[4].special = ConstValSpecialStatic;
fields[4].type = ira->codegen->builtin_types.entry_type;
fields[4].type = get_maybe_type(ira->codegen, ira->codegen->builtin_types.entry_type);
if (type_entry->data.fn.fn_type_id.async_allocator_type == nullptr)
fields[4].data.x_type = ira->codegen->builtin_types.entry_undef;
else
fields[4].data.x_type = type_entry->data.fn.fn_type_id.async_allocator_type;
fields[4].data.x_optional = nullptr;
else {
ConstExprValue *async_alloc_type = create_const_vals(1);
async_alloc_type->special = ConstValSpecialStatic;
async_alloc_type->type = ira->codegen->builtin_types.entry_type;
async_alloc_type->data.x_type = type_entry->data.fn.fn_type_id.async_allocator_type;
fields[4].data.x_optional = async_alloc_type;
}
// args: []TypeInfo.FnArg
TypeTableEntry *type_info_fn_arg_type = ir_type_info_get_type(ira, "FnArg");
size_t fn_arg_count = type_entry->data.fn.fn_type_id.param_count -
@@ -17157,12 +17173,17 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
inner_fields[1].type = ira->codegen->builtin_types.entry_bool;
inner_fields[1].data.x_bool = fn_param_info->is_noalias;
inner_fields[2].special = ConstValSpecialStatic;
inner_fields[2].type = ira->codegen->builtin_types.entry_type;
inner_fields[2].type = get_maybe_type(ira->codegen, ira->codegen->builtin_types.entry_type);

if (arg_is_generic)
inner_fields[2].data.x_type = ira->codegen->builtin_types.entry_undef;
else
inner_fields[2].data.x_type = fn_param_info->type;
inner_fields[2].data.x_optional = nullptr;
else {
ConstExprValue *arg_type = create_const_vals(1);
arg_type->special = ConstValSpecialStatic;
arg_type->type = ira->codegen->builtin_types.entry_type;
arg_type->data.x_type = fn_param_info->type;
inner_fields[2].data.x_optional = arg_type;
}

fn_arg_val->data.x_struct.fields = inner_fields;
fn_arg_val->data.x_struct.parent.id = ConstParentIdArray;
16 changes: 8 additions & 8 deletions test/cases/type_info.zig
Original file line number Diff line number Diff line change
@@ -107,11 +107,11 @@ test "type info: promise info" {
fn testPromise() void {
const null_promise_info = @typeInfo(promise);
assert(TypeId(null_promise_info) == TypeId.Promise);
assert(null_promise_info.Promise.child == @typeOf(undefined));
assert(null_promise_info.Promise.child == null);

const promise_info = @typeInfo(promise->usize);
assert(TypeId(promise_info) == TypeId.Promise);
assert(promise_info.Promise.child == usize);
assert(promise_info.Promise.child.? == usize);
}

test "type info: error set, error union info" {
@@ -165,7 +165,7 @@ fn testUnion() void {
const typeinfo_info = @typeInfo(TypeInfo);
assert(TypeId(typeinfo_info) == TypeId.Union);
assert(typeinfo_info.Union.layout == TypeInfo.ContainerLayout.Auto);
assert(typeinfo_info.Union.tag_type == TypeId);
assert(typeinfo_info.Union.tag_type.? == TypeId);
assert(typeinfo_info.Union.fields.len == 25);
assert(typeinfo_info.Union.fields[4].enum_field != null);
assert(typeinfo_info.Union.fields[4].enum_field.?.value == 4);
@@ -179,7 +179,7 @@ fn testUnion() void {

const notag_union_info = @typeInfo(TestNoTagUnion);
assert(TypeId(notag_union_info) == TypeId.Union);
assert(notag_union_info.Union.tag_type == @typeOf(undefined));
assert(notag_union_info.Union.tag_type == null);
assert(notag_union_info.Union.layout == TypeInfo.ContainerLayout.Auto);
assert(notag_union_info.Union.fields.len == 2);
assert(notag_union_info.Union.fields[0].enum_field == null);
@@ -191,7 +191,7 @@ fn testUnion() void {

const extern_union_info = @typeInfo(TestExternUnion);
assert(extern_union_info.Union.layout == TypeInfo.ContainerLayout.Extern);
assert(extern_union_info.Union.tag_type == @typeOf(undefined));
assert(extern_union_info.Union.tag_type == null);
assert(extern_union_info.Union.fields[0].enum_field == null);
assert(extern_union_info.Union.fields[0].field_type == *c_void);
}
@@ -238,13 +238,13 @@ fn testFunction() void {
assert(fn_info.Fn.is_generic);
assert(fn_info.Fn.args.len == 2);
assert(fn_info.Fn.is_var_args);
assert(fn_info.Fn.return_type == @typeOf(undefined));
assert(fn_info.Fn.async_allocator_type == @typeOf(undefined));
assert(fn_info.Fn.return_type == null);
assert(fn_info.Fn.async_allocator_type == null);

const test_instance: TestStruct = undefined;
const bound_fn_info = @typeInfo(@typeOf(test_instance.foo));
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 {