Skip to content

Commit 0d0d2fe

Browse files
committed
Auto merge of #88477 - sexxi-goose:issue-88476, r=nikomatsakis
2229: Don't move out of drop type Fixes #88476 r? `@nikomatsakis`
2 parents fdf6505 + 153aa71 commit 0d0d2fe

File tree

4 files changed

+239
-1
lines changed

4 files changed

+239
-1
lines changed

compiler/rustc_typeck/src/check/upvar.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
399399
}
400400
};
401401

402+
// This restriction needs to be applied after we have handled adjustments for `move`
403+
// closures. We want to make sure any adjustment that might make us move the place into
404+
// the closure gets handled.
405+
let (place, capture_kind) =
406+
restrict_precision_for_drop_types(self, place, capture_kind, usage_span);
407+
402408
capture_info.capture_kind = capture_kind;
409+
403410
let capture_info = if let Some(existing) = processed.get(&place) {
404411
determine_capture_info(*existing, capture_info)
405412
} else {
@@ -626,7 +633,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
626633
self.tcx.struct_span_lint_hir(
627634
lint::builtin::RUST_2021_INCOMPATIBLE_CLOSURE_CAPTURES,
628635
closure_hir_id,
629-
closure_head_span,
636+
closure_head_span,
630637
|lint| {
631638
let mut diagnostics_builder = lint.build(
632639
format!(
@@ -1852,6 +1859,31 @@ impl<'a, 'tcx> euv::Delegate<'tcx> for InferBorrowKind<'a, 'tcx> {
18521859
self.borrow(assignee_place, diag_expr_id, ty::BorrowKind::MutBorrow);
18531860
}
18541861
}
1862+
1863+
/// Rust doesn't permit moving fields out of a type that implements drop
1864+
fn restrict_precision_for_drop_types<'a, 'tcx>(
1865+
fcx: &'a FnCtxt<'a, 'tcx>,
1866+
mut place: Place<'tcx>,
1867+
mut curr_mode: ty::UpvarCapture<'tcx>,
1868+
span: Span,
1869+
) -> (Place<'tcx>, ty::UpvarCapture<'tcx>) {
1870+
let is_copy_type = fcx.infcx.type_is_copy_modulo_regions(fcx.param_env, place.ty(), span);
1871+
1872+
if let (false, UpvarCapture::ByValue(..)) = (is_copy_type, curr_mode) {
1873+
for i in 0..place.projections.len() {
1874+
match place.ty_before_projection(i).kind() {
1875+
ty::Adt(def, _) if def.destructor(fcx.tcx).is_some() => {
1876+
truncate_place_to_len_and_update_capture_kind(&mut place, &mut curr_mode, i);
1877+
break;
1878+
}
1879+
_ => {}
1880+
}
1881+
}
1882+
}
1883+
1884+
(place, curr_mode)
1885+
}
1886+
18551887
/// Truncate `place` so that an `unsafe` block isn't required to capture it.
18561888
/// - No projections are applied to raw pointers, since these require unsafe blocks. We capture
18571889
/// them completely.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// edition:2021
2+
3+
#![feature(rustc_attrs)]
4+
5+
// Test that we can't move out of struct that impls `Drop`.
6+
7+
8+
use std::rc::Rc;
9+
10+
// Test that we restrict precision when moving not-`Copy` types, if any of the parent paths
11+
// implement `Drop`. This is to ensure that we don't move out of a type that implements Drop.
12+
pub fn test1() {
13+
struct Foo(Rc<i32>);
14+
15+
impl Drop for Foo {
16+
fn drop(self: &mut Foo) {}
17+
}
18+
19+
let f = Foo(Rc::new(1));
20+
let x = #[rustc_capture_analysis] move || {
21+
//~^ ERROR: attributes on expressions are experimental
22+
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
23+
//~| ERROR: First Pass analysis includes:
24+
//~| ERROR: Min Capture analysis includes:
25+
println!("{:?}", f.0);
26+
//~^ NOTE: Capturing f[(0, 0)] -> ImmBorrow
27+
//~| NOTE: Min Capture f[] -> ByValue
28+
};
29+
30+
x();
31+
}
32+
33+
// Test that we don't restrict precision when moving `Copy` types(i.e. when copying),
34+
// even if any of the parent paths implement `Drop`.
35+
fn test2() {
36+
struct Character {
37+
hp: u32,
38+
name: String,
39+
}
40+
41+
impl Drop for Character {
42+
fn drop(&mut self) {}
43+
}
44+
45+
let character = Character { hp: 100, name: format!("A") };
46+
47+
let c = #[rustc_capture_analysis] move || {
48+
//~^ ERROR: attributes on expressions are experimental
49+
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
50+
//~| ERROR: First Pass analysis includes:
51+
//~| ERROR: Min Capture analysis includes:
52+
println!("{}", character.hp)
53+
//~^ NOTE: Capturing character[(0, 0)] -> ImmBorrow
54+
//~| NOTE: Min Capture character[(0, 0)] -> ByValue
55+
};
56+
57+
c();
58+
59+
println!("{}", character.name);
60+
}
61+
62+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
error[E0658]: attributes on expressions are experimental
2+
--> $DIR/issue-88476.rs:20:13
3+
|
4+
LL | let x = #[rustc_capture_analysis] move || {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information
8+
= help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable
9+
10+
error[E0658]: attributes on expressions are experimental
11+
--> $DIR/issue-88476.rs:47:13
12+
|
13+
LL | let c = #[rustc_capture_analysis] move || {
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
15+
|
16+
= note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information
17+
= help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable
18+
19+
error: First Pass analysis includes:
20+
--> $DIR/issue-88476.rs:20:39
21+
|
22+
LL | let x = #[rustc_capture_analysis] move || {
23+
| _______________________________________^
24+
LL | |
25+
LL | |
26+
LL | |
27+
... |
28+
LL | |
29+
LL | | };
30+
| |_____^
31+
|
32+
note: Capturing f[(0, 0)] -> ImmBorrow
33+
--> $DIR/issue-88476.rs:25:26
34+
|
35+
LL | println!("{:?}", f.0);
36+
| ^^^
37+
38+
error: Min Capture analysis includes:
39+
--> $DIR/issue-88476.rs:20:39
40+
|
41+
LL | let x = #[rustc_capture_analysis] move || {
42+
| _______________________________________^
43+
LL | |
44+
LL | |
45+
LL | |
46+
... |
47+
LL | |
48+
LL | | };
49+
| |_____^
50+
|
51+
note: Min Capture f[] -> ByValue
52+
--> $DIR/issue-88476.rs:25:26
53+
|
54+
LL | println!("{:?}", f.0);
55+
| ^^^
56+
57+
error: First Pass analysis includes:
58+
--> $DIR/issue-88476.rs:47:39
59+
|
60+
LL | let c = #[rustc_capture_analysis] move || {
61+
| _______________________________________^
62+
LL | |
63+
LL | |
64+
LL | |
65+
... |
66+
LL | |
67+
LL | | };
68+
| |_____^
69+
|
70+
note: Capturing character[(0, 0)] -> ImmBorrow
71+
--> $DIR/issue-88476.rs:52:24
72+
|
73+
LL | println!("{}", character.hp)
74+
| ^^^^^^^^^^^^
75+
76+
error: Min Capture analysis includes:
77+
--> $DIR/issue-88476.rs:47:39
78+
|
79+
LL | let c = #[rustc_capture_analysis] move || {
80+
| _______________________________________^
81+
LL | |
82+
LL | |
83+
LL | |
84+
... |
85+
LL | |
86+
LL | | };
87+
| |_____^
88+
|
89+
note: Min Capture character[(0, 0)] -> ByValue
90+
--> $DIR/issue-88476.rs:52:24
91+
|
92+
LL | println!("{}", character.hp)
93+
| ^^^^^^^^^^^^
94+
95+
error: aborting due to 6 previous errors
96+
97+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// check-pass
2+
// edition:2021
3+
4+
use std::rc::Rc;
5+
6+
// Test that we restrict precision when moving not-`Copy` types, if any of the parent paths
7+
// implement `Drop`. This is to ensure that we don't move out of a type that implements Drop.
8+
pub fn test1() {
9+
struct Foo(Rc<i32>);
10+
11+
impl Drop for Foo {
12+
fn drop(self: &mut Foo) {}
13+
}
14+
15+
let f = Foo(Rc::new(1));
16+
let x = move || {
17+
println!("{:?}", f.0);
18+
};
19+
20+
x();
21+
}
22+
23+
24+
// Test that we don't restrict precision when moving `Copy` types(i.e. when copying),
25+
// even if any of the parent paths implement `Drop`.
26+
pub fn test2() {
27+
struct Character {
28+
hp: u32,
29+
name: String,
30+
}
31+
32+
impl Drop for Character {
33+
fn drop(&mut self) {}
34+
}
35+
36+
let character = Character { hp: 100, name: format!("A") };
37+
38+
let c = move || {
39+
println!("{}", character.hp)
40+
};
41+
42+
c();
43+
44+
println!("{}", character.name);
45+
}
46+
47+
fn main() {}

0 commit comments

Comments
 (0)