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 {
4646
4747 var leaks: usize = 0;
4848 for (test_fn_list) |test_fn, i| {
49 const gpa_works = builtin.zig_backend == .stage1 or builtin.os.tag != .macos;
50 if (gpa_works) std.testing.allocator_instance = .{};
49 std.testing.allocator_instance = .{};
5150 defer {
52 if (gpa_works and std.testing.allocator_instance.deinit()) {
51 if (std.testing.allocator_instance.deinit()) {
5352 leaks += 1;
5453 }
5554 }
src/Sema.zig+11-3
......@@ -12371,7 +12371,7 @@ fn zirIntToPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1237112371 const type_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
1237212372 const type_res = try sema.resolveType(block, src, extra.lhs);
1237312373 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());
1237512375 const ptr_align = type_res.ptrAlignment(sema.mod.getTarget());
1237612376
1237712377 if (try sema.resolveDefinedValue(block, operand_src, operand_coerced)) |val| {
......@@ -15512,6 +15512,14 @@ fn coerce(
1551215512 return sema.addConstant(dest_ty, Value.@"null");
1551315513 }
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
1551515523 // T to ?T
1551615524 const child_type = try dest_ty.optionalChildAlloc(sema.arena);
1551715525 const intermediate = try sema.coerce(block, child_type, inst, inst_src);
......@@ -16791,6 +16799,7 @@ fn coerceCompatiblePtrs(
1679116799 inst: Air.Inst.Ref,
1679216800 inst_src: LazySrcLoc,
1679316801) !Air.Inst.Ref {
16802 // TODO check const/volatile/alignment
1679416803 if (try sema.resolveMaybeUndefVal(block, inst_src, inst)) |val| {
1679516804 // The comptime Value representation is compatible with both types.
1679616805 return sema.addConstant(dest_ty, val);
......@@ -18506,6 +18515,7 @@ fn resolveInferredErrorSet(sema: *Sema, inferred_error_set: *Module.Fn.InferredE
1850618515 if (inferred_error_set.is_resolved) {
1850718516 return;
1850818517 }
18518 inferred_error_set.is_resolved = true;
1850918519
1851018520 var it = inferred_error_set.inferred_error_sets.keyIterator();
1851118521 while (it.next()) |other_error_set_ptr| {
......@@ -18526,8 +18536,6 @@ fn resolveInferredErrorSet(sema: *Sema, inferred_error_set: *Module.Fn.InferredE
1852618536 if (other_error_set_ptr.*.is_anyerror)
1852718537 inferred_error_set.is_anyerror = true;
1852818538 }
18529
18530 inferred_error_set.is_resolved = true;
1853118539}
1853218540
1853318541fn resolveInferredErrorSetTy(sema: *Sema, ty: Type) CompileError!void {
src/type.zig+14-3
......@@ -2168,7 +2168,14 @@ pub const Type = extern union {
21682168 .mut_slice,
21692169 .optional_single_const_pointer,
21702170 .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
21732180 .manyptr_u8,
21742181 .manyptr_const_u8,
......@@ -2181,10 +2188,13 @@ pub const Type = extern union {
21812188 const ptr_info = self.castTag(.pointer).?.data;
21822189 if (ptr_info.@"align" != 0) {
21832190 return ptr_info.@"align";
2191 } else if (ptr_info.pointee_type.zigTypeTag() == .Opaque) {
2192 return 1;
21842193 } else {
21852194 return ptr_info.pointee_type.abiAlignment(target);
21862195 }
21872196 },
2197 .optional => return self.castTag(.optional).?.data.ptrAlignment(target),
21882198
21892199 else => unreachable,
21902200 }
......@@ -3235,8 +3245,9 @@ pub const Type = extern union {
32353245 return child_ty;
32363246 }
32373247 },
3238
3239 // TODO handle optionals
3248 .optional => ty.castTag(.optional).?.data.childType(),
3249 .optional_single_mut_pointer => ty.castPointer().?.data,
3250 .optional_single_const_pointer => ty.castPointer().?.data,
32403251
32413252 else => unreachable,
32423253 };