Skip to content

Commit 38cd948

Browse files
committed
Auto merge of #61959 - oli-obk:const_field_ice, r=eddyb
Fix a hash collision issue on the `const_field` query fixes #61530
2 parents 56a12b2 + d46a373 commit 38cd948

File tree

16 files changed

+85
-49
lines changed

16 files changed

+85
-49
lines changed

src/librustc/ich/impls_ty.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,18 @@ impl<'a> HashStable<StableHashingContext<'a>> for mir::interpret::Allocation {
175175
hcx: &mut StableHashingContext<'a>,
176176
hasher: &mut StableHasher<W>,
177177
) {
178-
self.bytes.hash_stable(hcx, hasher);
179-
for reloc in self.relocations.iter() {
178+
let mir::interpret::Allocation {
179+
bytes, relocations, undef_mask, align, mutability,
180+
extra: _,
181+
} = self;
182+
bytes.hash_stable(hcx, hasher);
183+
relocations.len().hash_stable(hcx, hasher);
184+
for reloc in relocations.iter() {
180185
reloc.hash_stable(hcx, hasher);
181186
}
182-
self.undef_mask.hash_stable(hcx, hasher);
183-
self.align.hash_stable(hcx, hasher);
184-
self.mutability.hash_stable(hcx, hasher);
187+
undef_mask.hash_stable(hcx, hasher);
188+
align.hash_stable(hcx, hasher);
189+
mutability.hash_stable(hcx, hasher);
185190
}
186191
}
187192

src/librustc/infer/freshen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
260260
ConstValue::Param(_) |
261261
ConstValue::Scalar(_) |
262262
ConstValue::Slice { .. } |
263-
ConstValue::ByRef(..) |
263+
ConstValue::ByRef { .. } |
264264
ConstValue::Unevaluated(..) => {}
265265
}
266266

src/librustc/mir/interpret/allocation.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
247247
assert_ne!(size.bytes(), 0, "0-sized accesses should never even get a `Pointer`");
248248
self.check_bounds(cx, ptr, size, CheckInAllocMsg::MemoryAccessTest)?;
249249

250-
self.mark_definedness(ptr, size, true)?;
250+
self.mark_definedness(ptr, size, true);
251251
self.clear_relocations(cx, ptr, size)?;
252252

253253
AllocationExtra::memory_written(self, ptr, size)?;
@@ -406,7 +406,10 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
406406
{
407407
let val = match val {
408408
ScalarMaybeUndef::Scalar(scalar) => scalar,
409-
ScalarMaybeUndef::Undef => return self.mark_definedness(ptr, type_size, false),
409+
ScalarMaybeUndef::Undef => {
410+
self.mark_definedness(ptr, type_size, false);
411+
return Ok(());
412+
},
410413
};
411414

412415
let bytes = match val.to_bits_or_ptr(type_size, cx) {
@@ -550,16 +553,15 @@ impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
550553
ptr: Pointer<Tag>,
551554
size: Size,
552555
new_state: bool,
553-
) -> InterpResult<'tcx> {
556+
) {
554557
if size.bytes() == 0 {
555-
return Ok(());
558+
return;
556559
}
557560
self.undef_mask.set_range(
558561
ptr.offset,
559562
ptr.offset + size,
560563
new_state,
561564
);
562-
Ok(())
563565
}
564566
}
565567

src/librustc/mir/interpret/value.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,21 @@ pub enum ConstValue<'tcx> {
4343
end: usize,
4444
},
4545

46-
/// An allocation together with a pointer into the allocation.
47-
/// Invariant: the pointer's `AllocId` resolves to the allocation.
48-
/// The alignment exists to allow `const_field` to have `ByRef` access to nonprimitive fields
49-
/// of `repr(packed)` structs. The alignment may be lower than the type of this constant.
50-
/// This permits reads with lower alignment than what the type would normally require.
51-
/// FIXME(RalfJ,oli-obk): The alignment checks are part of miri, but const eval doesn't really
52-
/// need them. Disabling them may be too hard though.
53-
ByRef(Pointer, Align, &'tcx Allocation),
46+
/// A value not represented/representable by `Scalar` or `Slice`
47+
ByRef {
48+
/// The alignment exists to allow `const_field` to have `ByRef` access to nonprimitive
49+
/// fields of `repr(packed)` structs. The alignment may be lower than the type of this
50+
/// constant. This permits reads with lower alignment than what the type would normally
51+
/// require.
52+
/// FIXME(RalfJ,oli-obk): The alignment checks are part of miri, but const eval doesn't
53+
/// really need them. Disabling them may be too hard though.
54+
align: Align,
55+
/// Offset into `alloc`
56+
offset: Size,
57+
/// The backing memory of the value, may contain more memory than needed for just the value
58+
/// in order to share `Allocation`s between values
59+
alloc: &'tcx Allocation,
60+
},
5461

5562
/// Used in the HIR by using `Unevaluated` everywhere and later normalizing to one of the other
5663
/// variants when the code is monomorphic enough for that.
@@ -67,7 +74,7 @@ impl<'tcx> ConstValue<'tcx> {
6774
ConstValue::Param(_) |
6875
ConstValue::Infer(_) |
6976
ConstValue::Placeholder(_) |
70-
ConstValue::ByRef(..) |
77+
ConstValue::ByRef{ .. } |
7178
ConstValue::Unevaluated(..) |
7279
ConstValue::Slice { .. } => None,
7380
ConstValue::Scalar(val) => Some(val),

src/librustc/ty/print/obsolete.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl DefPathBasedNames<'tcx> {
186186
// as well as the unprintable types of constants (see `push_type_name` for more details).
187187
pub fn push_const_name(&self, c: &Const<'tcx>, output: &mut String, debug: bool) {
188188
match c.val {
189-
ConstValue::Scalar(..) | ConstValue::Slice { .. } | ConstValue::ByRef(..) => {
189+
ConstValue::Scalar(..) | ConstValue::Slice { .. } | ConstValue::ByRef { .. } => {
190190
// FIXME(const_generics): we could probably do a better job here.
191191
write!(output, "{:?}", c).unwrap()
192192
}

src/librustc/ty/relate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
594594
ty: a.ty,
595595
}))
596596
}
597-
(ConstValue::ByRef(..), _) => {
597+
(ConstValue::ByRef { .. }, _) => {
598598
bug!(
599599
"non-Scalar ConstValue encountered in super_relate_consts {:?} {:?}",
600600
a,

src/librustc/ty/structural_impls.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,8 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::Const<'tcx> {
13351335
impl<'tcx> TypeFoldable<'tcx> for ConstValue<'tcx> {
13361336
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
13371337
match *self {
1338-
ConstValue::ByRef(ptr, align, alloc) => ConstValue::ByRef(ptr, align, alloc),
1338+
ConstValue::ByRef { offset, align, alloc } =>
1339+
ConstValue::ByRef { offset, align, alloc },
13391340
ConstValue::Infer(ic) => ConstValue::Infer(ic.fold_with(folder)),
13401341
ConstValue::Param(p) => ConstValue::Param(p.fold_with(folder)),
13411342
ConstValue::Placeholder(p) => ConstValue::Placeholder(p),
@@ -1348,7 +1349,7 @@ impl<'tcx> TypeFoldable<'tcx> for ConstValue<'tcx> {
13481349

13491350
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
13501351
match *self {
1351-
ConstValue::ByRef(..) => false,
1352+
ConstValue::ByRef { .. } => false,
13521353
ConstValue::Infer(ic) => ic.visit_with(visitor),
13531354
ConstValue::Param(p) => p.visit_with(visitor),
13541355
ConstValue::Placeholder(_) => false,

src/librustc_codegen_llvm/consts.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ pub fn codegen_static_initializer(
7171
let static_ = cx.tcx.const_eval(param_env.and(cid))?;
7272

7373
let alloc = match static_.val {
74-
ConstValue::ByRef(ptr, align, alloc) if ptr.offset.bytes() == 0 && align == alloc.align => {
74+
ConstValue::ByRef {
75+
offset, align, alloc,
76+
} if offset.bytes() == 0 && align == alloc.align => {
7577
alloc
7678
},
7779
_ => bug!("static const eval returned {:#?}", static_),

src/librustc_codegen_ssa/mir/operand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
109109
let b_llval = bx.const_usize((end - start) as u64);
110110
OperandValue::Pair(a_llval, b_llval)
111111
},
112-
ConstValue::ByRef(ptr, align, alloc) => {
113-
return bx.load_operand(bx.from_const_alloc(layout, align, alloc, ptr.offset));
112+
ConstValue::ByRef { offset, align, alloc } => {
113+
return bx.load_operand(bx.from_const_alloc(layout, align, alloc, offset));
114114
},
115115
};
116116

src/librustc_codegen_ssa/mir/place.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
424424
let layout = cx.layout_of(self.monomorphize(&ty));
425425
match bx.tcx().const_eval(param_env.and(cid)) {
426426
Ok(val) => match val.val {
427-
mir::interpret::ConstValue::ByRef(ptr, align, alloc) => {
428-
bx.cx().from_const_alloc(layout, align, alloc, ptr.offset)
427+
mir::interpret::ConstValue::ByRef { offset, align, alloc } => {
428+
bx.cx().from_const_alloc(layout, align, alloc, offset)
429429
}
430430
_ => bug!("promoteds should have an allocation: {:?}", val),
431431
},

0 commit comments

Comments
 (0)