Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit baa72e6

Browse files
authored
Rollup merge of rust-lang#132255 - workingjubilee:layout-is-🏚️, r=compiler-errors
Add `LayoutData::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 ae13340 + 641ce06 commit baa72e6

File tree

21 files changed

+44
-41
lines changed

21 files changed

+44
-41
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4100,6 +4100,7 @@ version = "0.0.0"
41004100
dependencies = [
41014101
"either",
41024102
"itertools",
4103+
"rustc_abi",
41034104
"rustc_arena",
41044105
"rustc_ast",
41054106
"rustc_attr",

compiler/rustc_abi/src/layout.rs

Lines changed: 2 additions & 2 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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)

0 commit comments

Comments
 (0)