Skip to content

Commit 5899a80

Browse files
committed
Auto merge of #116102 - cjgillot:indirect-scalar, r=oli-obk
Correct codegen of `ConstValue::Indirect` scalar and scalar pair This concerns 3 tricky cases with `ConstValue::Indirect`: - if we want a non-pointer scalar; - if we have non-zero offset; - if offset points to uninit memory => generate `poison` instead of an ICE. This case could happen in unreachable code, trying to extract a field from the wrong variant. Those cases are not currently emitted by the compiler, but are exercised by #116012.
2 parents 6710e33 + ac0683b commit 5899a80

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

Diff for: compiler/rustc_codegen_ssa/src/mir/operand.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,14 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
135135
assert_eq!(alloc_align, layout.align.abi);
136136

137137
let read_scalar = |start, size, s: abi::Scalar, ty| {
138-
let val = alloc
139-
.0
140-
.read_scalar(
141-
bx,
142-
alloc_range(start, size),
143-
/*read_provenance*/ matches!(s.primitive(), abi::Pointer(_)),
144-
)
145-
.unwrap();
146-
bx.scalar_to_backend(val, s, ty)
138+
match alloc.0.read_scalar(
139+
bx,
140+
alloc_range(start, size),
141+
/*read_provenance*/ matches!(s.primitive(), abi::Pointer(_)),
142+
) {
143+
Ok(val) => bx.scalar_to_backend(val, s, ty),
144+
Err(_) => bx.const_poison(ty),
145+
}
147146
};
148147

149148
// It may seem like all types with `Scalar` or `ScalarPair` ABI are fair game at this point.
@@ -156,18 +155,18 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
156155
Abi::Scalar(s @ abi::Scalar::Initialized { .. }) => {
157156
let size = s.size(bx);
158157
assert_eq!(size, layout.size, "abi::Scalar size does not match layout size");
159-
let val = read_scalar(Size::ZERO, size, s, bx.type_ptr());
158+
let val = read_scalar(offset, size, s, bx.backend_type(layout));
160159
OperandRef { val: OperandValue::Immediate(val), layout }
161160
}
162161
Abi::ScalarPair(
163162
a @ abi::Scalar::Initialized { .. },
164163
b @ abi::Scalar::Initialized { .. },
165164
) => {
166165
let (a_size, b_size) = (a.size(bx), b.size(bx));
167-
let b_offset = a_size.align_to(b.align(bx).abi);
166+
let b_offset = (offset + a_size).align_to(b.align(bx).abi);
168167
assert!(b_offset.bytes() > 0);
169168
let a_val = read_scalar(
170-
Size::ZERO,
169+
offset,
171170
a_size,
172171
a,
173172
bx.scalar_pair_element_backend_type(layout, 0, true),

0 commit comments

Comments
 (0)