Skip to content
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

Detect unconditional recursion in Drop #50049

Closed
omarabid opened this issue Apr 18, 2018 · 2 comments · Fixed by #113902
Closed

Detect unconditional recursion in Drop #50049

omarabid opened this issue Apr 18, 2018 · 2 comments · Fixed by #113902
Labels
A-destructors Area: Destructors (`Drop`, …) A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@omarabid
Copy link

omarabid commented Apr 18, 2018

If you instantiate the same struct in the Drop function, the program will loop indefinitely and crash.

The Code

struct ToDrop;

impl Drop for ToDrop {
    fn drop(&mut self) {
        let m = ToDrop;
        println!( "ToDrop is being dropped" );
    }
}
fn main() {
    let x =  ToDrop;
}

Well, I expected the compiler to catch this at compilation. So this could be an area of improvement.

Here is another variation that also causes a stackoverflow without intializing the same variable

struct A;
struct B;

impl Drop for A {
    fn drop( &mut self ) {
        let b = B;
    }
}

impl Drop for B {
    fn drop( &mut self ) {
        let a = A;
    }
}

fn main() {
    let a = A;
}

The error message at execution

thread 'main' has overflowed its stack
fatal runtime error: stack overflow
Abort trap: 6
@oli-obk
Copy link
Contributor

oli-obk commented Apr 19, 2018

This might be better suited as a clippy lint

@Mark-Simulacrum Mark-Simulacrum added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. labels May 29, 2018
@jonas-schievink
Copy link
Contributor

#55388 is a duplicate of this, but it has more information including mentoring instructions, so I'm kind of inclined to close this older issue instead

@jonas-schievink jonas-schievink added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-destructors Area: Destructors (`Drop`, …) labels Mar 23, 2020
@bors bors closed this as completed in 84ec263 Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-destructors Area: Destructors (`Drop`, …) A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants