rust-lang / rust-lang/rust-analyzer

Panic when crate graph is empty

Open
#23,342 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

I can reliably make rust-analyzer panic index out of bounds: the len is 0 but the index is 0 when there's no crate graph loaded.

This typically occurs when there's a discover command configured which doesn't support the current directory, but I imagine you'd hit it when you have a standalone Rust file too.

The indexing error is here:

https://github.com/rust-lang/rust-analyzer/blob/a879a4cbadc3bd3489ac05d529a2843c294c75fc/crates/hir/src/lib.rs#L4471-L4474

So e.g. when you type 1. without a crate graph, r-a panics when it tries to access the crate of i32:

https://github.com/rust-lang/rust-analyzer/blob/a879a4cbadc3bd3489ac05d529a2843c294c75fc/crates/ide-completion/src/context/analysis.rs#L1016

https://github.com/rust-lang/rust-analyzer/blob/a879a4cbadc3bd3489ac05d529a2843c294c75fc/crates/hir/src/lib.rs#L2406

This feels like the API is the wrong shape, and we shouldn't assume that all types have an associated type (I see HasCrate is implemented for Type).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the panic with an empty crate graph while typing 1., then inspect the indexing at crates/hir/src/lib.rs#L4471-L4474 and the callers in crates/ide-completion/src/context/analysis.rs#L1016 and crates/hir/src/lib.rs#L2406. Trace the HasCrate assumption for Type and add a regression test demonstrating the empty-graph case. Done means the scenario no longer panics.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.