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

The Y combinator causes hanging #11063

Open
Nugine opened this issue Dec 20, 2021 · 5 comments
Open

The Y combinator causes hanging #11063

Nugine opened this issue Dec 20, 2021 · 5 comments
Labels
A-ty type system / type inference / traits / method resolution C-bug Category: bug I-hang Issue: The analysis never terminates, due to infinite loops, deadlock, livelock, etc. S-unactionable Issue requires feedback, design decisions or is blocked on other work

Comments

@Nugine
Copy link

Nugine commented Dec 20, 2021

rust-analyzer version: 0add6e9 2021-12-20 dev

rustc version: rustc 1.59.0-nightly (efec54529 2021-12-04)

/// The Y combinator
pub fn y<'a, A, O>(f: impl Fn(&dyn Fn(A) -> O, A) -> O + 'a) -> impl Fn(A) -> O + 'a {
    fn wrap<A, O>(f: &impl Fn(&dyn Fn(A) -> O, A) -> O) -> impl Fn(A) -> O + Copy + '_ {
        move |a| f(&wrap(f), a)
    }
    move |a| wrap(&f)(a)
}

pub fn fib(n: u64) -> u64 {
    y(|f, n| match n {
        0 => 0,
        1 => 1,
        n => f(n - 1) + f(n - 2),
    })(n)
}

pub fn factorial(n: u64) -> u64 {
    y(|f, n| match n {
        0 => 1,
        n => n * f(n - 1),
    })(n)
}
@Veykril Veykril added A-ty type system / type inference / traits / method resolution S-actionable Someone could pick this issue up and work on it right now labels Dec 20, 2021
@flodiebold
Copy link
Member

Possibly rust-lang/chalk#688 again.

@flodiebold flodiebold added the C-bug Category: bug label Dec 20, 2021
@flodiebold
Copy link
Member

Can still reproduce.

@jonas-schievink jonas-schievink added the I-hang Issue: The analysis never terminates, due to infinite loops, deadlock, livelock, etc. label Jan 31, 2023
@lowr lowr added S-unactionable Issue requires feedback, design decisions or is blocked on other work and removed S-actionable Someone could pick this issue up and work on it right now labels Feb 13, 2023
@lowr
Copy link
Contributor

lowr commented Feb 13, 2023

Marking as unactionable as there seems to be nothing we can do in our repo.

Minimal test case:

//- minicore: fn, sized
fn y<P, R, F: FnOnce(P, &dyn FnOnce(P) -> R) -> R>(_: F) {}

fn test() {
    y(|_: usize, _| -> () {});
}

@lbfalvy
Copy link

lbfalvy commented Nov 7, 2023

Is there any workaround? The only way I'd found to rephrase affected types was to wrap the function reference in a tuple struct, but since Fn trait impls aren't stable this generates a bit of syntax clutter that I'd like to avoid.

@lbfalvy
Copy link

lbfalvy commented Nov 7, 2023

This issue is an absolute pain to track down and google in a codebase of any substantial size because the problematic types are pretty much always inferred. Would it be possible to detect this situation and abort with a descriptive message so the user knows what they need to avoid?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ty type system / type inference / traits / method resolution C-bug Category: bug I-hang Issue: The analysis never terminates, due to infinite loops, deadlock, livelock, etc. S-unactionable Issue requires feedback, design decisions or is blocked on other work
Projects
None yet
Development

No branches or pull requests

6 participants