Repository: rust
Issue: ICE: delegation: `unexpected parent of trait or impl item or item not found`
Labels: I-ICE,T-compiler,C-bug,S-bug-has-test,F-fn_delegation

<!--
[31mICE[0m: Rustc ./a.rs '' 'error: internal compiler error: compiler/rustc_ty_utils/src/assoc.rs:127:5: unexpected parent of trait or impl item or item not found: Mod(Mod { spans: ModSpans { inner_span: /tmp/im/a.rs:12:5: 15:2 (#0), inject_use_span: /tmp/im/a.rs:12:5: 12:5 (#0) }, item_ids: [ItemId { owner_id: DefId(0:10 ~ a[47be]::to_reuse::{use#0}) }, ItemId { owner_id: DefId(0:11 ~ a[47be]::to_reuse::foo) }] })', 'error: internal compiler error: compiler/rustc_ty_utils/src/assoc.rs:127:5: unexpected parent of trait or impl item or item not found: Mod(Mod { spans: ModSpans { inner_span: /tmp/im/a.rs:12:5: 15:2 (#0), inject_use_span: /tmp/im/a.rs:12:5: 12:5 (#0) }, item_ids: [ItemId { owner_id: DefId(0:10 ~ a[47be]::to_reuse::{use#0}) }, ItemId { owner_id: DefId(0:11 ~ a[47be]::to_reuse::foo) }] })'
File: /tmp/im/a.rs
-->

auto-reduced (treereduce-rust):
````rust

trait Trait {
    fn foo(&self) -> u32 { 0 }
}

struct F;
struct S;

mod to_reuse {
    pub fn foo(&self) -> u32 {}
}

impl Trait  S {
    reuse to_reuse::foo { self }
}


````

original:
````rust
#![feature(fn_delegation)]
#![allow(incomplete_features)]

trait Trait {
    fn foo(&self) -> u32 { 0 }
}

struct F;
struct S;

mod to_reuse {
    use crate::S;

    pub fn foo(&self) -> u32 {}
}

impl Trait  S {
    reuse to_reuse::foo { self }
    reuse to_reuse::foo;
    //~^ ERROR  duplicate definitions with name `foo`
}

fn main() { 0 }

````

Version information
````
rustc 1.81.0-nightly (52f3c71c8 2024-07-18)
binary: rustc
commit-hash: 52f3c71c8dc4aaed71e3035995fcbdd6d78c98c6
commit-date: 2024-07-18
host: x86_64-unknown-linux-gnu
release: 1.81.0-nightly
LLVM version: 18.1.7

````

Command:
`/home/matthias/.rustup/toolchains/master/bin/rustc `

<!--
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>Program output</strong></summary>
<p>

```
error: missing `for` in a trait impl
  --> /tmp/icemaker_global_tempdir.n4JRPjMXSZEC/rustc_testrunner_tmpdir_reporting.Tsi6i8ZZpYcI/mvce.rs:17:11
   |
17 | impl Trait  S {
   |           ^^
   |
help: add `for` here
   |
17 | impl Trait for S {
   |            +++

error: `self` parameter is only allowed in associated functions
  --> /tmp/icemaker_global_tempdir.n4JRPjMXSZEC/rustc_testrunner_tmpdir_reporting.Tsi6i8ZZpYcI/mvce.rs:14:16
   |
14 |     pub fn foo(&self) -> u32 {}
   |                ^^^^^ not semantically valid as function parameter
   |
   = note: associated functions are those in `impl` or `trait` definitions

error[E0658]: functions delegation is not yet fully implemented
  --> /tmp/icemaker_global_tempdir.n4JRPjMXSZEC/rustc_testrunner_tmpdir_reporting.Tsi6i8ZZpYcI/mvce.rs:18:5
   |
18 |     reuse to_reuse::foo { self }
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: see issue #118212 <https://github.com/rust-lang/rust/issues/118212> for more information
   = help: add `#![feature(fn_delegation)]` to the crate attributes to enable
   = note: this compiler was built on 2024-07-18; consider upgrading it if it is out of date

error[E0601]: `main` function not found in crate `mvce`
  --> /tmp/icemaker_global_tempdir.n4JRPjMXSZEC/rustc_testrunner_tmpdir_reporting.Tsi6i8ZZpYcI/mvce.rs:21:2
   |
21 | }
   |  ^ consider adding a `main` function to `/tmp/icemaker_global_tempdir.n4JRPjMXSZEC/rustc_testrunner_tmpdir_reporting.Tsi6i8ZZpYcI/mvce.rs`

error[E0308]: mismatched types
  --> /tmp/icemaker_global_tempdir.n4JRPjMXSZEC/rustc_testrunner_tmpdir_reporting.Tsi6i8ZZpYcI/mvce.rs:14:26
   |
14 |     pub fn foo(&self) -> u32 {}
   |            ---           ^^^ expected `u32`, found `()`
   |            |
   |            implicitly returns `()` as its body has no tail or `return` expression

error: internal compiler error: compiler/rustc_ty_utils/src/assoc.rs:127:5: unexpected parent of trait or impl item or item not found: Mod(Mod { spans: ModSpans { inner_span: /tmp/icemaker_global_tempdir.n4JRPjMXSZEC/rustc_testrunner_tmpdir_reporting.Tsi6i8ZZpYcI/mvce.rs:14:5: 15:2 (#0), inject_use_span: /tmp/icemaker_global_tempdir.n4JRPjMXSZEC/rustc_testrunner_tmpdir_reporting.Tsi6i8ZZpYcI/mvce.rs:14:5: 14:5 (#0) }, item_ids: [ItemId { owner_id: DefId(0:10 ~ mvce[d169]::to_reuse::foo) }] })
  --> /tmp/icemaker_global_tempdir.n4JRPjMXSZEC/rustc_testrunner_tmpdir_reporting.Tsi6i8ZZpYcI/mvce.rs:11:1
   |
11 | / mod to_reuse {
12 | |     
13 | |
14 | |     pub fn foo(&self) -> u32 {}
15 | | }
   | |_^

thread 'rustc' panicked at compiler/rustc_ty_utils/src/assoc.rs:127:5:
Box<dyn Any>
stack backtrace:
   0:     0x7933ea2a13e5 - std::backtrace_rs::backtrace::libunwind::trace::hc272dcb5eaa8302d
                               at /rustc/52f3c71c8dc4aaed71e3035995fcbdd6d78c98c6/library/std/src/../../backtrace/src/backtrace/libunwind.rs:116:5
   1:     0x7933ea2a13e5 - std::backtrace_rs::backtrace::trace_unsynchronized::h691a0ef10f8dc9a8
                               at /rustc/52f3c71c8dc4aaed71e3035995fcbdd6d78c98c6/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7933ea2a13e5 - std::sys::backtrace::_print_fmt::ha47904fbec98bf60
                               at /rustc/52f3c71c8dc4aaed71e3035995fcbdd6d78c98c6/library/std/src/sys/backtrace.rs:65:5
   3:     0x7933ea2a13e5 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h0e7a639705968ce6
                               at /rustc/52f3c71c8dc4aaed71e3035995fcbdd6d78c98c6/library/std/src/sys/backtrace.rs:40:26
   4:     0x7933ea2f0e3b - core::fmt::rt::Argument::fmt::h4d320f08a40785a8
                               at /rustc/52f3c71c8dc4aaed71e3035995fcbdd6d78c98c6/library/core/src/fmt/rt.rs:173:76
   5:     0x7933ea2f0e3b - core::fmt::write::hd7ed7bd021505a5c
                               at /rustc/52f3c71c8dc4aaed71e3035995fcbdd6d78c98c6/library/core/src/fmt/mod.rs:1182:21
   6:     0x7933ea295f4f - std::io::Write::write_fmt::hd51bd05baef3855c
                               at /rustc/52f3c71c8dc4aaed71e3035995fcbdd6d78c98c6/library/std/src/io/mod.rs:1827:15
   7:     0x7933ea2a3bd1 - std::sys::backtrace::BacktraceLock::print::h680bd12acab7503d
                               at /rustc/52f3c71c8dc4aaed71e3035995fcbdd6d78c98c6/library/std/src/sys/backtrace.rs:43:9
   8:     0x7933ea2a3bd1 - std::panicking::default_hook::{{closure}}::h0ef49fd74d19887c
                               at /rustc/52f3c71c8dc4aaed71e3035995fcbdd6d78c98c6/library/std/src/panicking.rs:269:22
   9:     0x7933ea2a38ac - std::panicking::default_hook::h90696ad068a27ec2
                               at /rustc/52f3c71c8dc4aaed71e3035995fcbdd6d78c98c6/library/std/src/panicking.rs:296:9
  10:     0x7933e681c48a - std[3252865f4bda34c5]::panicking::update_hook::<alloc[da3b8f8ba9a896aa]::boxed::Box<rustc_driver_impl[f13dd90190e9dbe1]::install_ice_hook::{closure#0}>>::{closure#0}
  11:     0x7933ea2a459f - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::haab09738ddd9b211
                               at /rustc/52f3c71c8dc4aaed71e3035995fcbdd6d78c98c6/library/alloc/src/boxed.rs:2084:9
  12:     0x7933ea2a459f - std::panicking::rust_panic_with_hook::hffc45dfe647fb2c9
                               at /rustc/52f3c71c8dc4aaed71e3035995fcbdd6d78c98c6/library/std/src/panicking.rs:808:13
  13:     0x7933e68575b1 - std[3252865f4bda34c5]::panicking::begin_panic::<rustc_errors[67cfb45084a69b61]::ExplicitBug>::{closure#0}
  14:     0x7933e684a3b6 - std[3252865f4bda34c5]::sys::backtrace::__rust_end_short_backtrace::<std[3252865f4bda34c5]::panicking::begin_panic<rustc_errors[67cfb45084a69b61]::ExplicitBug>::{closure#0}, !>
  15:     0x7933e6845686 - std[3252865f4bda34c5]::panicking::begin_panic::<rustc_errors[67cfb45084a69b61]::ExplicitBug>
  16:     0x7933e6860a31 - <rustc_errors[67cfb45084a69b61]::diagnostic::BugAbort as rustc_errors[67cfb45084a69b61]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  17:     0x7933e75c821d - <rustc_errors[67cfb45084a69b61]::DiagCtxtHandle>::span_bug::<rustc_span[dacc065a687a0f9e]::span_encoding::Span, alloc[da3b8f8ba9a896aa]::string::String>
  18:     0x7933e75e4928 - rustc_middle[dd9bfa5285b6d2e6]::util::bug::opt_span_bug_fmt::<rustc_span[dacc065a687a0f9e]::span_encoding::Span>::{closure#0}
  19:     0x7933e75e495a - rustc_middle[dd9bfa5285b6d2e6]::ty::context::tls::with_opt::<rustc_middle[dd9bfa5285b6d2e6]::util::bug::opt_span_bug_fmt<rustc_span[dacc065a687a0f9e]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  20:     0x7933e75d2a5b - rustc_middle[dd9bfa5285b6d2e6]::ty::context::tls::with_context_opt::<rustc_middle[dd9bfa5285b6d2e6]::ty::context::tls::with_opt<rustc_middle[dd9bfa5285b6d2e6]::util::bug::opt_span_bug_fmt<rustc_span[dacc065a687a0f9e]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  21:     0x7933e75d1d07 - rustc_middle[dd9bfa5285b6d2e6]::util::bug::span_bug_fmt::<rustc_span[dacc065a687a0f9e]::span_encoding::Span>
  22:     0x7933e92feb6d - rustc_query_impl[a7dfaff16252b164]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a7dfaff16252b164]::query_impl::associated_item::dynamic_query::{closure#2}::{closure#0}, rustc_middle[dd9bfa5285b6d2e6]::query::erase::Erased<[u8; 40usize]>>.cold
  23:     0x7933e8108343 - rustc_query_system[d599853b3d31bcb6]::query::plumbing::try_execute_query::<rustc_query_impl[a7dfaff16252b164]::DynamicConfig<rustc_query_system[d599853b3d31bcb6]::query::caches::DefIdCache<rustc_middle[dd9bfa5285b6d2e6]::query::erase::Erased<[u8; 40usize]>>, false, false, false>, rustc_query_impl[a7dfaff16252b164]::plumbing::QueryCtxt, false>
  24:     0x7933e8108023 - rustc_query_impl[a7dfaff16252b164]::query_impl::associated_item::get_query_non_incr::__rust_end_short_backtrace
  25:     0x7933e6a2dab9 - rustc_middle[dd9bfa5285b6d2e6]::query::plumbing::query_get_at::<rustc_query_system[d599853b3d31bcb6]::query::caches::DefIdCache<rustc_middle[dd9bfa5285b6d2e6]::query::erase::Erased<[u8; 40usize]>>>
  26:     0x7933e474a69f - <rustc_hir_typeck[245a4534b1520086]::fn_ctxt::FnCtxt>::lookup_probe
  27:     0x7933e8888d17 - <rustc_hir_typeck[245a4534b1520086]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  28:     0x7933e8883023 - <rustc_hir_typeck[245a4534b1520086]::fn_ctxt::FnCtxt>::check_block_with_expected
  29:     0x7933e8888c0b - <rustc_hir_typeck[245a4534b1520086]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  30:     0x7933e865be15 - rustc_hir_typeck[245a4534b1520086]::check::check_fn
  31:     0x7933e8651783 - rustc_hir_typeck[245a4534b1520086]::typeck
  32:     0x7933e8651129 - rustc_query_impl[a7dfaff16252b164]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a7dfaff16252b164]::query_impl::typeck::dynamic_query::{closure#2}::{closure#0}, rustc_middle[dd9bfa5285b6d2e6]::query::erase::Erased<[u8; 8usize]>>
  33:     0x7933e808c3f3 - rustc_query_system[d599853b3d31bcb6]::query::plumbing::try_execute_query::<rustc_query_impl[a7dfaff16252b164]::DynamicConfig<rustc_query_system[d599853b3d31bcb6]::query::caches::VecCache<rustc_span[dacc065a687a0f9e]::def_id::LocalDefId, rustc_middle[dd9bfa5285b6d2e6]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[a7dfaff16252b164]::plumbing::QueryCtxt, false>
  34:     0x7933e808b5cd - rustc_query_impl[a7dfaff16252b164]::query_impl::typeck::get_query_non_incr::__rust_end_short_backtrace
  35:     0x7933e808b1c4 - <rustc_middle[dd9bfa5285b6d2e6]::hir::map::Map>::par_body_owners::<rustc_hir_analysis[cb950f77af301e67]::check_crate::{closure#4}>::{closure#0}
  36:     0x7933e8088ffc - rustc_hir_analysis[cb950f77af301e67]::check_crate
  37:     0x7933e826f095 - rustc_interface[dbb229baa21df1c0]::passes::analysis
  38:     0x7933e826ec47 - rustc_query_impl[a7dfaff16252b164]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a7dfaff16252b164]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[dd9bfa5285b6d2e6]::query::erase::Erased<[u8; 1usize]>>
  39:     0x7933e8cec225 - rustc_query_system[d599853b3d31bcb6]::query::plumbing::try_execute_query::<rustc_query_impl[a7dfaff16252b164]::DynamicConfig<rustc_query_system[d599853b3d31bcb6]::query::caches::SingleCache<rustc_middle[dd9bfa5285b6d2e6]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[a7dfaff16252b164]::plumbing::QueryCtxt, false>
  40:     0x7933e8cebf8f - rustc_query_impl[a7dfaff16252b164]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  41:     0x7933e8c0a345 - rustc_interface[dbb229baa21df1c0]::interface::run_compiler::<core[67e71a49bc1f0e88]::result::Result<(), rustc_span[dacc065a687a0f9e]::ErrorGuaranteed>, rustc_driver_impl[f13dd90190e9dbe1]::run_compiler::{closure#0}>::{closure#1}
  42:     0x7933e8ba3189 - std[3252865f4bda34c5]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[dbb229baa21df1c0]::util::run_in_thread_with_globals<rustc_interface[dbb229baa21df1c0]::util::run_in_thread_pool_with_globals<rustc_interface[dbb229baa21df1c0]::interface::run_compiler<core[67e71a49bc1f0e88]::result::Result<(), rustc_span[dacc065a687a0f9e]::ErrorGuaranteed>, rustc_driver_impl[f13dd90190e9dbe1]::run_compiler::{closure#0}>::{closure#1}, core[67e71a49bc1f0e88]::result::Result<(), rustc_span[dacc065a687a0f9e]::ErrorGuaranteed>>::{closure#0}, core[67e71a49bc1f0e88]::result::Result<(), rustc_span[dacc065a687a0f9e]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[67e71a49bc1f0e88]::result::Result<(), rustc_span[dacc065a687a0f9e]::ErrorGuaranteed>>
  43:     0x7933e8ba2f3a - <<std[3252865f4bda34c5]::thread::Builder>::spawn_unchecked_<rustc_interface[dbb229baa21df1c0]::util::run_in_thread_with_globals<rustc_interface[dbb229baa21df1c0]::util::run_in_thread_pool_with_globals<rustc_interface[dbb229baa21df1c0]::interface::run_compiler<core[67e71a49bc1f0e88]::result::Result<(), rustc_span[dacc065a687a0f9e]::ErrorGuaranteed>, rustc_driver_impl[f13dd90190e9dbe1]::run_compiler::{closure#0}>::{closure#1}, core[67e71a49bc1f0e88]::result::Result<(), rustc_span[dacc065a687a0f9e]::ErrorGuaranteed>>::{closure#0}, core[67e71a49bc1f0e88]::result::Result<(), rustc_span[dacc065a687a0f9e]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[67e71a49bc1f0e88]::result::Result<(), rustc_span[dacc065a687a0f9e]::ErrorGuaranteed>>::{closure#2} as core[67e71a49bc1f0e88]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  44:     0x7933ea2ae5fb - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h779e14e1cec4b93e
                               at /rustc/52f3c71c8dc4aaed71e3035995fcbdd6d78c98c6/library/alloc/src/boxed.rs:2070:9
  45:     0x7933ea2ae5fb - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h93db42a0f7517b09
                               at /rustc/52f3c71c8dc4aaed71e3035995fcbdd6d78c98c6/library/alloc/src/boxed.rs:2070:9
  46:     0x7933ea2ae5fb - std::sys::pal::unix::thread::Thread::new::thread_start::h450a1bf9a7dd864a
                               at /rustc/52f3c71c8dc4aaed71e3035995fcbdd6d78c98c6/library/std/src/sys/pal/unix/thread.rs:108:17
  47:     0x7933e32a6ded - <unknown>
  48:     0x7933e332a0dc - <unknown>
  49:                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.81.0-nightly (52f3c71c8 2024-07-18) running on x86_64-unknown-linux-gnu

query stack during panic:
#0 [associated_item] computing associated item data for `to_reuse::foo`
#1 [typeck] type-checking `<impl at /tmp/icemaker_global_tempdir.n4JRPjMXSZEC/rustc_testrunner_tmpdir_reporting.Tsi6i8ZZpYcI/mvce.rs:17:1: 17:14>::foo`
end of query stack
error: aborting due to 6 previous errors

Some errors have detailed explanations: E0308, E0601, E0658.
For more information about an error, try `rustc --explain E0308`.

```

</p>
</details>

<!--
query stack:
error: internal compiler error: compiler/rustc_ty_utils/src/assoc.rs:127:5: unexpected parent of trait or impl item or item not found: Mod(Mod { spans: ModSpans { inner_span: /tmp/icemaker_global_tempdir.n4JRPjMXSZEC/rustc_testrunner_tmpdir_reporting.Tsi6i8ZZpYcI/mvce.rs:14:5: 15:2 (#0), inject_use_span: /tmp/icemaker_global_tempdir.n4JRPjMXSZEC/rustc_testrunner_tmpdir_reporting.Tsi6i8ZZpYcI/mvce.rs:14:5: 14:5 (#0) }, item_ids: [ItemId { owner_id: DefId(0:10 ~ mvce[d169]::to_reuse::foo) }] })
#0 [associated_item] computing associated item data for `to_reuse::foo`
#1 [typeck] type-checking `<impl at /tmp/icemaker_global_tempdir.n4JRPjMXSZEC/rustc_testrunner_tmpdir_reporting.Tsi6i8ZZpYcI/mvce.rs:17:1: 17:14>::foo`

-->
@rustbot label +F-fn_delegation