authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-02-26 22:57:05+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-26 18:44:23-05:00
logbf3c88b68d9f8042b79b7d69d401429811d9de7c
tree7efd2e71a3b105adf417c831b75aa23b7637af35
parent058e482247c73ecb6404c7c1e3d2194f4aa9e059

stage2: various fixes to get one test passing

* resolve error sets before merging them * implement tupleFieldPtr * make ret_ptr behave like alloc with zero sized types in llvm backend

5 files changed, 88 insertions(+), 13 deletions(-)

src/Module.zig+1-1
......@@ -1435,7 +1435,7 @@ pub const Fn = struct {
14351435 /// All currently known errors that this error set contains. This includes direct additions
14361436 /// via `return error.Foo;`, and possibly also errors that are returned from any dependent functions.
14371437 /// When the inferred error set is fully resolved, this map contains all the errors that the function might return.
1438 errors: std.StringHashMapUnmanaged(void) = .{},
1438 errors: ErrorSet.NameMap = .{},
14391439
14401440 /// Other inferred error sets which this inferred error set should include.
14411441 inferred_error_sets: std.AutoHashMapUnmanaged(*InferredErrorSet, void) = .{},
src/Sema.zig+78-10
......@@ -1613,10 +1613,15 @@ fn zirCoerceResultPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
16131613 //}
16141614
16151615 // The last one is always `store`.
1616 const trash_inst = trash_block.instructions.pop();
1617 assert(air_tags[trash_inst] == .store);
1618 assert(trash_inst == sema.air_instructions.len - 1);
1619 sema.air_instructions.len -= 1;
1616 const trash_inst = trash_block.instructions.items[trash_block.instructions.items.len - 1];
1617 if (air_tags[trash_inst] != .store) {
1618 // no store instruction is generated for zero sized types
1619 assert((try sema.typeHasOnePossibleValue(block, src, pointee_ty)) != null);
1620 } else {
1621 trash_block.instructions.items.len -= 1;
1622 assert(trash_inst == sema.air_instructions.len - 1);
1623 sema.air_instructions.len -= 1;
1624 }
16201625 }
16211626
16221627 const ptr_ty = try Type.ptr(sema.arena, .{
......@@ -5236,6 +5241,22 @@ fn zirMergeErrorSets(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr
52365241 if (lhs_ty.tag() == .anyerror or rhs_ty.tag() == .anyerror) {
52375242 return Air.Inst.Ref.anyerror_type;
52385243 }
5244
5245 if (lhs_ty.castTag(.error_set_inferred)) |payload| {
5246 try sema.resolveInferredErrorSet(payload.data);
5247 // isAnyError might have changed from a false negative to a true positive after resolution.
5248 if (lhs_ty.isAnyError()) {
5249 return Air.Inst.Ref.anyerror_type;
5250 }
5251 }
5252 if (rhs_ty.castTag(.error_set_inferred)) |payload| {
5253 try sema.resolveInferredErrorSet(payload.data);
5254 // isAnyError might have changed from a false negative to a true positive after resolution.
5255 if (rhs_ty.isAnyError()) {
5256 return Air.Inst.Ref.anyerror_type;
5257 }
5258 }
5259
52395260 // Resolve both error sets now.
52405261 const lhs_names = lhs_ty.errorSetNames();
52415262 const rhs_names = rhs_ty.errorSetNames();
......@@ -6809,6 +6830,10 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
68096830 }
68106831 }
68116832
6833 if (operand_ty.castTag(.error_set_inferred)) |inferred| {
6834 try sema.resolveInferredErrorSet(inferred.data);
6835 }
6836
68126837 if (operand_ty.isAnyError()) {
68136838 if (special_prong != .@"else") {
68146839 return sema.fail(
......@@ -14597,6 +14622,12 @@ fn elemPtr(
1459714622 },
1459814623 .Array => return sema.elemPtrArray(block, array_ptr_src, array_ptr, elem_index, elem_index_src),
1459914624 .Vector => return sema.fail(block, src, "TODO implement Sema for elemPtr for vector", .{}),
14625 .Struct => {
14626 // Tuple field access.
14627 const index_val = try sema.resolveConstValue(block, elem_index_src, elem_index);
14628 const index = @intCast(u32, index_val.toUnsignedInt());
14629 return sema.tupleFieldPtr(block, array_ptr, index, src, elem_index_src);
14630 },
1460014631 else => unreachable,
1460114632 }
1460214633}
......@@ -14673,6 +14704,45 @@ fn elemVal(
1467314704 }
1467414705}
1467514706
14707fn tupleFieldPtr(
14708 sema: *Sema,
14709 block: *Block,
14710 tuple_ptr: Air.Inst.Ref,
14711 field_index: u32,
14712 tuple_src: LazySrcLoc,
14713 field_index_src: LazySrcLoc,
14714) CompileError!Air.Inst.Ref {
14715 const tuple_ptr_ty = sema.typeOf(tuple_ptr);
14716 const tuple_ty = tuple_ptr_ty.childType();
14717 const tuple_info = tuple_ty.castTag(.tuple).?.data;
14718
14719 if (field_index > tuple_info.types.len) {
14720 return sema.fail(block, field_index_src, "index {d} outside tuple of length {d}", .{
14721 field_index, tuple_info.types.len,
14722 });
14723 }
14724
14725 const field_ty = tuple_info.types[field_index];
14726 const ptr_field_ty = try Type.ptr(sema.arena, .{
14727 .pointee_type = field_ty,
14728 .mutable = tuple_ptr_ty.ptrIsMutable(),
14729 .@"addrspace" = tuple_ptr_ty.ptrAddressSpace(),
14730 });
14731
14732 if (try sema.resolveMaybeUndefVal(block, tuple_src, tuple_ptr)) |tuple_ptr_val| {
14733 return sema.addConstant(
14734 ptr_field_ty,
14735 try Value.Tag.field_ptr.create(sema.arena, .{
14736 .container_ptr = tuple_ptr_val,
14737 .field_index = field_index,
14738 }),
14739 );
14740 }
14741
14742 try sema.requireRuntimeBlock(block, tuple_src);
14743 return block.addStructFieldPtr(tuple_ptr, field_index, ptr_field_ty);
14744}
14745
1467614746fn tupleField(
1467714747 sema: *Sema,
1467814748 block: *Block,
......@@ -15273,9 +15343,8 @@ fn coerceInMemoryAllowedErrorSets(
1527315343 return .no_match;
1527415344 }
1527515345
15276 var it = src_data.errors.keyIterator();
15277 while (it.next()) |name_ptr| {
15278 if (!dest_ty.errorSetHasField(name_ptr.*)) {
15346 for (src_data.errors.keys()) |key| {
15347 if (!dest_ty.errorSetHasField(key)) {
1527915348 return .no_match;
1528015349 }
1528115350 }
......@@ -17525,9 +17594,8 @@ fn resolveInferredErrorSet(sema: *Sema, inferred_error_set: *Module.Fn.InferredE
1752517594 try sema.ensureDeclAnalyzed(decl); // To ensure that all dependencies are properly added to the set.
1752617595 try sema.resolveInferredErrorSet(other_error_set_ptr.*);
1752717596
17528 var error_it = other_error_set_ptr.*.errors.keyIterator();
17529 while (error_it.next()) |entry| {
17530 try inferred_error_set.errors.put(sema.gpa, entry.*, {});
17597 for (other_error_set_ptr.*.errors.keys()) |key| {
17598 try inferred_error_set.errors.put(sema.gpa, key, {});
1753117599 }
1753217600 if (other_error_set_ptr.*.is_anyerror)
1753317601 inferred_error_set.is_anyerror = true;
src/codegen/llvm.zig+2-1
......@@ -4025,7 +4025,7 @@ pub const FuncGen = struct {
40254025 if (self.liveness.isUnused(inst)) return null;
40264026 const ptr_ty = self.air.typeOfIndex(inst);
40274027 const ret_ty = ptr_ty.childType();
4028 if (!ret_ty.isFnOrHasRuntimeBits()) return null;
4028 if (!ret_ty.isFnOrHasRuntimeBits()) return self.dg.lowerPtrToVoid(ptr_ty);
40294029 if (self.ret_ptr) |ret_ptr| return ret_ptr;
40304030 const ret_llvm_ty = try self.dg.llvmType(ret_ty);
40314031 const target = self.dg.module.getTarget();
......@@ -4845,6 +4845,7 @@ pub const FuncGen = struct {
48454845 struct_ptr_ty: Type,
48464846 field_index: u32,
48474847 ) !?*const llvm.Value {
4848 if (self.liveness.isUnused(inst)) return null;
48484849 const struct_ty = struct_ptr_ty.childType();
48494850 switch (struct_ty.zigTypeTag()) {
48504851 .Struct => switch (struct_ty.containerLayout()) {
src/type.zig+6
......@@ -3878,6 +3878,12 @@ pub const Type = extern union {
38783878 },
38793879 .error_set_merged => ty.castTag(.error_set_merged).?.data.keys(),
38803880 .error_set => ty.castTag(.error_set).?.data.names.keys(),
3881 .error_set_inferred => {
3882 const inferred_error_set = ty.castTag(.error_set_inferred).?.data;
3883 assert(inferred_error_set.is_resolved);
3884 assert(!inferred_error_set.is_anyerror);
3885 return inferred_error_set.errors.keys();
3886 },
38813887 else => unreachable,
38823888 };
38833889 }
test/behavior.zig+1-1
......@@ -132,6 +132,7 @@ test {
132132 _ = @import("behavior/bugs/3384.zig");
133133 _ = @import("behavior/bugs/3742.zig");
134134 _ = @import("behavior/bugs/5398.zig");
135 _ = @import("behavior/bugs/5487.zig");
135136 _ = @import("behavior/struct_contains_null_ptr_itself.zig");
136137 _ = @import("behavior/switch_prong_err_enum.zig");
137138 _ = @import("behavior/switch_prong_implicit_cast.zig");
......@@ -153,7 +154,6 @@ test {
153154 _ = @import("behavior/bugs/1851.zig");
154155 _ = @import("behavior/bugs/3779.zig");
155156 _ = @import("behavior/bugs/5413.zig");
156 _ = @import("behavior/bugs/5487.zig");
157157 _ = @import("behavior/bugs/6456.zig");
158158 _ = @import("behavior/bugs/6781.zig");
159159 _ = @import("behavior/bugs/7003.zig");