Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions compiler/rustc_codegen_cranelift/src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ pub(crate) fn codegen_const_value<'tcx>(
let base_addr = match fx.tcx.global_alloc(alloc_id) {
GlobalAlloc::Memory(alloc) => {
if alloc.inner().len() == 0 {
assert_eq!(offset, Size::ZERO);
fx.bcx.ins().iconst(fx.pointer_type, alloc.inner().align.bytes() as i64)
fx.bcx.ins().iconst(fx.pointer_type, alloc.inner().align.bytes().wrapping_add(offset.bytes()) as i64)
} else {
let data_id = data_id_for_alloc_id(
&mut fx.constants_cx,
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_codegen_gcc/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,9 @@ impl<'gcc, 'tcx> ConstCodegenMethods for CodegenCx<'gcc, 'tcx> {
// This avoids generating a zero-sized constant value and actually needing a
// real address at runtime.
if alloc.inner().len() == 0 {
assert_eq!(offset.bytes(), 0);
let val = self.const_usize(alloc.inner().align.bytes());
let val = self.const_usize(
alloc.inner().align.bytes().wrapping_add(offset.bytes()),
);
return if matches!(layout.primitive(), Pointer(_)) {
self.context.new_cast(None, val, ty)
} else {
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_codegen_llvm/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,9 @@ impl<'ll, 'tcx> ConstCodegenMethods for CodegenCx<'ll, 'tcx> {
// This avoids generating a zero-sized constant value and actually needing a
// real address at runtime.
if alloc.inner().len() == 0 {
assert_eq!(offset.bytes(), 0);
let llval = self.const_usize(alloc.inner().align.bytes());
let llval = self.const_usize(
alloc.inner().align.bytes().wrapping_add(offset.bytes()),
);
return if matches!(layout.primitive(), Pointer(_)) {
unsafe { llvm::LLVMConstIntToPtr(llval, llty) }
} else {
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/consts/zst_no_llvm_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ struct Foo;

static FOO: Foo = Foo;

// This tests for regression of https://github.com/rust-lang/rust/issues/147516
const A: *const () = (&() as *const ()).wrapping_byte_add(2);
const B: *const () = (&Foo as *const _ as *const ()).wrapping_byte_add(2);
const C: *const () = (&Foo as *const _ as *const ()).wrapping_byte_add(usize::MAX);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this at all. Can you explain a little? Perhaps this test needs an explanatory comment at the top?

fn main() {
// There's no stable guarantee that these are true.
// However, we want them to be true so that our LLVM IR and runtime are a bit faster:
Expand All @@ -15,6 +20,10 @@ fn main() {
let x: &'static Foo = &Foo;
assert_eq!(x as *const Foo as usize, 4);

assert_eq!(A.addr(), 3);
assert_eq!(B.addr(), 6);
assert_eq!(C.addr(), 3);

// The exact addresses returned by these library functions are not necessarily stable guarantees
// but for now we assert that we're still matching.
#[allow(dangling_pointers_from_temporaries)]
Expand Down
Loading