Skip to content

Commit f9192ad

Browse files
jacobly0andrewrk
authored andcommitted
llvm: fix lowering of non-byte-aligned field pointers
* When a field starts at some bit offset within a byte you need to load starting from that byte and shift, not starting from the next byte, so a rounded-down divide is required here, not a rounded-up one. * Remove paragraph from doc that no longer relates to anything. Closes #12363
1 parent c7f9833 commit f9192ad

File tree

2 files changed

+1
-6
lines changed

2 files changed

+1
-6
lines changed

doc/langref.html.in

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3341,7 +3341,6 @@ fn doTheTest() !void {
33413341
Zig allows the address to be taken of a non-byte-aligned field:
33423342
</p>
33433343
{#code_begin|test|pointer_to_non-byte_aligned_field#}
3344-
{#backend_stage1#}
33453344
const std = @import("std");
33463345
const expect = std.testing.expect;
33473346

@@ -3398,7 +3397,6 @@ fn bar(x: *const u3) u3 {
33983397
Pointers to non-ABI-aligned fields share the same address as the other fields within their host integer:
33993398
</p>
34003399
{#code_begin|test|packed_struct_field_addrs#}
3401-
{#backend_stage1#}
34023400
const std = @import("std");
34033401
const expect = std.testing.expect;
34043402

@@ -3463,9 +3461,6 @@ test "overaligned pointer to packed struct" {
34633461
try expect(ptr_to_b.* == 2);
34643462
}
34653463
{#code_end#}
3466-
<p>When this bug is fixed, the above test in the documentation will unexpectedly pass, which will
3467-
cause the test suite to fail, notifying the bug fixer to update these docs.
3468-
</p>
34693464
<p>
34703465
It's also possible to set alignment of struct fields:
34713466
</p>

src/codegen/llvm.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3943,7 +3943,7 @@ pub const DeclGen = struct {
39433943
}
39443944
break :b b;
39453945
};
3946-
const byte_offset = llvm_usize.constInt((prev_bits + 7) / 8, .False);
3946+
const byte_offset = llvm_usize.constInt(prev_bits / 8, .False);
39473947
const field_addr = base_addr.constAdd(byte_offset);
39483948
bitcast_needed = false;
39493949
const final_llvm_ty = (try dg.lowerType(ptr_child_ty)).pointerType(0);

0 commit comments

Comments
 (0)