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

Stack overflow regression #67082

Closed
KamilaBorowska opened this issue Dec 6, 2019 · 1 comment
Closed

Stack overflow regression #67082

KamilaBorowska opened this issue Dec 6, 2019 · 1 comment

Comments

@KamilaBorowska
Copy link
Contributor

KamilaBorowska commented Dec 6, 2019

I tried this code (this is #57156 by the way causing regression AGAIN, so it may be a good idea to add this to tests):

use std::cell::Cell;

trait CloneablePredicate<'a, T>: 'a + Fn(T) -> bool {
    fn clone_boxed(&self) -> Box<dyn CloneablePredicate<'a, T, Output = bool>>;
}

impl<'a, T, F> CloneablePredicate<'a, T> for F
where
    F: 'a + Clone + Fn(T) -> bool,
{
    fn clone_boxed(&self) -> Box<dyn CloneablePredicate<'a, T, Output = bool>> {
        Box::new(self.clone())
    }
}

impl<'a, T> Clone for Box<dyn CloneablePredicate<'a, T, Output = bool>>
where
    T: 'a,
{
    fn clone(&self) -> Self {
        self.clone_boxed()
    }
}

fn main() {
    let cell = Cell::new(4);
    let x: Box<dyn CloneablePredicate<i32, Output = bool>> = Box::new(move |x| {
        let minus_one = cell.get() - 1;
        cell.set(minus_one);
        x == minus_one
    });
    let y = x.clone();
    println!("{}", x(3));
    println!("{}", x(3));
    println!("{}", y(3));
    println!("{}", y(3));
}

When running in Rust 1.35, it causes a stack overflow.

$ rustc +1.34.0 ex.rs; ./ex
true
false
true
false
$ rustc +1.35.0 ex.rs; ./ex

thread 'main' has overflowed its stack
fatal runtime error: stack overflow

Meta:

$ rustc +1.34.0 --version --verbose
rustc 1.34.0 (91856ed52 2019-04-10)
binary: rustc
commit-hash: 91856ed52c58aa5ba66a015354d1cc69e9779bdf
commit-date: 2019-04-10
host: x86_64-unknown-linux-gnu
release: 1.34.0
LLVM version: 8.0
$ rustc +1.35.0 --version --verbose
rustc 1.35.0 (3c235d560 2019-05-20)
binary: rustc
commit-hash: 3c235d5600393dfe6c36eeed34042efad8d4f26e
commit-date: 2019-05-20
host: x86_64-unknown-linux-gnu
release: 1.35.0
LLVM version: 8.0
@KamilaBorowska
Copy link
Contributor Author

KamilaBorowska commented Dec 6, 2019

Nevermind, this is a side-effect of introducing Fn implementation for Box<impl Fn<A> + ?Sized>. (**self).clone_boxed() is a viable workaround. Considering how contrived the entire thing is, it's probably fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant