-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Add the #[manually_drop]
attribute.
#101586
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9308e4c
Add the `#[manually_drop]` attribute.
ssbr fb064e3
Replace manually_drop lang item with attribute.
ssbr 8090b31
Improve error message for `#![manually_drop]`
ssbr 8b0f5f8
Fix bad-item tests, which I apparently forgot to do
ssbr 89f4a86
Add an enum variant per review comment
ssbr 8a23f4c
Fix test.
ssbr 8e01e2a
Ah. Move tests into tests dir, and `--bless` them.
ssbr 3468fac
... actually run git add...
ssbr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/doc/unstable-book/src/language-features/manually-drop-attr.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# `manually_drop_attr` | ||
|
||
The tracking issue for this feature is: [#100344] | ||
|
||
[#100344]: https://github.com/rust-lang/rust/issues/100344 | ||
|
||
The `manually_drop_attr` feature enables the `#[manually_drop]` attribute, which disables the drop glue for the type it is applied to. | ||
|
||
For example, `std::mem::ManuallyDrop` is implemented as follows: | ||
|
||
```rs | ||
#[manually_drop] | ||
struct ManuallyDrop<T>(T); | ||
``` | ||
|
||
But you can also use the attribute to change the order in which fields are dropped, by overriding `Drop`: | ||
|
||
```rs | ||
/// This struct changes the order in which `x` and `y` are dropped from the default. | ||
#[manually_drop] | ||
struct MyStruct { | ||
x: String, | ||
y: String, | ||
} | ||
|
||
impl Drop for MyStruct { | ||
fn drop(&mut self) { | ||
unsafe { | ||
std::ptr::drop_in_place(&mut self.y); | ||
std::ptr::drop_in_place(&mut self.x); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
This can be useful in combination with `repr(C)`, to decouple the layout from the destruction order. See MCP [#135](https://github.com/rust-lang/lang-team/issues/135). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,6 +112,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClone { | |
} | ||
|
||
if let ty::Adt(def, _) = arg_ty.kind() { | ||
// FIXME: This is not correct with `#[manually_drop]`, as that is just like any other type. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this should get merged like this, could you open up a Clippy issue describing the possible extension that could be done here, please? |
||
if def.is_manually_drop() { | ||
continue; | ||
} | ||
|
5 changes: 5 additions & 0 deletions
5
tests/ui/manually_drop_attr/feature-gate-manually_drop_attr.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#[manually_drop] | ||
//~^ ERROR the `#[manually_drop]` attribute is an experimental feature | ||
struct Foo {} | ||
|
||
fn main() {} |
12 changes: 12 additions & 0 deletions
12
tests/ui/manually_drop_attr/feature-gate-manually_drop_attr.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0658]: the `#[manually_drop]` attribute is an experimental feature | ||
--> $DIR/feature-gate-manually_drop_attr.rs:1:1 | ||
| | ||
LL | #[manually_drop] | ||
| ^^^^^^^^^^^^^^^^ | ||
| | ||
= note: see issue #100344 <https://github.com/rust-lang/rust/issues/100344> for more information | ||
= help: add `#![feature(manually_drop_attr)]` to the crate attributes to enable | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#![feature(manually_drop_attr)] | ||
#![forbid(unused_attributes)] | ||
#![manually_drop] | ||
//~^ ERROR attribute should be applied to a struct or enum | ||
|
||
#[manually_drop] | ||
//~^ ERROR attribute should be applied to a struct or enum | ||
fn foo() {} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
error: attribute should be applied to a struct or enum | ||
--> $DIR/manually_drop-bad-item.rs:6:1 | ||
| | ||
LL | #[manually_drop] | ||
| ^^^^^^^^^^^^^^^^ | ||
LL | | ||
LL | fn foo() {} | ||
| ----------- not a struct or enum | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/manually_drop-bad-item.rs:2:11 | ||
| | ||
LL | #![forbid(unused_attributes)] | ||
| ^^^^^^^^^^^^^^^^^ | ||
|
||
error: attribute should be applied to a struct or enum | ||
--> $DIR/manually_drop-bad-item.rs:3:1 | ||
| | ||
LL | #![manually_drop] | ||
| ^^^^^^^^^^^^^^^^^ not a struct or enum | ||
|
||
error: aborting due to 2 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//! A test of `#[manually_drop]` on a type that *does* have a `Drop` impl. | ||
//! | ||
//! The mirror image of `manually_drop-nodestructor.rs` | ||
#![feature(manually_drop_attr)] | ||
// run-pass | ||
extern crate core; | ||
use core::cell::Cell; | ||
|
||
struct DropCounter<'a>(&'a Cell<isize>); | ||
impl<'a> Drop for DropCounter<'a> { | ||
fn drop(&mut self) { | ||
self.0.set(self.0.get() + 1); | ||
} | ||
} | ||
|
||
#[manually_drop] | ||
struct ManuallyDropped<'a> { | ||
field_1: DropCounter<'a>, | ||
field_2: DropCounter<'a>, | ||
} | ||
|
||
impl<'a> Drop for ManuallyDropped<'a> { | ||
fn drop(&mut self) { | ||
// just do a LITTLE dropping. | ||
unsafe { | ||
core::ptr::drop_in_place(&mut self.field_1) | ||
} | ||
} | ||
} | ||
|
||
#[manually_drop] | ||
enum ManuallyDroppedEnum<'a> { | ||
_A, | ||
B(DropCounter<'a>, DropCounter<'a>), | ||
} | ||
|
||
impl<'a> Drop for ManuallyDroppedEnum<'a> { | ||
fn drop(&mut self) { | ||
// just do a LITTLE dropping. | ||
if let ManuallyDroppedEnum::B(a, _) = self { | ||
unsafe { | ||
core::ptr::drop_in_place(a); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Dropping a `#[manually_drop]` struct does not implicitly drop its fields. | ||
/// | ||
/// (Though it does run `Drop`, which can choose to drop them explicitly.) | ||
fn test_destruction() { | ||
let counter = Cell::new(0); | ||
core::mem::drop(ManuallyDropped { | ||
field_1: DropCounter(&counter), | ||
field_2: DropCounter(&counter), | ||
}); | ||
// We only run the drop specifically requested in the Drop impl. | ||
assert_eq!(counter.get(), 1); | ||
assert!(core::mem::needs_drop::<ManuallyDropped>()); | ||
|
||
core::mem::drop(ManuallyDroppedEnum::B(DropCounter(&counter), DropCounter(&counter))); | ||
assert_eq!(counter.get(), 2); | ||
assert!(core::mem::needs_drop::<ManuallyDroppedEnum>()); | ||
|
||
} | ||
|
||
/// Assignment does still drop the fields. | ||
fn test_assignment() { | ||
let counter = Cell::new(0); | ||
let mut manually_dropped = ManuallyDropped { | ||
field_1: DropCounter(&counter), | ||
field_2: DropCounter(&counter), | ||
}; | ||
assert_eq!(counter.get(), 0); | ||
manually_dropped.field_1 = DropCounter(&counter); | ||
manually_dropped.field_2 = DropCounter(&counter); | ||
assert_eq!(counter.get(), 2); | ||
} | ||
|
||
fn main() { | ||
test_destruction(); | ||
test_assignment(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//! The drop checker only complains about a `#[manually_drop]` type if it _itself_ defines `Drop`. | ||
|
||
// FIXME: this does test dropck, does it also test needs_drop? | ||
|
||
#![feature(manually_drop_attr)] | ||
|
||
|
||
// For example, this is absolutely fine: | ||
|
||
#[manually_drop] | ||
struct ManuallyDrop<T>(T); | ||
|
||
fn drop_out_of_order_ok<T>(x: T) { | ||
let mut manually_dropped = ManuallyDrop(None); | ||
// x will be dropped before manually_dropped. | ||
let x = x; | ||
// ... but this is still fine, because it doesn't have logic on Drop. | ||
manually_dropped.0 = Some(&x); | ||
} | ||
|
||
// ... but this is not: | ||
|
||
#[manually_drop] | ||
struct ManuallyDropWithDestructor<T>(T); | ||
impl<T> Drop for ManuallyDropWithDestructor<T> { | ||
fn drop(&mut self) { | ||
// maybe we read self.0 here! | ||
} | ||
} | ||
|
||
fn drop_out_of_order_not_ok<T>(x: T) { | ||
let mut manually_dropped_bad = ManuallyDropWithDestructor(None); | ||
let x = x; | ||
manually_dropped_bad.0 = Some(&x); | ||
//~^ ERROR `x` does not live long enough | ||
} | ||
|
||
fn main() {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this instead be changed to check against the lang_item DefId then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could do that, but then we're committing to ManuallyDrop remaining special, right? (And this e.g. prohibits removing the lang item)