Skip to content

Commit 994793f

Browse files
committed
PR fixup
1 parent d2cbe21 commit 994793f

19 files changed

+86
-62
lines changed

Diff for: compiler/rustc_ty_utils/src/needs_drop.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,22 @@ fn adt_significant_drop_tys(
240240
def_id: DefId,
241241
) -> Result<&ty::List<Ty<'_>>, AlwaysRequiresDrop> {
242242
let adt_has_dtor = |adt_def: &ty::AdtDef| {
243-
adt_def.destructor(tcx).map(|dtor| {
244-
if tcx.has_attr(dtor.did, sym::rustc_insignificant_dtor) {
245-
DtorType::Insignificant
246-
} else {
247-
DtorType::Significant
248-
}
249-
})
243+
let is_marked_insig = tcx.has_attr(adt_def.did, sym::rustc_insignificant_dtor);
244+
if is_marked_insig {
245+
// In some cases like `std::collections::HashMap` where the struct is a wrapper around
246+
// a type that is a Drop type, and the wrapped type (eg: `hashbrown::HashMap`) lies
247+
// outside stdlib, we might choose to still annotate the the wrapper (std HashMap) with
248+
// `rustc_insignificant_dtor`, even if the type itself doesn't have a `Drop` impl.
249+
Some(DtorType::Insignificant)
250+
} else if adt_def.destructor(tcx).is_some() {
251+
// There is a Drop impl and the type isn't marked insignificant, therefore Drop must be
252+
// significant.
253+
Some(DtorType::Significant)
254+
} else {
255+
// No destructor found nor the type is annotated with `rustc_insignificant_dtor`, we
256+
// treat this as the simple case of Drop impl for type.
257+
None
258+
}
250259
};
251260
adt_drop_tys_helper(tcx, def_id, adt_has_dtor)
252261
}

Diff for: library/alloc/src/collections/btree/map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,14 @@ pub(super) const MIN_LEN: usize = node::MIN_LEN_AFTER_SPLIT;
155155
/// ```
156156
#[stable(feature = "rust1", since = "1.0.0")]
157157
#[cfg_attr(not(test), rustc_diagnostic_item = "BTreeMap")]
158+
#[rustc_insignificant_dtor]
158159
pub struct BTreeMap<K, V> {
159160
root: Option<Root<K, V>>,
160161
length: usize,
161162
}
162163

163164
#[stable(feature = "btree_drop", since = "1.7.0")]
164165
unsafe impl<#[may_dangle] K, #[may_dangle] V> Drop for BTreeMap<K, V> {
165-
#[rustc_insignificant_dtor]
166166
fn drop(&mut self) {
167167
drop(unsafe { ptr::read(self) }.into_iter())
168168
}
@@ -331,6 +331,7 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IterMut<'_, K, V> {
331331
///
332332
/// [`into_iter`]: IntoIterator::into_iter
333333
#[stable(feature = "rust1", since = "1.0.0")]
334+
#[rustc_insignificant_dtor]
334335
pub struct IntoIter<K, V> {
335336
range: LazyLeafRange<marker::Dying, K, V>,
336337
length: usize,
@@ -1460,7 +1461,6 @@ impl<K, V> IntoIterator for BTreeMap<K, V> {
14601461

14611462
#[stable(feature = "btree_drop", since = "1.7.0")]
14621463
impl<K, V> Drop for IntoIter<K, V> {
1463-
#[rustc_insignificant_dtor]
14641464
fn drop(&mut self) {
14651465
struct DropGuard<'a, K, V>(&'a mut IntoIter<K, V>);
14661466

Diff for: library/alloc/src/collections/linked_list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ mod tests;
4343
/// more memory efficient, and make better use of CPU cache.
4444
#[stable(feature = "rust1", since = "1.0.0")]
4545
#[cfg_attr(not(test), rustc_diagnostic_item = "LinkedList")]
46+
#[rustc_insignificant_dtor]
4647
pub struct LinkedList<T> {
4748
head: Option<NonNull<Node<T>>>,
4849
tail: Option<NonNull<Node<T>>>,
@@ -975,7 +976,6 @@ impl<T> LinkedList<T> {
975976

976977
#[stable(feature = "rust1", since = "1.0.0")]
977978
unsafe impl<#[may_dangle] T> Drop for LinkedList<T> {
978-
#[rustc_insignificant_dtor]
979979
fn drop(&mut self) {
980980
struct DropGuard<'a, T>(&'a mut LinkedList<T>);
981981

Diff for: library/alloc/src/collections/vec_deque/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ const MAXIMUM_ZST_CAPACITY: usize = 1 << (usize::BITS - 1); // Largest possible
9090
/// [`make_contiguous`]: VecDeque::make_contiguous
9191
#[cfg_attr(not(test), rustc_diagnostic_item = "vecdeque_type")]
9292
#[stable(feature = "rust1", since = "1.0.0")]
93+
#[rustc_insignificant_dtor]
9394
pub struct VecDeque<
9495
T,
9596
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
@@ -130,7 +131,6 @@ impl<T: Clone, A: Allocator + Clone> Clone for VecDeque<T, A> {
130131

131132
#[stable(feature = "rust1", since = "1.0.0")]
132133
unsafe impl<#[may_dangle] T, A: Allocator> Drop for VecDeque<T, A> {
133-
#[rustc_insignificant_dtor]
134134
fn drop(&mut self) {
135135
/// Runs the destructor for all items in the slice when it gets dropped (normally or
136136
/// during unwinding).

Diff for: library/alloc/src/rc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ struct RcBox<T: ?Sized> {
305305
/// [get_mut]: Rc::get_mut
306306
#[cfg_attr(not(test), rustc_diagnostic_item = "Rc")]
307307
#[stable(feature = "rust1", since = "1.0.0")]
308+
#[rustc_insignificant_dtor]
308309
pub struct Rc<T: ?Sized> {
309310
ptr: NonNull<RcBox<T>>,
310311
phantom: PhantomData<RcBox<T>>,
@@ -1441,7 +1442,6 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Rc<T> {
14411442
/// drop(foo); // Doesn't print anything
14421443
/// drop(foo2); // Prints "dropped!"
14431444
/// ```
1444-
#[rustc_insignificant_dtor]
14451445
fn drop(&mut self) {
14461446
unsafe {
14471447
self.inner().dec_strong();

Diff for: library/alloc/src/vec/into_iter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use core::slice::{self};
2222
/// let iter: std::vec::IntoIter<_> = v.into_iter();
2323
/// ```
2424
#[stable(feature = "rust1", since = "1.0.0")]
25+
#[rustc_insignificant_dtor]
2526
pub struct IntoIter<
2627
T,
2728
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
@@ -246,7 +247,6 @@ impl<T: Clone, A: Allocator + Clone> Clone for IntoIter<T, A> {
246247

247248
#[stable(feature = "rust1", since = "1.0.0")]
248249
unsafe impl<#[may_dangle] T, A: Allocator> Drop for IntoIter<T, A> {
249-
#[rustc_insignificant_dtor]
250250
fn drop(&mut self) {
251251
struct DropGuard<'a, T, A: Allocator>(&'a mut IntoIter<T, A>);
252252

Diff for: library/alloc/src/vec/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ mod spec_extend;
394394
/// [owned slice]: Box
395395
#[stable(feature = "rust1", since = "1.0.0")]
396396
#[cfg_attr(not(test), rustc_diagnostic_item = "vec_type")]
397+
#[rustc_insignificant_dtor]
397398
pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> {
398399
buf: RawVec<T, A>,
399400
len: usize,
@@ -2746,7 +2747,6 @@ impl<T: Ord, A: Allocator> Ord for Vec<T, A> {
27462747

27472748
#[stable(feature = "rust1", since = "1.0.0")]
27482749
unsafe impl<#[may_dangle] T, A: Allocator> Drop for Vec<T, A> {
2749-
#[rustc_insignificant_dtor]
27502750
fn drop(&mut self) {
27512751
unsafe {
27522752
// use drop for [T]

Diff for: library/core/src/array/iter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{
1010

1111
/// A by-value [array] iterator.
1212
#[stable(feature = "array_value_iter", since = "1.51.0")]
13+
#[rustc_insignificant_dtor]
1314
pub struct IntoIter<T, const N: usize> {
1415
/// This is the array we are iterating over.
1516
///
@@ -180,7 +181,6 @@ impl<T, const N: usize> DoubleEndedIterator for IntoIter<T, N> {
180181

181182
#[stable(feature = "array_value_iter_impls", since = "1.40.0")]
182183
impl<T, const N: usize> Drop for IntoIter<T, N> {
183-
#[rustc_insignificant_dtor]
184184
fn drop(&mut self) {
185185
// SAFETY: This is safe: `as_mut_slice` returns exactly the sub-slice
186186
// of elements that have not been moved out yet and that remain

Diff for: library/std/src/collections/hash/map.rs

+1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ use crate::sys;
205205
206206
#[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_type")]
207207
#[stable(feature = "rust1", since = "1.0.0")]
208+
#[rustc_insignificant_dtor]
208209
pub struct HashMap<K, V, S = RandomState> {
209210
base: base::HashMap<K, V, S>,
210211
}

Diff for: library/std/src/lazy.rs

-1
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,6 @@ impl<T> SyncOnceCell<T> {
492492
}
493493

494494
unsafe impl<#[may_dangle] T> Drop for SyncOnceCell<T> {
495-
#[rustc_insignificant_dtor]
496495
fn drop(&mut self) {
497496
if self.is_initialized() {
498497
// SAFETY: The cell is initialized and being dropped, so it can't

Diff for: src/test/ui/closures/2229_closure_analysis/migrations/insignificant_drop.fixed

+2-6
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@
44
#![deny(rust_2021_incompatible_closure_captures)]
55
#![allow(unused)]
66

7-
#![feature(once_cell)]
8-
97
// Test cases for types that implement an insignificant drop (stlib defined)
108

119
macro_rules! test_insig_dtor_for_type {
1210
($t: ty, $disambiguator: ident) => {
1311
mod $disambiguator {
1412
use std::collections::*;
15-
use std::lazy::SyncOnceCell;
1613
use std::rc::Rc;
1714
use std::sync::Mutex;
1815

19-
fn _test_for_type(t: $t) {
16+
fn test_for_type(t: $t) {
2017
let tup = (Mutex::new(0), t);
2118

2219
let _c = || tup.0;
@@ -29,12 +26,11 @@ test_insig_dtor_for_type!(i32, prim_i32);
2926
test_insig_dtor_for_type!(Vec<i32>, vec_i32);
3027
test_insig_dtor_for_type!(String, string);
3128
test_insig_dtor_for_type!(Vec<String>, vec_string);
32-
//test_insig_dtor_for_type!(HashMap<String, String>, hash_map);
29+
test_insig_dtor_for_type!(HashMap<String, String>, hash_map);
3330
test_insig_dtor_for_type!(BTreeMap<String, i32>, btree_map);
3431
test_insig_dtor_for_type!(LinkedList<String>, linked_list);
3532
test_insig_dtor_for_type!(Rc<i32>, rc_i32);
3633
test_insig_dtor_for_type!(Rc<String>, rc_string);
37-
test_insig_dtor_for_type!(SyncOnceCell<String>, onecell);
3834
test_insig_dtor_for_type!(std::vec::IntoIter<String>, vec_into_iter);
3935
test_insig_dtor_for_type!(btree_map::IntoIter<String, String>, btree_map_into_iter);
4036
test_insig_dtor_for_type!(std::array::IntoIter<String, 5>, array_into_iter);

Diff for: src/test/ui/closures/2229_closure_analysis/migrations/insignificant_drop.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@
44
#![deny(rust_2021_incompatible_closure_captures)]
55
#![allow(unused)]
66

7-
#![feature(once_cell)]
8-
97
// Test cases for types that implement an insignificant drop (stlib defined)
108

119
macro_rules! test_insig_dtor_for_type {
1210
($t: ty, $disambiguator: ident) => {
1311
mod $disambiguator {
1412
use std::collections::*;
15-
use std::lazy::SyncOnceCell;
1613
use std::rc::Rc;
1714
use std::sync::Mutex;
1815

19-
fn _test_for_type(t: $t) {
16+
fn test_for_type(t: $t) {
2017
let tup = (Mutex::new(0), t);
2118

2219
let _c = || tup.0;
@@ -29,12 +26,11 @@ test_insig_dtor_for_type!(i32, prim_i32);
2926
test_insig_dtor_for_type!(Vec<i32>, vec_i32);
3027
test_insig_dtor_for_type!(String, string);
3128
test_insig_dtor_for_type!(Vec<String>, vec_string);
32-
//test_insig_dtor_for_type!(HashMap<String, String>, hash_map);
29+
test_insig_dtor_for_type!(HashMap<String, String>, hash_map);
3330
test_insig_dtor_for_type!(BTreeMap<String, i32>, btree_map);
3431
test_insig_dtor_for_type!(LinkedList<String>, linked_list);
3532
test_insig_dtor_for_type!(Rc<i32>, rc_i32);
3633
test_insig_dtor_for_type!(Rc<String>, rc_string);
37-
test_insig_dtor_for_type!(SyncOnceCell<String>, onecell);
3834
test_insig_dtor_for_type!(std::vec::IntoIter<String>, vec_into_iter);
3935
test_insig_dtor_for_type!(btree_map::IntoIter<String, String>, btree_map_into_iter);
4036
test_insig_dtor_for_type!(std::array::IntoIter<String, 5>, array_into_iter);

Diff for: src/test/ui/closures/2229_closure_analysis/migrations/insignificant_drop_attr_migrations.fixed

+4-15
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
#![feature(rustc_attrs)]
66
#![allow(unused)]
77

8-
use std::sync::Mutex;
8+
use std::sync::Mutex;
99

10+
#[rustc_insignificant_dtor]
1011
struct InsignificantDropPoint {
1112
x: i32,
1213
y: Mutex<i32>,
1314
}
1415

1516
impl Drop for InsignificantDropPoint {
16-
#[rustc_insignificant_dtor]
1717
fn drop(&mut self) {}
1818
}
1919

@@ -23,25 +23,14 @@ impl Drop for SigDrop {
2323
fn drop(&mut self) {}
2424
}
2525

26+
#[rustc_insignificant_dtor]
2627
struct GenericStruct<T>(T, T);
2728

28-
struct Wrapper<T>(GenericStruct<T>, i32);
29-
3029
impl<T> Drop for GenericStruct<T> {
31-
#[rustc_insignificant_dtor]
3230
fn drop(&mut self) {}
3331
}
3432

35-
// Test no migration because InsignificantDropPoint is marked as insignificant
36-
fn insign_dtor() {
37-
let t = (
38-
InsignificantDropPoint { x: 0, y: Mutex::new(0) },
39-
InsignificantDropPoint { x: 0, y: Mutex::new(0) }
40-
);
41-
42-
let c = || t.0;
43-
44-
}
33+
struct Wrapper<T>(GenericStruct<T>, i32);
4534

4635
// `SigDrop` implements drop and therefore needs to be migrated.
4736
fn significant_drop_needs_migration() {

Diff for: src/test/ui/closures/2229_closure_analysis/migrations/insignificant_drop_attr_migrations.rs

+4-15
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
#![feature(rustc_attrs)]
66
#![allow(unused)]
77

8-
use std::sync::Mutex;
8+
use std::sync::Mutex;
99

10+
#[rustc_insignificant_dtor]
1011
struct InsignificantDropPoint {
1112
x: i32,
1213
y: Mutex<i32>,
1314
}
1415

1516
impl Drop for InsignificantDropPoint {
16-
#[rustc_insignificant_dtor]
1717
fn drop(&mut self) {}
1818
}
1919

@@ -23,25 +23,14 @@ impl Drop for SigDrop {
2323
fn drop(&mut self) {}
2424
}
2525

26+
#[rustc_insignificant_dtor]
2627
struct GenericStruct<T>(T, T);
2728

28-
struct Wrapper<T>(GenericStruct<T>, i32);
29-
3029
impl<T> Drop for GenericStruct<T> {
31-
#[rustc_insignificant_dtor]
3230
fn drop(&mut self) {}
3331
}
3432

35-
// Test no migration because InsignificantDropPoint is marked as insignificant
36-
fn insign_dtor() {
37-
let t = (
38-
InsignificantDropPoint { x: 0, y: Mutex::new(0) },
39-
InsignificantDropPoint { x: 0, y: Mutex::new(0) }
40-
);
41-
42-
let c = || t.0;
43-
44-
}
33+
struct Wrapper<T>(GenericStruct<T>, i32);
4534

4635
// `SigDrop` implements drop and therefore needs to be migrated.
4736
fn significant_drop_needs_migration() {

Diff for: src/test/ui/closures/2229_closure_analysis/migrations/insignificant_drop_attr_migrations.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: changes to closure capture in Rust 2021 will affect drop order
2-
--> $DIR/insignificant_drop_attr_migrations.rs:50:13
2+
--> $DIR/insignificant_drop_attr_migrations.rs:39:13
33
|
44
LL | let c = || {
55
| ^^
@@ -23,7 +23,7 @@ LL + let _ = &t;
2323
|
2424

2525
error: changes to closure capture in Rust 2021 will affect drop order
26-
--> $DIR/insignificant_drop_attr_migrations.rs:70:13
26+
--> $DIR/insignificant_drop_attr_migrations.rs:59:13
2727
|
2828
LL | let c = move || {
2929
| ^^^^^^^

Diff for: src/test/ui/closures/2229_closure_analysis/migrations/insignificant_drop_attr_no_migrations.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
#![deny(rust_2021_incompatible_closure_captures)]
44
#![feature(rustc_attrs)]
55
#![allow(unused)]
6+
#[rustc_insignificant_dtor]
67

78
struct InsignificantDropPoint {
89
x: i32,
910
y: i32,
1011
}
1112

1213
impl Drop for InsignificantDropPoint {
13-
#[rustc_insignificant_dtor]
1414
fn drop(&mut self) {}
1515
}
1616

Diff for: src/test/ui/closures/2229_closure_analysis/migrations/significant_drop.fixed

+14
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,19 @@ fn test9_drop_order_and_nested_closures() {
202202
b();
203203
}
204204

205+
// Test that we migrate if drop order of Vec<T> would be affected if T is a significant drop type
206+
fn test10_vec_of_significant_drop_type() {
207+
208+
let tup = (Foo(0), vec![Foo(3)]);
209+
210+
let _c = || { let _ = &tup; tup.0 };
211+
//~^ ERROR: drop order
212+
//~| NOTE: for more information, see
213+
//~| HELP: add a dummy let to cause `tup` to be fully captured
214+
//~| NOTE: in Rust 2018, this closure captures all of `tup`, but in Rust 2021, it will only capture `tup.0`
215+
}
216+
//~^ NOTE: in Rust 2018, `tup` is dropped here, but in Rust 2021, only `tup.0` will be dropped here as part of the closure
217+
205218
fn main() {
206219
test1_all_need_migration();
207220
test2_only_precise_paths_need_migration();
@@ -212,4 +225,5 @@ fn main() {
212225
test7_move_closures_non_copy_types_might_need_migration();
213226
test8_drop_order_and_blocks();
214227
test9_drop_order_and_nested_closures();
228+
test10_vec_of_significant_drop_type();
215229
}

0 commit comments

Comments
 (0)