Repository: rust
Issue: ICE: `Encountered error SignatureMismatch`
Labels: I-ICE,T-compiler,C-bug,A-mir-opt-inlining,S-bug-has-test,T-types

<!--
[31mICE[0m: Rustc ./a.rs '-Zvalidate-mir -Zinline-mir=yes -ooutputfile -Zdump-mir-dir=dir' 'error: internal compiler error: compiler/rustc_traits/src/codegen.rs:45:13: Encountered error `SignatureMismatch(SignatureMismatchData { found_trait_ref: <{closure@/tmp/im/a.rs:17:15: 17:20} as std::ops::FnMut<(i32, i32)>>, expected_trait_ref: <{closure@/tmp/im/a.rs:17:15: 17:20} as std::ops::FnMut<(i32, i16)>>, terr: Sorts(ExpectedFound { expected: i16, found: i32 }) })` selecting `<{closure@/tmp/im/a.rs:17:15: 17:20} as std::ops::FnMut<(i32, i16)>>` during codegen', 'error: internal compiler error: compiler/rustc_traits/src/codegen.rs:45:13: Encountered error `SignatureMismatch(SignatureMismatchData { found_trait_ref: <{closure@/tmp/im/a.rs:17:15: 17:20} as std::ops::FnMut<(i32, i32)>>, expected_trait_ref: <{closure@/tmp/im/a.rs:17:15: 17:20} as std::ops::FnMut<(i32, i16)>>, terr: Sorts(ExpectedFound { expected: i16, found: i32 }) })` selecting `<{closure@/tmp/im/a.rs:17:15: 17:20} as std::ops::FnMut<(i32, i16)>>` during codegen'
File: /tmp/im/a.rs
-->

auto-reduced (treereduce-rust):
````rust
//@compile-flags: -Zvalidate-mir -Zinline-mir=yes
use std::vec::IntoIter;

pub(crate) trait Foo: Iterator<Item = <Self as Foo>::Key> {
    type Key;
}

impl Foo for IntoIter<i16> {}

fn sum_foo<F: Foo<Key = i32>>(f: F) -> i32 {
    f.fold(0, |a, b| a + b)
}

fn main() {
    let x = sum_foo(vec![11, 10, 1].into_iter());
}

````

original:
````rust
//@ run-pass
// Test case where an associated type is referenced from within the
// supertrait definition. Issue #20220.


use std::vec::IntoIter;

pub(crate) trait Foo: Iterator<Item=<Self as Foo>::Key> {
    type Key;
}

impl Foo for IntoIter<i16> {
    type Key = i32;
}

fn sum_foo<F:Foo<Key=i32>>(f: F) -> i32 {
    f.fold(0, |a,b| a + b)
}

fn main() {
    let x = sum_foo(vec![11, 10, 1].into_iter());
    assert_eq!(x, 22);
}

````

Version information
````
rustc 1.85.0-nightly (5a6036a18 2024-12-11)
binary: rustc
commit-hash: 5a6036a1802262f8cf02192b02026688d396f1d7
commit-date: 2024-12-11
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.5

````

Possibly related line of code:
https://github.com/rust-lang/rust/blob/5a6036a1802262f8cf02192b02026688d396f1d7/compiler/rustc_traits/src/codegen.rs#L39-L51

Command:
`/home/matthias/.rustup/toolchains/master/bin/rustc -Zvalidate-mir -Zinline-mir=yes`

<details><summary><strong>Program output</strong></summary>
<p>

```
error[E0046]: not all trait items implemented, missing: `Key`
 --> /tmp/icemaker_global_tempdir.xwRxwlsen7Ee/rustc_testrunner_tmpdir_reporting.HhkZSZGzNZAb/mvce.rs:7:1
  |
4 |     type Key;
  |     -------- `Key` from trait
...
7 | impl Foo for IntoIter<i16> {}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `Key` in implementation

warning: unused variable: `x`
  --> /tmp/icemaker_global_tempdir.xwRxwlsen7Ee/rustc_testrunner_tmpdir_reporting.HhkZSZGzNZAb/mvce.rs:14:9
   |
14 |     let x = sum_foo(vec![11, 10, 1].into_iter());
   |         ^ help: if this is intentional, prefix it with an underscore: `_x`
   |
   = note: `#[warn(unused_variables)]` on by default

error: internal compiler error: compiler/rustc_traits/src/codegen.rs:45:13: Encountered error `SignatureMismatch(SignatureMismatchData { found_trait_ref: <{closure@/tmp/icemaker_global_tempdir.xwRxwlsen7Ee/rustc_testrunner_tmpdir_reporting.HhkZSZGzNZAb/mvce.rs:10:15: 10:21} as std::ops::FnMut<(i32, i32)>>, expected_trait_ref: <{closure@/tmp/icemaker_global_tempdir.xwRxwlsen7Ee/rustc_testrunner_tmpdir_reporting.HhkZSZGzNZAb/mvce.rs:10:15: 10:21} as std::ops::FnMut<(i32, i16)>>, terr: Sorts(ExpectedFound { expected: i16, found: i32 }) })` selecting `<{closure@/tmp/icemaker_global_tempdir.xwRxwlsen7Ee/rustc_testrunner_tmpdir_reporting.HhkZSZGzNZAb/mvce.rs:10:15: 10:21} as std::ops::FnMut<(i32, i16)>>` during codegen

thread 'rustc' panicked at compiler/rustc_traits/src/codegen.rs:45:13:
Box<dyn Any>
stack backtrace:
   0:     0x7bfe54f5c11a - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::hc45fac407a6ff7a2
   1:     0x7bfe55613c26 - core::fmt::write::h06d264594044da0f
   2:     0x7bfe565c8a51 - std::io::Write::write_fmt::h996e36892eed4c22
   3:     0x7bfe54f5bf72 - std::sys::backtrace::BacktraceLock::print::hef4faa4abc10c4e1
   4:     0x7bfe54f5e48a - std::panicking::default_hook::{{closure}}::h93c9dc5f751f518c
   5:     0x7bfe54f5e2d3 - std::panicking::default_hook::h7e0cdf55d7d92b2f
   6:     0x7bfe540c1bc8 - std[2b58fe5c94a29799]::panicking::update_hook::<alloc[fb981c72c3bff65d]::boxed::Box<rustc_driver_impl[497e5fb2fdc9e592]::install_ice_hook::{closure#0}>>::{closure#0}
   7:     0x7bfe54f5ec88 - std::panicking::rust_panic_with_hook::he1642df02554cf98
   8:     0x7bfe540f77d1 - std[2b58fe5c94a29799]::panicking::begin_panic::<rustc_errors[252cf4afcb204d9b]::ExplicitBug>::{closure#0}
   9:     0x7bfe540ec976 - std[2b58fe5c94a29799]::sys::backtrace::__rust_end_short_backtrace::<std[2b58fe5c94a29799]::panicking::begin_panic<rustc_errors[252cf4afcb204d9b]::ExplicitBug>::{closure#0}, !>
  10:     0x7bfe540e9409 - std[2b58fe5c94a29799]::panicking::begin_panic::<rustc_errors[252cf4afcb204d9b]::ExplicitBug>
  11:     0x7bfe54101771 - <rustc_errors[252cf4afcb204d9b]::diagnostic::BugAbort as rustc_errors[252cf4afcb204d9b]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  12:     0x7bfe546f0ad3 - rustc_middle[fc7bfb690a1873b0]::util::bug::opt_span_bug_fmt::<rustc_span[7131546b166d82b0]::span_encoding::Span>::{closure#0}
  13:     0x7bfe546d69da - rustc_middle[fc7bfb690a1873b0]::ty::context::tls::with_opt::<rustc_middle[fc7bfb690a1873b0]::util::bug::opt_span_bug_fmt<rustc_span[7131546b166d82b0]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  14:     0x7bfe546d686b - rustc_middle[fc7bfb690a1873b0]::ty::context::tls::with_context_opt::<rustc_middle[fc7bfb690a1873b0]::ty::context::tls::with_opt<rustc_middle[fc7bfb690a1873b0]::util::bug::opt_span_bug_fmt<rustc_span[7131546b166d82b0]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  15:     0x7bfe529ee880 - rustc_middle[fc7bfb690a1873b0]::util::bug::bug_fmt
  16:     0x7bfe52d8cfcf - rustc_traits[84112b02c32ecba7]::codegen::codegen_select_candidate
  17:     0x7bfe55fd6951 - rustc_query_impl[cb988c35710418b0]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[cb988c35710418b0]::query_impl::codegen_select_candidate::dynamic_query::{closure#2}::{closure#0}, rustc_middle[fc7bfb690a1873b0]::query::erase::Erased<[u8; 16usize]>>
  18:     0x7bfe55fd6cf7 - rustc_query_system[104428f5be780cfb]::query::plumbing::try_execute_query::<rustc_query_impl[cb988c35710418b0]::DynamicConfig<rustc_query_system[104428f5be780cfb]::query::caches::DefaultCache<rustc_middle[fc7bfb690a1873b0]::ty::PseudoCanonicalInput<rustc_type_ir[d093507361b4f614]::predicate::TraitRef<rustc_middle[fc7bfb690a1873b0]::ty::context::TyCtxt>>, rustc_middle[fc7bfb690a1873b0]::query::erase::Erased<[u8; 16usize]>>, false, false, false>, rustc_query_impl[cb988c35710418b0]::plumbing::QueryCtxt, false>
  19:     0x7bfe55fd68ef - rustc_query_impl[cb988c35710418b0]::query_impl::codegen_select_candidate::get_query_non_incr::__rust_end_short_backtrace
  20:     0x7bfe530dd6e0 - rustc_ty_utils[7184913b0844be97]::instance::resolve_instance_raw
  21:     0x7bfe55910632 - rustc_query_impl[cb988c35710418b0]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[cb988c35710418b0]::query_impl::resolve_instance_raw::dynamic_query::{closure#2}::{closure#0}, rustc_middle[fc7bfb690a1873b0]::query::erase::Erased<[u8; 32usize]>>
  22:     0x7bfe559109ec - rustc_query_system[104428f5be780cfb]::query::plumbing::try_execute_query::<rustc_query_impl[cb988c35710418b0]::DynamicConfig<rustc_query_system[104428f5be780cfb]::query::caches::DefaultCache<rustc_middle[fc7bfb690a1873b0]::ty::PseudoCanonicalInput<(rustc_span[7131546b166d82b0]::def_id::DefId, &rustc_middle[fc7bfb690a1873b0]::ty::list::RawList<(), rustc_middle[fc7bfb690a1873b0]::ty::generic_args::GenericArg>)>, rustc_middle[fc7bfb690a1873b0]::query::erase::Erased<[u8; 32usize]>>, false, false, false>, rustc_query_impl[cb988c35710418b0]::plumbing::QueryCtxt, false>
  23:     0x7bfe559105b0 - rustc_query_impl[cb988c35710418b0]::query_impl::resolve_instance_raw::get_query_non_incr::__rust_end_short_backtrace
  24:     0x7bfe5565955f - <rustc_middle[fc7bfb690a1873b0]::ty::instance::Instance>::try_resolve
  25:     0x7bfe561b3626 - rustc_mir_transform[cc0fe7e8983fa4e4]::inline::cycle::mir_callgraph_reachable::process
  26:     0x7bfe561b435a - rustc_mir_transform[cc0fe7e8983fa4e4]::inline::cycle::mir_callgraph_reachable::process
  27:     0x7bfe561b2fb1 - rustc_mir_transform[cc0fe7e8983fa4e4]::inline::cycle::mir_callgraph_reachable
  28:     0x7bfe561b2ea9 - rustc_query_impl[cb988c35710418b0]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[cb988c35710418b0]::query_impl::mir_callgraph_reachable::dynamic_query::{closure#2}::{closure#0}, rustc_middle[fc7bfb690a1873b0]::query::erase::Erased<[u8; 1usize]>>
  29:     0x7bfe561b2e6b - <rustc_query_impl[cb988c35710418b0]::query_impl::mir_callgraph_reachable::dynamic_query::{closure#2} as core[9c63f579735f14fb]::ops::function::FnOnce<(rustc_middle[fc7bfb690a1873b0]::ty::context::TyCtxt, (rustc_middle[fc7bfb690a1873b0]::ty::instance::Instance, rustc_span[7131546b166d82b0]::def_id::LocalDefId))>>::call_once
  30:     0x7bfe561b21e1 - rustc_query_system[104428f5be780cfb]::query::plumbing::try_execute_query::<rustc_query_impl[cb988c35710418b0]::DynamicConfig<rustc_query_system[104428f5be780cfb]::query::caches::DefaultCache<(rustc_middle[fc7bfb690a1873b0]::ty::instance::Instance, rustc_span[7131546b166d82b0]::def_id::LocalDefId), rustc_middle[fc7bfb690a1873b0]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[cb988c35710418b0]::plumbing::QueryCtxt, false>
  31:     0x7bfe561b1f33 - rustc_query_impl[cb988c35710418b0]::query_impl::mir_callgraph_reachable::get_query_non_incr::__rust_end_short_backtrace
  32:     0x7bfe5612f23a - <rustc_mir_transform[cc0fe7e8983fa4e4]::inline::Inliner>::try_inlining
  33:     0x7bfe56120c68 - <rustc_mir_transform[cc0fe7e8983fa4e4]::inline::Inliner>::process_blocks
  34:     0x7bfe56120119 - <rustc_mir_transform[cc0fe7e8983fa4e4]::inline::Inline as rustc_mir_transform[cc0fe7e8983fa4e4]::pass_manager::MirPass>::run_pass
  35:     0x7bfe55607eee - rustc_mir_transform[cc0fe7e8983fa4e4]::pass_manager::run_passes_inner
  36:     0x7bfe55bae8e0 - rustc_mir_transform[cc0fe7e8983fa4e4]::optimized_mir
  37:     0x7bfe55bae1b1 - rustc_query_impl[cb988c35710418b0]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[cb988c35710418b0]::query_impl::optimized_mir::dynamic_query::{closure#2}::{closure#0}, rustc_middle[fc7bfb690a1873b0]::query::erase::Erased<[u8; 8usize]>>
  38:     0x7bfe55641d5f - rustc_query_system[104428f5be780cfb]::query::plumbing::try_execute_query::<rustc_query_impl[cb988c35710418b0]::DynamicConfig<rustc_query_system[104428f5be780cfb]::query::caches::DefIdCache<rustc_middle[fc7bfb690a1873b0]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[cb988c35710418b0]::plumbing::QueryCtxt, false>
  39:     0x7bfe5564121f - rustc_query_impl[cb988c35710418b0]::query_impl::optimized_mir::get_query_non_incr::__rust_end_short_backtrace
  40:     0x7bfe52c9d589 - <rustc_middle[fc7bfb690a1873b0]::ty::context::TyCtxt>::instance_mir
  41:     0x7bfe55a80a58 - rustc_interface[e1f6b3f7cc83500f]::passes::run_required_analyses
  42:     0x7bfe5655351e - rustc_interface[e1f6b3f7cc83500f]::passes::analysis
  43:     0x7bfe565534ef - rustc_query_impl[cb988c35710418b0]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[cb988c35710418b0]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[fc7bfb690a1873b0]::query::erase::Erased<[u8; 0usize]>>
  44:     0x7bfe56635915 - rustc_query_system[104428f5be780cfb]::query::plumbing::try_execute_query::<rustc_query_impl[cb988c35710418b0]::DynamicConfig<rustc_query_system[104428f5be780cfb]::query::caches::SingleCache<rustc_middle[fc7bfb690a1873b0]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[cb988c35710418b0]::plumbing::QueryCtxt, false>
  45:     0x7bfe5663564e - rustc_query_impl[cb988c35710418b0]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  46:     0x7bfe5668016e - rustc_interface[e1f6b3f7cc83500f]::interface::run_compiler::<(), rustc_driver_impl[497e5fb2fdc9e592]::run_compiler::{closure#0}>::{closure#1}
  47:     0x7bfe564ef507 - std[2b58fe5c94a29799]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[e1f6b3f7cc83500f]::util::run_in_thread_with_globals<rustc_interface[e1f6b3f7cc83500f]::util::run_in_thread_pool_with_globals<rustc_interface[e1f6b3f7cc83500f]::interface::run_compiler<(), rustc_driver_impl[497e5fb2fdc9e592]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  48:     0x7bfe564ef9a2 - <<std[2b58fe5c94a29799]::thread::Builder>::spawn_unchecked_<rustc_interface[e1f6b3f7cc83500f]::util::run_in_thread_with_globals<rustc_interface[e1f6b3f7cc83500f]::util::run_in_thread_pool_with_globals<rustc_interface[e1f6b3f7cc83500f]::interface::run_compiler<(), rustc_driver_impl[497e5fb2fdc9e592]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[9c63f579735f14fb]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  49:     0x7bfe564f0f6f - std::sys::pal::unix::thread::Thread::new::thread_start::hef76b0be97717a17
  50:     0x7bfe5086a39d - <unknown>
  51:     0x7bfe508ef49c - <unknown>
  52:                0x0 - <unknown>

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: please make sure that you have updated to the latest nightly

note: rustc 1.85.0-nightly (5a6036a18 2024-12-11) running on x86_64-unknown-linux-gnu

note: compiler flags: -Z validate-mir -Z inline-mir=yes -Z dump-mir-dir=dir

query stack during panic:
#0 [codegen_select_candidate] computing candidate for `<{closure@/tmp/icemaker_global_tempdir.xwRxwlsen7Ee/rustc_testrunner_tmpdir_reporting.HhkZSZGzNZAb/mvce.rs:10:15: 10:21} as core::ops::function::FnMut<(i32, i16)>>`
#1 [resolve_instance_raw] resolving instance `<{closure@/tmp/icemaker_global_tempdir.xwRxwlsen7Ee/rustc_testrunner_tmpdir_reporting.HhkZSZGzNZAb/mvce.rs:10:15: 10:21} as core::ops::function::FnMut<(i32, i16)>>::call_mut`
end of query stack
error: aborting due to 2 previous errors; 1 warning emitted

For more information about this error, try `rustc --explain E0046`.

```

</p>
</details>

<!--
query stack:
#0 [codegen_select_candidate] computing candidate for `<{closure@/tmp/icemaker_global_tempdir.xwRxwlsen7Ee/rustc_testrunner_tmpdir_reporting.HhkZSZGzNZAb/mvce.rs:10:15: 10:21} as core::ops::function::FnMut<(i32, i16)>>`
#1 [resolve_instance_raw] resolving instance `<{closure@/tmp/icemaker_global_tempdir.xwRxwlsen7Ee/rustc_testrunner_tmpdir_reporting.HhkZSZGzNZAb/mvce.rs:10:15: 10:21} as core::ops::function::FnMut<(i32, i16)>>::call_mut`

-->