Repository: rust
Issue: Generic `(&A -> T) -> (&'a A -> Box<dyn Any + 'a>)` crashes
Labels: I-ICE,A-borrow-checker,T-compiler,C-bug,S-has-mcve

I'm not sure if this is a minimal example, but I couldn't shrink it any more.

Generic `T` is necessary, `A` can be any type.

### Code

```Rust
type A = ();

fn f<T>(f: impl FnOnce(&A) -> T) -> impl for<'a> FnOnce(&'a A) -> Box<dyn std::any::Any + 'a> {
    move |a| Box::new(f(a))
}
```


### Meta
The bug exists in both the latest stable and the latest nightly.

`rustc --version --verbose`:
```
rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: x86_64-pc-windows-msvc
release: 1.78.0
LLVM version: 18.1.2

rustc 1.80.0-nightly (f67a1acc0 2024-06-01)
binary: rustc
commit-hash: f67a1acc04c7ecbf05751b17592dd8d245b75256
commit-date: 2024-06-01
host: x86_64-pc-windows-msvc
release: 1.80.0-nightly
LLVM version: 18.1.6
```

### Error output

```
thread 'rustc' panicked at compiler\rustc_borrowck\src\type_check\free_region_relations.rs:97:9:
can't find an upper bound!?
```

<details><summary><strong>Backtrace</strong></summary>
<p>

```
stack backtrace:
   0:     0x7ffd6322d623 - std::backtrace_rs::backtrace::dbghelp64::trace
                               at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\std\src\..\..\backtrace\src\backtrace\dbghelp64.rs:91
   1:     0x7ffd6322d623 - std::backtrace_rs::backtrace::trace_unsynchronized
                               at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\std\src\..\..\backtrace\src\backtrace\mod.rs:66
   2:     0x7ffd6322d623 - std::backtrace::Backtrace::create
                               at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\std\src\backtrace.rs:331
   3:     0x7ffd6322d56a - std::backtrace::Backtrace::force_capture
                               at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\std\src\backtrace.rs:312
   4:     0x7ffd5f276140 - memchr
   5:     0x7ffd63247187 - alloc::boxed::impl$50::call
                               at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\alloc\src\boxed.rs:2077
   6:     0x7ffd63247187 - std::panicking::rust_panic_with_hook
                               at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\std\src\panicking.rs:799
   7:     0x7ffd63246fd9 - std::panicking::begin_panic_handler::closure$0
                               at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\std\src\panicking.rs:656
   8:     0x7ffd6324450f - std::sys_common::backtrace::__rust_end_short_backtrace<std::panicking::begin_panic_handler::closure_env$0,never$>
                               at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\std\src\sys_common\backtrace.rs:171
   9:     0x7ffd63246cc8 - std::panicking::begin_panic_handler
                               at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\std\src\panicking.rs:652
  10:     0x7ffd6329e054 - core::panicking::panic_fmt
                               at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\core\src\panicking.rs:72
  11:     0x7ffd5f9a01c6 - <rustc_borrowck[47dc6bdb74f71e32]::region_infer::RegionInferenceContext>::eval_equal
  12:     0x7ffd5e41001e - rustc_borrowck[47dc6bdb74f71e32]::dataflow::calculate_borrows_out_of_scope_at_location
  13:     0x7ffd5e4f66e1 - <rustc_borrowck[47dc6bdb74f71e32]::type_check::relate_tys::NllTypeRelating as rustc_middle[415a98e6f835205a]::ty::relate::TypeRelation>::regions
  14:     0x7ffd5e45b7fc - rustc_borrowck[47dc6bdb74f71e32]::mir_borrowck
  15:     0x7ffd5e91619a - rustc_query_impl[f6e26b53264cdf7d]::plumbing::query_key_hash_verify_all
  16:     0x7ffd5e860989 - rustc_ty_utils[2e11d027a4d062f9]::ty::self_ty_of_trait_impl_enabling_order_dep_trait_object_hack
  17:     0x7ffd5e91fc2f - rustc_query_impl[f6e26b53264cdf7d]::plumbing::query_key_hash_verify_all
  18:     0x7ffd5d8b01d8 - <rustc_pattern_analysis[cee2a93862cfc37c]::rustc::RustcPatCtxt as rustc_pattern_analysis[cee2a93862cfc37c]::PatCx>::lint_non_contiguous_range_endpoints
  19:     0x7ffd5d8ca93a - <rustc_borrowck[47dc6bdb74f71e32]::type_check::relate_tys::NllTypeRelating as rustc_infer[97ab8c565f63bbbc]::infer::relate::combine::ObligationEmittingRelation>::register_obligations
  20:     0x7ffd5e4b2e87 - <rustc_borrowck[47dc6bdb74f71e32]::type_check::relate_tys::NllTypeRelating as rustc_middle[415a98e6f835205a]::ty::relate::TypeRelation>::regions
  21:     0x7ffd5e435c3f - <&mut rustc_borrowck[47dc6bdb74f71e32]::type_check::constraint_conversion::ConstraintConversion as rustc_infer[97ab8c565f63bbbc]::infer::outlives::obligations::TypeOutlivesDelegate>::push_verify
  22:     0x7ffd5e403ee7 - rustc_borrowck[47dc6bdb74f71e32]::dataflow::calculate_borrows_out_of_scope_at_location
  23:     0x7ffd5e4f66e1 - <rustc_borrowck[47dc6bdb74f71e32]::type_check::relate_tys::NllTypeRelating as rustc_middle[415a98e6f835205a]::ty::relate::TypeRelation>::regions
  24:     0x7ffd5e45b7fc - rustc_borrowck[47dc6bdb74f71e32]::mir_borrowck
  25:     0x7ffd5e91619a - rustc_query_impl[f6e26b53264cdf7d]::plumbing::query_key_hash_verify_all
  26:     0x7ffd5e860989 - rustc_ty_utils[2e11d027a4d062f9]::ty::self_ty_of_trait_impl_enabling_order_dep_trait_object_hack
  27:     0x7ffd5e91fc2f - rustc_query_impl[f6e26b53264cdf7d]::plumbing::query_key_hash_verify_all
  28:     0x7ffd5e0d6245 - <rustc_hir_typeck[8b16c411857e2a7d]::upvar::InferBorrowKind as rustc_hir_typeck[8b16c411857e2a7d]::expr_use_visitor::Delegate>::borrow
  29:     0x7ffd5d829b13 - rustc_hir_analysis[fa2df6f2e5566255]::collect::type_of::type_of_opaque
  30:     0x7ffd5da99a4a - <dyn std[bf373edb648c61c3]::io::Write as nu_ansi_term[7ca241b111b77143]::write::AnyWrite>::write_str
  31:     0x7ffd5e809f2f - rustc_ty_utils[2e11d027a4d062f9]::ty::self_ty_of_trait_impl_enabling_order_dep_trait_object_hack
  32:     0x7ffd5daa0a55 - rustc_query_impl[f6e26b53264cdf7d]::query_system
  33:     0x7ffd5f601bef - <rustc_hir_typeck[8b16c411857e2a7d]::upvar::InferBorrowKind as rustc_hir_typeck[8b16c411857e2a7d]::expr_use_visitor::Delegate>::mutate
  34:     0x7ffd5e13b958 - rustc_hir_analysis[fa2df6f2e5566255]::collect::type_of::type_of
  35:     0x7ffd5e91759f - rustc_query_impl[f6e26b53264cdf7d]::plumbing::query_key_hash_verify_all
  36:     0x7ffd5e809f2f - rustc_ty_utils[2e11d027a4d062f9]::ty::self_ty_of_trait_impl_enabling_order_dep_trait_object_hack
  37:     0x7ffd5e918c56 - rustc_query_impl[f6e26b53264cdf7d]::plumbing::query_key_hash_verify_all
  38:     0x7ffd5f601bef - <rustc_hir_typeck[8b16c411857e2a7d]::upvar::InferBorrowKind as rustc_hir_typeck[8b16c411857e2a7d]::expr_use_visitor::Delegate>::mutate
  39:     0x7ffd5e1026a4 - rustc_hir_analysis[fa2df6f2e5566255]::check::wfcheck::check_well_formed
  40:     0x7ffd5e91685a - rustc_query_impl[f6e26b53264cdf7d]::plumbing::query_key_hash_verify_all
  41:     0x7ffd5e866e35 - rustc_ty_utils[2e11d027a4d062f9]::ty::self_ty_of_trait_impl_enabling_order_dep_trait_object_hack
  42:     0x7ffd5e926039 - rustc_query_impl[f6e26b53264cdf7d]::plumbing::query_key_hash_verify_all
  43:     0x7ffd5e11498e - rustc_hir_analysis[fa2df6f2e5566255]::check::wfcheck::check_mod_type_wf
  44:     0x7ffd5da9a64a - <dyn std[bf373edb648c61c3]::io::Write as nu_ansi_term[7ca241b111b77143]::write::AnyWrite>::write_str
  45:     0x7ffd5d9e5f45 - rustc_ty_utils[2e11d027a4d062f9]::ty::adt_sized_constraint
  46:     0x7ffd5daabbc2 - rustc_query_impl[f6e26b53264cdf7d]::query_system
  47:     0x7ffd5e1562df - rustc_hir_analysis[fa2df6f2e5566255]::check_crate
  48:     0x7ffd5deb27f2 - <rustc_interface[3972c3fa63dc4815]::passes::LintStoreExpandImpl as rustc_expand[aab009fac10e5cb6]::base::LintStoreExpand>::pre_expansion_lint
  49:     0x7ffd5b0f7517 - rustc_interface[3972c3fa63dc4815]::passes::create_global_ctxt
  50:     0x7ffd5da9c0ba - <dyn std[bf373edb648c61c3]::io::Write as nu_ansi_term[7ca241b111b77143]::write::AnyWrite>::write_str
  51:     0x7ffd5d9bbfc9 - rustc_ty_utils[2e11d027a4d062f9]::ty::adt_sized_constraint
  52:     0x7ffd5daa1623 - rustc_query_impl[f6e26b53264cdf7d]::query_system
  53:     0x7ffd5b0b4d8d - _LNan_C
  54:     0x7ffd5b0b214f - _LNan_C
  55:     0x7ffd5b0b9e49 - _LNan_C
  56:     0x7ffd6325838d - alloc::boxed::impl$48::call_once
                               at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\alloc\src\boxed.rs:2063
  57:     0x7ffd6325838d - alloc::boxed::impl$48::call_once
                               at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\alloc\src\boxed.rs:2063
  58:     0x7ffd6325838d - std::sys::pal::windows::thread::impl$0::new::thread_start
                               at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\std\src\sys\pal\windows\thread.rs:52
  59:     0x7ffddef27344 - BaseThreadInitThunk
  60:     0x7ffddf8226b1 - RtlUserThreadStart


rustc version: 1.80.0-nightly (f67a1acc0 2024-06-01)
platform: x86_64-pc-windows-msvc

query stack during panic:
#0 [mir_borrowck] borrow-checking `f::{closure#0}`
#1 [mir_borrowck] borrow-checking `f`
#2 [type_of_opaque] computing type of opaque `f::{opaque#0}`
#3 [type_of] computing type of `f::{opaque#0}`
#4 [check_well_formed] checking that `f::{opaque#0}` is well-formed
#5 [check_mod_type_wf] checking that types are well-formed in top-level module
#6 [analysis] running analysis passes on this crate
end of query stack
delayed bug: {OpaqueTypeKey { def_id: DefId(0:10 ~ testing[c4fb]::f::{opaque#0}), args: [T/#0, impl FnOnce(&A) -> T/#1] }: OpaqueTypeDecl { hidden_type: OpaqueHiddenType { span: src\lib.rs:4:5: 4:28 (#0), ty: Closure(DefId(0:7 ~ testing[c4fb]::f::{closure#0}), [T/#0, impl FnOnce(&A) -> T/#1, i32, Binder(extern "RustCall" fn((&'^0 (),)) -> std::boxed::Box<dyn [Binder(Trait(std::any::Any), [])] + '^0, std::alloc::Global>, [Region(BrAnon)]), (impl FnOnce(&A) -> T/#1,)]) } }}
   0: std::backtrace_rs::backtrace::dbghelp64::trace
             at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\std\src\..\..\backtrace\src\backtrace\dbghelp64.rs:91
   1: std::backtrace_rs::backtrace::trace_unsynchronized
             at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\std\src\..\..\backtrace\src\backtrace\mod.rs:66
   2: std::backtrace::Backtrace::create
             at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\std\src\backtrace.rs:331
   3: std::backtrace::Backtrace::capture
             at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\std\src\backtrace.rs:296
   4: <rustc_errors::DiagCtxt>::emit_future_breakage_report
   5: <rustc_errors::DiagCtxt>::emit_diagnostic
   6: <rustc_span::ErrorGuaranteed as rustc_errors::diagnostic::EmissionGuarantee>::emit_producing_guarantee
   7: <rustc_infer::infer::opaque_types::table::OpaqueTypeStorage as core::ops::drop::Drop>::drop
   8: rustc_pattern_analysis::analyze_match
   9: <rustc_pattern_analysis::errors::NonExhaustiveOmittedPatternLintOnArm as rustc_errors::diagnostic::LintDiagnostic<()>>::decorate_lint
  10: <rustc_borrowck::type_check::relate_tys::NllTypeRelating as rustc_middle::ty::relate::TypeRelation>::regions
  11: _CallSettingFrame
             at D:\a\_work\1\s\src\vctools\crt\vcruntime\src\eh\amd64\handlers.asm:49
  12: __FrameHandler3::FrameUnwindToState
             at D:\a\_work\1\s\src\vctools\crt\vcruntime\src\eh\frame.cpp:1222
  13: __FrameHandler3::FrameUnwindToEmptyState
             at D:\a\_work\1\s\src\vctools\crt\vcruntime\src\eh\risctrnsctrl.cpp:255
  14: __InternalCxxFrameHandler<__FrameHandler3>
             at D:\a\_work\1\s\src\vctools\crt\vcruntime\src\eh\frame.cpp:355
  15: __CxxFrameHandler3
             at D:\a\_work\1\s\src\vctools\crt\vcruntime\src\eh\risctrnsctrl.cpp:281
  16: _chkstk
  17: RtlUnwindEx
  18: __FrameHandler3::UnwindNestedFrames
             at D:\a\_work\1\s\src\vctools\crt\vcruntime\src\eh\risctrnsctrl.cpp:759
  19: CatchIt<__FrameHandler3>
             at D:\a\_work\1\s\src\vctools\crt\vcruntime\src\eh\frame.cpp:1326
  20: FindHandler<__FrameHandler3>
             at D:\a\_work\1\s\src\vctools\crt\vcruntime\src\eh\frame.cpp:670
  21: __InternalCxxFrameHandler<__FrameHandler3>
             at D:\a\_work\1\s\src\vctools\crt\vcruntime\src\eh\frame.cpp:396
  22: __CxxFrameHandler3
             at D:\a\_work\1\s\src\vctools\crt\vcruntime\src\eh\risctrnsctrl.cpp:281
  23: _chkstk
  24: RtlRaiseException
  25: RtlRaiseException
  26: RaiseException
  27: _CxxThrowException
             at D:\a\_work\1\s\src\vctools\crt\vcruntime\src\eh\throw.cpp:81
  28: panic_unwind::real_imp::panic
             at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\panic_unwind\src\seh.rs:353
  29: panic_unwind::__rust_start_panic
             at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\panic_unwind\src\lib.rs:108
  30: std::panicking::rust_panic
             at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\std\src\panicking.rs:847
  31: std::panicking::rust_panic_with_hook
             at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\std\src\panicking.rs:817
  32: std::panicking::begin_panic_handler::closure$0
             at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\std\src\panicking.rs:656
  33: std::sys_common::backtrace::__rust_end_short_backtrace<std::panicking::begin_panic_handler::closure_env$0,never$>
             at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\std\src\sys_common\backtrace.rs:171
  34: std::panicking::begin_panic_handler
             at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\std\src\panicking.rs:652
  35: core::panicking::panic_fmt
             at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\core\src\panicking.rs:72
  36: <rustc_borrowck::region_infer::RegionInferenceContext>::eval_equal
  37: rustc_borrowck::dataflow::calculate_borrows_out_of_scope_at_location
  38: <rustc_borrowck::type_check::relate_tys::NllTypeRelating as rustc_middle::ty::relate::TypeRelation>::regions
  39: rustc_borrowck::mir_borrowck
  40: rustc_query_impl::plumbing::query_key_hash_verify_all
  41: rustc_ty_utils::ty::self_ty_of_trait_impl_enabling_order_dep_trait_object_hack
  42: rustc_query_impl::plumbing::query_key_hash_verify_all
  43: <rustc_pattern_analysis::rustc::RustcPatCtxt as rustc_pattern_analysis::PatCx>::lint_non_contiguous_range_endpoints
  44: <rustc_borrowck::type_check::relate_tys::NllTypeRelating as rustc_infer::infer::relate::combine::ObligationEmittingRelation>::register_obligations
  45: <rustc_borrowck::type_check::relate_tys::NllTypeRelating as rustc_middle::ty::relate::TypeRelation>::regions
  46: <&mut rustc_borrowck::type_check::constraint_conversion::ConstraintConversion as rustc_infer::infer::outlives::obligations::TypeOutlivesDelegate>::push_verify
  47: rustc_borrowck::dataflow::calculate_borrows_out_of_scope_at_location
  48: <rustc_borrowck::type_check::relate_tys::NllTypeRelating as rustc_middle::ty::relate::TypeRelation>::regions
  49: rustc_borrowck::mir_borrowck
  50: rustc_query_impl::plumbing::query_key_hash_verify_all
  51: rustc_ty_utils::ty::self_ty_of_trait_impl_enabling_order_dep_trait_object_hack
  52: rustc_query_impl::plumbing::query_key_hash_verify_all
  53: <rustc_hir_typeck::upvar::InferBorrowKind as rustc_hir_typeck::expr_use_visitor::Delegate>::borrow
  54: rustc_hir_analysis::collect::type_of::type_of_opaque
  55: <dyn std::io::Write as nu_ansi_term::write::AnyWrite>::write_str
  56: rustc_ty_utils::ty::self_ty_of_trait_impl_enabling_order_dep_trait_object_hack
  57: rustc_query_impl::query_system
  58: <rustc_hir_typeck::upvar::InferBorrowKind as rustc_hir_typeck::expr_use_visitor::Delegate>::mutate
  59: rustc_hir_analysis::collect::type_of::type_of
  60: rustc_query_impl::plumbing::query_key_hash_verify_all
  61: rustc_ty_utils::ty::self_ty_of_trait_impl_enabling_order_dep_trait_object_hack
  62: rustc_query_impl::plumbing::query_key_hash_verify_all
  63: <rustc_hir_typeck::upvar::InferBorrowKind as rustc_hir_typeck::expr_use_visitor::Delegate>::mutate
  64: rustc_hir_analysis::check::wfcheck::check_well_formed
  65: rustc_query_impl::plumbing::query_key_hash_verify_all
  66: rustc_ty_utils::ty::self_ty_of_trait_impl_enabling_order_dep_trait_object_hack
  67: rustc_query_impl::plumbing::query_key_hash_verify_all
  68: rustc_hir_analysis::check::wfcheck::check_mod_type_wf
  69: <dyn std::io::Write as nu_ansi_term::write::AnyWrite>::write_str
  70: rustc_ty_utils::ty::adt_sized_constraint
  71: rustc_query_impl::query_system
  72: rustc_hir_analysis::check_crate
  73: <rustc_interface::passes::LintStoreExpandImpl as rustc_expand::base::LintStoreExpand>::pre_expansion_lint
  74: rustc_interface::passes::create_global_ctxt
  75: <dyn std::io::Write as nu_ansi_term::write::AnyWrite>::write_str
  76: rustc_ty_utils::ty::adt_sized_constraint
  77: rustc_query_impl::query_system
  78: _LNan_C
  79: _LNan_C
  80: _LNan_C
  81: alloc::boxed::impl$48::call_once
             at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\alloc\src\boxed.rs:2063
  82: alloc::boxed::impl$48::call_once
             at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\alloc\src\boxed.rs:2063
  83: std::sys::pal::windows::thread::impl$0::new::thread_start
             at /rustc/f67a1acc04c7ecbf05751b17592dd8d245b75256/library\std\src\sys\pal\windows\thread.rs:52
  84: BaseThreadInitThunk
  85: RtlUserThreadStart
```

</p>
</details>