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

This commit is contained in:
Veikka Tuominen
2022-03-01 21:57:06 +02:00
parent 58530c1736
commit 403a1fe5d7
3 changed files with 27 additions and 9 deletions

View File

@@ -46,10 +46,9 @@ pub fn main() void {
var leaks: usize = 0;
for (test_fn_list) |test_fn, i| {
const gpa_works = builtin.zig_backend == .stage1 or builtin.os.tag != .macos;
if (gpa_works) std.testing.allocator_instance = .{};
std.testing.allocator_instance = .{};
defer {
if (gpa_works and std.testing.allocator_instance.deinit()) {
if (std.testing.allocator_instance.deinit()) {
leaks += 1;
}
}

View File

@@ -12371,7 +12371,7 @@ fn zirIntToPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
const type_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
const type_res = try sema.resolveType(block, src, extra.lhs);
try sema.checkPtrType(block, type_src, type_res);
_ = try sema.resolveTypeLayout(block, src, type_res.childType());
try sema.resolveTypeLayout(block, src, type_res.elemType2());
const ptr_align = type_res.ptrAlignment(sema.mod.getTarget());
if (try sema.resolveDefinedValue(block, operand_src, operand_coerced)) |val| {
@@ -15512,6 +15512,14 @@ fn coerce(
return sema.addConstant(dest_ty, Value.@"null");
}
// cast from ?*T and ?[*]T to ?*anyopaque
// but don't do it if the source type is a double pointer
if (dest_ty.isPtrLikeOptional() and dest_ty.elemType2().tag() == .anyopaque and
inst_ty.isPtrLikeOptional() and inst_ty.elemType2().zigTypeTag() != .Pointer)
{
return sema.coerceCompatiblePtrs(block, dest_ty, inst, inst_src);
}
// T to ?T
const child_type = try dest_ty.optionalChildAlloc(sema.arena);
const intermediate = try sema.coerce(block, child_type, inst, inst_src);
@@ -16791,6 +16799,7 @@ fn coerceCompatiblePtrs(
inst: Air.Inst.Ref,
inst_src: LazySrcLoc,
) !Air.Inst.Ref {
// TODO check const/volatile/alignment
if (try sema.resolveMaybeUndefVal(block, inst_src, inst)) |val| {
// The comptime Value representation is compatible with both types.
return sema.addConstant(dest_ty, val);
@@ -18506,6 +18515,7 @@ fn resolveInferredErrorSet(sema: *Sema, inferred_error_set: *Module.Fn.InferredE
if (inferred_error_set.is_resolved) {
return;
}
inferred_error_set.is_resolved = true;
var it = inferred_error_set.inferred_error_sets.keyIterator();
while (it.next()) |other_error_set_ptr| {
@@ -18526,8 +18536,6 @@ fn resolveInferredErrorSet(sema: *Sema, inferred_error_set: *Module.Fn.InferredE
if (other_error_set_ptr.*.is_anyerror)
inferred_error_set.is_anyerror = true;
}
inferred_error_set.is_resolved = true;
}
fn resolveInferredErrorSetTy(sema: *Sema, ty: Type) CompileError!void {

View File

@@ -2168,7 +2168,14 @@ pub const Type = extern union {
.mut_slice,
.optional_single_const_pointer,
.optional_single_mut_pointer,
=> return self.cast(Payload.ElemType).?.data.abiAlignment(target),
=> {
const child_type = self.cast(Payload.ElemType).?.data;
if (child_type.zigTypeTag() == .Opaque) {
return 1;
} else {
return child_type.abiAlignment(target);
}
},
.manyptr_u8,
.manyptr_const_u8,
@@ -2181,10 +2188,13 @@ pub const Type = extern union {
const ptr_info = self.castTag(.pointer).?.data;
if (ptr_info.@"align" != 0) {
return ptr_info.@"align";
} else if (ptr_info.pointee_type.zigTypeTag() == .Opaque) {
return 1;
} else {
return ptr_info.pointee_type.abiAlignment(target);
}
},
.optional => return self.castTag(.optional).?.data.ptrAlignment(target),
else => unreachable,
}
@@ -3235,8 +3245,9 @@ pub const Type = extern union {
return child_ty;
}
},
// TODO handle optionals
.optional => ty.castTag(.optional).?.data.childType(),
.optional_single_mut_pointer => ty.castPointer().?.data,
.optional_single_const_pointer => ty.castPointer().?.data,
else => unreachable,
};