Skip to content

Commit 6e5cca7

Browse files
arora-amanroxelo
andcommitted
Use min_captures for creating UpvarSusbts::tupled_upvar_tys
- final_upvar_tys now reads types from places instead of using `node_ty` Co-authored-by: Roxane Fruytier <roxane.fruytier@hotmail.com>
1 parent 76c68aa commit 6e5cca7

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

compiler/rustc_middle/src/ty/context.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,19 @@ impl<'tcx> TypeckResults<'tcx> {
624624
LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.pat_adjustments }
625625
}
626626

627+
/// For a given closure, returns the iterator of `ty::CapturedPlace`s that are captured
628+
/// by the closure.
629+
pub fn closure_min_captures_flattened(
630+
&self,
631+
closure_def_id: DefId,
632+
) -> impl Iterator<Item = &ty::CapturedPlace<'tcx>> {
633+
self.closure_min_captures
634+
.get(&closure_def_id)
635+
.map(|closure_min_captures| closure_min_captures.values().flat_map(|v| v.iter()))
636+
.into_iter()
637+
.flatten()
638+
}
639+
627640
pub fn upvar_capture(&self, upvar_id: ty::UpvarId) -> ty::UpvarCapture<'tcx> {
628641
self.upvar_capture_map[&upvar_id]
629642
}

compiler/rustc_typeck/src/check/upvar.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
202202
// inference algorithm will reject it).
203203

204204
// Equate the type variables for the upvars with the actual types.
205-
let final_upvar_tys = self.final_upvar_tys(closure_hir_id);
205+
let final_upvar_tys = self.final_upvar_tys(closure_def_id);
206206
debug!(
207207
"analyze_closure: id={:?} substs={:?} final_upvar_tys={:?}",
208208
closure_hir_id, substs, final_upvar_tys
@@ -222,36 +222,33 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
222222
}
223223

224224
// Returns a list of `Ty`s for each upvar.
225-
fn final_upvar_tys(&self, closure_id: hir::HirId) -> Vec<Ty<'tcx>> {
225+
fn final_upvar_tys(&self, closure_id: DefId) -> Vec<Ty<'tcx>> {
226226
// Presently an unboxed closure type cannot "escape" out of a
227227
// function, so we will only encounter ones that originated in the
228228
// local crate or were inlined into it along with some function.
229229
// This may change if abstract return types of some sort are
230230
// implemented.
231231
let tcx = self.tcx;
232-
let closure_def_id = tcx.hir().local_def_id(closure_id);
233232

234233
self.typeck_results
235234
.borrow()
236-
.closure_captures
237-
.get(&closure_def_id.to_def_id())
238-
.iter()
239-
.flat_map(|upvars| {
240-
upvars.iter().map(|(&var_hir_id, _)| {
241-
let upvar_ty = self.node_ty(var_hir_id);
242-
let upvar_id = ty::UpvarId::new(var_hir_id, closure_def_id);
243-
let capture = self.typeck_results.borrow().upvar_capture(upvar_id);
244-
245-
debug!("var_id={:?} upvar_ty={:?} capture={:?}", var_hir_id, upvar_ty, capture);
246-
247-
match capture {
248-
ty::UpvarCapture::ByValue(_) => upvar_ty,
249-
ty::UpvarCapture::ByRef(borrow) => tcx.mk_ref(
250-
borrow.region,
251-
ty::TypeAndMut { ty: upvar_ty, mutbl: borrow.kind.to_mutbl_lossy() },
252-
),
253-
}
254-
})
235+
.closure_min_captures_flattened(closure_id)
236+
.map(|captured_place| {
237+
let upvar_ty = captured_place.place.ty();
238+
let capture = captured_place.info.capture_kind;
239+
240+
debug!(
241+
"place={:?} upvar_ty={:?} capture={:?}",
242+
captured_place.place, upvar_ty, capture
243+
);
244+
245+
match capture {
246+
ty::UpvarCapture::ByValue(_) => upvar_ty,
247+
ty::UpvarCapture::ByRef(borrow) => tcx.mk_ref(
248+
borrow.region,
249+
ty::TypeAndMut { ty: upvar_ty, mutbl: borrow.kind.to_mutbl_lossy() },
250+
),
251+
}
255252
})
256253
.collect()
257254
}

0 commit comments

Comments
 (0)