rust-lang / rust-analyzer

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

RA panic: unexpected lifetime vars in fn ptr: NoSolution #17167

Closed miraclx closed 4 days ago

miraclx commented 2 weeks ago

rust-analyzer version: 0.3.1940-standalone (f216be4a0 2024-04-27)

rustc version: rustc 1.77.2 (25ef9e3d8 2024-04-09)

editor or extension: VSCode + RA v0.3.1940

relevant settings: normal

repository link (if public, optional): -

code snippet to reproduce:

trait MyTrait {}

type MyFunc<'a> = fn() -> Box<dyn MyTrait + 'a>;

thread_local! {
    static MY_TYPE: MyFunc<'static> = handler;
}

fn handler() -> Box<dyn MyTrait + 'static> {
    todo!()
}

This immediately produces #17124 in the form of:

Stack Trace ```console thread 'Worker' panicked at /Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/chalk-ir-0.97.0/src/lib.rs:2730:10: index out of bounds: the len is 0 but the index is 0 stack backtrace: 0: _rust_begin_unwind 1: core::panicking::panic_fmt 2: core::panicking::panic_bounds_check 3: as chalk_ir::fold::TypeFolder>::fold_free_var_lifetime 4: chalk_ir::fold::TypeSuperFoldable::super_fold_with 5: chalk_ir::_:: for chalk_ir::DynTy>::try_fold_with 6: chalk_ir::fold::TypeSuperFoldable::super_fold_with 7: chalk_ir::fold::boring_impls:: for chalk_ir::GenericArg>::try_fold_with 8: as core::iter::traits::iterator::Iterator>::next 9: as core::iter::traits::collect::Extend<::Item>>::extend 10: chalk_ir::fold::boring_impls:: for chalk_ir::Substitution>::try_fold_with 11: chalk_ir::fold::TypeSuperFoldable::super_fold_with 12: chalk_ir::fold::boring_impls:: for chalk_ir::GenericArg>::try_fold_with 13: as core::iter::traits::iterator::Iterator>::next 14: as core::iter::traits::collect::Extend<::Item>>::extend 15: chalk_ir::fold::boring_impls:: for chalk_ir::Substitution>::try_fold_with 16: chalk_solve::infer::instantiate::>::instantiate_binders_existentially 17: chalk_solve::infer::unify::Unifier::relate_ty_ty 18: as chalk_ir::zip::Zipper>::zip_tys 19: chalk_solve::infer::unify::Unifier::relate 20: chalk_solve::infer::unify::>::relate 21: hir_ty::infer::coerce::::unify_and 22: hir_ty::infer::coerce::::coerce_inner 23: hir_ty::infer::coerce::::coerce 24: hir_ty::infer::coerce::::coerce 25: hir_ty::infer::expr::::infer_expr_coerce 26: hir_ty::infer::expr::::infer_block 27: hir_ty::infer::expr::::infer_expr_inner 28: hir_ty::infer::expr::::infer_return 29: hir_ty::infer::infer_query 30: salsa::Cycle::catch 31: salsa::derived::slot::Slot::execute 32: salsa::derived::slot::Slot::read 33: as salsa::plumbing::QueryStorageOps>::fetch 34: ::infer 35: hir::DefWithBody::diagnostics 36: hir::ModuleDef::diagnostics 37: hir::Module::diagnostics 38: hir::DefWithBody::diagnostics 39: hir::ModuleDef::diagnostics 40: hir::Module::diagnostics 41: ide_diagnostics::diagnostics 42: std::panicking::try 43: ide::Analysis::assists_with_fixes 44: rust_analyzer::handlers::request::handle_code_action 45: std::panicking::try 46: core::ops::function::FnOnce::call_once{{vtable.shim}} note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. ```

And when I hover over the MY_TYPE ident, I get the following:

thread 'Worker' panicked at crates/hir-ty/src/lib.rs:550:22:
unexpected lifetime vars in fn ptr: NoSolution
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::result::unwrap_failed
   3: hir_ty::CallableSig::from_fn_ptr
   4: <chalk_ir::Ty<hir_ty::interner::Interner> as hir_ty::chalk_ext::TyExt>::callable_sig
   5: hir_ty::infer::unify::InferenceTable::callable_sig
   6: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_inner
   7: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr
   8: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_inner
   9: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_coerce_never
  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_coerce
  12: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_block
  13: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_expr_inner
  14: hir_ty::infer::expr::<impl hir_ty::infer::InferenceContext>::infer_return
  15: hir_ty::infer::infer_query
  16: salsa::Cycle::catch
  17: salsa::derived::slot::Slot<Q,MP>::execute
  18: salsa::derived::slot::Slot<Q,MP>::read
  19: <salsa::derived::DerivedStorage<Q,MP> as salsa::plumbing::QueryStorageOps<Q>>::fetch
  20: <DB as hir_ty::db::HirDatabase>::infer
  21: hir::source_analyzer::SourceAnalyzer::new_for_body
  22: hir::semantics::SemanticsImpl::analyze_impl
  23: ide_db::defs::NameRefClass::classify
  24: ide_db::search::FindUsages::found_name_ref
  25: ide_db::search::FindUsages::search
  26: hashbrown::raw::RawIterRange<T>::fold_impl
  27: <core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::fold
  28: ide::highlight_related::highlight_related
  29: std::panicking::try
  30: ide::Analysis::highlight_related
  31: rust_analyzer::handlers::request::handle_document_highlight
  32: std::panicking::try
  33: core::ops::function::FnOnce::call_once{{vtable.shim}}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

When I inline the type alias in this form:

trait MyTrait {}

thread_local! {
    static MY_TYPE: fn() -> Box<dyn MyTrait + 'static> = handler;
}

fn handler() -> Box<dyn MyTrait + 'static> {
    todo!()
}

Both errors go away.

dfireBird commented 1 week 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.

miraclx commented 4 days ago

Sorry for the delay, yeah I just checked and I can confirm this is indeed fixed on:

rust-analyzer version: 0.3.1958-standalone (5bf2f85c8 2024-05-09)

Thank you so much!