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

thread '<unnamed>' panicked at 'index out of bounds: the len is 1 but the index is 1', crates/hir/src/semantics.rs:934:36 #11298

Closed
f32by opened this issue Jan 16, 2022 · 3 comments
Labels
A-macro macro expansion Broken Window Bugs / technical debt to be addressed immediately C-bug Category: bug

Comments

@f32by
Copy link

f32by commented Jan 16, 2022

rust-analyzer version: 7a52f83 2022-01-16 nightly

rustc version: rustc 1.60.0-nightly (ec4bcaac4 2022-01-15)

What's happening: VSCode keeps saying A request has failed. See the output for more information. when scrolling or triggering highlight in a file containg some code snippet.

Code to reproduce this:

#[cfg_attr(feature = "whatever", repr(C))]
#[derive(Clone)]
pub struct Test;

Backtrace:

[Error - 7:08:07 PM] Request textDocument/semanticTokens/full failed.
  Message: server panicked: index out of bounds: the len is 1 but the index is 1
  Code: -32603 
Panic context:
> 
version: 7a52f8370 2022-01-16 nightly
request: textDocument/semanticTokens/range SemanticTokensRangeParams {
    work_done_progress_params: WorkDoneProgressParams {
        work_done_token: None,
    },
    partial_result_params: PartialResultParams {
        partial_result_token: None,
    },
    text_document: TextDocumentIdentifier {
        uri: Url {
            scheme: "file",
            cannot_be_a_base: false,
            username: "",
            password: None,
            host: None,
            port: None,
            path: "************",
            query: None,
            fragment: None,
        },
    },
    range: Range {
        start: Position {
            line: 0,
            character: 0,
        },
        end: Position {
            line: 7,
            character: 0,
        },
    },
}

thread '<unnamed>' panicked at 'index out of bounds: the len is 1 but the index is 1', crates/hir/src/semantics.rs:934:36
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic_bounds_check
   3: hir::semantics::SemanticsImpl::resolve_derive_ident
   4: ide::syntax_highlighting::highlight::token
   5: ide::syntax_highlighting::highlight
   6: std::panicking::try
   7: rust_analyzer::handlers::handle_semantic_tokens_range
   8: std::panicking::try
   9: <F as threadpool::FnBox>::call_box
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Information maybe useful:

This panic only happens when #[cfg_attr] is placed in front of a #[derive]. If the order is reversed then everything works. I also found a colorized tokenization difference:

  1. #[derive] is placed in front of #[cfg_attr]. Everything works and VSCode's Inspect editor tokens and scopes works properly.
    work

  2. #[cfg_attr] is placed in front of #[derive]. This will cause a panic and Inspect editor tokens and scopes does not work either.
    notwork
    CleanShot 2022-01-16 at 19 19 56

@Veykril
Copy link
Member

Veykril commented Jan 16, 2022

Ah, ye this seems to happen with cfg_attrs that have their feature flag not set, that brings indices out of order in a specific code path I imagine
https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/hir/src/semantics.rs#L934 is the panic location

@Veykril
Copy link
Member

Veykril commented Jan 16, 2022

Ye the problem is this here https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/hir_def/src/attr.rs#L87-L93
we index via the ast_index into the attribute slice, but the attribute slice doesn't contain the attributes behind a cfg_attr if they a disabled obviously

@Veykril Veykril added A-macro macro expansion Broken Window Bugs / technical debt to be addressed immediately C-bug Category: bug labels Jan 16, 2022
bors bot added a commit that referenced this issue Jan 23, 2022
11334: fix: don't panic in semantics due to `cfg_attr` disrupting offsets r=Veykril a=Veykril

Reduces the panic in #11298 to an early return, that means we won't resolve these cases again for now, but this is better than constantly panicking in highlighting and hovering.
bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Jan 24, 2022
2022-01-24

11334: fix: don't panic in semantics due to `cfg_attr` disrupting
offsets

Reduces the panic in
rust-lang/rust-analyzer#11298 to an
early return, that means we won't resolve these cases again for
now, but this is better than constantly panicking in highlighting
and hovering.

2022-01-17

11287: fix: rust-analyzer spamming overly error message when
workspace not being loaded

Fixes #10120
@jonas-schievink
Copy link
Contributor

The panic was fixed by #11334

bors bot added a commit that referenced this issue Jan 30, 2022
11382: fix: Fix `cfg_attr` invalidating derive identifier IDE functionalities r=Veykril a=Veykril

Proper fix for #11298
bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
bors bot added a commit that referenced this issue Jan 30, 2022
11382: fix: Fix `cfg_attr` invalidating derive identifier IDE functionalities r=Veykril a=Veykril

Proper fix for #11298
bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
bors bot added a commit that referenced this issue Jan 30, 2022
11382: fix: Fix `cfg_attr` invalidating derive identifier IDE functionalities r=Veykril a=Veykril

Proper fix for #11298
bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Feb 6, 2022
2022-01-31

fix: Fix `cfg_attr` invalidating derive identifier IDE functionalities r=Veykril a=Veykril

Proper fix for rust-lang/rust-analyzer#11298
bors r+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macro macro expansion Broken Window Bugs / technical debt to be addressed immediately C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

3 participants