From c39de61d2a8fd1f3226e623d67e1e5bac593dac6 Mon Sep 17 00:00:00 2001 From: b-naber Date: Wed, 23 Nov 2022 17:42:12 +0100 Subject: [PATCH] include closures and generators in try_compute_field_ty --- .../src/build/expr/as_place.rs | 42 +++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) 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 5c9459c97f4..bf417dae785 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_place.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_place.rs @@ -397,13 +397,49 @@ fn try_compute_field_ty( .all_fields() .nth(field_idx) .unwrap_or_else(|| { - bug!("expected to take field idx {:?} of fields of {:?}", field_idx, adt_def) + bug!( + "expected to take field with idx {:?} of fields of {:?}", + field_idx, + adt_def + ) }) .ty(cx.tcx, substs), ty::Tuple(elems) => elems.iter().nth(field_idx).unwrap_or_else(|| { - bug!("expected to take field idx {:?} of {:?}", field_idx, elems) + bug!("expected to take field with idx {:?} of {:?}", field_idx, elems) }), - _ => return None, + ty::Closure(_, substs) => { + let substs = substs.as_closure(); + let Some(f_ty) = substs.upvar_tys().nth(field_idx) else { + bug!("expected to take field with idx {:?} of {:?}", field_idx, substs.upvar_tys().collect::>()); + }; + + f_ty + } + &ty::Generator(def_id, substs, _) => { + if let Some(var) = variant_index { + let gen_body = cx.tcx.optimized_mir(def_id); + let Some(layout) = gen_body.generator_layout() else { + bug!("No generator layout for {:?}", base_ty); + }; + + let Some(&local) = layout.variant_fields[var].get(field) else { + bug!("expected to take field {:?} of {:?}", field, layout.variant_fields[var]); + }; + + let Some(&f_ty) = layout.field_tys.get(local) else { + bug!("expected to get element for {:?} in {:?}", local, layout.field_tys); + }; + + f_ty + } else { + let Some(f_ty) = substs.as_generator().prefix_tys().nth(field.index()) else { + bug!("expected to take index {:?} in {:?}", field.index(), substs.as_generator().prefix_tys().collect::>()); + }; + + f_ty + } + } + _ => bug!("couldn't create field type, unexpected base type: {:?}", base_ty), }; Some(cx.tcx.normalize_erasing_regions(cx.param_env, field_ty))