Stream: t-compiler/rust-analyzer

Topic: stack overflows


Jeremy Kolb (Sep 17 2019 at 17:34, on Zulip):

Something changed between yesterday and today. I am now getting a lot of exciting

thread '<unknown>' has overflowed its stack
[Error - 1:33:16 PM] Connection to server got closed. Server will not be restarted.
[Error - 1:33:16 PM] Request textDocument/codeAction failed.

matklad (Sep 17 2019 at 17:38, on Zulip):

Yeah, noticed this as well! Rolled back the latest commit from master just moments ago

Jeremy Kolb (Sep 17 2019 at 17:40, on Zulip):

Did you do a force push? Looks like I have to force the checkout

Jeremy Kolb (Sep 17 2019 at 17:41, on Zulip):

I mean a reset

matklad (Sep 17 2019 at 17:43, on Zulip):

Yeah, I've force pushed

Jeremy Kolb (Sep 17 2019 at 17:46, on Zulip):

That seems to have fixed it

Florian Diebold (Sep 17 2019 at 17:49, on Zulip):

hm, I'll check it

Florian Diebold (Sep 17 2019 at 18:06, on Zulip):

oh...

fn test<T: Trait>() where T::Item: Trait2

we want to lower the types in the where clauses, so we need to select the trait where Item comes from, so we look into the where clauses, so we need to lower the types in the where clauses...

Florian Diebold (Sep 17 2019 at 18:31, on Zulip):

ah... should have read rustc :big_smile: https://github.com/rust-lang/rust/blob/9150f844e2624eb013ec78ca08c1d416e6644026/src/librustc_typeck/astconv.rs#L46

Lukas Wirth (Mar 17 2021 at 22:40, on Zulip):

Since today I've started seeing some random stack overflows every now and then when editing files. Have yet to see a pattern though :confused:
I also seem to get a few HIR display panics like https://github.com/rust-analyzer/rust-analyzer/issues/8077 since yesterday I think

matklad (Mar 18 2021 at 09:09, on Zulip):

https://crates.io/crates/backtrace-on-stack-overflow might be useful

Laurențiu (Mar 18 2021 at 09:10, on Zulip):

I noticed some clippy warnings about some recursive Display impls, but they're probably FPs

Laurențiu (Mar 18 2021 at 09:10, on Zulip):

Also, that was before yesterday

matklad (Mar 18 2021 at 09:53, on Zulip):

Hm, I am seeing a panic in attrs handing, not a stack overflow:

thread '<unnamed>' panicked at 'cannot find `Attr` at index 0', crates/hir_def/src/attr.rs:395:32
stack backtrace:
   0: rust_begin_unwind
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b/library/std/src/panicking.rs:493:5
   1: std::panicking::begin_panic_fmt
             at /rustc/cb75ad5db02783e8b0222fee363c5f63f7e2cf5b/library/std/src/panicking.rs:435:5
   2: hir_def::attr::AttrSourceMap::source_of::{{closure}}
   3: hir_def::attr::AttrSourceMap::source_of
   4: ide::syntax_highlighting::inject::doc_comment
   5: ide::syntax_highlighting::highlight
   6: std::panicking::try
   7: ide::Analysis::highlight
   8: rust_analyzer::handlers::handle_semantic_tokens_full
Lukas Wirth (Mar 18 2021 at 09:55, on Zulip):

That one looks like its on me :sweat_smile:

Lukas Wirth (Mar 18 2021 at 10:07, on Zulip):

Though that panic is weird, I'm not sure why that's happening :confused:

Lukas Wirth (Mar 18 2021 at 10:14, on Zulip):

Okay I can reproduce it reliably i think

//! A simplified AST that only contains items.

mod lower;

This seems to panic :/

Lukas Wirth (Mar 18 2021 at 10:56, on Zulip):

Okay the problem is that the Attribute source mapping doesnt work so easily. Attributes of outline modules can have two different owner nodes. I guess wrapping Attr in an InFile would help here to track where they came from? Would that be an okay approach to fix this?

Actually no this wouldn't help

Lukas Wirth (Mar 18 2021 at 11:58, on Zulip):

On another Note, did completions get slower? I feel like VSCode is showing me only basic text completions quite frequently now

matklad (Mar 18 2021 at 12:55, on Zulip):

it might have, we enabled auto-imports in more cases. Really, we should "somehow" push auto-imports to be instant. I don't know how, but, intuitevely, auto-import suggestion don't change much, so the lookup should be fashayt

Lukas Wirth (Mar 18 2021 at 15:18, on Zulip):

Okay I am actually able to reproduce the stack overflow, given

fn group_label(candidate: &ImportCandidate) -> GroupLabel {
    let name = match candidate {
        ImportCandidate::Path(it) => &it.name,
        ImportCandidate::TraitAssocItem(it) | ImportCandidate::TraitMethod(it) => {
            &it.assoc_item_name
        } // <------
    }
    .text();
    GroupLabel(format!("Qualify {}", name))
}

in handlers/qualify_path.rs as an example
When deleting the annotated } the server completely dies on a stack overflow

Lukas Wirth (Mar 18 2021 at 15:18, on Zulip):

This seems to happen with any match

Lukas Wirth (Mar 18 2021 at 15:20, on Zulip):

Oh no if one doesn't fix the syntax error it just keeps crashing on restart :laughing:

Lukas Wirth (Mar 18 2021 at 15:23, on Zulip):

matklad said:

https://crates.io/crates/backtrace-on-stack-overflow might be useful

Guess I'll try to debug this later with the crate you mentioned

Lukas Wirth (Mar 18 2021 at 15:50, on Zulip):

Okay I don't think I need the backtrace(+ I dont have a linux machine atm so I cant really use it), but if Im guessing this right the problem is expression-statement error recovery.

if let x = x {;

also causes crashes. And it only crashes when a lot of source follows the syntax error which makes me believe it is error recovery causing problems?

matklad (Mar 18 2021 at 15:57, on Zulip):

Syntax error recovery? Yeah, that might be it!

I think the best way to debug this is to paste the offending code into parser_smoke_test https://github.com/rust-analyzer/rust-analyzer/blob/d704750ba982153d92ccff90cf236121641b9da3/crates/syntax/src/tests.rs#L32-L43

Lukas Wirth (Mar 18 2021 at 16:09, on Zulip):

Hm, doesn't stack overflow there :/ guess its not error recovery then, thought then I'm keen to find out what it is instead

Lukas Wirth (Mar 18 2021 at 18:14, on Zulip):

So apparently Declvalidator::validate_item is recursing endlessly

Lukas Wirth (Mar 18 2021 at 18:44, on Zulip):

So Im pretty sure the reason for why this stack overflow is happening now is thanks to local nameres :laughing:

Lukas Wirth (Mar 18 2021 at 18:45, on Zulip):

Though I wonder why this is happening when there is such a syntax error, from the looks of it the syntax tree gets very much malformed and I guess the HIR gets incorrectly constructred therefor

Jonas Schievink [he/him] (Mar 18 2021 at 18:45, on Zulip):

oof

Jonas Schievink [he/him] (Mar 18 2021 at 18:45, on Zulip):

any way to reproduce this?

Lukas Wirth (Mar 18 2021 at 18:45, on Zulip):

very easily

Lukas Wirth (Mar 18 2021 at 18:46, on Zulip):

Putting if let x = x{; here https://github.com/rust-analyzer/rust-analyzer/blob/d0805c6444e06e082465cd1a064c83c0f90faf71/crates/ide_assists/src/handlers/qualify_path.rs#L45

Lukas Wirth (Mar 18 2021 at 18:46, on Zulip):

crashes the server

Lukas Wirth (Mar 18 2021 at 18:47, on Zulip):

and commenting out validate_item prevents the crash

Lukas Wirth (Mar 18 2021 at 18:48, on Zulip):

If I see this right the syntax tree is completely incorrect after this, basically everything that follows the incorrect if let there seems to become the if let nodes child node

Lukas Wirth (Mar 18 2021 at 18:49, on Zulip):

So I imagine the problem is the HIR getting cycles somehow due to this?

Jonas Schievink [he/him] (Mar 18 2021 at 18:49, on Zulip):

could be!

Jonas Schievink [he/him] (Mar 18 2021 at 18:49, on Zulip):

I've seen hangs and crashes related to local nameres when refactoring ItemTree

Lukas Wirth (Mar 18 2021 at 18:59, on Zulip):

Yep so everything after the syntax error becomes a local item to the first function syntax tree wise it turns out :sweat_smile:

Lukas Wirth (Mar 18 2021 at 18:59, on Zulip):
fn qualify_path()  {
    if let x = x {;
}

mod tests {
    use super::*;
}

minimal repro that crashes

Lukas Wirth (Mar 18 2021 at 19:00, on Zulip):

Corresponding RAST, which shows the incorrect nesting https://gist.github.com/Veykril/bfb1f9327c906bc61cd88a6ca914accb

Lukas Wirth (Mar 18 2021 at 19:04, on Zulip):

Okay I can hand this over to you I think Jonas, its not even syntax error related

Lukas Wirth (Mar 18 2021 at 19:04, on Zulip):

(deleted)

Lukas Wirth (Mar 18 2021 at 19:04, on Zulip):

(deleted)

Lukas Wirth (Mar 18 2021 at 19:07, on Zulip):

This crashes :smile:

fn qualify() {
    mod foo {
        use super::*;
    }
}
Lukas Wirth (Mar 18 2021 at 19:07, on Zulip):

(deleted)

Jonas Schievink [he/him] (Mar 18 2021 at 19:09, on Zulip):

It happens when computing diagnostics

#8848 0x00005555560bfc56 in hir_ty::diagnostics::decl_check::DeclValidator::validate_item ()
#8849 0x00005555560bfc56 in hir_ty::diagnostics::decl_check::DeclValidator::validate_item ()
#8850 0x00005555560bfc56 in hir_ty::diagnostics::decl_check::DeclValidator::validate_item ()
#8851 0x00005555560bfc56 in hir_ty::diagnostics::decl_check::DeclValidator::validate_item ()
#8852 0x00005555560bfc56 in hir_ty::diagnostics::decl_check::DeclValidator::validate_item ()
#8853 0x00005555560bfc56 in hir_ty::diagnostics::decl_check::DeclValidator::validate_item ()
#8854 0x00005555560bfc56 in hir_ty::diagnostics::decl_check::DeclValidator::validate_item ()
#8855 0x00005555560bfc56 in hir_ty::diagnostics::decl_check::DeclValidator::validate_item ()
#8856 0x00005555560bfc56 in hir_ty::diagnostics::decl_check::DeclValidator::validate_item ()
#8857 0x00005555560bfc56 in hir_ty::diagnostics::decl_check::DeclValidator::validate_item ()
Jonas Schievink [he/him] (Mar 18 2021 at 19:11, on Zulip):

I think I found the issue

Lukas Wirth (Mar 18 2021 at 19:11, on Zulip):

Ye looks like using importing something from the parent via super inside a module that is inside a function doesn't work too well

Jonas Schievink [he/him] (Mar 18 2021 at 19:12, on Zulip):

we were recursively computing diagnostics for all in-scope functions, not just functions that were defined inside a block scope

Lukas Wirth (Mar 18 2021 at 19:12, on Zulip):

oooh

Lukas Wirth (Mar 18 2021 at 19:12, on Zulip):

Ye that explains it then :big_smile:

Lukas Wirth (Mar 18 2021 at 19:18, on Zulip):

Well that was a rather simple fix :laughing:

Jonas Schievink [he/him] (Mar 18 2021 at 19:19, on Zulip):

yup, love to see it :laughing:

Last update: Jul 26 2021 at 14:00UTC