authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-17 19:33:19-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-17 20:48:09-07:00
logfeb4b01b38a13c85a7510a937cbdbede29f21764
treecfc4194a90e21d1a54425dc1676fb31c4f0ea63e
parentb837855317c3f73248e1ea4f3cad6b06d2568443

Sema: fix typeinfo for sentinels of array and pointer


3 files changed, 33 insertions(+), 17 deletions(-)

src/Sema.zig+29-13
...@@ -9640,9 +9640,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -9640,9 +9640,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
9640 // alignment: comptime_int,9640 // alignment: comptime_int,
9641 field_values[1] = try Value.Tag.int_u64.create(sema.arena, ty.abiAlignment(target));9641 field_values[1] = try Value.Tag.int_u64.create(sema.arena, ty.abiAlignment(target));
9642 // is_generic: bool,9642 // is_generic: bool,
9643 field_values[2] = if (info.is_generic) Value.@"true" else Value.@"false";9643 field_values[2] = Value.makeBool(info.is_generic);
9644 // is_var_args: bool,9644 // is_var_args: bool,
9645 field_values[3] = if (info.is_var_args) Value.@"true" else Value.@"false";9645 field_values[3] = Value.makeBool(info.is_var_args);
9646 // return_type: ?type,9646 // return_type: ?type,
9647 field_values[4] = try Value.Tag.ty.create(sema.arena, ty.fnReturnType());9647 field_values[4] = try Value.Tag.ty.create(sema.arena, ty.fnReturnType());
9648 // args: []const FnArg,9648 // args: []const FnArg,
...@@ -9699,9 +9699,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -9699,9 +9699,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
9699 // size: Size,9699 // size: Size,
9700 field_values[0] = try Value.Tag.enum_field_index.create(sema.arena, @enumToInt(info.size));9700 field_values[0] = try Value.Tag.enum_field_index.create(sema.arena, @enumToInt(info.size));
9701 // is_const: bool,9701 // is_const: bool,
9702 field_values[1] = if (!info.mutable) Value.@"true" else Value.@"false";9702 field_values[1] = Value.makeBool(!info.mutable);
9703 // is_volatile: bool,9703 // is_volatile: bool,
9704 field_values[2] = if (info.@"volatile") Value.@"true" else Value.@"false";9704 field_values[2] = Value.makeBool(info.@"volatile");
9705 // alignment: comptime_int,9705 // alignment: comptime_int,
9706 field_values[3] = try Value.Tag.int_u64.create(sema.arena, alignment);9706 field_values[3] = try Value.Tag.int_u64.create(sema.arena, alignment);
9707 // address_space: AddressSpace9707 // address_space: AddressSpace
...@@ -9709,9 +9709,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -9709,9 +9709,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
9709 // child: type,9709 // child: type,
9710 field_values[5] = try Value.Tag.ty.create(sema.arena, info.pointee_type);9710 field_values[5] = try Value.Tag.ty.create(sema.arena, info.pointee_type);
9711 // is_allowzero: bool,9711 // is_allowzero: bool,
9712 field_values[6] = if (info.@"allowzero") Value.@"true" else Value.@"false";9712 field_values[6] = Value.makeBool(info.@"allowzero");
9713 // sentinel: anytype,9713 // sentinel: ?*const anyopaque,
9714 field_values[7] = if (info.sentinel) |some| try Value.Tag.opt_payload.create(sema.arena, some) else Value.@"null";9714 field_values[7] = try sema.optRefValue(block, src, info.pointee_type, info.sentinel);
97159715
9716 return sema.addConstant(9716 return sema.addConstant(
9717 type_info_ty,9717 type_info_ty,
...@@ -9728,8 +9728,8 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -9728,8 +9728,8 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
9728 field_values[0] = try Value.Tag.int_u64.create(sema.arena, info.len);9728 field_values[0] = try Value.Tag.int_u64.create(sema.arena, info.len);
9729 // child: type,9729 // child: type,
9730 field_values[1] = try Value.Tag.ty.create(sema.arena, info.elem_type);9730 field_values[1] = try Value.Tag.ty.create(sema.arena, info.elem_type);
9731 // sentinel: anytype,9731 // sentinel: ?*const anyopaque,
9732 field_values[2] = if (info.sentinel) |some| try Value.Tag.opt_payload.create(sema.arena, some) else Value.@"null";9732 field_values[2] = try sema.optRefValue(block, src, info.elem_type, info.sentinel);
97339733
9734 return sema.addConstant(9734 return sema.addConstant(
9735 type_info_ty,9735 type_info_ty,
...@@ -9788,7 +9788,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -9788,7 +9788,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
9788 var int_tag_type_buffer: Type.Payload.Bits = undefined;9788 var int_tag_type_buffer: Type.Payload.Bits = undefined;
9789 const int_tag_ty = try ty.intTagType(&int_tag_type_buffer).copy(sema.arena);9789 const int_tag_ty = try ty.intTagType(&int_tag_type_buffer).copy(sema.arena);
97909790
9791 const is_exhaustive = if (ty.isNonexhaustiveEnum()) Value.@"false" else Value.@"true";9791 const is_exhaustive = Value.makeBool(!ty.isNonexhaustiveEnum());
97929792
9793 var fields_anon_decl = try block.startAnonDecl(src);9793 var fields_anon_decl = try block.startAnonDecl(src);
9794 defer fields_anon_decl.deinit();9794 defer fields_anon_decl.deinit();
...@@ -10050,14 +10050,12 @@ fn typeInfoDecls(...@@ -10050,14 +10050,12 @@ fn typeInfoDecls(
10050 break :v try Value.Tag.decl_ref.create(decls_anon_decl.arena(), new_decl);10050 break :v try Value.Tag.decl_ref.create(decls_anon_decl.arena(), new_decl);
10051 };10051 };
1005210052
10053 const is_pub = if (decl.is_pub) Value.@"true" else Value.@"false";
10054
10055 const fields = try decls_anon_decl.arena().create([2]Value);10053 const fields = try decls_anon_decl.arena().create([2]Value);
10056 fields.* = .{10054 fields.* = .{
10057 //name: []const u8,10055 //name: []const u8,
10058 name_val,10056 name_val,
10059 //is_pub: bool,10057 //is_pub: bool,
10060 is_pub,10058 Value.makeBool(decl.is_pub),
10061 };10059 };
10062 decls_val.* = try Value.Tag.@"struct".create(decls_anon_decl.arena(), fields);10060 decls_val.* = try Value.Tag.@"struct".create(decls_anon_decl.arena(), fields);
10063 }10061 }
...@@ -15660,6 +15658,24 @@ fn ensureDeclAnalyzed(sema: *Sema, decl: *Decl) CompileError!void {...@@ -15660,6 +15658,24 @@ fn ensureDeclAnalyzed(sema: *Sema, decl: *Decl) CompileError!void {
15660 };15658 };
15661}15659}
1566215660
15661fn refValue(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type, val: Value) !Value {
15662 var anon_decl = try block.startAnonDecl(src);
15663 defer anon_decl.deinit();
15664 const decl = try anon_decl.finish(
15665 try ty.copy(anon_decl.arena()),
15666 try val.copy(anon_decl.arena()),
15667 );
15668 try sema.mod.declareDeclDependency(sema.owner_decl, decl);
15669 return try Value.Tag.decl_ref.create(sema.arena, decl);
15670}
15671
15672fn optRefValue(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type, opt_val: ?Value) !Value {
15673 const val = opt_val orelse return Value.@"null";
15674 const ptr_val = try refValue(sema, block, src, ty, val);
15675 const result = try Value.Tag.opt_payload.create(sema.arena, ptr_val);
15676 return result;
15677}
15678
15663fn analyzeDeclRef(sema: *Sema, decl: *Decl) CompileError!Air.Inst.Ref {15679fn analyzeDeclRef(sema: *Sema, decl: *Decl) CompileError!Air.Inst.Ref {
15664 try sema.mod.declareDeclDependency(sema.owner_decl, decl);15680 try sema.mod.declareDeclDependency(sema.owner_decl, decl);
15665 try sema.ensureDeclAnalyzed(decl);15681 try sema.ensureDeclAnalyzed(decl);
src/value.zig+4
...@@ -3971,6 +3971,10 @@ pub const Value = extern union {...@@ -3971,6 +3971,10 @@ pub const Value = extern union {
3971 pub const @"null" = initTag(.null_value);3971 pub const @"null" = initTag(.null_value);
3972 pub const @"false" = initTag(.bool_false);3972 pub const @"false" = initTag(.bool_false);
3973 pub const @"true" = initTag(.bool_true);3973 pub const @"true" = initTag(.bool_true);
3974
3975 pub fn makeBool(x: bool) Value {
3976 return if (x) Value.@"true" else Value.@"false";
3977 }
3974};3978};
39753979
3976var negative_one_payload: Value.Payload.I64 = .{3980var negative_one_payload: Value.Payload.I64 = .{
test/behavior/type_info.zig-4
...@@ -103,8 +103,6 @@ fn testUnknownLenPtr() !void {...@@ -103,8 +103,6 @@ fn testUnknownLenPtr() !void {
103}103}
104104
105test "type info: null terminated pointer type info" {105test "type info: null terminated pointer type info" {
106 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
107
108 try testNullTerminatedPtr();106 try testNullTerminatedPtr();
109 comptime try testNullTerminatedPtr();107 comptime try testNullTerminatedPtr();
110}108}
...@@ -136,8 +134,6 @@ fn testSlice() !void {...@@ -136,8 +134,6 @@ fn testSlice() !void {
136}134}
137135
138test "type info: array type info" {136test "type info: array type info" {
139 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
140
141 try testArray();137 try testArray();
142 comptime try testArray();138 comptime try testArray();
143}139}