Skip to content

thread 'Worker' panicked at 'index out of bounds: the len is 0 but the index is 0', chalk-ir-0.89.0\src\fold\subst.rs:55:19 #14966

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

Closed
frozenlib opened this issue Jun 4, 2023 · 0 comments · Fixed by #14971
Labels
A-ty type system / type inference / traits / method resolution C-bug Category: bug I-panic

Comments

@frozenlib
Copy link

The following code will cause rust-analyzer to panic.

How to reproduce

  1. Create a new crate with cargo init --lib.
  2. Paste the following code into lib.rs.
  3. Click on the beginning of the file and move the cursor there.
trait A<T> {
    fn a(&self);
}
struct B;
impl B {
    pub fn b<T>(s: &dyn A<T>) -> Self {
        B
    }
}
struct C;
impl C {
    fn c<T>(a: &dyn A<T>) -> Self {
        let mut c = C;
        let b = B::b(a);
        c.d(|| a.a());
        c
    }
    fn d(&mut self, f: impl FnOnce()) {}
}

rust-analyzer version: rust-analyzer version: 0.3.1533-standalone (505fd09 2023-05-28)
rustc version: rustc 1.70.0 (90c541806 2023-05-31)
relevant settings: Use default settings

Output:

Output :
Panic context:
> 
version: 0.3.1533-standalone (505fd09f9 2023-05-28)
request: textDocument/codeAction CodeActionParams {
    text_document: TextDocumentIdentifier {
        uri: Url {
            scheme: "file",
            cannot_be_a_base: false,
            username: "",
            password: None,
            host: None,
            port: None,
            path: "/z%3A/crash_example/src/lib.rs",
            query: None,
            fragment: None,
        },
    },
    range: Range {
        start: Position {
            line: 0,
            character: 0,
        },
        end: Position {
            line: 0,
            character: 0,
        },
    },
    context: CodeActionContext {
        diagnostics: [],
        only: None,
        trigger_kind: Some(
            Automatic,
        ),
    },
    work_done_progress_params: WorkDoneProgressParams {
        work_done_token: None,
    },
    partial_result_params: PartialResultParams {
        partial_result_token: None,
    },
}

thread 'Worker' panicked at 'index out of bounds: the len is 0 but the index is 0', C:\Users\runneradmin\.cargo\registry\src\git.colasdn.top-1ecc6299db9ec823\chalk-ir-0.89.0\src\fold\subst.rs:55:19
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library\std\src\panicking.rs:579
   1: core::panicking::panic_fmt
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library\core\src\panicking.rs:64
   2: core::panicking::panic_bounds_check
             at /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library\core\src\panicking.rs:159
   3: chalk_ir::visit::TypeVisitor::visit_ty
   4: chalk_ir::fold::TypeSuperFoldable::super_fold_with
   5: <core::iter::adapters::GenericShunt<I,R> as core::iter::traits::iterator::Iterator>::next
   6: <smallvec::SmallVec<A> as core::iter::traits::collect::Extend<<A as smallvec::Array>::Item>>::extend
   7: chalk_ir::fold::boring_impls::<impl chalk_ir::fold::TypeFoldable<I> for chalk_ir::Substitution<I>>::try_fold_with
   8: chalk_ir::_::<impl chalk_ir::fold::TypeFoldable<I> for chalk_ir::WhereClause<I>>::try_fold_with
   9: chalk_solve::infer::instantiate::<impl chalk_solve::infer::InferenceTable<I>>::instantiate_binders_universally
  10: <chalk_solve::infer::unify::Unifier<I> as chalk_ir::zip::Zipper<I>>::zip_binders
  11: <chalk_solve::infer::unify::Unifier<I> as chalk_ir::zip::Zipper<I>>::zip_binders
  12: chalk_solve::infer::unify::Unifier<I>::relate_ty_ty
  13: <chalk_solve::infer::unify::Unifier<I> as chalk_ir::zip::Zipper<I>>::zip_tys
  14: chalk_solve::infer::unify::Unifier<I>::relate_ty_ty
  15: chalk_solve::infer::unify::Unifier<I>::generalize_ty
  16: chalk_solve::infer::unify::Unifier<I>::relate_ty_ty
  17: <chalk_solve::infer::unify::Unifier<I> as chalk_ir::zip::Zipper<I>>::zip_tys
  18: chalk_ir::zip::Zipper::zip_substs
  19: chalk_ir::_::<impl chalk_ir::zip::Zip<I> for chalk_ir::WhereClause<I>>::zip_with
  20: chalk_ir::_::<impl chalk_ir::zip::Zip<I> for chalk_ir::DomainGoal<I>>::zip_with
  21: chalk_solve::infer::unify::Unifier<I>::relate
  22: chalk_recursive::fulfill::Fulfill<I,Solver>::new_with_clause
  23: chalk_recursive::solve::SolveIteration::solve_iteration
  24: chalk_recursive::solve::SolveIteration::solve_iteration
  25: chalk_recursive::fixed_point::RecursiveContext<K,V>::solve_goal
  26: chalk_recursive::fixed_point::RecursiveContext<K,V>::solve_root_goal
  27: hir_ty::traits::trait_solve_query
  28: salsa::runtime::Runtime::execute_query_implementation
  29: core::ptr::drop_in_place<salsa::derived::slot::QueryState<hir_ty::db::ConstEvalDiscriminantQuery>>
  30: salsa::derived::slot::Slot<Q,MP>::read
  31: <salsa::derived::DerivedStorage<Q,MP> as salsa::plumbing::QueryStorageOps<Q>>::try_fetch
  32: salsa::QueryTable<Q>::get
  33: <DB as hir_ty::db::HirDatabase>::trait_solve_query
  34: hir_ty::db::trait_solve_wait
  35: <DB as hir_ty::db::HirDatabase>::trait_solve
  36: <chalk_ir::Ty<hir_ty::interner::Interner> as hir_ty::chalk_ext::TyExt>::is_copy
  37: hir_ty::mir::lower::MirLowerCtx::push_storage_live_for_local
  38: hir_ty::mir::lower::MirLowerCtx::push_storage_live_for_local
  39: hir_ty::mir::lower::MirLowerCtx::lower_expr_to_some_operand
  40: hir_ty::mir::lower::MirLowerCtx::lower_expr_to_some_operand
  41: hir_ty::mir::lower::MirLowerCtx::lower_expr_to_some_operand
  42: hir_ty::mir::lower::lower_to_mir
  43: hir_ty::mir::lower::mir_body_query
  44: salsa::runtime::Runtime::execute_query_implementation
  45: core::ptr::drop_in_place<salsa::derived::slot::QueryState<hir_ty::db::ConstEvalDiscriminantQuery>>
  46: salsa::derived::slot::Slot<Q,MP>::read
  47: <salsa::derived::DerivedStorage<Q,MP> as salsa::plumbing::QueryStorageOps<Q>>::try_fetch
  48: <DB as hir_ty::db::HirDatabase>::mir_body
  49: hir_ty::mir::borrowck::borrowck_query
  50: salsa::runtime::Runtime::execute_query_implementation
  51: core::ptr::drop_in_place<salsa::derived::slot::QueryState<hir_ty::db::ConstEvalDiscriminantQuery>>
  52: salsa::derived::slot::Slot<Q,MP>::read
  53: <salsa::derived::DerivedStorage<Q,MP> as salsa::plumbing::QueryStorageOps<Q>>::try_fetch
  54: <DB as hir_ty::db::HirDatabase>::borrowck
  55: hir::DefWithBody::diagnostics
  56: hir::Module::diagnostics
  57: ide_diagnostics::diagnostics
  58: std::panicking::try
  59: rust_analyzer::handlers::request::handle_code_action
  60: std::panicking::try
  61: <F as threadpool::FnBox>::call_box
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
@frozenlib frozenlib added the C-bug Category: bug label Jun 4, 2023
@lowr lowr added A-ty type system / type inference / traits / method resolution I-panic labels Jun 4, 2023
@bors bors closed this as completed in 2f1b7ce Jun 4, 2023
Kmeakin pushed a commit to Kmeakin/rust-analyzer that referenced this issue Jun 5, 2023
…r, r=HKalbasi

fix: consider outer binders when folding captured items' type

Fixes rust-lang#14966

Basically, the crash is caused by us producing a broken type and passing it to chalk: `&dyn for<type> [for<> Implemented(^1.0: A<^0.0>)]` (notice the innermost bound var `^0.0` has no corresponding binder). It's created in `CapturedItemWithoutTy::with_ty()`, which didn't consider outer binders when folding types to replace placeholders with bound variables.

The fix is one-liner, but I've also refactored the surrounding code a little.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ty type system / type inference / traits / method resolution C-bug Category: bug I-panic
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants