rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.38k stars 1.62k forks source link

thread 'Worker' panicked at crates/hir-ty/src/infer/unify.rs:276:33: #18109

Closed nilehmann closed 2 months ago

nilehmann commented 2 months ago

I was editing my code and suddenly started getting the following crash every time I tried to hover over something. I don't know exactly what triggered :S

hread 'Worker' panicked at crates/hir-ty/src/infer/unify.rs:276:33:
index out of bounds: the len is 67 but the index is 67
stack backtrace:
   0: rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic_bounds_check
   3: hir_ty::infer::coerce::<impl hir_ty::infer::unify::InferenceTable>::coerce_inner
   4: hir_ty::infer::coerce::<impl hir_ty::infer::unify::InferenceTable>::coerce
   5: hir_ty::infer::coerce::<impl hir_ty::infer::InferenceContext>::coerce
   6: hir_ty::infer::coerce::CoerceMany::coerce
   7: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_inner
   8: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_coerce
   9: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_block
  10: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_inner
  11: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_inner
  12: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_coerce
  13: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_block
  14: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_inner
  15: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr
  16: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_inner
  17: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_inner
  18: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_coerce
  19: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_block
  20: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_inner
  21: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_return
  22: hir_ty::infer::infer_query
  23: salsa::Cycle::catch
  24: salsa::derived::slot::Slot<Q>::execute
  25: salsa::derived::slot::Slot<Q>::read
  26: <DB as hir_ty::db::HirDatabase>::infer::__shim
  27: <DB as hir_ty::db::HirDatabase>::infer
  28: hir::semantics::SemanticsImpl::analyze_impl
  29: hir::semantics::SemanticsImpl::resolve_bind_pat_to_const
  30: ide_db::defs::NameClass::classify
  31: ide_db::defs::IdentClass::classify_node
  32: ide::hover::hover_offset
  33: salsa::Cancelled::catch
  34: rust_analyzer::handlers::request::handle_hover
  35: core::ops::function::FnOnce::call_once{{vtable.shim}}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
[Error - 8:34:04 PM] Request textDocument/hover failed.
  Message: request handler panicked: index out of bounds: the len is 67 but the index is 67
  Code: -32603 

rust-analyzer version: rust-analyzer version: 0.4.2108-standalone [/home/nlehmann/.vscode/extensions/rust-lang.rust-analyzer-0.4.2108-linux-x64/server/rust-analyzer]

rustc version: rustc 1.82.0-nightly (636d7ff91 2024-08-19)

editor or extension: VSCode

repository link (if public, optional): https://github.com/flux-rs/flux/tree/snapshot-for-ra

nilehmann commented 2 months ago

ah I know, it's probably because I'm matching on !

https://github.com/flux-rs/flux/blob/snapshot-for-ra/crates/flux-desugar/src/resolver.rs#L712

edit: ah no I don't think it's that. I commented that part and I still get the error.

ShoyuVanilla commented 2 months ago

67 is quite a long length for a substitution 😮 (edit: oh, its not a subst but a variable table 😅 ) I'll look into this if this is not resolved by the time I get back from vacation

ShoyuVanilla commented 2 months ago

I've tried rust-analyzer analysis-stats and opening most of the .rs files - including crates/flux-desugar/src/resolver.rs - in flux with my editor but failed to reproduce this. Could you let me know in which file were you hovering when this panic happned? @nilehmann

edit: oh, nevermind. I've managed to 😅 crates/flux-desugar/src/resolver/refinement_resolver.rs is the culprit

ShoyuVanilla commented 2 months ago

Reduced to;

struct Map<T, U>(T, U);

impl<T, U> Map<T, U> {
    fn new() -> Self { loop {} }
    fn get(&self, _: &T) -> Option<&U> { loop {} }
}

fn test(x: bool) {
    let map = Map::new();
    let _ = match x {
        true => {
            let Some(val) = map.get(&8) else { return };
            *val
        }
        false => return,
    };
}