authorgravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-02-27 10:42:36-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-27 15:29:38-05:00
log0bdc3d8f4e065f87083b52700350ca96330463ad
treed2e5c2386004a28e40f721a49885000f1651a0d7
parentb5066fdae292f44a6e4c60db9a9db012672dd10e

stage2: Implement `@Type` for Array, Optional, Float, and ErrorUnion


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

src/Sema.zig+68-4
...@@ -11937,7 +11937,23 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I...@@ -11937,7 +11937,23 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
11937 const ty = try Type.vector(sema.arena, len, try child_ty.copy(sema.arena));11937 const ty = try Type.vector(sema.arena, len, try child_ty.copy(sema.arena));
11938 return sema.addType(ty);11938 return sema.addType(ty);
11939 },11939 },
11940 .Float => return sema.fail(block, src, "TODO: Sema.zirReify for Float", .{}),11940 .Float => {
11941 const struct_val = union_val.val.castTag(.@"struct").?.data;
11942 // TODO use reflection instead of magic numbers here
11943 // bits: comptime_int,
11944 const bits_val = struct_val[0];
11945
11946 const bits = @intCast(u16, bits_val.toUnsignedInt());
11947 const ty = switch (bits) {
11948 16 => Type.@"f16",
11949 32 => Type.@"f32",
11950 64 => Type.@"f64",
11951 80 => Type.@"f80",
11952 128 => Type.@"f128",
11953 else => return sema.fail(block, src, "{}-bit float unsupported", .{bits}),
11954 };
11955 return sema.addType(ty);
11956 },
11941 .Pointer => {11957 .Pointer => {
11942 const struct_val = union_val.val.castTag(.@"struct").?.data;11958 const struct_val = union_val.val.castTag(.@"struct").?.data;
11943 // TODO use reflection instead of magic numbers here11959 // TODO use reflection instead of magic numbers here
...@@ -11977,10 +11993,58 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I...@@ -11977,10 +11993,58 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
11977 });11993 });
11978 return sema.addType(ty);11994 return sema.addType(ty);
11979 },11995 },
11980 .Array => return sema.fail(block, src, "TODO: Sema.zirReify for Array", .{}),11996 .Array => {
11997 const struct_val = union_val.val.castTag(.@"struct").?.data;
11998 // TODO use reflection instead of magic numbers here
11999 // len: comptime_int,
12000 const len_val = struct_val[0];
12001 // child: type,
12002 const child_val = struct_val[1];
12003 // sentinel: ?*const anyopaque,
12004 const sentinel_val = struct_val[2];
12005
12006 const len = len_val.toUnsignedInt();
12007 var buffer: Value.ToTypeBuffer = undefined;
12008 const child_ty = try child_val.toType(&buffer).copy(sema.arena);
12009 const sentinel = if (sentinel_val.castTag(.opt_payload)) |p| blk: {
12010 const ptr_ty = try Type.ptr(sema.arena, .{ .@"addrspace" = .generic, .pointee_type = child_ty });
12011 break :blk (try sema.pointerDeref(block, src, p.data, ptr_ty)).?;
12012 } else null;
12013
12014 const ty = try Type.array(sema.arena, len, sentinel, child_ty);
12015 return sema.addType(ty);
12016 },
11981 .Struct => return sema.fail(block, src, "TODO: Sema.zirReify for Struct", .{}),12017 .Struct => return sema.fail(block, src, "TODO: Sema.zirReify for Struct", .{}),
11982 .Optional => return sema.fail(block, src, "TODO: Sema.zirReify for Optional", .{}),12018 .Optional => {
11983 .ErrorUnion => return sema.fail(block, src, "TODO: Sema.zirReify for ErrorUnion", .{}),12019 const struct_val = union_val.val.castTag(.@"struct").?.data;
12020 // TODO use reflection instead of magic numbers here
12021 // child: type,
12022 const child_val = struct_val[0];
12023
12024 var buffer: Value.ToTypeBuffer = undefined;
12025 const child_ty = try child_val.toType(&buffer).copy(sema.arena);
12026
12027 const ty = try Type.optional(sema.arena, child_ty);
12028 return sema.addType(ty);
12029 },
12030 .ErrorUnion => {
12031 const struct_val = union_val.val.castTag(.@"struct").?.data;
12032 // TODO use reflection instead of magic numbers here
12033 // error_set: type,
12034 const error_set_val = struct_val[0];
12035 // payload: type,
12036 const payload_val = struct_val[1];
12037
12038 var buffer: Value.ToTypeBuffer = undefined;
12039 const error_set_ty = try error_set_val.toType(&buffer).copy(sema.arena);
12040 const payload_ty = try payload_val.toType(&buffer).copy(sema.arena);
12041
12042 const ty = try Type.Tag.error_union.create(sema.arena, .{
12043 .error_set = error_set_ty,
12044 .payload = payload_ty,
12045 });
12046 return sema.addType(ty);
12047 },
11984 .ErrorSet => return sema.fail(block, src, "TODO: Sema.zirReify for ErrorSet", .{}),12048 .ErrorSet => return sema.fail(block, src, "TODO: Sema.zirReify for ErrorSet", .{}),
11985 .Enum => return sema.fail(block, src, "TODO: Sema.zirReify for Enum", .{}),12049 .Enum => return sema.fail(block, src, "TODO: Sema.zirReify for Enum", .{}),
11986 .Union => return sema.fail(block, src, "TODO: Sema.zirReify for Union", .{}),12050 .Union => return sema.fail(block, src, "TODO: Sema.zirReify for Union", .{}),
test/behavior/type.zig-10
...@@ -104,8 +104,6 @@ test "Type.Pointer" {...@@ -104,8 +104,6 @@ test "Type.Pointer" {
104}104}
105105
106test "Type.Float" {106test "Type.Float" {
107 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
108
109 try testing.expect(f16 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 16 } }));107 try testing.expect(f16 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 16 } }));
110 try testing.expect(f32 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 32 } }));108 try testing.expect(f32 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 32 } }));
111 try testing.expect(f64 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 64 } }));109 try testing.expect(f64 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 64 } }));
...@@ -115,8 +113,6 @@ test "Type.Float" {...@@ -115,8 +113,6 @@ test "Type.Float" {
115}113}
116114
117test "Type.Array" {115test "Type.Array" {
118 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
119
120 try testing.expect([123]u8 == @Type(TypeInfo{116 try testing.expect([123]u8 == @Type(TypeInfo{
121 .Array = TypeInfo.Array{117 .Array = TypeInfo.Array{
122 .len = 123,118 .len = 123,
...@@ -190,8 +186,6 @@ test "@Type picks up the sentinel value from TypeInfo" {...@@ -190,8 +186,6 @@ test "@Type picks up the sentinel value from TypeInfo" {
190}186}
191187
192test "Type.Optional" {188test "Type.Optional" {
193 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
194
195 try testTypes(&[_]type{189 try testTypes(&[_]type{
196 ?u8,190 ?u8,
197 ?*u8,191 ?*u8,
...@@ -202,8 +196,6 @@ test "Type.Optional" {...@@ -202,8 +196,6 @@ test "Type.Optional" {
202}196}
203197
204test "Type.ErrorUnion" {198test "Type.ErrorUnion" {
205 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
206
207 try testTypes(&[_]type{199 try testTypes(&[_]type{
208 error{}!void,200 error{}!void,
209 error{Error}!void,201 error{Error}!void,
...@@ -227,8 +219,6 @@ test "Type.Opaque" {...@@ -227,8 +219,6 @@ test "Type.Opaque" {
227}219}
228220
229test "Type.Vector" {221test "Type.Vector" {
230 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
231
232 try testTypes(&[_]type{222 try testTypes(&[_]type{
233 @Vector(0, u8),223 @Vector(0, u8),
234 @Vector(4, u8),224 @Vector(4, u8),