forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#84152 - sexxi-goose:insignificant_dtor, r=nik…
…omatsakis Insignificant destructors rfc 2229 - Adds new attribute `rustc_insignificant_dtor` to annotate the drop method. - Adds a query to check if a type has a significant drop. - Updates closure analysis to check for significant drops rather than just drop. A type marked with the attribute `rustc_insignificant_dtor` is considered to not be significant. A drop is significant if it is implemented by the user or does anything that will have any observable behavior (other than freeing up memory). rust-lang/project-rfc-2229#35
- Loading branch information
Showing
11 changed files
with
332 additions
and
12 deletions.
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
67 changes: 67 additions & 0 deletions
67
...est/ui/closures/2229_closure_analysis/migrations/insignificant_drop_attr_migrations.fixed
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,67 @@ | ||
// run-rustfix | ||
|
||
#![deny(disjoint_capture_migration)] | ||
//~^ NOTE: the lint level is defined here | ||
|
||
#![feature(rustc_attrs)] | ||
#![allow(unused)] | ||
|
||
struct InsignificantDropPoint { | ||
x: i32, | ||
y: i32, | ||
} | ||
|
||
impl Drop for InsignificantDropPoint { | ||
#[rustc_insignificant_dtor] | ||
fn drop(&mut self) {} | ||
} | ||
|
||
struct SigDrop; | ||
|
||
impl Drop for SigDrop { | ||
fn drop(&mut self) {} | ||
} | ||
|
||
struct GenericStruct<T>(T, T); | ||
|
||
struct Wrapper<T>(GenericStruct<T>, i32); | ||
|
||
impl<T> Drop for GenericStruct<T> { | ||
#[rustc_insignificant_dtor] | ||
fn drop(&mut self) {} | ||
} | ||
|
||
// `SigDrop` implements drop and therefore needs to be migrated. | ||
fn significant_drop_needs_migration() { | ||
let t = (SigDrop {}, SigDrop {}); | ||
|
||
let c = || { let _ = &t; | ||
//~^ ERROR: drop order affected for closure because of `capture_disjoint_fields` | ||
//~| HELP: add a dummy let to cause `t` to be fully captured | ||
let _t = t.0; | ||
}; | ||
|
||
c(); | ||
} | ||
|
||
// Even if a type implements an insignificant drop, if it's | ||
// elements have a significant drop then the overall type is | ||
// consdered to have an significant drop. Since the elements | ||
// of `GenericStruct` implement drop, migration is required. | ||
fn generic_struct_with_significant_drop_needs_migration() { | ||
let t = Wrapper(GenericStruct(SigDrop {}, SigDrop {}), 5); | ||
|
||
// move is used to force i32 to be copied instead of being a ref | ||
let c = move || { let _ = &t; | ||
//~^ ERROR: drop order affected for closure because of `capture_disjoint_fields` | ||
//~| HELP: add a dummy let to cause `t` to be fully captured | ||
let _t = t.1; | ||
}; | ||
|
||
c(); | ||
} | ||
|
||
fn main() { | ||
significant_drop_needs_migration(); | ||
generic_struct_with_significant_drop_needs_migration(); | ||
} |
67 changes: 67 additions & 0 deletions
67
src/test/ui/closures/2229_closure_analysis/migrations/insignificant_drop_attr_migrations.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,67 @@ | ||
// run-rustfix | ||
|
||
#![deny(disjoint_capture_migration)] | ||
//~^ NOTE: the lint level is defined here | ||
|
||
#![feature(rustc_attrs)] | ||
#![allow(unused)] | ||
|
||
struct InsignificantDropPoint { | ||
x: i32, | ||
y: i32, | ||
} | ||
|
||
impl Drop for InsignificantDropPoint { | ||
#[rustc_insignificant_dtor] | ||
fn drop(&mut self) {} | ||
} | ||
|
||
struct SigDrop; | ||
|
||
impl Drop for SigDrop { | ||
fn drop(&mut self) {} | ||
} | ||
|
||
struct GenericStruct<T>(T, T); | ||
|
||
struct Wrapper<T>(GenericStruct<T>, i32); | ||
|
||
impl<T> Drop for GenericStruct<T> { | ||
#[rustc_insignificant_dtor] | ||
fn drop(&mut self) {} | ||
} | ||
|
||
// `SigDrop` implements drop and therefore needs to be migrated. | ||
fn significant_drop_needs_migration() { | ||
let t = (SigDrop {}, SigDrop {}); | ||
|
||
let c = || { | ||
//~^ ERROR: drop order affected for closure because of `capture_disjoint_fields` | ||
//~| HELP: add a dummy let to cause `t` to be fully captured | ||
let _t = t.0; | ||
}; | ||
|
||
c(); | ||
} | ||
|
||
// Even if a type implements an insignificant drop, if it's | ||
// elements have a significant drop then the overall type is | ||
// consdered to have an significant drop. Since the elements | ||
// of `GenericStruct` implement drop, migration is required. | ||
fn generic_struct_with_significant_drop_needs_migration() { | ||
let t = Wrapper(GenericStruct(SigDrop {}, SigDrop {}), 5); | ||
|
||
// move is used to force i32 to be copied instead of being a ref | ||
let c = move || { | ||
//~^ ERROR: drop order affected for closure because of `capture_disjoint_fields` | ||
//~| HELP: add a dummy let to cause `t` to be fully captured | ||
let _t = t.1; | ||
}; | ||
|
||
c(); | ||
} | ||
|
||
fn main() { | ||
significant_drop_needs_migration(); | ||
generic_struct_with_significant_drop_needs_migration(); | ||
} |
Oops, something went wrong.