authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-03-01 21:57:06+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-03-02 12:26:04+02:00
log403a1fe5d767e801b58fc706eaa54f1171ae27de
treecba681d1d2a81d83a323b444bf1135860e4f9354
parent58530c17364d2525655c90e97fd2cdd2937e5c19

stage2: add cast from ?*T to ?*anyopaque


3 files changed, 27 insertions(+), 9 deletions(-)

lib/std/special/test_runner.zig+2-3
...@@ -46,10 +46,9 @@ pub fn main() void {...@@ -46,10 +46,9 @@ pub fn main() void {
4646
47 var leaks: usize = 0;47 var leaks: usize = 0;
48 for (test_fn_list) |test_fn, i| {48 for (test_fn_list) |test_fn, i| {
49 const gpa_works = builtin.zig_backend == .stage1 or builtin.os.tag != .macos;49 std.testing.allocator_instance = .{};
50 if (gpa_works) std.testing.allocator_instance = .{};
51 defer {50 defer {
52 if (gpa_works and std.testing.allocator_instance.deinit()) {51 if (std.testing.allocator_instance.deinit()) {
53 leaks += 1;52 leaks += 1;
54 }53 }
55 }54 }
src/Sema.zig+11-3
...@@ -12371,7 +12371,7 @@ fn zirIntToPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -12371,7 +12371,7 @@ fn zirIntToPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
12371 const type_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };12371 const type_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
12372 const type_res = try sema.resolveType(block, src, extra.lhs);12372 const type_res = try sema.resolveType(block, src, extra.lhs);
12373 try sema.checkPtrType(block, type_src, type_res);12373 try sema.checkPtrType(block, type_src, type_res);
12374 _ = try sema.resolveTypeLayout(block, src, type_res.childType());12374 try sema.resolveTypeLayout(block, src, type_res.elemType2());
12375 const ptr_align = type_res.ptrAlignment(sema.mod.getTarget());12375 const ptr_align = type_res.ptrAlignment(sema.mod.getTarget());
1237612376
12377 if (try sema.resolveDefinedValue(block, operand_src, operand_coerced)) |val| {12377 if (try sema.resolveDefinedValue(block, operand_src, operand_coerced)) |val| {
...@@ -15512,6 +15512,14 @@ fn coerce(...@@ -15512,6 +15512,14 @@ fn coerce(
15512 return sema.addConstant(dest_ty, Value.@"null");15512 return sema.addConstant(dest_ty, Value.@"null");
15513 }15513 }
1551415514
15515 // cast from ?*T and ?[*]T to ?*anyopaque
15516 // but don't do it if the source type is a double pointer
15517 if (dest_ty.isPtrLikeOptional() and dest_ty.elemType2().tag() == .anyopaque and
15518 inst_ty.isPtrLikeOptional() and inst_ty.elemType2().zigTypeTag() != .Pointer)
15519 {
15520 return sema.coerceCompatiblePtrs(block, dest_ty, inst, inst_src);
15521 }
15522
15515 // T to ?T15523 // T to ?T
15516 const child_type = try dest_ty.optionalChildAlloc(sema.arena);15524 const child_type = try dest_ty.optionalChildAlloc(sema.arena);
15517 const intermediate = try sema.coerce(block, child_type, inst, inst_src);15525 const intermediate = try sema.coerce(block, child_type, inst, inst_src);
...@@ -16791,6 +16799,7 @@ fn coerceCompatiblePtrs(...@@ -16791,6 +16799,7 @@ fn coerceCompatiblePtrs(
16791 inst: Air.Inst.Ref,16799 inst: Air.Inst.Ref,
16792 inst_src: LazySrcLoc,16800 inst_src: LazySrcLoc,
16793) !Air.Inst.Ref {16801) !Air.Inst.Ref {
16802 // TODO check const/volatile/alignment
16794 if (try sema.resolveMaybeUndefVal(block, inst_src, inst)) |val| {16803 if (try sema.resolveMaybeUndefVal(block, inst_src, inst)) |val| {
16795 // The comptime Value representation is compatible with both types.16804 // The comptime Value representation is compatible with both types.
16796 return sema.addConstant(dest_ty, val);16805 return sema.addConstant(dest_ty, val);
...@@ -18506,6 +18515,7 @@ fn resolveInferredErrorSet(sema: *Sema, inferred_error_set: *Module.Fn.InferredE...@@ -18506,6 +18515,7 @@ fn resolveInferredErrorSet(sema: *Sema, inferred_error_set: *Module.Fn.InferredE
18506 if (inferred_error_set.is_resolved) {18515 if (inferred_error_set.is_resolved) {
18507 return;18516 return;
18508 }18517 }
18518 inferred_error_set.is_resolved = true;
1850918519
18510 var it = inferred_error_set.inferred_error_sets.keyIterator();18520 var it = inferred_error_set.inferred_error_sets.keyIterator();
18511 while (it.next()) |other_error_set_ptr| {18521 while (it.next()) |other_error_set_ptr| {
...@@ -18526,8 +18536,6 @@ fn resolveInferredErrorSet(sema: *Sema, inferred_error_set: *Module.Fn.InferredE...@@ -18526,8 +18536,6 @@ fn resolveInferredErrorSet(sema: *Sema, inferred_error_set: *Module.Fn.InferredE
18526 if (other_error_set_ptr.*.is_anyerror)18536 if (other_error_set_ptr.*.is_anyerror)
18527 inferred_error_set.is_anyerror = true;18537 inferred_error_set.is_anyerror = true;
18528 }18538 }
18529
18530 inferred_error_set.is_resolved = true;
18531}18539}
1853218540
18533fn resolveInferredErrorSetTy(sema: *Sema, ty: Type) CompileError!void {18541fn resolveInferredErrorSetTy(sema: *Sema, ty: Type) CompileError!void {
src/type.zig+14-3
...@@ -2168,7 +2168,14 @@ pub const Type = extern union {...@@ -2168,7 +2168,14 @@ pub const Type = extern union {
2168 .mut_slice,2168 .mut_slice,
2169 .optional_single_const_pointer,2169 .optional_single_const_pointer,
2170 .optional_single_mut_pointer,2170 .optional_single_mut_pointer,
2171 => return self.cast(Payload.ElemType).?.data.abiAlignment(target),2171 => {
2172 const child_type = self.cast(Payload.ElemType).?.data;
2173 if (child_type.zigTypeTag() == .Opaque) {
2174 return 1;
2175 } else {
2176 return child_type.abiAlignment(target);
2177 }
2178 },
21722179
2173 .manyptr_u8,2180 .manyptr_u8,
2174 .manyptr_const_u8,2181 .manyptr_const_u8,
...@@ -2181,10 +2188,13 @@ pub const Type = extern union {...@@ -2181,10 +2188,13 @@ pub const Type = extern union {
2181 const ptr_info = self.castTag(.pointer).?.data;2188 const ptr_info = self.castTag(.pointer).?.data;
2182 if (ptr_info.@"align" != 0) {2189 if (ptr_info.@"align" != 0) {
2183 return ptr_info.@"align";2190 return ptr_info.@"align";
2191 } else if (ptr_info.pointee_type.zigTypeTag() == .Opaque) {
2192 return 1;
2184 } else {2193 } else {
2185 return ptr_info.pointee_type.abiAlignment(target);2194 return ptr_info.pointee_type.abiAlignment(target);
2186 }2195 }
2187 },2196 },
2197 .optional => return self.castTag(.optional).?.data.ptrAlignment(target),
21882198
2189 else => unreachable,2199 else => unreachable,
2190 }2200 }
...@@ -3235,8 +3245,9 @@ pub const Type = extern union {...@@ -3235,8 +3245,9 @@ pub const Type = extern union {
3235 return child_ty;3245 return child_ty;
3236 }3246 }
3237 },3247 },
32383248 .optional => ty.castTag(.optional).?.data.childType(),
3239 // TODO handle optionals3249 .optional_single_mut_pointer => ty.castPointer().?.data,
3250 .optional_single_const_pointer => ty.castPointer().?.data,
32403251
3241 else => unreachable,3252 else => unreachable,
3242 };3253 };