forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
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#113902 - Enselic:lint-recursive-drop, r=oli-obk
Make `unconditional_recursion` warning detect recursive drops Closes rust-lang#55388 Also closes rust-lang#50049 unless we want to keep it for the second example which this PR does not solve, but I think it is better to track that work in rust-lang#57965. r? `@oli-obk` since you are the mentor for rust-lang#55388 Unresolved questions: - [x] There are two false positives that must be fixed before merging (see diff). I suspect the best way to solve them is to perform analysis after drop elaboration instead of before, as now, but I have not explored that any further yet. Could that be an option? **Answer:** Yes, that solved the problem. `@rustbot` label +T-compiler +C-enhancement +A-lint
- Loading branch information
Showing
7 changed files
with
157 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Because drop recursion can only be detected after drop elaboration which | ||
// happens for codegen: | ||
// build-fail | ||
|
||
#![deny(unconditional_recursion)] | ||
#![allow(dead_code)] | ||
|
||
pub struct RecursiveDrop; | ||
|
||
impl Drop for RecursiveDrop { | ||
fn drop(&mut self) { //~ ERROR function cannot return without recursing | ||
let _ = RecursiveDrop; | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
struct NotRecursiveDrop1; | ||
|
||
impl Drop for NotRecursiveDrop1 { | ||
fn drop(&mut self) { | ||
// Before drop elaboration, the MIR can look like a recursive drop will | ||
// occur. But it will not, since forget() prevents drop() from running. | ||
let taken = std::mem::take(self); | ||
std::mem::forget(taken); | ||
} | ||
} | ||
|
||
struct NotRecursiveDrop2; | ||
|
||
impl Drop for NotRecursiveDrop2 { | ||
fn drop(&mut self) { | ||
// Before drop elaboration, the MIR can look like a recursive drop will | ||
// occur. But it will not, since this will panic. | ||
std::panic::panic_any(NotRecursiveDrop2); | ||
} | ||
} | ||
|
||
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,17 @@ | ||
error: function cannot return without recursing | ||
--> $DIR/lint-unconditional-drop-recursion.rs:11:5 | ||
| | ||
LL | fn drop(&mut self) { | ||
| ^^^^^^^^^^^^^^^^^^ cannot return without recursing | ||
LL | let _ = RecursiveDrop; | ||
| - recursive call site | ||
| | ||
= help: a `loop` may express intention better if this is on purpose | ||
note: the lint level is defined here | ||
--> $DIR/lint-unconditional-drop-recursion.rs:5:9 | ||
| | ||
LL | #![deny(unconditional_recursion)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|