Repository: rust
Issue: Using a trait's Associated Type in a different trait's impl declaration crashes
Labels: I-ICE,T-compiler,C-bug,S-has-mcve,S-bug-has-test,A-coherence

<!--
Thank you for finding an Internal Compiler Error! 🧊  If possible, try to provide
a minimal verifiable example. You can read "Rust Bug Minimization Patterns" for
how to create smaller examples.
http://blog.pnkfx.org/blog/2019/11/18/rust-bug-minimization-patterns/
-->

### Code

```Rust
use std::collections::HashMap;

pub trait MyTrait {
    type Item;
    fn foo(&self, other: &Self) -> ();
}

impl<K,V> MyTrait for HashMap<K, V>
where
K: ::std::hash::Hash,
{
    type Item = HashMap<K, ::core::option::Option<V>>;
    fn foo(&self, other: &Self) -> () {
    }
}

impl<K,V> ::core::convert::From<HashMap<K, V>> for <HashMap<K, V> as MyTrait>::Item
where
K: ::std::hash::Hash,
{
    fn from(item: ::core::convert::From<HashMap<K, V>>) -> <HashMap<K, V> as MyTrait>::Item {
        ()
    }
}

fn main() {
    println!("Hello, world!");
}

```


### Meta
<!--
If you're using the stable version of the compiler, you should also check if the
bug also exists in the beta or nightly versions.
-->

`rustc --version --verbose`:
```
rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: x86_64-unknown-linux-gnu
release: 1.81.0
LLVM version: 18.1.7
```

### Error output

```
   Compiling rust_bug v0.1.0 (/workspaces/rust_bug)
thread 'rustc' panicked at compiler/rustc_hir_analysis/src/coherence/orphan.rs:554:60:
no entry found for key
stack backtrace:
   0:     0x7f8bf48343e5 - std::backtrace_rs::backtrace::libunwind::trace::h649ab3318d3445c5
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/../../backtrace/src/backtrace/libunwind.rs:116:5
   1:     0x7f8bf48343e5 - std::backtrace_rs::backtrace::trace_unsynchronized::hf4bb60c3387150c3
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f8bf48343e5 - std::sys::backtrace::_print_fmt::hd9186c800e44bd00
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/sys/backtrace.rs:65:5
   3:     0x7f8bf48343e5 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h1b9dad2a88e955ff
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/sys/backtrace.rs:40:26
   4:     0x7f8bf4883eeb - core::fmt::rt::Argument::fmt::h351a7824f737a6a0
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/fmt/rt.rs:173:76
   5:     0x7f8bf4883eeb - core::fmt::write::h4b5a1270214bc4a7
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/fmt/mod.rs:1182:21
   6:     0x7f8bf4828f6f - std::io::Write::write_fmt::hd04af345a50c312d
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/io/mod.rs:1827:15
   7:     0x7f8bf4836bd1 - std::sys::backtrace::BacktraceLock::print::h68d41b51481bce5c
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/sys/backtrace.rs:43:9
   8:     0x7f8bf4836bd1 - std::panicking::default_hook::{{closure}}::h96ab15e9936be7ed
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:269:22
   9:     0x7f8bf48368ac - std::panicking::default_hook::h3cacb9c27561ad33
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:296:9
  10:     0x7f8bf7b3b420 - std[1f2242ed6435445e]::panicking::update_hook::<alloc[7b1462a1eb55c293]::boxed::Box<rustc_driver_impl[8683aa37472b7dde]::install_ice_hook::{closure#0}>>::{closure#0}
  11:     0x7f8bf483759f - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::hce7569f4ca5d1b64
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/alloc/src/boxed.rs:2084:9
  12:     0x7f8bf483759f - std::panicking::rust_panic_with_hook::hfe205f6954b2c97b
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:808:13
  13:     0x7f8bf48371c7 - std::panicking::begin_panic_handler::{{closure}}::h6cb44b3a50f28c44
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:674:13
  14:     0x7f8bf48348a9 - std::sys::backtrace::__rust_end_short_backtrace::hf1c1f2a92799bb0e
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/sys/backtrace.rs:168:18
  15:     0x7f8bf4836e54 - rust_begin_unwind
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:665:5
  16:     0x7f8bf48804a3 - core::panicking::panic_fmt::h3d8fc78294164da7
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/panicking.rs:74:14
  17:     0x7f8bf48802fb - core::panicking::panic_display::h1c0e44fa90890272
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/panicking.rs:264:5
  18:     0x7f8bf48802fb - core::option::expect_failed::h3a757a693188cc6e
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/option.rs:2030:5
  19:     0x7f8bf7cc724d - <rustc_hir_analysis[c1a678e360ecab51]::coherence::orphan::TyVarReplacer as rustc_type_ir[6247808700e50c64]::fold::TypeFolder<rustc_middle[ba2289ab3ae064d4]::ty::context::TyCtxt>>::fold_ty
  20:     0x7f8bf7c3ee55 - <&rustc_middle[ba2289ab3ae064d4]::ty::list::RawList<(), rustc_middle[ba2289ab3ae064d4]::ty::generic_args::GenericArg> as rustc_type_ir[6247808700e50c64]::fold::TypeFoldable<rustc_middle[ba2289ab3ae064d4]::ty::context::TyCtxt>>::try_fold_with::<rustc_hir_analysis[c1a678e360ecab51]::coherence::orphan::TyVarReplacer>
  21:     0x7f8bf7cc6e75 - <rustc_hir_analysis[c1a678e360ecab51]::coherence::orphan::TyVarReplacer as rustc_type_ir[6247808700e50c64]::fold::TypeFolder<rustc_middle[ba2289ab3ae064d4]::ty::context::TyCtxt>>::fold_ty
  22:     0x7f8bf6a655ee - rustc_hir_analysis[c1a678e360ecab51]::coherence::orphan::orphan_check
  23:     0x7f8bf6a62581 - rustc_hir_analysis[c1a678e360ecab51]::coherence::orphan::orphan_check_impl
  24:     0x7f8bf954e8c7 - rustc_query_impl[3625cc0592f96219]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[3625cc0592f96219]::query_impl::orphan_check_impl::dynamic_query::{closure#2}::{closure#0}, rustc_middle[ba2289ab3ae064d4]::query::erase::Erased<[u8; 1usize]>>
  25:     0x7f8bf956adc7 - rustc_query_system[200ca28aa7d9732c]::query::plumbing::try_execute_query::<rustc_query_impl[3625cc0592f96219]::DynamicConfig<rustc_query_system[200ca28aa7d9732c]::query::caches::VecCache<rustc_span[28a649581f99a5bd]::def_id::LocalDefId, rustc_middle[ba2289ab3ae064d4]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[3625cc0592f96219]::plumbing::QueryCtxt, true>
  26:     0x7f8bf9c0a11d - rustc_query_impl[3625cc0592f96219]::query_impl::orphan_check_impl::get_query_incr::__rust_end_short_backtrace
  27:     0x7f8bf9c06ac9 - rustc_hir_analysis[c1a678e360ecab51]::coherence::coherent_trait
  28:     0x7f8bf9c0622f - rustc_query_impl[3625cc0592f96219]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[3625cc0592f96219]::query_impl::coherent_trait::dynamic_query::{closure#2}::{closure#0}, rustc_middle[ba2289ab3ae064d4]::query::erase::Erased<[u8; 1usize]>>
  29:     0x7f8bf956cd78 - rustc_query_system[200ca28aa7d9732c]::query::plumbing::try_execute_query::<rustc_query_impl[3625cc0592f96219]::DynamicConfig<rustc_query_system[200ca28aa7d9732c]::query::caches::DefIdCache<rustc_middle[ba2289ab3ae064d4]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[3625cc0592f96219]::plumbing::QueryCtxt, true>
  30:     0x7f8bf98c392f - rustc_query_impl[3625cc0592f96219]::query_impl::coherent_trait::get_query_incr::__rust_end_short_backtrace
  31:     0x7f8bf9c8a24e - rustc_hir_analysis[c1a678e360ecab51]::check::wfcheck::check_item
  32:     0x7f8bf9c85fbe - rustc_hir_analysis[c1a678e360ecab51]::check::wfcheck::check_well_formed
  33:     0x7f8bf9c85efb - rustc_query_impl[3625cc0592f96219]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[3625cc0592f96219]::query_impl::check_well_formed::dynamic_query::{closure#2}::{closure#0}, rustc_middle[ba2289ab3ae064d4]::query::erase::Erased<[u8; 1usize]>>
  34:     0x7f8bf9c82756 - rustc_query_system[200ca28aa7d9732c]::query::plumbing::try_execute_query::<rustc_query_impl[3625cc0592f96219]::DynamicConfig<rustc_query_system[200ca28aa7d9732c]::query::caches::VecCache<rustc_hir[22cf0f8ce801b62b]::hir_id::OwnerId, rustc_middle[ba2289ab3ae064d4]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[3625cc0592f96219]::plumbing::QueryCtxt, true>
  35:     0x7f8bf9c821f7 - rustc_query_impl[3625cc0592f96219]::query_impl::check_well_formed::get_query_incr::__rust_end_short_backtrace
  36:     0x7f8bf9c837dc - rustc_hir_analysis[c1a678e360ecab51]::check::wfcheck::check_mod_type_wf
  37:     0x7f8bf9c83697 - rustc_query_impl[3625cc0592f96219]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[3625cc0592f96219]::query_impl::check_mod_type_wf::dynamic_query::{closure#2}::{closure#0}, rustc_middle[ba2289ab3ae064d4]::query::erase::Erased<[u8; 1usize]>>
  38:     0x7f8bf98c7b2f - rustc_query_system[200ca28aa7d9732c]::query::plumbing::try_execute_query::<rustc_query_impl[3625cc0592f96219]::DynamicConfig<rustc_query_system[200ca28aa7d9732c]::query::caches::DefaultCache<rustc_span[28a649581f99a5bd]::def_id::LocalModDefId, rustc_middle[ba2289ab3ae064d4]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[3625cc0592f96219]::plumbing::QueryCtxt, true>
  39:     0x7f8bf98c76e4 - rustc_query_impl[3625cc0592f96219]::query_impl::check_mod_type_wf::get_query_incr::__rust_end_short_backtrace
  40:     0x7f8bf92e8519 - rustc_hir_analysis[c1a678e360ecab51]::check_crate
  41:     0x7f8bf98b8be6 - rustc_interface[53a414ae04dc6ffb]::passes::analysis
  42:     0x7f8bf98b85e7 - rustc_query_impl[3625cc0592f96219]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[3625cc0592f96219]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[ba2289ab3ae064d4]::query::erase::Erased<[u8; 1usize]>>
  43:     0x7f8bfa14418b - rustc_query_system[200ca28aa7d9732c]::query::plumbing::try_execute_query::<rustc_query_impl[3625cc0592f96219]::DynamicConfig<rustc_query_system[200ca28aa7d9732c]::query::caches::SingleCache<rustc_middle[ba2289ab3ae064d4]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[3625cc0592f96219]::plumbing::QueryCtxt, true>
  44:     0x7f8bfa143e78 - rustc_query_impl[3625cc0592f96219]::query_impl::analysis::get_query_incr::__rust_end_short_backtrace
  45:     0x7f8bf9dd4256 - rustc_interface[53a414ae04dc6ffb]::interface::run_compiler::<core[3cad2706d8bdcdc4]::result::Result<(), rustc_span[28a649581f99a5bd]::ErrorGuaranteed>, rustc_driver_impl[8683aa37472b7dde]::run_compiler::{closure#0}>::{closure#1}
  46:     0x7f8bf9d1b95b - std[1f2242ed6435445e]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[53a414ae04dc6ffb]::util::run_in_thread_with_globals<rustc_interface[53a414ae04dc6ffb]::interface::run_compiler<core[3cad2706d8bdcdc4]::result::Result<(), rustc_span[28a649581f99a5bd]::ErrorGuaranteed>, rustc_driver_impl[8683aa37472b7dde]::run_compiler::{closure#0}>::{closure#1}, core[3cad2706d8bdcdc4]::result::Result<(), rustc_span[28a649581f99a5bd]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[3cad2706d8bdcdc4]::result::Result<(), rustc_span[28a649581f99a5bd]::ErrorGuaranteed>>
  47:     0x7f8bf9d1b72a - <<std[1f2242ed6435445e]::thread::Builder>::spawn_unchecked_<rustc_interface[53a414ae04dc6ffb]::util::run_in_thread_with_globals<rustc_interface[53a414ae04dc6ffb]::interface::run_compiler<core[3cad2706d8bdcdc4]::result::Result<(), rustc_span[28a649581f99a5bd]::ErrorGuaranteed>, rustc_driver_impl[8683aa37472b7dde]::run_compiler::{closure#0}>::{closure#1}, core[3cad2706d8bdcdc4]::result::Result<(), rustc_span[28a649581f99a5bd]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[3cad2706d8bdcdc4]::result::Result<(), rustc_span[28a649581f99a5bd]::ErrorGuaranteed>>::{closure#1} as core[3cad2706d8bdcdc4]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  48:     0x7f8bf48415fb - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::ha1963004222e7822
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/alloc/src/boxed.rs:2070:9
  49:     0x7f8bf48415fb - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h1086ced1f7c494c2
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/alloc/src/boxed.rs:2070:9
  50:     0x7f8bf48415fb - std::sys::pal::unix::thread::Thread::new::thread_start::ha8af9c992ef0b208
                               at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/sys/pal/unix/thread.rs:108:17
  51:     0x7f8bf4748ea7 - start_thread
  52:     0x7f8bf4666acf - clone
  53:                0x0 - <unknown>

error: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.81.0 (eeb90cda1 2024-09-04) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED]

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [orphan_check_impl] checking whether impl `<impl at src/main.rs:17:1: 19:22>` follows the orphan rules
#1 [coherent_trait] coherence checking all impls of trait `core::convert::From`
end of query stack
error: could not compile `rust_bug` (bin "rust_bug")
```

<!--
Include a backtrace in the code block by setting `RUST_BACKTRACE=1` in your
environment. E.g. `RUST_BACKTRACE=1 cargo build`.
-->
<details><summary><strong>Backtrace</strong></summary>
<p>

```
thread 'rustc' panicked at compiler/rustc_hir_analysis/src/coherence/orphan.rs:554:60:
no entry found for key
stack backtrace:
   0: rust_begin_unwind
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:665:5
   1: core::panicking::panic_fmt
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/panicking.rs:74:14
   2: core::panicking::panic_display
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/panicking.rs:264:5
   3: core::option::expect_failed
             at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/option.rs:2030:5
   4: <rustc_hir_analysis::coherence::orphan::TyVarReplacer as rustc_type_ir::fold::TypeFolder<rustc_middle::ty::context::TyCtxt>>::fold_ty
   5: <&rustc_middle::ty::list::RawList<(), rustc_middle::ty::generic_args::GenericArg> as rustc_type_ir::fold::TypeFoldable<rustc_middle::ty::context::TyCtxt>>::try_fold_with::<rustc_hir_analysis::coherence::orphan::TyVarReplacer>
   6: <rustc_hir_analysis::coherence::orphan::TyVarReplacer as rustc_type_ir::fold::TypeFolder<rustc_middle::ty::context::TyCtxt>>::fold_ty
   7: rustc_hir_analysis::coherence::orphan::orphan_check
   8: rustc_hir_analysis::coherence::orphan::orphan_check_impl
      [... omitted 1 frame ...]
   9: rustc_hir_analysis::coherence::coherent_trait
      [... omitted 1 frame ...]
  10: rustc_hir_analysis::check::wfcheck::check_item
  11: rustc_hir_analysis::check::wfcheck::check_well_formed
      [... omitted 1 frame ...]
  12: rustc_hir_analysis::check::wfcheck::check_mod_type_wf
      [... omitted 1 frame ...]
  13: rustc_hir_analysis::check_crate
  14: rustc_interface::passes::analysis
      [... omitted 1 frame ...]
  15: rustc_interface::interface::run_compiler::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

error: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.81.0 (eeb90cda1 2024-09-04) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED]

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [orphan_check_impl] checking whether impl `<impl at src/main.rs:17:1: 19:22>` follows the orphan rules
#1 [coherent_trait] coherence checking all impls of trait `core::convert::From`
#2 [check_well_formed] checking that `<impl at src/main.rs:17:1: 19:22>` is well-formed
#3 [check_mod_type_wf] checking that types are well-formed in top-level module
#4 [analysis] running analysis passes on this crate
end of query stack
```

</p>
</details>