Skip to content

Commit a00edce

Browse files
authoredMay 2, 2021
Rollup merge of #84358 - sexxi-goose:print_captures_borrowck_rebased, r=nikomatsakis
Update closure capture error logging for disjoint captures for disjoint captures Improved error logging when `#![feature(capture_disjoint_fields)]` is used. Closes rust-lang/project-rfc-2229#8 Closes rust-lang/project-rfc-2229#36 Closes rust-lang/project-rfc-2229#39 Closes #76005
2 parents 89ebad5 + 404cc33 commit a00edce

File tree

57 files changed

+534
-152
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+534
-152
lines changed
 

‎compiler/rustc_middle/src/mir/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,15 @@ impl BorrowKind {
683683
BorrowKind::Mut { allow_two_phase_borrow } => allow_two_phase_borrow,
684684
}
685685
}
686+
687+
pub fn describe_mutability(&self) -> String {
688+
match *self {
689+
BorrowKind::Shared | BorrowKind::Shallow | BorrowKind::Unique => {
690+
"immutable".to_string()
691+
}
692+
BorrowKind::Mut { .. } => "mutable".to_string(),
693+
}
694+
}
686695
}
687696

688697
///////////////////////////////////////////////////////////////////////////
@@ -2369,6 +2378,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
23692378
};
23702379
let mut struct_fmt = fmt.debug_struct(&name);
23712380

2381+
// FIXME(project-rfc-2229#48): This should be a list of capture names/places
23722382
if let Some(upvars) = tcx.upvars_mentioned(def_id) {
23732383
for (&var_id, place) in iter::zip(upvars.keys(), places) {
23742384
let var_name = tcx.hir().name(var_id);
@@ -2388,6 +2398,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
23882398
let name = format!("[generator@{:?}]", tcx.hir().span(hir_id));
23892399
let mut struct_fmt = fmt.debug_struct(&name);
23902400

2401+
// FIXME(project-rfc-2229#48): This should be a list of capture names/places
23912402
if let Some(upvars) = tcx.upvars_mentioned(def_id) {
23922403
for (&var_id, place) in iter::zip(upvars.keys(), places) {
23932404
let var_name = tcx.hir().name(var_id);

‎compiler/rustc_middle/src/ty/closure.rs

+20
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ pub struct CapturedPlace<'tcx> {
151151
}
152152

153153
impl CapturedPlace<'tcx> {
154+
pub fn to_string(&self, tcx: TyCtxt<'tcx>) -> String {
155+
place_to_string_for_capture(tcx, &self.place)
156+
}
157+
154158
/// Returns the hir-id of the root variable for the captured place.
155159
/// e.g., if `a.b.c` was captured, would return the hir-id for `a`.
156160
pub fn get_root_variable(&self) -> hir::HirId {
@@ -168,6 +172,22 @@ impl CapturedPlace<'tcx> {
168172
}
169173
}
170174

175+
/// Return span pointing to use that resulted in selecting the captured path
176+
pub fn get_path_span(&self, tcx: TyCtxt<'tcx>) -> Span {
177+
if let Some(path_expr_id) = self.info.path_expr_id {
178+
tcx.hir().span(path_expr_id)
179+
} else if let Some(capture_kind_expr_id) = self.info.capture_kind_expr_id {
180+
tcx.hir().span(capture_kind_expr_id)
181+
} else {
182+
// Fallback on upvars mentioned if neither path or capture expr id is captured
183+
184+
// Safe to unwrap since we know this place is captured by the closure, therefore the closure must have upvars.
185+
tcx.upvars_mentioned(self.get_closure_local_def_id()).unwrap()
186+
[&self.get_root_variable()]
187+
.span
188+
}
189+
}
190+
171191
/// Return span pointing to use that resulted in selecting the current capture kind
172192
pub fn get_capture_kind_span(&self, tcx: TyCtxt<'tcx>) -> Span {
173193
if let Some(capture_kind_expr_id) = self.info.capture_kind_expr_id {

0 commit comments

Comments
 (0)
Please sign in to comment.