Skip to content

Commit

Permalink
Reduce verbosity of capture analysis logs
Browse files Browse the repository at this point in the history
Co-authored-by: Jenny Wills <[email protected]>
  • Loading branch information
roxelo authored and jenniferwills committed Oct 28, 2020
1 parent 08f33f2 commit 075ea11
Show file tree
Hide file tree
Showing 21 changed files with 154 additions and 400 deletions.
63 changes: 52 additions & 11 deletions compiler/rustc_typeck/src/check/upvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,22 @@ use rustc_hir::def_id::DefId;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_infer::infer::UpvarRegion;
use rustc_middle::hir::place::{Place, PlaceBase, PlaceWithHirId};
use rustc_middle::hir::place::{Place, PlaceBase, PlaceWithHirId, ProjectionKind};
use rustc_middle::ty::{self, Ty, TyCtxt, UpvarSubsts};
use rustc_span::sym;
use rustc_span::{Span, Symbol};

macro_rules! log_capture_analysis {
($fcx:expr, $closure_def_id:expr, $fmt:literal) => {
if $fcx.should_log_capture_analysis($closure_def_id) {
print!("For closure={:?}: ", $closure_def_id);
println!($fmt);
eprint!("For closure={:?}: ", $closure_def_id);
eprintln!($fmt);
}
};

($fcx:expr, $closure_def_id:expr, $fmt:literal, $($args:expr),*) => {
if $fcx.should_log_capture_analysis($closure_def_id) {
print!("For closure={:?}: ", $closure_def_id);
println!($fmt, $($args),*);
eprint!("For closure={:?}: ", $closure_def_id);
}
};
}
Expand Down Expand Up @@ -92,6 +91,53 @@ impl<'a, 'tcx> Visitor<'tcx> for InferBorrowKindVisitor<'a, 'tcx> {
}

impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
fn construct_capture_info_string(
&self,
place: &Place<'tcx>,
capture_info: &ty::CaptureInfo<'tcx>,
) -> String {
let variable_name = match place.base {
PlaceBase::Upvar(upvar_id) => var_name(self.tcx, upvar_id.var_path.hir_id).to_string(),
_ => bug!("Capture_information should only contain upvars"),
};

let mut projections_str = String::new();
for (i, item) in place.projections.iter().enumerate() {
let proj = match item.kind {
ProjectionKind::Field(a, b) => format!("({:?}, {:?})", a, b),
ProjectionKind::Deref => String::from("Deref"),
ProjectionKind::Index => String::from("Index"),
ProjectionKind::Subslice => String::from("Subslice"),
};
if i != 0 {
projections_str.push_str(",");
}
projections_str.push_str(proj.as_str());
}

let capture_kind_str = match capture_info.capture_kind {
ty::UpvarCapture::ByValue(value) => format!("{:?}", value),
ty::UpvarCapture::ByRef(borrow) => format!("{:?}", borrow.kind),
};
format!("{}[{}] -> {}", variable_name, projections_str, capture_kind_str)
}

fn log_new_capture_analysis(
&self,
closure_def_id: rustc_hir::def_id::DefId,
capture_information: &FxIndexMap<Place<'tcx>, ty::CaptureInfo<'tcx>>,
) {
if self.should_log_capture_analysis(closure_def_id) {
for (place, capture_info) in capture_information {
let capture_str = self.construct_capture_info_string(place, capture_info);
let output_str = format!("Capturing {}", capture_str);

let span = self.tcx.hir().span(capture_info.expr_id.unwrap());
self.tcx.sess.span_err(span, &output_str);
}
}
}

/// Analysis starting point.
fn analyze_closure(
&self,
Expand Down Expand Up @@ -169,12 +215,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
)
.consume_body(body);

log_capture_analysis!(
self,
closure_def_id,
"capture information: {:#?}",
delegate.capture_information
);
self.log_new_capture_analysis(closure_def_id, &delegate.capture_information);

if let Some(closure_substs) = infer_kind {
// Unify the (as yet unbound) type variable in the closure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ LL | #![feature(capture_disjoint_fields)]
= note: `#[warn(incomplete_features)]` on by default
= note: see issue #53488 <https://github.com/rust-lang/rust/issues/53488> for more information

error: aborting due to previous error; 1 warning emitted
For closure=DefId(0:4 ~ arrays_completely_captured[317d]::main::{closure#0}): Using new-style capture analysis
error: Capturing m[] -> MutBorrow
--> $DIR/arrays-completely-captured.rs:12:9
|
LL | m[0] += 10;
| ^

error: aborting due to 2 previous errors; 1 warning emitted

For more information about this error, try `rustc --explain E0658`.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ LL | #![feature(capture_disjoint_fields)]
= note: `#[warn(incomplete_features)]` on by default
= note: see issue #53488 <https://github.com/rust-lang/rust/issues/53488> for more information

error: aborting due to previous error; 1 warning emitted
For closure=DefId(0:7 ~ capture_disjoint_field_struct[317d]::main::{closure#0}): Using new-style capture analysis
error: Capturing p[(0, 0)] -> ImmBorrow
--> $DIR/capture-disjoint-field-struct.rs:18:24
|
LL | println!("{}", p.x);
| ^^^

error: aborting due to 2 previous errors; 1 warning emitted

For more information about this error, try `rustc --explain E0658`.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ LL | #![feature(capture_disjoint_fields)]
= note: `#[warn(incomplete_features)]` on by default
= note: see issue #53488 <https://github.com/rust-lang/rust/issues/53488> for more information

error: aborting due to previous error; 1 warning emitted
For closure=DefId(0:4 ~ capture_disjoint_field_tuple[317d]::main::{closure#0}): Using new-style capture analysis
error: Capturing t[(0, 0)] -> ImmBorrow
--> $DIR/capture-disjoint-field-tuple.rs:10:24
|
LL | println!("{}", t.0);
| ^^^

error: aborting due to 2 previous errors; 1 warning emitted

For more information about this error, try `rustc --explain E0658`.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ LL | #![feature(capture_disjoint_fields)]
= note: `#[warn(incomplete_features)]` on by default
= note: see issue #53488 <https://github.com/rust-lang/rust/issues/53488> for more information

error: aborting due to previous error; 1 warning emitted
For closure=DefId(0:4 ~ feature_gate_capture_disjoint_fields[317d]::main::{closure#0}): Using new-style capture analysis
error: Capturing s[] -> ImmBorrow
--> $DIR/feature-gate-capture_disjoint_fields.rs:11:69
|
LL | println!("This uses new capture analyysis to capture s={}", s);
| ^

error: aborting due to 2 previous errors; 1 warning emitted

For more information about this error, try `rustc --explain E0658`.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,12 @@ LL | #![feature(capture_disjoint_fields)]
= note: `#[warn(incomplete_features)]` on by default
= note: see issue #53488 <https://github.com/rust-lang/rust/issues/53488> for more information

error[E0502]: cannot borrow `self.list` as mutable because it is also borrowed as immutable
--> $DIR/filter-on-struct-member.rs:22:9
For closure=DefId(0:12 ~ filter_on_struct_member[317d]::{impl#1}::update::{closure#0}): Using new-style capture analysis
error: Capturing self[Deref,(0, 0)] -> ImmBorrow
--> $DIR/filter-on-struct-member.rs:24:17
|
LL | self.list.retain(
| ^ ------ immutable borrow later used by call
| _________|
| |
LL | | #[rustc_capture_analysis]
LL | | |v| self.filter.allowed(*v),
| | --- ---- first borrow occurs due to use of `self` in closure
| | |
| | immutable borrow occurs here
LL | | );
| |_________^ mutable borrow occurs here
LL | |v| self.filter.allowed(*v),
| ^^^^^^^^^^^

error: aborting due to previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0502`.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ LL | #![feature(capture_disjoint_fields)]
= note: `#[warn(incomplete_features)]` on by default
= note: see issue #53488 <https://github.com/rust-lang/rust/issues/53488> for more information

error: aborting due to previous error; 1 warning emitted
For closure=DefId(0:9 ~ multilevel_path_1[317d]::main::{closure#0}): Using new-style capture analysis
error: Capturing w[(0, 0)] -> ImmBorrow
--> $DIR/multilevel-path-1.rs:25:19
|
LL | let wp = &w.p;
| ^^^

error: aborting due to 2 previous errors; 1 warning emitted

For more information about this error, try `rustc --explain E0658`.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ LL | #![feature(capture_disjoint_fields)]
= note: `#[warn(incomplete_features)]` on by default
= note: see issue #53488 <https://github.com/rust-lang/rust/issues/53488> for more information

error: aborting due to previous error; 1 warning emitted
For closure=DefId(0:9 ~ multilevel_path_2[317d]::main::{closure#0}): Using new-style capture analysis
error: Capturing w[(0, 0),(0, 0)] -> ImmBorrow
--> $DIR/multilevel-path-2.rs:22:24
|
LL | println!("{}", w.p.x);
| ^^^^^

error: aborting due to 2 previous errors; 1 warning emitted

For more information about this error, try `rustc --explain E0658`.
Loading

0 comments on commit 075ea11

Please sign in to comment.