rust-lang / rust-analyzer

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

panic in fold subst #17080

Closed ConradIrwin closed 5 months ago

ConradIrwin commented 6 months ago

This is a serious regression in nightly and it's important to fix it before the next release.

Since Friday last week, I (and one other colleague) have been getting panics from rust-analyzer when working on https://github.com/zed/zed.

The full log is here: https://gist.github.com/ConradIrwin/823575d9f9279844c9b61b8eb7571829, but the salient piece seem to be:

thread 'Worker' panicked at /Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/chalk-ir-0.97.0/src/fold/subst.rs:77:19:
2024-04-15T19:37:36.170802Z ERROR rust_analyzer::handlers::request: assertion failed: !!text[usize::from(position.offset)..].starts_with(char_typed)
Panic context:
> fetch_native_diagnostics
index out of bounds: the len is 1 but the index is 2
stack backtrace:
0: _rust_begin_unwind
1: core::panicking::panic_fmt
2: core::panicking::panic_bounds_check
3: <chalk_ir::fold::subst::Subst<I> as chalk_ir::fold::TypeFolder<I>>::fold_free_var_lifetime
...

Although it seems like the panic is in chalk, and chalk was recently updated, the crash from Friday was on the prior version: https://gist.github.com/ConradIrwin/4af43965942a9f5e98d173528b5f2fee .

Running "/Users/conrad/Library/Application Support/Zed/languages/rust-analyzer/rust-analyzer-2024-04-15" analysis-stats . reproduces the crash:


Database loaded:     31.45s, 0b (metadata 571.32ms, 0b; build 30.12s, 0b)
  item trees: 755
Item Tree Collection: 595.65ms, 0b
  crates: 147, mods: 925, decls: 37953, bodies: 34260, adts: 3543, consts: 2805
Item Collection:     33.04s, 0b
Body lowering:       5.78s, 0b
8519/34260 83% processing: collab::db::queries::messages::latest_channel_messagesthread 'main' panicked at /Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/chalk-ir-0.97.0/src/fold/subst.rs:77:19:
index out of bounds: the len is 1 but the index is 2
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic_bounds_check
   3: <chalk_ir::fold::subst::Subst<I> as chalk_ir::fold::TypeFolder<I>>::fold_free_var_lifetime
   4: chalk_ir::fold::TypeSuperFoldable::super_fold_with
   5: chalk_ir::_::<impl chalk_ir::fold::TypeFoldable<I> for chalk_ir::DynTy<I>>::try_fold_with
   6: chalk_ir::fold::TypeSuperFoldable::super_fold_with
   7: chalk_ir::fold::boring_impls::<impl chalk_ir::fold::TypeFoldable<I> for chalk_ir::GenericArg<I>>::try_fold_with
   8: <core::iter::adapters::GenericShunt<I,R> as core::iter::traits::iterator::Iterator>::next
   9: <smallvec::SmallVec<A> as core::iter::traits::collect::Extend<<A as smallvec::Array>::Item>>::extend
  10: chalk_ir::fold::boring_impls::<impl chalk_ir::fold::TypeFoldable<I> for chalk_ir::Substitution<I>>::try_fold_with
  11: chalk_ir::fold::TypeSuperFoldable::super_fold_with
  12: chalk_ir::fold::boring_impls::<impl chalk_ir::fold::TypeFoldable<I> for chalk_ir::GenericArg<I>>::try_fold_with
  13: <core::iter::adapters::GenericShunt<I,R> as core::iter::traits::iterator::Iterator>::next
  14: <smallvec::SmallVec<A> as core::iter::traits::collect::Extend<<A as smallvec::Array>::Item>>::extend
  15: chalk_ir::fold::boring_impls::<impl chalk_ir::fold::TypeFoldable<I> for chalk_ir::Substitution<I>>::try_fold_with
  16: chalk_ir::fold::TypeSuperFoldable::super_fold_with
  17: chalk_ir::fold::boring_impls::<impl chalk_ir::fold::TypeFoldable<I> for chalk_ir::GenericArg<I>>::try_fold_with
  18: <core::iter::adapters::GenericShunt<I,R> as core::iter::traits::iterator::Iterator>::next
  19: <smallvec::SmallVec<A> as core::iter::traits::collect::Extend<<A as smallvec::Array>::Item>>::extend
  20: chalk_ir::fold::boring_impls::<impl chalk_ir::fold::TypeFoldable<I> for chalk_ir::Substitution<I>>::try_fold_with
  21: chalk_ir::fold::TypeSuperFoldable::super_fold_with
  22: chalk_ir::_::<impl chalk_ir::fold::TypeFoldable<I> for chalk_ir::WhereClause<I>>::try_fold_with
  23: chalk_ir::fold::binder_impls::<impl chalk_ir::fold::TypeFoldable<I> for chalk_ir::Binders<T>>::try_fold_with
  24: chalk_ir::Binders<T>::substitute
  25: <chalk_solve::rust_ir::OpaqueTyDatum<I> as chalk_solve::clauses::program_clauses::ToProgramClauses<I>>::to_program_clauses
  26: chalk_solve::clauses::program_clauses_that_could_match
  27: chalk_recursive::solve::SolveIterationHelpers::solve_from_clauses
  28: chalk_recursive::solve::SolveIteration::solve_iteration
  29: chalk_recursive::fixed_point::RecursiveContext<K,V>::solve_goal
  30: chalk_recursive::fixed_point::RecursiveContext<K,V>::solve_root_goal
  31: hir_ty::traits::solve::{{closure}}
  32: hir_ty::traits::trait_solve_query
  33: std::panicking::try
  34: salsa::derived::slot::Slot<Q,MP>::execute
  35: salsa::derived::slot::Slot<Q,MP>::read
  36: <salsa::derived::DerivedStorage<Q,MP> as salsa::plumbing::QueryStorageOps<Q>>::fetch
  37: <DB as hir_ty::db::HirDatabase>::trait_solve
  38: hir_ty::infer::unify::InferenceTable::try_resolve_obligation
  39: hir_ty::infer::unify::InferenceTable::register_obligation_in_env
  40: hir_ty::infer::InferenceContext::resolve_associated_type_with_params
  41: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_inner
  42: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::check_call_arguments
  43: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_inner
  44: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr
  45: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_inner
  46: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_coerce
  47: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_block
  48: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_inner
  49: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_coerce
  50: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_block
  51: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_inner
  52: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_return
  53: hir_ty::infer::infer_query
  54: salsa::Cycle::catch
  55: salsa::derived::slot::Slot<Q,MP>::execute
  56: salsa::derived::slot::Slot<Q,MP>::read
  57: <salsa::derived::DerivedStorage<Q,MP> as salsa::plumbing::QueryStorageOps<Q>>::fetch
  58: <DB as hir_ty::db::HirDatabase>::infer
  59: rust_analyzer::cli::analysis_stats::<impl rust_analyzer::cli::flags::AnalysisStats>::run_inference
  60: rust_analyzer::cli::analysis_stats::<impl rust_analyzer::cli::flags::AnalysisStats>::run
  61: rust_analyzer::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
ConradIrwin commented 6 months ago

Narrowing down the code a little bit, it seems to be caused by this: https://github.com/zed-industries/zed/blob/3bc1a41fd46028700b2900661800b293a88d202b^/crates/collab/src/db/queries/channels.rs#L577-L600.

ConradIrwin commented 6 months ago

Git bisect blames https://github.com/rust-lang/rust-analyzer/commit/0927f86247ec454e7b48e22595783bb58f6a2cd1, though from scanning the diff it's not very obvious why that change would cause this.

cc @Veykril

Veykril commented 6 months ago

Potentially https://github.com/rust-lang/rust-analyzer/issues/17017, which is just surfaced by my PR

jost-s commented 5 months ago

I'm getting panics from chalk as well since a couple of days ago, but a different error. They happen very frequently, almost every time I request code completion while working on https://github.com/holochain/holochain.

thread 'Worker' panicked at /Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/chalk-ir-0.97.0/src/lib.rs:1455:33:
called `Option::unwrap()` on a `None` value
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic
   3: core::option::unwrap_failed
   4: <chalk_ir::SubstFolder<I,A> as chalk_ir::fold::TypeFolder<I>>::fold_free_var_lifetime
   5: chalk_ir::fold::TypeSuperFoldable::super_fold_with
   6: chalk_ir::_::<impl chalk_ir::fold::TypeFoldable<I> for chalk_ir::DynTy<I>>::try_fold_with
   7: chalk_ir::fold::TypeSuperFoldable::super_fold_with
   8: chalk_ir::fold::boring_impls::<impl chalk_ir::fold::TypeFoldable<I> for chalk_ir::GenericArg<I>>::try_fold_with
   9: <core::iter::adapters::GenericShunt<I,R> as core::iter::traits::iterator::Iterator>::next
  10: <smallvec::SmallVec<A> as core::iter::traits::collect::Extend<<A as smallvec::Array>::Item>>::extend
  11: chalk_ir::fold::boring_impls::<impl chalk_ir::fold::TypeFoldable<I> for chalk_ir::Substitution<I>>::try_fold_with
  12: chalk_ir::fold::TypeSuperFoldable::super_fold_with
  13: chalk_ir::fold::boring_impls::<impl chalk_ir::fold::TypeFoldable<I> for chalk_ir::GenericArg<I>>::try_fold_with
  14: <core::iter::adapters::GenericShunt<I,R> as core::iter::traits::iterator::Iterator>::next
  15: <smallvec::SmallVec<A> as core::iter::traits::collect::Extend<<A as smallvec::Array>::Item>>::extend
  16: chalk_ir::fold::boring_impls::<impl chalk_ir::fold::TypeFoldable<I> for chalk_ir::Substitution<I>>::try_fold_with
  17: chalk_ir::fold::TypeSuperFoldable::super_fold_with
  18: chalk_ir::_::<impl chalk_ir::fold::TypeFoldable<I> for chalk_ir::WhereClause<I>>::try_fold_with
  19: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter
  20: chalk_ir::fold::boring_impls::<impl chalk_ir::fold::TypeFoldable<I> for chalk_ir::QuantifiedWhereClauses<I>>::try_fold_with
  21: chalk_solve::infer::instantiate::<impl chalk_solve::infer::InferenceTable<I>>::instantiate_binders_existentially
  22: <chalk_solve::infer::unify::Unifier<I> as chalk_ir::zip::Zipper<I>>::zip_binders
  23: chalk_solve::infer::unify::Unifier<I>::relate_ty_ty
  24: <chalk_solve::infer::unify::Unifier<I> as chalk_ir::zip::Zipper<I>>::zip_tys
  25: chalk_ir::zip::Zipper::zip_substs
  26: chalk_solve::infer::unify::Unifier<I>::relate_ty_ty
  27: <chalk_solve::infer::unify::Unifier<I> as chalk_ir::zip::Zipper<I>>::zip_tys
  28: chalk_solve::infer::unify::Unifier<I>::relate_ty_ty
  29: chalk_solve::infer::unify::Unifier<I>::relate_var_ty
  30: chalk_solve::infer::unify::Unifier<I>::relate_ty_ty
  31: <chalk_solve::infer::unify::Unifier<I> as chalk_ir::zip::Zipper<I>>::zip_tys
  32: chalk_ir::zip::Zipper::zip_substs
  33: chalk_ir::_::<impl chalk_ir::zip::Zip<I> for chalk_ir::WhereClause<I>>::zip_with
  34: chalk_solve::infer::unify::Unifier<I>::relate
  35: chalk_solve::infer::unify::<impl chalk_solve::infer::InferenceTable<I>>::relate
  36: chalk_recursive::solve::SolveIteration::solve_iteration
  37: chalk_recursive::fixed_point::RecursiveContext<K,V>::solve_goal
  38: chalk_recursive::fixed_point::RecursiveContext<K,V>::solve_root_goal
  39: hir_ty::traits::solve::{{closure}}
  40: hir_ty::traits::trait_solve_query
  41: std::panicking::try
  42: salsa::derived::slot::Slot<Q,MP>::execute
  43: salsa::derived::slot::Slot<Q,MP>::read
  44: <salsa::derived::DerivedStorage<Q,MP> as salsa::plumbing::QueryStorageOps<Q>>::fetch
  45: <DB as hir_ty::db::HirDatabase>::trait_solve
  46: hir_ty::infer::unify::InferenceTable::callable_sig
  47: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_inner
  48: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::check_call_arguments
  49: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_inner
  50: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_coerce
  51: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_block
  52: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_inner
  53: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_inner
  54: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_coerce
  55: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_block
  56: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_inner
  57: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_return
  58: hir_ty::infer::infer_query
  59: salsa::Cycle::catch
  60: salsa::derived::slot::Slot<Q,MP>::execute
  61: salsa::derived::slot::Slot<Q,MP>::read
  62: <salsa::derived::DerivedStorage<Q,MP> as salsa::plumbing::QueryStorageOps<Q>>::fetch
  63: <DB as hir_ty::db::HirDatabase>::infer
  64: hir::DefWithBody::diagnostics
  65: hir::Module::diagnostics
  66: hir::Module::diagnostics
  67: hir::Module::diagnostics
  68: ide_diagnostics::diagnostics
  69: salsa::Cancelled::catch
  70: core::ops::function::impls::<impl core::ops::function::FnMut<A> for &mut F>::call_mut
  71: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter
  72: rust_analyzer::diagnostics::fetch_native_diagnostics
  73: core::ops::function::FnOnce::call_once{{vtable.shim}}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
Panic context:
> fetch_native_diagnostics
neekolas commented 5 months ago

Any updates on this? I've had to revert to an older version for https://github.com/xmtp/libxmtp

lnicola commented 5 months ago

@neekolas #17017 was just fixed. I've triggered a nightly, it should be ready in 20 minutes or so. You can then download it or enable pre-release versions in Code in order to test it.

neekolas commented 5 months ago

@lnicola Thank you! Just switched over now. Will let you know if I run into the same issue with the new version.

dfireBird commented 5 months ago

Hello, A fix was pushed with PR #17190 and it was released as part of today's release (version number: 0.4.1949). Can you please test it and report whether the issue is fixed for you or not? Thanks.

ConradIrwin commented 5 months ago

Thanks! It looks like it's working on the code above, thank you!

jost-s commented 5 months ago

Mmh, I had downgraded to 0.3.1860 and the latest I can update to is 0.3.1950

image

Is this still only on nightly? Second question, why are there no version numbers on the release page?

dfireBird commented 5 months ago

It's not only on nightly, the fix is released with the weekly release. If you can try to upgrade to last version and use Show RA version on command view of VSCode and see if that point to the commit: c4618fe

jost-s commented 5 months ago

No, it doesn't show that commit

image

Ah, after updating it shows that commit, even though it mentions version 0.3.1950

image