Skip to content

Stack overflow in compiler involving closures #22638

Closed
@frewsxcv

Description

@frewsxcv

@jimtla pasted his code he was working with on IRC and I simplified it a bit:

#[derive(Clone)]
struct A (B);

impl A {
    pub fn matches<F: Fn()>(&self, f: &F) {
        let &A(ref term) = self;
        term.matches(f);
    }
}

#[derive(Clone)]
enum B {
    Variant1,
    Variant2(C),
}

impl B {
    pub fn matches<F: Fn()>(&self, f: &F) {
        match self {
            &B::Variant2(ref factor) => {
                factor.matches(&|| ())
            }
            _ => unreachable!("")
        }
    }
}

#[derive(Clone)]
struct C (D);

impl C {
    pub fn matches<F: Fn()>(&self, f: &F) {
        let &C(ref base) = self;
        base.matches(&|| {
            C(base.clone()).matches(f)
        })
    }
}

#[derive(Clone)]
struct D (Box<A>);

impl D {
    pub fn matches<F: Fn()>(&self, f: &F) {
        let &D(ref a) = self;
        a.matches(f)
    }
}

pub fn matches() {
    A(B::Variant1).matches(&(|| ()))
}

fn main() {}
/t/m/h/src (master|…) $ rustc lib.rs
lib.rs:18:36: 18:37 warning: unused variable: `f`, #[warn(unused_variables)] on by default
lib.rs:18     pub fn matches<F: Fn()>(&self, f: &F) {
                                             ^

thread 'rustc' has overflowed its stack
fish: Job 1, 'rustc lib.rs ' terminated by signal SIGILL (Illegal instruction)

This might be a duplicate of #21410

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions