Skip to content

Commit 4c8e860

Browse files
authored
Unrolled build for rust-lang#132255
Rollup merge of rust-lang#132255 - workingjubilee:layout-is-🏚️, r=compiler-errors Add `LayoutS::is_uninhabited` and use it Use accessors for the things that accessors are good at: reducing everyone's need to be nosy and peek at the internals of every data structure.
2 parents a9d1762 + 641ce06 commit 4c8e860

File tree

21 files changed

+44
-41
lines changed

21 files changed

+44
-41
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4095,6 +4095,7 @@ version = "0.0.0"
40954095
dependencies = [
40964096
"either",
40974097
"itertools",
4098+
"rustc_abi",
40984099
"rustc_arena",
40994100
"rustc_ast",
41004101
"rustc_attr",

compiler/rustc_abi/src/layout.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ where
2828
VariantIdx: Idx,
2929
F: Deref<Target = &'a LayoutData<FieldIdx, VariantIdx>> + fmt::Debug,
3030
{
31-
let uninhabited = fields.iter().any(|f| f.abi.is_uninhabited());
31+
let uninhabited = fields.iter().any(|f| f.is_uninhabited());
3232
// We cannot ignore alignment; that might lead us to entirely discard a variant and
3333
// produce an enum that is less aligned than it should be!
3434
let is_1zst = fields.iter().all(|f| f.is_1zst());
@@ -681,7 +681,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
681681
let discr_type = repr.discr_type();
682682
let bits = Integer::from_attr(dl, discr_type).size().bits();
683683
for (i, mut val) in discriminants {
684-
if !repr.c() && variants[i].iter().any(|f| f.abi.is_uninhabited()) {
684+
if !repr.c() && variants[i].iter().any(|f| f.is_uninhabited()) {
685685
continue;
686686
}
687687
if discr_type.is_signed() {

compiler/rustc_abi/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,11 @@ impl<FieldIdx: Idx, VariantIdx: Idx> LayoutData<FieldIdx, VariantIdx> {
16521652
}
16531653
}
16541654

1655+
/// Returns `true` if this is an uninhabited type
1656+
pub fn is_uninhabited(&self) -> bool {
1657+
self.abi.is_uninhabited()
1658+
}
1659+
16551660
pub fn scalar<C: HasDataLayout>(cx: &C, scalar: Scalar) -> Self {
16561661
let largest_niche = Niche::from_scalar(cx, Size::ZERO, scalar);
16571662
let size = scalar.size(cx);

compiler/rustc_codegen_llvm/src/abi.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
415415
instance: Option<ty::Instance<'tcx>>,
416416
) {
417417
let mut func_attrs = SmallVec::<[_; 3]>::new();
418-
if self.ret.layout.abi.is_uninhabited() {
418+
if self.ret.layout.is_uninhabited() {
419419
func_attrs.push(llvm::AttributeKind::NoReturn.create_attr(cx.llcx));
420420
}
421421
if !self.can_unwind {
@@ -532,7 +532,7 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
532532

533533
fn apply_attrs_callsite(&self, bx: &mut Builder<'_, 'll, 'tcx>, callsite: &'ll Value) {
534534
let mut func_attrs = SmallVec::<[_; 2]>::new();
535-
if self.ret.layout.abi.is_uninhabited() {
535+
if self.ret.layout.is_uninhabited() {
536536
func_attrs.push(llvm::AttributeKind::NoReturn.create_attr(bx.cx.llcx));
537537
}
538538
if !self.can_unwind {

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ impl<'ll, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
364364

365365
let mut flags = DIFlags::FlagPrototyped;
366366

367-
if fn_abi.ret.layout.abi.is_uninhabited() {
367+
if fn_abi.ret.layout.is_uninhabited() {
368368
flags |= DIFlags::FlagNoReturn;
369369
}
370370

compiler/rustc_codegen_ssa/src/mir/block.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
438438
_ => bug!("C-variadic function must have a `VaList` place"),
439439
}
440440
}
441-
if self.fn_abi.ret.layout.abi.is_uninhabited() {
441+
if self.fn_abi.ret.layout.is_uninhabited() {
442442
// Functions with uninhabited return values are marked `noreturn`,
443443
// so we should make sure that we never actually do.
444444
// We play it safe by using a well-defined `abort`, but we could go for immediate UB
@@ -774,7 +774,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
774774
Some(if do_panic {
775775
let msg_str = with_no_visible_paths!({
776776
with_no_trimmed_paths!({
777-
if layout.abi.is_uninhabited() {
777+
if layout.is_uninhabited() {
778778
// Use this error even for the other intrinsics as it is more precise.
779779
format!("attempted to instantiate uninhabited type `{ty}`")
780780
} else if requirement == ValidityRequirement::Zero {

compiler/rustc_codegen_ssa/src/mir/place.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl<V: CodegenObject> PlaceValue<V> {
5555
/// Creates a `PlaceRef` to this location with the given type.
5656
pub fn with_type<'tcx>(self, layout: TyAndLayout<'tcx>) -> PlaceRef<'tcx, V> {
5757
assert!(
58-
layout.is_unsized() || layout.abi.is_uninhabited() || self.llextra.is_none(),
58+
layout.is_unsized() || layout.is_uninhabited() || self.llextra.is_none(),
5959
"Had pointer metadata {:?} for sized type {layout:?}",
6060
self.llextra,
6161
);
@@ -239,7 +239,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
239239
let dl = &bx.tcx().data_layout;
240240
let cast_to_layout = bx.cx().layout_of(cast_to);
241241
let cast_to = bx.cx().immediate_backend_type(cast_to_layout);
242-
if self.layout.abi.is_uninhabited() {
242+
if self.layout.is_uninhabited() {
243243
return bx.cx().const_poison(cast_to);
244244
}
245245
let (tag_scalar, tag_encoding, tag_field) = match self.layout.variants {
@@ -358,7 +358,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
358358
bx: &mut Bx,
359359
variant_index: VariantIdx,
360360
) {
361-
if self.layout.for_variant(bx.cx(), variant_index).abi.is_uninhabited() {
361+
if self.layout.for_variant(bx.cx(), variant_index).is_uninhabited() {
362362
// We play it safe by using a well-defined `abort`, but we could go for immediate UB
363363
// if that turns out to be helpful.
364364
bx.abort();

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
203203
) -> Option<OperandValue<Bx::Value>> {
204204
// Check for transmutes that are always UB.
205205
if operand.layout.size != cast.size
206-
|| operand.layout.abi.is_uninhabited()
207-
|| cast.abi.is_uninhabited()
206+
|| operand.layout.is_uninhabited()
207+
|| cast.is_uninhabited()
208208
{
209-
if !operand.layout.abi.is_uninhabited() {
209+
if !operand.layout.is_uninhabited() {
210210
// Since this is known statically and the input could have existed
211211
// without already having hit UB, might as well trap for it.
212212
bx.abort();
@@ -555,7 +555,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
555555

556556
assert!(bx.cx().is_backend_immediate(cast));
557557
let to_backend_ty = bx.cx().immediate_backend_type(cast);
558-
if operand.layout.abi.is_uninhabited() {
558+
if operand.layout.is_uninhabited() {
559559
let val = OperandValue::Immediate(bx.cx().const_poison(to_backend_ty));
560560
return OperandRef { val, layout: cast };
561561
}

compiler/rustc_const_eval/src/const_eval/machine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
395395

396396
#[inline(always)]
397397
fn enforce_validity(ecx: &InterpCx<'tcx, Self>, layout: TyAndLayout<'tcx>) -> bool {
398-
ecx.tcx.sess.opts.unstable_opts.extra_const_ub_checks || layout.abi.is_uninhabited()
398+
ecx.tcx.sess.opts.unstable_opts.extra_const_ub_checks || layout.is_uninhabited()
399399
}
400400

401401
fn load_mir(

compiler/rustc_const_eval/src/interpret/discriminant.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
2727
// discriminant, so we cannot do anything here.
2828
// When evaluating we will always error before even getting here, but ConstProp 'executes'
2929
// dead code, so we cannot ICE here.
30-
if dest.layout().for_variant(self, variant_index).abi.is_uninhabited() {
30+
if dest.layout().for_variant(self, variant_index).is_uninhabited() {
3131
throw_ub!(UninhabitedEnumVariantWritten(variant_index))
3232
}
3333

@@ -86,7 +86,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
8686
// For consistency with `write_discriminant`, and to make sure that
8787
// `project_downcast` cannot fail due to strange layouts, we declare immediate UB
8888
// for uninhabited variants.
89-
if op.layout().for_variant(self, index).abi.is_uninhabited() {
89+
if op.layout().for_variant(self, index).is_uninhabited() {
9090
throw_ub!(UninhabitedEnumVariantRead(index))
9191
}
9292
}
@@ -203,7 +203,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
203203
// Reading the discriminant of an uninhabited variant is UB. This is the basis for the
204204
// `uninhabited_enum_branching` MIR pass. It also ensures consistency with
205205
// `write_discriminant`.
206-
if op.layout().for_variant(self, index).abi.is_uninhabited() {
206+
if op.layout().for_variant(self, index).is_uninhabited() {
207207
throw_ub!(UninhabitedEnumVariantRead(index))
208208
}
209209
interp_ok(index)

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
364364
let msg = match requirement {
365365
// For *all* intrinsics we first check `is_uninhabited` to give a more specific
366366
// error message.
367-
_ if layout.abi.is_uninhabited() => format!(
367+
_ if layout.is_uninhabited() => format!(
368368
"aborted execution: attempted to instantiate uninhabited type `{ty}`"
369369
),
370370
ValidityRequirement::Inhabited => bug!("handled earlier"),

compiler/rustc_const_eval/src/interpret/operator.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
315315
let ptr = left.to_scalar().to_pointer(self)?;
316316
let pointee_ty = left.layout.ty.builtin_deref(true).unwrap();
317317
let pointee_layout = self.layout_of(pointee_ty)?;
318-
assert!(pointee_layout.abi.is_sized());
318+
assert!(pointee_layout.is_sized());
319319

320320
// The size always fits in `i64` as it can be at most `isize::MAX`.
321321
let pointee_size = i64::try_from(pointee_layout.size.bytes()).unwrap();
@@ -518,14 +518,14 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
518518

519519
interp_ok(match null_op {
520520
SizeOf => {
521-
if !layout.abi.is_sized() {
521+
if !layout.is_sized() {
522522
span_bug!(self.cur_span(), "unsized type for `NullaryOp::SizeOf`");
523523
}
524524
let val = layout.size.bytes();
525525
ImmTy::from_uint(val, usize_layout())
526526
}
527527
AlignOf => {
528-
if !layout.abi.is_sized() {
528+
if !layout.is_sized() {
529529
span_bug!(self.cur_span(), "unsized type for `NullaryOp::AlignOf`");
530530
}
531531
let val = layout.align.abi.bytes();

compiler/rustc_const_eval/src/interpret/validity.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
542542
throw_validation_failure!(self.path, NullPtr { ptr_kind })
543543
}
544544
// Do not allow references to uninhabited types.
545-
if place.layout.abi.is_uninhabited() {
545+
if place.layout.is_uninhabited() {
546546
let ty = place.layout.ty;
547547
throw_validation_failure!(self.path, PtrToUninhabited { ptr_kind, ty })
548548
}
@@ -867,7 +867,7 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
867867
/// Add the entire given place to the "data" range of this visit.
868868
fn add_data_range_place(&mut self, place: &PlaceTy<'tcx, M::Provenance>) {
869869
// Only sized places can be added this way.
870-
debug_assert!(place.layout.abi.is_sized());
870+
debug_assert!(place.layout.is_sized());
871871
if let Some(data_bytes) = self.data_bytes.as_mut() {
872872
let offset = Self::data_range_offset(self.ecx, place);
873873
data_bytes.add_range(offset, place.layout.size);
@@ -945,7 +945,7 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
945945
layout: TyAndLayout<'tcx>,
946946
) -> Cow<'e, RangeSet> {
947947
assert!(layout.ty.is_union());
948-
assert!(layout.abi.is_sized(), "there are no unsized unions");
948+
assert!(layout.is_sized(), "there are no unsized unions");
949949
let layout_cx = LayoutCx::new(*ecx.tcx, ecx.param_env);
950950
return M::cached_union_data_range(ecx, layout.ty, || {
951951
let mut out = RangeSet(Vec::new());

compiler/rustc_const_eval/src/util/check_validity_requirement.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn check_validity_requirement<'tcx>(
2929

3030
// There is nothing strict or lax about inhabitedness.
3131
if kind == ValidityRequirement::Inhabited {
32-
return Ok(!layout.abi.is_uninhabited());
32+
return Ok(!layout.is_uninhabited());
3333
}
3434

3535
let layout_cx = LayoutCx::new(tcx, param_env_and_ty.param_env);

compiler/rustc_hir_analysis/src/check/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ fn check_static_inhabited(tcx: TyCtxt<'_>, def_id: LocalDefId) {
172172
return;
173173
}
174174
};
175-
if layout.abi.is_uninhabited() {
175+
if layout.is_uninhabited() {
176176
tcx.node_span_lint(
177177
UNINHABITED_STATIC,
178178
tcx.local_def_id_to_hir_id(def_id),

compiler/rustc_middle/src/ty/layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ impl<'tcx> SizeSkeleton<'tcx> {
346346
// First try computing a static layout.
347347
let err = match tcx.layout_of(param_env.and(ty)) {
348348
Ok(layout) => {
349-
if layout.abi.is_sized() {
349+
if layout.is_sized() {
350350
return Ok(SizeSkeleton::Known(layout.size, Some(layout.align.abi)));
351351
} else {
352352
// Just to be safe, don't claim a known layout for unsized types.

compiler/rustc_mir_transform/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2021"
77
# tidy-alphabetical-start
88
either = "1"
99
itertools = "0.12"
10+
rustc_abi = { path = "../rustc_abi" }
1011
rustc_arena = { path = "../rustc_arena" }
1112
rustc_ast = { path = "../rustc_ast" }
1213
rustc_attr = { path = "../rustc_attr" }

compiler/rustc_mir_transform/src/unreachable_enum_branching.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! A pass that eliminates branches on uninhabited or unreachable enum variants.
22
3+
use rustc_abi::Variants;
34
use rustc_data_structures::fx::FxHashSet;
45
use rustc_middle::bug;
56
use rustc_middle::mir::patch::MirPatch;
@@ -9,7 +10,6 @@ use rustc_middle::mir::{
910
};
1011
use rustc_middle::ty::layout::TyAndLayout;
1112
use rustc_middle::ty::{Ty, TyCtxt};
12-
use rustc_target::abi::{Abi, Variants};
1313
use tracing::trace;
1414

1515
pub(super) struct UnreachableEnumBranching;
@@ -65,7 +65,7 @@ fn variant_discriminants<'tcx>(
6565
Variants::Multiple { variants, .. } => variants
6666
.iter_enumerated()
6767
.filter_map(|(idx, layout)| {
68-
(layout.abi != Abi::Uninhabited)
68+
(!layout.is_uninhabited())
6969
.then(|| ty.discriminant_for_variant(tcx, idx).unwrap().val)
7070
})
7171
.collect(),

compiler/rustc_transmute/src/layout/tree.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,7 @@ pub(crate) mod rustc {
339339
// 2. enums that delegate their layout to a variant
340340
// 3. enums with multiple variants
341341
match layout.variants() {
342-
Variants::Single { .. }
343-
if layout.abi.is_uninhabited() && layout.size == Size::ZERO =>
344-
{
342+
Variants::Single { .. } if layout.is_uninhabited() && layout.size == Size::ZERO => {
345343
// The layout representation of uninhabited, ZST enums is
346344
// defined to be like that of the `!` type, as opposed of a
347345
// typical enum. Consequently, they cannot be descended into

compiler/rustc_ty_utils/src/layout/invariant.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub(super) fn partially_check_layout<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLa
1010

1111
// Type-level uninhabitedness should always imply ABI uninhabitedness.
1212
if layout.ty.is_privately_uninhabited(tcx, cx.param_env) {
13-
assert!(layout.abi.is_uninhabited());
13+
assert!(layout.is_uninhabited());
1414
}
1515

1616
if layout.size.bytes() % layout.align.abi.bytes() != 0 {
@@ -262,9 +262,7 @@ pub(super) fn partially_check_layout<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLa
262262
)
263263
}
264264
// Skip empty variants.
265-
if variant.size == Size::ZERO
266-
|| variant.fields.count() == 0
267-
|| variant.abi.is_uninhabited()
265+
if variant.size == Size::ZERO || variant.fields.count() == 0 || variant.is_uninhabited()
268266
{
269267
// These are never actually accessed anyway, so we can skip the coherence check
270268
// for them. They also fail that check, since they have

src/librustdoc/html/render/type_layout.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ pub(crate) fn document_type_layout<'a, 'cx: 'a>(
6060
span_bug!(tcx.def_span(ty_def_id), "not an adt")
6161
};
6262
let name = adt.variant(variant_idx).name;
63-
let is_unsized = variant_layout.abi.is_unsized();
64-
let is_uninhabited = variant_layout.abi.is_uninhabited();
63+
let is_unsized = variant_layout.is_unsized();
64+
let is_uninhabited = variant_layout.is_uninhabited();
6565
let size = variant_layout.size.bytes() - tag_size;
6666
let type_layout_size = TypeLayoutSize { is_unsized, is_uninhabited, size };
6767
(name, type_layout_size)
@@ -72,8 +72,8 @@ pub(crate) fn document_type_layout<'a, 'cx: 'a>(
7272
};
7373

7474
let type_layout_size = tcx.layout_of(param_env.and(ty)).map(|layout| {
75-
let is_unsized = layout.abi.is_unsized();
76-
let is_uninhabited = layout.abi.is_uninhabited();
75+
let is_unsized = layout.is_unsized();
76+
let is_uninhabited = layout.is_uninhabited();
7777
let size = layout.size.bytes();
7878
TypeLayoutSize { is_unsized, is_uninhabited, size }
7979
});

0 commit comments

Comments
 (0)