Skip to content

Commit 8076b59

Browse files
authored
Rollup merge of #104357 - RalfJung:is-sized, r=cjgillot
add is_sized method on Abi and Layout, and use it This avoids the double negation of `!is_unsized()` that we have quite a lot.
2 parents eefea28 + c780217 commit 8076b59

File tree

21 files changed

+39
-29
lines changed

21 files changed

+39
-29
lines changed

Diff for: compiler/rustc_codegen_cranelift/src/constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub(crate) fn codegen_const_value<'tcx>(
128128
ty: Ty<'tcx>,
129129
) -> CValue<'tcx> {
130130
let layout = fx.layout_of(ty);
131-
assert!(!layout.is_unsized(), "sized const value");
131+
assert!(layout.is_sized(), "unsized const value");
132132

133133
if layout.is_zst() {
134134
return CValue::by_ref(crate::Pointer::dangling(layout.align.pref), layout);

Diff for: compiler/rustc_codegen_cranelift/src/value_and_place.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn codegen_field<'tcx>(
1919
};
2020

2121
if let Some(extra) = extra {
22-
if !field_layout.is_unsized() {
22+
if field_layout.is_sized() {
2323
return simple(fx);
2424
}
2525
match field_layout.ty.kind() {
@@ -364,7 +364,7 @@ impl<'tcx> CPlace<'tcx> {
364364
fx: &mut FunctionCx<'_, '_, 'tcx>,
365365
layout: TyAndLayout<'tcx>,
366366
) -> CPlace<'tcx> {
367-
assert!(!layout.is_unsized());
367+
assert!(layout.is_sized());
368368
if layout.size.bytes() == 0 {
369369
return CPlace {
370370
inner: CPlaceInner::Addr(Pointer::dangling(layout.align.pref), None),
@@ -825,7 +825,7 @@ impl<'tcx> CPlace<'tcx> {
825825
fx: &FunctionCx<'_, '_, 'tcx>,
826826
variant: VariantIdx,
827827
) -> Self {
828-
assert!(!self.layout().is_unsized());
828+
assert!(self.layout().is_sized());
829829
let layout = self.layout().for_variant(fx, variant);
830830
CPlace { inner: self.inner, layout }
831831
}

Diff for: compiler/rustc_codegen_gcc/src/type_.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ pub fn struct_fields<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, layout: TyAndLayout
277277
offset = target_offset + field.size;
278278
prev_effective_align = effective_field_align;
279279
}
280-
if !layout.is_unsized() && field_count > 0 {
280+
if layout.is_sized() && field_count > 0 {
281281
if offset > layout.size {
282282
bug!("layout: {:#?} stride: {:?} offset: {:?}", layout, layout.size, offset);
283283
}

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub(crate) fn fat_pointer_kind<'ll, 'tcx>(
7272
layout.is_unsized()
7373
);
7474

75-
if !layout.is_unsized() {
75+
if layout.is_sized() {
7676
return None;
7777
}
7878

Diff for: compiler/rustc_codegen_llvm/src/type_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ fn struct_llfields<'a, 'tcx>(
140140
prev_effective_align = effective_field_align;
141141
}
142142
let padding_used = result.len() > field_count;
143-
if !layout.is_unsized() && field_count > 0 {
143+
if layout.is_sized() && field_count > 0 {
144144
if offset > layout.size {
145145
bug!("layout: {:#?} stride: {:?} offset: {:?}", layout, layout.size, offset);
146146
}

Diff for: compiler/rustc_codegen_ssa/src/glue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
1515
) -> (Bx::Value, Bx::Value) {
1616
let layout = bx.layout_of(t);
1717
debug!("size_and_align_of_dst(ty={}, info={:?}): layout: {:?}", t, info, layout);
18-
if !layout.is_unsized() {
18+
if layout.is_sized() {
1919
let size = bx.const_usize(layout.size.bytes());
2020
let align = bx.const_usize(layout.align.abi.bytes());
2121
return (size, align);

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub struct PlaceRef<'tcx, V> {
2929

3030
impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
3131
pub fn new_sized(llval: V, layout: TyAndLayout<'tcx>) -> PlaceRef<'tcx, V> {
32-
assert!(!layout.is_unsized());
32+
assert!(layout.is_sized());
3333
PlaceRef { llval, llextra: None, layout, align: layout.align.abi }
3434
}
3535

@@ -38,7 +38,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
3838
layout: TyAndLayout<'tcx>,
3939
align: Align,
4040
) -> PlaceRef<'tcx, V> {
41-
assert!(!layout.is_unsized());
41+
assert!(layout.is_sized());
4242
PlaceRef { llval, llextra: None, layout, align }
4343
}
4444

@@ -48,7 +48,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
4848
bx: &mut Bx,
4949
layout: TyAndLayout<'tcx>,
5050
) -> Self {
51-
assert!(!layout.is_unsized(), "tried to statically allocate unsized place");
51+
assert!(layout.is_sized(), "tried to statically allocate unsized place");
5252
let tmp = bx.alloca(bx.cx().backend_type(layout), layout.align.abi);
5353
Self::new_sized(tmp, layout)
5454
}
@@ -145,7 +145,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
145145
);
146146
return simple();
147147
}
148-
_ if !field.is_unsized() => return simple(),
148+
_ if field.is_sized() => return simple(),
149149
ty::Slice(..) | ty::Str | ty::Foreign(..) => return simple(),
150150
ty::Adt(def, _) => {
151151
if def.repr().packed() {

Diff for: compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn eval_body_using_ecx<'mir, 'tcx>(
4646
ecx.tcx.def_kind(cid.instance.def_id())
4747
);
4848
let layout = ecx.layout_of(body.bound_return_ty().subst(tcx, cid.instance.substs))?;
49-
assert!(!layout.is_unsized());
49+
assert!(layout.is_sized());
5050
let ret = ecx.allocate(layout, MemoryKind::Stack)?;
5151

5252
trace!(

Diff for: compiler/rustc_const_eval/src/interpret/eval_context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
572572
metadata: &MemPlaceMeta<M::Provenance>,
573573
layout: &TyAndLayout<'tcx>,
574574
) -> InterpResult<'tcx, Option<(Size, Align)>> {
575-
if !layout.is_unsized() {
575+
if layout.is_sized() {
576576
return Ok(Some((layout.size, layout.align.abi)));
577577
}
578578
match layout.ty.kind() {

Diff for: compiler/rustc_const_eval/src/interpret/intrinsics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
713713
rhs: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>,
714714
) -> InterpResult<'tcx, Scalar<M::Provenance>> {
715715
let layout = self.layout_of(lhs.layout.ty.builtin_deref(true).unwrap().ty)?;
716-
assert!(!layout.is_unsized());
716+
assert!(layout.is_sized());
717717

718718
let get_bytes = |this: &InterpCx<'mir, 'tcx, M>,
719719
op: &OpTy<'tcx, <M as Machine<'mir, 'tcx>>::Provenance>,

Diff for: compiler/rustc_const_eval/src/interpret/memory.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
683683
// Use size and align of the type.
684684
let ty = self.tcx.type_of(def_id);
685685
let layout = self.tcx.layout_of(ParamEnv::empty().and(ty)).unwrap();
686-
assert!(!layout.is_unsized());
686+
assert!(layout.is_sized());
687687
(layout.size, layout.align.abi, AllocKind::LiveData)
688688
}
689689
Some(GlobalAlloc::Memory(alloc)) => {

Diff for: compiler/rustc_const_eval/src/interpret/operand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl<'tcx, Prov: Provenance> OpTy<'tcx, Prov> {
280280
layout: TyAndLayout<'tcx>,
281281
cx: &impl HasDataLayout,
282282
) -> InterpResult<'tcx, Self> {
283-
assert!(!layout.is_unsized());
283+
assert!(layout.is_sized());
284284
self.offset_with_meta(offset, MemPlaceMeta::None, layout, cx)
285285
}
286286
}

Diff for: compiler/rustc_const_eval/src/interpret/place.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ impl<'tcx, Prov: Provenance> MPlaceTy<'tcx, Prov> {
201201
layout: TyAndLayout<'tcx>,
202202
cx: &impl HasDataLayout,
203203
) -> InterpResult<'tcx, Self> {
204-
assert!(!layout.is_unsized());
204+
assert!(layout.is_sized());
205205
self.offset_with_meta(offset, MemPlaceMeta::None, layout, cx)
206206
}
207207

@@ -340,7 +340,7 @@ where
340340
&self,
341341
place: &MPlaceTy<'tcx, M::Provenance>,
342342
) -> InterpResult<'tcx, Option<AllocRef<'_, 'tcx, M::Provenance, M::AllocExtra>>> {
343-
assert!(!place.layout.is_unsized());
343+
assert!(place.layout.is_sized());
344344
assert!(!place.meta.has_meta());
345345
let size = place.layout.size;
346346
self.get_ptr_alloc(place.ptr, size, place.align)
@@ -351,7 +351,7 @@ where
351351
&mut self,
352352
place: &MPlaceTy<'tcx, M::Provenance>,
353353
) -> InterpResult<'tcx, Option<AllocRefMut<'_, 'tcx, M::Provenance, M::AllocExtra>>> {
354-
assert!(!place.layout.is_unsized());
354+
assert!(place.layout.is_sized());
355355
assert!(!place.meta.has_meta());
356356
let size = place.layout.size;
357357
self.get_ptr_alloc_mut(place.ptr, size, place.align)
@@ -485,7 +485,7 @@ where
485485
src: Immediate<M::Provenance>,
486486
dest: &PlaceTy<'tcx, M::Provenance>,
487487
) -> InterpResult<'tcx> {
488-
assert!(!dest.layout.is_unsized(), "Cannot write unsized data");
488+
assert!(dest.layout.is_sized(), "Cannot write unsized data");
489489
trace!("write_immediate: {:?} <- {:?}: {}", *dest, src, dest.layout.ty);
490490

491491
// See if we can avoid an allocation. This is the counterpart to `read_immediate_raw`,
@@ -746,7 +746,7 @@ where
746746
layout: TyAndLayout<'tcx>,
747747
kind: MemoryKind<M::MemoryKind>,
748748
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
749-
assert!(!layout.is_unsized());
749+
assert!(layout.is_sized());
750750
let ptr = self.allocate_ptr(layout.size, layout.align.abi, kind)?;
751751
Ok(MPlaceTy::from_aligned_ptr(ptr.into(), layout))
752752
}

Diff for: compiler/rustc_const_eval/src/interpret/step.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
209209

210210
Repeat(ref operand, _) => {
211211
let src = self.eval_operand(operand, None)?;
212-
assert!(!src.layout.is_unsized());
212+
assert!(src.layout.is_sized());
213213
let dest = self.force_allocation(&dest)?;
214214
let length = dest.len(self)?;
215215

Diff for: compiler/rustc_const_eval/src/interpret/traits.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
5353
) -> InterpResult<'tcx, (Size, Align)> {
5454
let (ty, _trait_ref) = self.get_ptr_vtable(vtable)?;
5555
let layout = self.layout_of(ty)?;
56-
assert!(!layout.is_unsized(), "there are no vtables for unsized types");
56+
assert!(layout.is_sized(), "there are no vtables for unsized types");
5757
Ok((layout.size, layout.align.abi))
5858
}
5959
}

Diff for: compiler/rustc_middle/src/ty/vtable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub(super) fn vtable_allocation_provider<'tcx>(
6666
let layout = tcx
6767
.layout_of(ty::ParamEnv::reveal_all().and(ty))
6868
.expect("failed to build vtable representation");
69-
assert!(!layout.is_unsized(), "can't create a vtable for an unsized type");
69+
assert!(layout.is_sized(), "can't create a vtable for an unsized type");
7070
let size = layout.size.bytes();
7171
let align = layout.align.abi.bytes();
7272

Diff for: compiler/rustc_mir_transform/src/const_prop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
385385
// I don't know how return types can seem to be unsized but this happens in the
386386
// `type/type-unsatisfiable.rs` test.
387387
.filter(|ret_layout| {
388-
!ret_layout.is_unsized() && ret_layout.size < Size::from_bytes(MAX_ALLOC_LIMIT)
388+
ret_layout.is_sized() && ret_layout.size < Size::from_bytes(MAX_ALLOC_LIMIT)
389389
})
390390
.unwrap_or_else(|| ecx.layout_of(tcx.types.unit).unwrap());
391391

Diff for: compiler/rustc_mir_transform/src/const_prop_lint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
199199
// I don't know how return types can seem to be unsized but this happens in the
200200
// `type/type-unsatisfiable.rs` test.
201201
.filter(|ret_layout| {
202-
!ret_layout.is_unsized() && ret_layout.size < Size::from_bytes(MAX_ALLOC_LIMIT)
202+
ret_layout.is_sized() && ret_layout.size < Size::from_bytes(MAX_ALLOC_LIMIT)
203203
})
204204
.unwrap_or_else(|| ecx.layout_of(tcx.types.unit).unwrap());
205205

Diff for: compiler/rustc_target/src/abi/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,11 @@ impl Abi {
10831083
}
10841084
}
10851085

1086+
#[inline]
1087+
pub fn is_sized(&self) -> bool {
1088+
!self.is_unsized()
1089+
}
1090+
10861091
/// Returns `true` if this is a single signed integer scalar
10871092
#[inline]
10881093
pub fn is_signed(&self) -> bool {
@@ -1490,6 +1495,11 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
14901495
self.abi.is_unsized()
14911496
}
14921497

1498+
#[inline]
1499+
pub fn is_sized(&self) -> bool {
1500+
self.abi.is_sized()
1501+
}
1502+
14931503
/// Returns `true` if the type is a ZST and not unsized.
14941504
pub fn is_zst(&self) -> bool {
14951505
match self.abi {

Diff for: compiler/rustc_ty_utils/src/layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ fn layout_of_uncached<'tcx>(
668668
let mut abi = Abi::Aggregate { sized: true };
669669
let index = VariantIdx::new(0);
670670
for field in &variants[index] {
671-
assert!(!field.is_unsized());
671+
assert!(field.is_sized());
672672
align = align.max(field.align);
673673

674674
// If all non-ZST fields have the same ABI, forward this ABI

Diff for: src/tools/miri/src/stacked_borrows/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
10531053
// pointers we need to retag, so we can stop recursion early.
10541054
// This optimization is crucial for ZSTs, because they can contain way more fields
10551055
// than we can ever visit.
1056-
if !place.layout.is_unsized() && place.layout.size < self.ecx.pointer_size() {
1056+
if place.layout.is_sized() && place.layout.size < self.ecx.pointer_size() {
10571057
return Ok(());
10581058
}
10591059

0 commit comments

Comments
 (0)