Skip to content

Commit a6d691b

Browse files
committed
Fix End-user description not implemented for field access on `TyClosure
Fixes #45698
1 parent dbeb5bf commit a6d691b

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/librustc_mir/borrow_check.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! This query borrow-checks the MIR to (further) ensure it is not broken.
1212
1313
use rustc::hir;
14+
use rustc::hir::def::Def;
1415
use rustc::hir::def_id::{DefId};
1516
use rustc::infer::{InferCtxt};
1617
use rustc::ty::{self, TyCtxt, ParamEnv};
@@ -1549,6 +1550,7 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
15491550
}
15501551
}
15511552

1553+
// FIXME Instead of passing usize, Field should be passed
15521554
// End-user visible description of the `field_index`nth field of `base`
15531555
fn describe_field(&self, base: &Lvalue, field_index: usize) -> String {
15541556
match *base {
@@ -1600,7 +1602,20 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
16001602
},
16011603
ty::TyArray(ty, _) | ty::TySlice(ty) => {
16021604
self.describe_field_from_ty(&ty, field_index)
1603-
}
1605+
},
1606+
ty::TyClosure(closure_def_id, _) => {
1607+
// Convert the def-id into a node-id. node-ids are only valid for
1608+
// the local code in the current crate, so this returns an `Option` in case
1609+
// the closure comes from another crate. But in that case we wouldn't
1610+
// be borrowck'ing it, so we can just unwrap:
1611+
let node_id = self.tcx.hir.as_local_node_id(closure_def_id).unwrap();
1612+
let local_def = self.tcx.with_freevars(node_id, |fv| fv[field_index].def);
1613+
1614+
match local_def {
1615+
Def::Local(local_node_id) => self.tcx.hir.name(local_node_id).to_string(),
1616+
_ => unreachable!()
1617+
}
1618+
}
16041619
_ => {
16051620
// Might need a revision when the fields in trait RFC is implemented
16061621
// (https://github.com/rust-lang/rfcs/pull/1546)

0 commit comments

Comments
 (0)