authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-12-02 18:51:26+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-12-03 00:09:23+02:00
log7f9e841f746bb3eaf6ac205092a30bc7ed12a068
tree14b168089558d7c1c27a0ea2f6f69b8d1fbc1813
parent59dad43de26a89ca72a97224a171d724dcc6ee41

Sema: do not forcibly canonicalize unresolved pointer element type

Closes #13308

3 files changed, 24 insertions(+), 12 deletions(-)

src/Sema.zig+2-10
......@@ -16825,7 +16825,7 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1682516825 const bitoffset_src: LazySrcLoc = .{ .node_offset_ptr_bitoffset = extra.data.src_node };
1682616826 const hostsize_src: LazySrcLoc = .{ .node_offset_ptr_hostsize = extra.data.src_node };
1682716827
16828 const unresolved_elem_ty = blk: {
16828 const elem_ty = blk: {
1682916829 const air_inst = try sema.resolveInst(extra.data.elem_type);
1683016830 const ty = sema.analyzeAsType(block, elem_ty_src, air_inst) catch |err| {
1683116831 if (err == error.AnalysisFail and sema.err != null and sema.typeOf(air_inst).isSinglePointer()) {
......@@ -16854,7 +16854,7 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1685416854 // Check if this happens to be the lazy alignment of our element type, in
1685516855 // which case we can make this 0 without resolving it.
1685616856 if (val.castTag(.lazy_align)) |payload| {
16857 if (payload.data.eql(unresolved_elem_ty, sema.mod)) {
16857 if (payload.data.eql(elem_ty, sema.mod)) {
1685816858 break :blk 0;
1685916859 }
1686016860 }
......@@ -16887,14 +16887,6 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1688716887 return sema.fail(block, bitoffset_src, "bit offset starts after end of host integer", .{});
1688816888 }
1688916889
16890 const elem_ty = if (abi_align == 0)
16891 unresolved_elem_ty
16892 else t: {
16893 const elem_ty = try sema.resolveTypeFields(unresolved_elem_ty);
16894 try sema.resolveTypeLayout(elem_ty);
16895 break :t elem_ty;
16896 };
16897
1689816890 if (elem_ty.zigTypeTag() == .NoReturn) {
1689916891 return sema.fail(block, elem_ty_src, "pointer to noreturn not allowed", .{});
1690016892 } else if (elem_ty.zigTypeTag() == .Fn) {
src/type.zig+10-2
......@@ -6491,8 +6491,16 @@ pub const Type = extern union {
64916491 // type, we change it to 0 here. If this causes an assertion trip because the
64926492 // pointee type needs to be resolved more, that needs to be done before calling
64936493 // this ptr() function.
6494 if (d.@"align" != 0 and d.@"align" == d.pointee_type.abiAlignment(target)) {
6495 d.@"align" = 0;
6494 if (d.@"align" != 0) canonicalize: {
6495 if (d.pointee_type.castTag(.@"struct")) |struct_ty| {
6496 if (!struct_ty.data.haveLayout()) break :canonicalize;
6497 }
6498 if (d.pointee_type.cast(Payload.Union)) |union_ty| {
6499 if (!union_ty.data.haveLayout()) break :canonicalize;
6500 }
6501 if (d.@"align" == d.pointee_type.abiAlignment(target)) {
6502 d.@"align" = 0;
6503 }
64966504 }
64976505
64986506 // Canonicalize host_size. If it matches the bit size of the pointee type,
test/behavior/struct.zig+12
......@@ -1406,3 +1406,15 @@ test "address of zero-bit field is equal to address of only field" {
14061406 try std.testing.expectEqual(&a, a_ptr);
14071407 }
14081408}
1409
1410test "struct field has a pointer to an aligned version of itself" {
1411 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1412
1413 const E = struct {
1414 next: *align(1) @This(),
1415 };
1416 var e: E = undefined;
1417 e = .{ .next = &e };
1418
1419 try expect(&e == e.next);
1420}