1
Fork 0

Generate the right MIR for by use closures

This commit is contained in:
Santiago Pastorino 2024-12-17 12:27:02 -03:00
parent 81a926cc2a
commit 57cb498989
No known key found for this signature in database
GPG key ID: 8131A24E0C79EFAF
10 changed files with 65 additions and 47 deletions

View file

@ -37,7 +37,7 @@ pub(crate) fn closure_saved_names_of_captured_variables<'tcx>(
.map(|captured_place| {
let name = captured_place.to_symbol();
match captured_place.info.capture_kind {
ty::UpvarCapture::ByValue => name,
ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse => name,
ty::UpvarCapture::ByRef(..) => Symbol::intern(&format!("_ref__{name}")),
}
})
@ -871,7 +871,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let mut projs = closure_env_projs.clone();
projs.push(ProjectionElem::Field(FieldIdx::new(i), ty));
match capture {
ty::UpvarCapture::ByValue => {}
ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse => {}
ty::UpvarCapture::ByRef(..) => {
projs.push(ProjectionElem::Deref);
}

View file

@ -652,7 +652,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
}
},
hir::ExprKind::Closure { .. } => {
hir::ExprKind::Closure(hir::Closure { .. }) => {
let closure_ty = self.typeck_results.expr_ty(expr);
let (def_id, args, movability) = match *closure_ty.kind() {
ty::Closure(def_id, args) => (def_id, UpvarArgs::Closure(args), None),
@ -1252,6 +1252,17 @@ impl<'tcx> ThirBuildCx<'tcx> {
match upvar_capture {
ty::UpvarCapture::ByValue => captured_place_expr,
ty::UpvarCapture::ByUse => {
let span = captured_place_expr.span;
let expr_id = self.thir.exprs.push(captured_place_expr);
Expr {
temp_lifetime: TempLifetime { temp_lifetime, backwards_incompatible },
ty: upvar_ty,
span: closure_expr.span,
kind: ExprKind::ByUse { expr: expr_id, span },
}
}
ty::UpvarCapture::ByRef(upvar_borrow) => {
let borrow_kind = match upvar_borrow {
ty::BorrowKind::Immutable => BorrowKind::Shared,