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

analysis-stats hangs with fixed-point combinator #9813

Open
atomicky opened this issue Aug 8, 2021 · 2 comments
Open

analysis-stats hangs with fixed-point combinator #9813

atomicky opened this issue Aug 8, 2021 · 2 comments
Labels
A-ty type system / type inference / traits / method resolution S-unactionable Issue requires feedback, design decisions or is blocked on other work

Comments

@atomicky
Copy link

atomicky commented Aug 8, 2021

rust-analyzer (installed by VS Code plugin) hangs and rustfmt takes too long (infinite?) time if I add the following code:

#[inline]
pub fn fix<A, B, F>(f: &F) -> impl '_ + Fn(A) -> B
where
    F: Fn(&dyn Fn(A) -> B, A) -> B,
{
    #[inline]
    move |x| f(&fix(f), x)
}

#[inline]
pub fn get_fact() -> impl 'static + Fn(u16) -> u64 {
    fix(&|f, n| {
        if n < 2 {
            1u64
        } else {
            n as u64 * f(n - 1)
        }
    })
}

There is no problem with rust-analyzer (except for #8917) if I comment out this code and re-open VS Code.

In this code, I define fixed-point combinator and use it to implement a closure that calculate factorial. The code compiles and no problem occur at execution (see https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=82edb19f2e437ab6ff5a8045c4ab0593). Therefore, the code is not in itself a bad one. Maybe cyclic definition problem or something (not with rustc but only with rust-analyzer) happens, but I'm not sure.

The hanging process name was rust-analyzer-x86_64-unknown-linux-gnu. I also checked that ~/.config/Code/User/globalStorage/matklad.rust-analyzer/rust-analyzer-x86_64-unknown-linux-gnu analysis-stats PATH/TO/project_dir also hangs while processing get_fact:

$ ~/.config/Code/User/globalStorage/matklad.rust-analyzer/rust-analyzer-x86_64-unknown-linux-gnu analysis-stats .
Failed to create perf counter: Operation not permitted (os error 1)
Database loaded:     500.93ms
Failed to create perf counter: Operation not permitted (os error 1)
  crates: 1, mods: 1, decls: 3, fns: 3
Item Collection:     1.26s
Failed to create perf counter: Operation not permitted (os error 1)
1/3 33% processing: get_fact^C

I use following version with VS Code plugin on Kubuntu 20.04:

$ ls ~/.config/Code/User/globalStorage/matklad.rust-analyzer
rust-analyzer-linux  rust-analyzer-x86_64-unknown-linux-gnu
$ ~/.config/Code/User/globalStorage/matklad.rust-analyzer/rust-analyzer-x86_64-unknown-linux-gnu --version
rust-analyzer df0936b4a 2021-08-02 stable
$ ~/.config/Code/User/globalStorage/matklad.rust-analyzer/rust-analyzer-linux --version
rust-analyzer ac30710
@flodiebold
Copy link
Member

Looks like it might be another case of rust-lang/chalk#688.

@atomicky
Copy link
Author

You are right. It turns out that this issue is almost same one as #7680. All problems I mentioned above also happen with the following code:

fn f<R>(_: impl Fn(&dyn Fn() -> R) -> R) {}

fn main() {
    f(|_| ());
}

If I use the original code mentioned at that issue (use Box<dyn Fn() -> R> instead of &dyn Fn() -> R), some problems improve:

  • rust-analyzer VS Code plugin and some operation (e.g. format) seems responsive.
  • rust-analyzer process will be terminated when VS Code is closed.

However, analysis-stats command remain hanging and rust-analyzer plugin consume CPU usage with Box<dyn Fn() -> R>.

@Veykril Veykril added A-ty type system / type inference / traits / method resolution S-unactionable Issue requires feedback, design decisions or is blocked on other work labels May 28, 2022
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 S-unactionable Issue requires feedback, design decisions or is blocked on other work
Projects
None yet
Development

No branches or pull requests

3 participants