Skip to content

Commit c15f855

Browse files
authored
Rollup merge of #147576 - Mark-Simulacrum:fix-offset-zst, r=nnethercote,RalfJung
Fix ICE on offsetted ZST pointer I'm not sure this is the *right* fix, but it's simple enough and does roughly what I'd expect. Like with the previous optimization to codegen usize rather than a zero-sized static, there's no guarantee that we continue returning a particular value from the offsetting. A grep for `const_usize.*align` found the same code copied to rustc_codegen_gcc and cranelift but a quick skim didn't find other cases of similar 'optimization'. That said, I'm not convinced I caught everything, it's not trivial to search for this. Closes rust-lang/rust#147516
2 parents a63c039 + 10ee204 commit c15f855

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/constant.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use std::cmp::Ordering;
55
use cranelift_module::*;
66
use rustc_data_structures::fx::FxHashSet;
77
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
8-
use rustc_middle::mir::interpret::{AllocId, GlobalAlloc, Scalar, read_target_uint};
8+
use rustc_middle::mir::interpret::{
9+
AllocId, GlobalAlloc, PointerArithmetic, Scalar, read_target_uint,
10+
};
911
use rustc_middle::ty::{ExistentialTraitRef, ScalarInt};
1012

1113
use crate::prelude::*;
@@ -138,8 +140,11 @@ pub(crate) fn codegen_const_value<'tcx>(
138140
let base_addr = match fx.tcx.global_alloc(alloc_id) {
139141
GlobalAlloc::Memory(alloc) => {
140142
if alloc.inner().len() == 0 {
141-
assert_eq!(offset, Size::ZERO);
142-
fx.bcx.ins().iconst(fx.pointer_type, alloc.inner().align.bytes() as i64)
143+
let val = alloc.inner().align.bytes().wrapping_add(offset.bytes());
144+
fx.bcx.ins().iconst(
145+
fx.pointer_type,
146+
fx.tcx.truncate_to_target_usize(val) as i64,
147+
)
143148
} else {
144149
let data_id = data_id_for_alloc_id(
145150
&mut fx.constants_cx,

0 commit comments

Comments
 (0)