diff --git a/compiler/rustc_mir_build/src/build/expr/as_place.rs b/compiler/rustc_mir_build/src/build/expr/as_place.rs index b682b0c3bd4..36f08a7a48f 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_place.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_place.rs @@ -325,6 +325,13 @@ pub(crate) fn clone_project(&self, elem: PlaceElem<'tcx>) -> Self { } } + /// Similar to `Place::ty` but needed during mir building. + /// + /// Applies the projections in the `PlaceBuilder` to the base + /// type. + /// + /// Fallible as the root of this place may be an upvar for + /// which no base type can be determined. pub fn try_compute_ty( &self, local_decls: &D, diff --git a/compiler/rustc_mir_build/src/build/matches/util.rs b/compiler/rustc_mir_build/src/build/matches/util.rs index 7423b5e1ae3..fb2a5be28a2 100644 --- a/compiler/rustc_mir_build/src/build/matches/util.rs +++ b/compiler/rustc_mir_build/src/build/matches/util.rs @@ -54,10 +54,20 @@ pub(crate) fn field_match_pairs_tuple_struct<'pat>( let variant_idx = opt_variant_idx.unwrap(); adt_def.variant(variant_idx).fields[field_idx].ty(self.tcx, substs) } - ty::Adt(adt_def, substs) => { - adt_def.all_fields().collect::>()[field_idx].ty(self.tcx, substs) - } - ty::Tuple(elems) => elems.to_vec()[field_idx], + ty::Adt(adt_def, substs) => adt_def + .all_fields() + .nth(field_idx) + .unwrap_or_else(|| { + bug!( + "expected to take field idx {:?} of fields of {:?}", + field_idx, + adt_def + ) + }) + .ty(self.tcx, substs), + ty::Tuple(elems) => elems.iter().nth(field_idx).unwrap_or_else(|| { + bug!("expected to take field idx {:?} of {:?}", field_idx, elems) + }), _ => bug!( "no field available, place_ty: {:#?}, kind: {:?}", place_ty,