Skip to content

Detect unconditional recursion in Drop #50049

Closed
@omarabid

Description

@omarabid

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-destructorsArea: Destructors (`Drop`, …)A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions