about summary refs log tree commit diff
path: root/compiler/rustc_const_eval
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2023-09-12 20:51:00 +0200
committerRalf Jung <post@ralfj.de>2023-09-12 20:52:05 +0200
commit60091fe92403f5a4aca1d273295668b546bf2bf2 (patch)
tree228709642cf413c8c4e1802fb7fad2687407aab6 /compiler/rustc_const_eval
parente5fedceabf4e0564231db592b6d1f35e1ca27908 (diff)
downloadrust-60091fe92403f5a4aca1d273295668b546bf2bf2.tar.gz
rust-60091fe92403f5a4aca1d273295668b546bf2bf2.zip
add helper method for finding the one non-1-ZST field
Diffstat (limited to 'compiler/rustc_const_eval')
-rw-r--r--compiler/rustc_const_eval/src/interpret/terminator.rs39
1 files changed, 7 insertions, 32 deletions
diff --git a/compiler/rustc_const_eval/src/interpret/terminator.rs b/compiler/rustc_const_eval/src/interpret/terminator.rs
index 7e22981542e..e15499bc68d 100644
--- a/compiler/rustc_const_eval/src/interpret/terminator.rs
+++ b/compiler/rustc_const_eval/src/interpret/terminator.rs
@@ -269,19 +269,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
         match layout.ty.kind() {
             ty::Adt(adt_def, _) if adt_def.repr().transparent() && may_unfold(*adt_def) => {
                 assert!(!adt_def.is_enum());
-                // Find the non-1-ZST field.
-                let mut non_1zst_fields = (0..layout.fields.count()).filter_map(|idx| {
-                    let field = layout.field(self, idx);
-                    if field.is_1zst() { None } else { Some(field) }
-                });
-                let first = non_1zst_fields.next().expect("`unfold_transparent` called on 1-ZST");
-                assert!(
-                    non_1zst_fields.next().is_none(),
-                    "more than one non-1-ZST field in a transparent type"
-                );
-
-                // Found it!
-                self.unfold_transparent(first, may_unfold)
+                // Find the non-1-ZST field, and recurse.
+                let (_, field) = layout.non_1zst_field(self).unwrap();
+                self.unfold_transparent(field, may_unfold)
             }
             // Not a transparent type, no further unfolding.
             _ => layout,
@@ -797,25 +787,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
                         _ => {
                             // Not there yet, search for the only non-ZST field.
                             // (The rules for `DispatchFromDyn` ensure there's exactly one such field.)
-                            let mut non_zst_field = None;
-                            for i in 0..receiver.layout.fields.count() {
-                                let field = self.project_field(&receiver, i)?;
-                                let zst = field.layout.is_1zst();
-                                if !zst {
-                                    assert!(
-                                        non_zst_field.is_none(),
-                                        "multiple non-1-ZST fields in dyn receiver type {}",
-                                        receiver.layout.ty
-                                    );
-                                    non_zst_field = Some(field);
-                                }
-                            }
-                            receiver = non_zst_field.unwrap_or_else(|| {
-                                panic!(
-                                    "no non-1-ZST fields in dyn receiver type {}",
-                                    receiver.layout.ty
-                                )
-                            });
+                            let (idx, _) = receiver.layout.non_1zst_field(self).expect(
+                                "not exactly one non-1-ZST field in a `DispatchFromDyn` type",
+                            );
+                            receiver = self.project_field(&receiver, idx)?;
                         }
                     }
                 };