Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editon 2021 enables precise capture #86661

Merged
merged 2 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions compiler/rustc_mir_build/src/build/expr/as_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ fn to_upvars_resolved_place_builder<'a, 'tcx>(
ty::ClosureKind::FnOnce => {}
}

// We won't be building MIR if the closure wasn't local
let closure_hir_id = tcx.hir().local_def_id_to_hir_id(closure_def_id.expect_local());
let closure_span = tcx.hir().span(closure_hir_id);

let (capture_index, capture) = if let Some(capture_details) =
find_capture_matching_projections(
typeck_results,
Expand All @@ -226,7 +230,7 @@ fn to_upvars_resolved_place_builder<'a, 'tcx>(
) {
capture_details
} else {
if !tcx.features().capture_disjoint_fields {
if !enable_precise_capture(tcx, closure_span) {
bug!(
"No associated capture found for {:?}[{:#?}] even though \
capture_disjoint_fields isn't enabled",
Expand All @@ -242,8 +246,7 @@ fn to_upvars_resolved_place_builder<'a, 'tcx>(
return Err(from_builder);
};

let closure_ty = typeck_results
.node_type(tcx.hir().local_def_id_to_hir_id(closure_def_id.expect_local()));
let closure_ty = typeck_results.node_type(closure_hir_id);

let substs = match closure_ty.kind() {
ty::Closure(_, substs) => ty::UpvarSubsts::Closure(substs),
Expand Down Expand Up @@ -780,3 +783,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
}
}

/// Precise capture is enabled if the feature gate `capture_disjoint_fields` is enabled or if
/// user is using Rust Edition 2021 or higher.
fn enable_precise_capture(tcx: TyCtxt<'_>, closure_span: Span) -> bool {
tcx.features().capture_disjoint_fields || closure_span.rust_2021()
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#![feature(capture_disjoint_fields)]
//~^ 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>
// edition:2021
#![feature(rustc_attrs)]

// Ensure that capture analysis results in arrays being completely captured.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
error[E0658]: attributes on expressions are experimental
--> $DIR/arrays-completely-captured.rs:11:17
--> $DIR/arrays-completely-captured.rs:8:17
|
LL | let mut c = #[rustc_capture_analysis]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information
= help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable

warning: the feature `capture_disjoint_fields` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/arrays-completely-captured.rs:1: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: First Pass analysis includes:
--> $DIR/arrays-completely-captured.rs:14:5
--> $DIR/arrays-completely-captured.rs:11:5
|
LL | / || {
LL | |
Expand All @@ -29,13 +20,13 @@ LL | | };
| |_____^
|
note: Capturing m[] -> MutBorrow
--> $DIR/arrays-completely-captured.rs:17:9
--> $DIR/arrays-completely-captured.rs:14:9
|
LL | m[0] += 10;
| ^

error: Min Capture analysis includes:
--> $DIR/arrays-completely-captured.rs:14:5
--> $DIR/arrays-completely-captured.rs:11:5
|
LL | / || {
LL | |
Expand All @@ -47,11 +38,11 @@ LL | | };
| |_____^
|
note: Min Capture m[] -> MutBorrow
--> $DIR/arrays-completely-captured.rs:17:9
--> $DIR/arrays-completely-captured.rs:14:9
|
LL | m[0] += 10;
| ^

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

For more information about this error, try `rustc --explain E0658`.
8 changes: 2 additions & 6 deletions src/test/ui/closures/2229_closure_analysis/by_value.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
// edition:2021

// Test that we handle derferences properly when only some of the captures are being moved with
// `capture_disjoint_fields` enabled.


#![feature(capture_disjoint_fields)]
//~^ 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)]

#[derive(Debug, Default)]
Expand Down
27 changes: 9 additions & 18 deletions src/test/ui/closures/2229_closure_analysis/by_value.stderr
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
error[E0658]: attributes on expressions are experimental
--> $DIR/by_value.rs:22:13
--> $DIR/by_value.rs:18:13
|
LL | let c = #[rustc_capture_analysis]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information
= help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable

warning: the feature `capture_disjoint_fields` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/by_value.rs:5: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: First Pass analysis includes:
--> $DIR/by_value.rs:25:5
--> $DIR/by_value.rs:21:5
|
LL | / || {
LL | |
Expand All @@ -29,23 +20,23 @@ LL | | };
| |_____^
|
note: Capturing t[(0, 0),Deref,(0, 0)] -> ImmBorrow
--> $DIR/by_value.rs:28:17
--> $DIR/by_value.rs:24:17
|
LL | let p = t.0.0;
| ^^^^^
note: Capturing t[(0, 0)] -> ByValue
--> $DIR/by_value.rs:28:17
--> $DIR/by_value.rs:24:17
|
LL | let p = t.0.0;
| ^^^^^
note: Capturing t[(1, 0)] -> ImmBorrow
--> $DIR/by_value.rs:32:29
--> $DIR/by_value.rs:28:29
|
LL | println!("{} {:?}", t.1, p);
| ^^^

error: Min Capture analysis includes:
--> $DIR/by_value.rs:25:5
--> $DIR/by_value.rs:21:5
|
LL | / || {
LL | |
Expand All @@ -57,16 +48,16 @@ LL | | };
| |_____^
|
note: Min Capture t[(0, 0)] -> ByValue
--> $DIR/by_value.rs:28:17
--> $DIR/by_value.rs:24:17
|
LL | let p = t.0.0;
| ^^^^^
note: Min Capture t[(1, 0)] -> ImmBorrow
--> $DIR/by_value.rs:32:29
--> $DIR/by_value.rs:28:29
|
LL | println!("{} {:?}", t.1, p);
| ^^^

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

For more information about this error, try `rustc --explain E0658`.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#![feature(capture_disjoint_fields)]
//~^ 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>
// edition:2021

#![feature(rustc_attrs)]

#[derive(Debug)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
error[E0658]: attributes on expressions are experimental
--> $DIR/capture-analysis-1.rs:17:13
--> $DIR/capture-analysis-1.rs:15:13
|
LL | let c = #[rustc_capture_analysis]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information
= help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable

warning: the feature `capture_disjoint_fields` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/capture-analysis-1.rs:1: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: First Pass analysis includes:
--> $DIR/capture-analysis-1.rs:20:5
--> $DIR/capture-analysis-1.rs:18:5
|
LL | / || {
LL | |
Expand All @@ -29,28 +20,28 @@ LL | | };
| |_____^
|
note: Capturing p[] -> ImmBorrow
--> $DIR/capture-analysis-1.rs:23:26
--> $DIR/capture-analysis-1.rs:21:26
|
LL | println!("{:?}", p);
| ^
note: Capturing p[(0, 0)] -> ImmBorrow
--> $DIR/capture-analysis-1.rs:26:26
--> $DIR/capture-analysis-1.rs:24:26
|
LL | println!("{:?}", p.x);
| ^^^
note: Capturing q[(0, 0)] -> ImmBorrow
--> $DIR/capture-analysis-1.rs:29:26
--> $DIR/capture-analysis-1.rs:27:26
|
LL | println!("{:?}", q.x);
| ^^^
note: Capturing q[] -> ImmBorrow
--> $DIR/capture-analysis-1.rs:31:26
--> $DIR/capture-analysis-1.rs:29:26
|
LL | println!("{:?}", q);
| ^

error: Min Capture analysis includes:
--> $DIR/capture-analysis-1.rs:20:5
--> $DIR/capture-analysis-1.rs:18:5
|
LL | / || {
LL | |
Expand All @@ -62,16 +53,16 @@ LL | | };
| |_____^
|
note: Min Capture p[] -> ImmBorrow
--> $DIR/capture-analysis-1.rs:23:26
--> $DIR/capture-analysis-1.rs:21:26
|
LL | println!("{:?}", p);
| ^
note: Min Capture q[] -> ImmBorrow
--> $DIR/capture-analysis-1.rs:31:26
--> $DIR/capture-analysis-1.rs:29:26
|
LL | println!("{:?}", q);
| ^

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

For more information about this error, try `rustc --explain E0658`.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#![feature(capture_disjoint_fields)]
//~^ 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>
// edition:2021

#![feature(rustc_attrs)]

#[derive(Debug)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
error[E0658]: attributes on expressions are experimental
--> $DIR/capture-analysis-2.rs:16:13
--> $DIR/capture-analysis-2.rs:14:13
|
LL | let c = #[rustc_capture_analysis]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information
= help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable

warning: the feature `capture_disjoint_fields` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/capture-analysis-2.rs:1: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: First Pass analysis includes:
--> $DIR/capture-analysis-2.rs:19:5
--> $DIR/capture-analysis-2.rs:17:5
|
LL | / || {
LL | |
Expand All @@ -29,18 +20,18 @@ LL | | };
| |_____^
|
note: Capturing p[(0, 0)] -> ByValue
--> $DIR/capture-analysis-2.rs:22:18
--> $DIR/capture-analysis-2.rs:20:18
|
LL | let _x = p.x;
| ^^^
note: Capturing p[] -> ImmBorrow
--> $DIR/capture-analysis-2.rs:25:26
--> $DIR/capture-analysis-2.rs:23:26
|
LL | println!("{:?}", p);
| ^

error: Min Capture analysis includes:
--> $DIR/capture-analysis-2.rs:19:5
--> $DIR/capture-analysis-2.rs:17:5
|
LL | / || {
LL | |
Expand All @@ -52,14 +43,14 @@ LL | | };
| |_____^
|
note: Min Capture p[] -> ByValue
--> $DIR/capture-analysis-2.rs:22:18
--> $DIR/capture-analysis-2.rs:20:18
|
LL | let _x = p.x;
| ^^^ p[] captured as ByValue here
...
LL | println!("{:?}", p);
| ^ p[] used here

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

For more information about this error, try `rustc --explain E0658`.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#![feature(capture_disjoint_fields)]
//~^ 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>
// edition:2021

#![feature(rustc_attrs)]

#[derive(Debug)]
Expand Down
Loading