authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-10-25 12:25:34+01:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-10-28 21:56:28+02:00
log7bc95316982da0da6fd5d935645972fe5464e46e
tree8ba243f467260d4cb3847b59facd0b252e32d6fe
parent0adc144f88b567054e63df808aac0227445e88bb

stage1: Correctly generated optional constant values

Closes #6799

2 files changed, 14 insertions(+), 3 deletions(-)

src/stage1/ir.cpp+3
...@@ -16313,6 +16313,9 @@ static void set_optional_payload(ZigValue *opt_val, ZigValue *payload) {...@@ -16313,6 +16313,9 @@ static void set_optional_payload(ZigValue *opt_val, ZigValue *payload) {
16313 assert(opt_val->type->id == ZigTypeIdOptional);16313 assert(opt_val->type->id == ZigTypeIdOptional);
16314 if (payload == nullptr) {16314 if (payload == nullptr) {
16315 set_optional_value_to_null(opt_val);16315 set_optional_value_to_null(opt_val);
16316 } else if (get_src_ptr_type(opt_val->type)) {
16317 assert(get_src_ptr_type(payload->type));
16318 opt_val->data.x_ptr = payload->data.x_ptr;
16316 } else if (is_opt_err_set(opt_val->type)) {16319 } else if (is_opt_err_set(opt_val->type)) {
16317 assert(payload->type->id == ZigTypeIdErrorSet);16320 assert(payload->type->id == ZigTypeIdErrorSet);
16318 opt_val->data.x_err_set = payload->data.x_err_set;16321 opt_val->data.x_err_set = payload->data.x_err_set;
test/stage1/behavior/type_info.zig+11-3
...@@ -1,10 +1,13 @@...@@ -1,10 +1,13 @@
1const std = @import("std");1const std = @import("std");
2const expect = std.testing.expect;2const builtin = std.builtin;
3const mem = std.mem;3const mem = std.mem;
4const builtin = @import("builtin");4
5const TypeInfo = builtin.TypeInfo;5const TypeInfo = builtin.TypeInfo;
6const TypeId = builtin.TypeId;6const TypeId = builtin.TypeId;
77
8const expect = std.testing.expect;
9const expectEqualStrings = std.testing.expectEqualStrings;
10
8test "type info: tag type, void info" {11test "type info: tag type, void info" {
9 testBasic();12 testBasic();
10 comptime testBasic();13 comptime testBasic();
...@@ -232,10 +235,14 @@ test "type info: struct info" {...@@ -232,10 +235,14 @@ test "type info: struct info" {
232235
233fn testStruct() void {236fn testStruct() void {
234 const unpacked_struct_info = @typeInfo(TestUnpackedStruct);237 const unpacked_struct_info = @typeInfo(TestUnpackedStruct);
238 expect(unpacked_struct_info.Struct.is_tuple == false);
235 expect(unpacked_struct_info.Struct.fields[0].alignment == @alignOf(u32));239 expect(unpacked_struct_info.Struct.fields[0].alignment == @alignOf(u32));
240 expect(unpacked_struct_info.Struct.fields[0].default_value.? == 4);
241 expectEqualStrings("foobar", unpacked_struct_info.Struct.fields[1].default_value.?);
236242
237 const struct_info = @typeInfo(TestStruct);243 const struct_info = @typeInfo(TestStruct);
238 expect(struct_info == .Struct);244 expect(struct_info == .Struct);
245 expect(struct_info.Struct.is_tuple == false);
239 expect(struct_info.Struct.layout == .Packed);246 expect(struct_info.Struct.layout == .Packed);
240 expect(struct_info.Struct.fields.len == 4);247 expect(struct_info.Struct.fields.len == 4);
241 expect(struct_info.Struct.fields[0].alignment == 2 * @alignOf(usize));248 expect(struct_info.Struct.fields[0].alignment == 2 * @alignOf(usize));
...@@ -253,6 +260,7 @@ fn testStruct() void {...@@ -253,6 +260,7 @@ fn testStruct() void {
253260
254const TestUnpackedStruct = struct {261const TestUnpackedStruct = struct {
255 fieldA: u32 = 4,262 fieldA: u32 = 4,
263 fieldB: *const [6:0]u8 = "foobar",
256};264};
257265
258const TestStruct = packed struct {266const TestStruct = packed struct {
...@@ -371,7 +379,7 @@ test "type info: extern fns with and without lib names" {...@@ -371,7 +379,7 @@ test "type info: extern fns with and without lib names" {
371 if (std.mem.eql(u8, decl.name, "bar1")) {379 if (std.mem.eql(u8, decl.name, "bar1")) {
372 expect(decl.data.Fn.lib_name == null);380 expect(decl.data.Fn.lib_name == null);
373 } else {381 } else {
374 std.testing.expectEqual(@as([]const u8, "cool"), decl.data.Fn.lib_name.?);382 expectEqualStrings("cool", decl.data.Fn.lib_name.?);
375 }383 }
376 }384 }
377 }385 }