Skip to content

Commit

Permalink
Log closure as well
Browse files Browse the repository at this point in the history
  • Loading branch information
arora-aman committed Nov 13, 2020
1 parent d0fac05 commit c50e57f
Show file tree
Hide file tree
Showing 27 changed files with 727 additions and 219 deletions.
15 changes: 11 additions & 4 deletions compiler/rustc_typeck/src/check/upvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
"For closure={:?}, capture_information={:#?}",
closure_def_id, delegate.capture_information
);
self.log_closure_capture_info(closure_def_id, &delegate.capture_information, span);
self.log_capture_analysis_first_pass(closure_def_id, &delegate.capture_information, span);

if let Some(closure_substs) = infer_kind {
// Unify the (as yet unbound) type variable in the closure
Expand Down Expand Up @@ -499,20 +499,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.tcx.has_attr(closure_def_id, sym::rustc_capture_analysis)
}

fn log_closure_capture_info(
fn log_capture_analysis_first_pass(
&self,
closure_def_id: rustc_hir::def_id::DefId,
capture_information: &FxIndexMap<Place<'tcx>, ty::CaptureInfo<'tcx>>,
closure_span: Span,
) {
if self.should_log_capture_analysis(closure_def_id) {
let mut diag =
self.tcx.sess.struct_span_err(closure_span, "First Pass analysis includes:");
for (place, capture_info) in capture_information {
let capture_str = construct_capture_info_string(self.tcx, place, capture_info);
let output_str = format!("Capturing {}", capture_str);

let span = capture_info.expr_id.map_or(closure_span, |e| self.tcx.hir().span(e));
self.tcx.sess.span_err(span, &output_str);
diag.span_note(span, &output_str);
}
diag.emit();
}
}

Expand All @@ -521,6 +524,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if let Some(min_captures) =
self.typeck_results.borrow().closure_min_captures.get(&closure_def_id)
{
let mut diag =
self.tcx.sess.struct_span_err(closure_span, "Min Capture analysis includes:");

for (_, min_captures_for_var) in min_captures {
for capture in min_captures_for_var {
let place = &capture.place;
Expand All @@ -532,9 +538,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

let span =
capture_info.expr_id.map_or(closure_span, |e| self.tcx.hir().span(e));
self.tcx.sess.span_err(span, &output_str);
diag.span_note(span, &output_str);
}
}
diag.emit();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![feature(capture_disjoint_fields)]
//~^ WARNING the feature `capture_disjoint_fields` is incomplete
//~^ WARNING: the feature `capture_disjoint_fields` is incomplete
//~| `#[warn(incomplete_features)]` on by default
//~| see issue #53488 <https://github.com/rust-lang/rust/issues/53488>
#![feature(rustc_attrs)]

// Ensure that capture analysis results in arrays being completely captured.
Expand All @@ -8,10 +10,13 @@ fn main() {

let mut c = #[rustc_capture_analysis]
//~^ ERROR: attributes on expressions are experimental
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
|| {
//~^ ERROR: First Pass analysis includes:
//~| ERROR: Min Capture analysis includes:
m[0] += 10;
//~^ ERROR: Capturing m[] -> MutBorrow
//~| ERROR: Min Capture m[] -> MutBorrow
//~^ NOTE: Capturing m[] -> MutBorrow
//~| NOTE: Min Capture m[] -> MutBorrow
m[1] += 40;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0658]: attributes on expressions are experimental
--> $DIR/arrays-completely-captured.rs:9:17
--> $DIR/arrays-completely-captured.rs:11:17
|
LL | let mut c = #[rustc_capture_analysis]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -16,14 +16,38 @@ 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: Capturing m[] -> MutBorrow
--> $DIR/arrays-completely-captured.rs:12:9
error: First Pass analysis includes:
--> $DIR/arrays-completely-captured.rs:14:5
|
LL | / || {
LL | |
LL | |
LL | | m[0] += 10;
... |
LL | | m[1] += 40;
LL | | };
| |_____^
|
note: Capturing m[] -> MutBorrow
--> $DIR/arrays-completely-captured.rs:17:9
|
LL | m[0] += 10;
| ^

error: Min Capture m[] -> MutBorrow
--> $DIR/arrays-completely-captured.rs:12:9
error: Min Capture analysis includes:
--> $DIR/arrays-completely-captured.rs:14:5
|
LL | / || {
LL | |
LL | |
LL | | m[0] += 10;
... |
LL | | m[1] += 40;
LL | | };
| |_____^
|
note: Min Capture m[] -> MutBorrow
--> $DIR/arrays-completely-captured.rs:17:9
|
LL | m[0] += 10;
| ^
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// FIXME(arora-aman) add run-pass once 2229 is implemented

#![feature(capture_disjoint_fields)]
//~^ WARNING the feature `capture_disjoint_fields` is incomplete
//~^ WARNING: the feature `capture_disjoint_fields` is incomplete
//~| NOTE: `#[warn(incomplete_features)]` on by default
//~| NOTE: see issue #53488 <https://github.com/rust-lang/rust/issues/53488>
#![feature(rustc_attrs)]

struct Point {
Expand All @@ -14,10 +16,13 @@ fn main() {

let c = #[rustc_capture_analysis]
//~^ ERROR: attributes on expressions are experimental
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
|| {
//~^ First Pass analysis includes:
//~| Min Capture analysis includes:
println!("{}", p.x);
//~^ ERROR: Capturing p[(0, 0)] -> ImmBorrow
//~| ERROR: Min Capture p[(0, 0)] -> ImmBorrow
//~^ NOTE: Capturing p[(0, 0)] -> ImmBorrow
//~| NOTE: Min Capture p[(0, 0)] -> ImmBorrow
};

// `c` should only capture `p.x`, therefore mutating `p.y` is allowed.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0658]: attributes on expressions are experimental
--> $DIR/capture-disjoint-field-struct.rs:15:13
--> $DIR/capture-disjoint-field-struct.rs:17:13
|
LL | let c = #[rustc_capture_analysis]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -16,14 +16,38 @@ 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: Capturing p[(0, 0)] -> ImmBorrow
--> $DIR/capture-disjoint-field-struct.rs:18:24
error: First Pass analysis includes:
--> $DIR/capture-disjoint-field-struct.rs:20:5
|
LL | / || {
LL | |
LL | |
LL | | println!("{}", p.x);
LL | |
LL | |
LL | | };
| |_____^
|
note: Capturing p[(0, 0)] -> ImmBorrow
--> $DIR/capture-disjoint-field-struct.rs:23:24
|
LL | println!("{}", p.x);
| ^^^

error: Min Capture p[(0, 0)] -> ImmBorrow
--> $DIR/capture-disjoint-field-struct.rs:18:24
error: Min Capture analysis includes:
--> $DIR/capture-disjoint-field-struct.rs:20:5
|
LL | / || {
LL | |
LL | |
LL | | println!("{}", p.x);
LL | |
LL | |
LL | | };
| |_____^
|
note: Min Capture p[(0, 0)] -> ImmBorrow
--> $DIR/capture-disjoint-field-struct.rs:23:24
|
LL | println!("{}", p.x);
| ^^^
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
// FIXME(arora-aman) add run-pass once 2229 is implemented

#![feature(capture_disjoint_fields)]
//~^ WARNING the feature `capture_disjoint_fields` is incomplete
//~^ WARNING: the feature `capture_disjoint_fields` is incomplete
//~| NOTE: `#[warn(incomplete_features)]` on by default
//~| NOTE: see issue #53488 <https://github.com/rust-lang/rust/issues/53488>
#![feature(rustc_attrs)]

fn main() {
let mut t = (10, 10);

let c = #[rustc_capture_analysis]
//~^ ERROR: attributes on expressions are experimental
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
|| {
//~^ First Pass analysis includes:
//~| Min Capture analysis includes:
println!("{}", t.0);
//~^ ERROR: Capturing t[(0, 0)] -> ImmBorrow
//~| ERROR: Min Capture t[(0, 0)] -> ImmBorrow
//~^ NOTE: Capturing t[(0, 0)] -> ImmBorrow
//~| NOTE: Min Capture t[(0, 0)] -> ImmBorrow
};

// `c` only captures t.0, therefore mutating t.1 is allowed.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0658]: attributes on expressions are experimental
--> $DIR/capture-disjoint-field-tuple.rs:10:13
--> $DIR/capture-disjoint-field-tuple.rs:12:13
|
LL | let c = #[rustc_capture_analysis]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -16,14 +16,38 @@ 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: Capturing t[(0, 0)] -> ImmBorrow
--> $DIR/capture-disjoint-field-tuple.rs:13:24
error: First Pass analysis includes:
--> $DIR/capture-disjoint-field-tuple.rs:15:5
|
LL | / || {
LL | |
LL | |
LL | | println!("{}", t.0);
LL | |
LL | |
LL | | };
| |_____^
|
note: Capturing t[(0, 0)] -> ImmBorrow
--> $DIR/capture-disjoint-field-tuple.rs:18:24
|
LL | println!("{}", t.0);
| ^^^

error: Min Capture t[(0, 0)] -> ImmBorrow
--> $DIR/capture-disjoint-field-tuple.rs:13:24
error: Min Capture analysis includes:
--> $DIR/capture-disjoint-field-tuple.rs:15:5
|
LL | / || {
LL | |
LL | |
LL | | println!("{}", t.0);
LL | |
LL | |
LL | | };
| |_____^
|
note: Min Capture t[(0, 0)] -> ImmBorrow
--> $DIR/capture-disjoint-field-tuple.rs:18:24
|
LL | println!("{}", t.0);
| ^^^
Expand Down
28 changes: 18 additions & 10 deletions src/test/ui/closures/2229_closure_analysis/capture-enums.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![feature(capture_disjoint_fields)]
//~^ WARNING the feature `capture_disjoint_fields` is incomplete
//~^ WARNING: the feature `capture_disjoint_fields` is incomplete
//~| NOTE: `#[warn(incomplete_features)]` on by default
//~| NOTE: see issue #53488 <https://github.com/rust-lang/rust/issues/53488>
#![feature(rustc_attrs)]

enum Info {
Expand All @@ -15,18 +17,21 @@ fn multi_variant_enum() {

let c = #[rustc_capture_analysis]
//~^ ERROR: attributes on expressions are experimental
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
|| {
//~^ First Pass analysis includes:
//~| Min Capture analysis includes:
if let Info::Point(_, _, str) = point {
//~^ Capturing point[] -> ImmBorrow
//~| Capturing point[(2, 0)] -> ByValue
//~| Min Capture point[] -> ByValue
//~^ NOTE: Capturing point[] -> ImmBorrow
//~| NOTE: Capturing point[(2, 0)] -> ByValue
//~| NOTE: Min Capture point[] -> ByValue
println!("{}", str);
}

if let Info::Meta(_, v) = meta {
//~^ Capturing meta[] -> ImmBorrow
//~| Capturing meta[(1, 1)] -> ByValue
//~| Min Capture meta[] -> ByValue
//~^ NOTE: Capturing meta[] -> ImmBorrow
//~| NOTE: Capturing meta[(1, 1)] -> ByValue
//~| NOTE: Min Capture meta[] -> ByValue
println!("{:?}", v);
}
};
Expand All @@ -43,10 +48,13 @@ fn single_variant_enum() {

let c = #[rustc_capture_analysis]
//~^ ERROR: attributes on expressions are experimental
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
|| {
let SingleVariant::Point(_, _, str) = point;
//~^ Capturing point[(2, 0)] -> ByValue
//~| Min Capture point[(2, 0)] -> ByValue
//~^ First Pass analysis includes:
//~| Min Capture analysis includes:
let SingleVariant::Point(_, _, str) = point;
//~^ NOTE: Capturing point[(2, 0)] -> ByValue
//~| NOTE: Min Capture point[(2, 0)] -> ByValue
println!("{}", str);
};

Expand Down
Loading

0 comments on commit c50e57f

Please sign in to comment.