Repository: rust-lang/rust (Rust)

Issue:
ICE: privacy: assertion failed: `AssocFn == Closure`
<!--
[31mICE[0m: Rustc ./37b51aa64d02aa93e235707c812d00fb55d9e6aa.rs '' 'thread 'rustc' panicked at /rustc/8681e077b8afa99d60acf8f8470a012a3ce709a5/compiler/rustc_privacy/src/lib.rs:1774:13: 'assertion `left == right` failed'', 'thread 'rustc' panicked at /rustc/8681e077b8afa99d60acf8f8470a012a3ce709a5/compiler/rustc_privacy/src/lib.rs:1774:13: 'assertion `left == right` failed''
File: /tmp/im/37b51aa64d02aa93e235707c812d00fb55d9e6aa.rs
-->

auto-reduced (treereduce-rust):
````rust
trait MyTrait {
    async fn bar(&self) -> i32;
}

impl MyTrait for i32 {
    async fn MyTrait(&self) -> i32 {
        *self
    }

    async fn bar(&self) -> i32 {}
}


````

original:
````rust
// check-pass
// edition: 2021

#![feature(async_fn_in_trait)]
#![allow(incomplete_features)]

trait MyTrait {
    async fn foo(&self) -> i32;
    async fn bar(&self) -> i32;
}

impl MyTrait for i32 {
    async fn MyTrait(&self) -> i32 {
        *self
    }

    async fn bar(&self) -> i32 {
        self.foo().await
    }
}

fn main() {
    let x = 5;
    // Calling from non-async context
    let _ = x.foo();
    let _ = x.bar();
    // Calling from async block in non-async context
    async {
        let _: i32 = x.foo().await;
        let _: i32 = x.bar().await;
    };
}

````

Version information
````
rustc 1.76.0-nightly (8681e077b 2023-12-18)
binary: rustc
commit-hash: 8681e077b8afa99d60acf8f8470a012a3ce709a5
commit-date: 2023-12-18
host: x86_64-unknown-linux-gnu
release: 1.76.0-nightly
LLVM version: 17.0.6

````

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[E0670]: `async fn` is not permitted in Rust 2015
 --> /tmp/icemaker_global_tempdir.11SBEZ78Tsfu/rustc_testrunner_tmpdir_reporting.GGifX7KAr0cn/mvce.rs:2:5
  |
2 |     async fn bar(&self) -> i32;
  |     ^^^^^ to use `async fn`, switch to Rust 2018 or later
  |
  = help: pass `--edition 2021` to `rustc`
  = note: for more on editions, read https://doc.rust-lang.org/edition-guide

error[E0670]: `async fn` is not permitted in Rust 2015
 --> /tmp/icemaker_global_tempdir.11SBEZ78Tsfu/rustc_testrunner_tmpdir_reporting.GGifX7KAr0cn/mvce.rs:6:5
  |
6 |     async fn MyTrait(&self) -> i32 {
  |     ^^^^^ to use `async fn`, switch to Rust 2018 or later
  |
  = help: pass `--edition 2021` to `rustc`
  = note: for more on editions, read https://doc.rust-lang.org/edition-guide

error[E0670]: `async fn` is not permitted in Rust 2015
  --> /tmp/icemaker_global_tempdir.11SBEZ78Tsfu/rustc_testrunner_tmpdir_reporting.GGifX7KAr0cn/mvce.rs:10:5
   |
10 |     async fn bar(&self) -> i32 {}
   |     ^^^^^ to use `async fn`, switch to Rust 2018 or later
   |
   = help: pass `--edition 2021` to `rustc`
   = note: for more on editions, read https://doc.rust-lang.org/edition-guide

error[E0407]: method `MyTrait` is not a member of trait `MyTrait`
 --> /tmp/icemaker_global_tempdir.11SBEZ78Tsfu/rustc_testrunner_tmpdir_reporting.GGifX7KAr0cn/mvce.rs:6:5
  |
6 | /     async fn MyTrait(&self) -> i32 {
7 | |         *self
8 | |     }
  | |_____^ not a member of trait `MyTrait`

error[E0601]: `main` function not found in crate `mvce`
  --> /tmp/icemaker_global_tempdir.11SBEZ78Tsfu/rustc_testrunner_tmpdir_reporting.GGifX7KAr0cn/mvce.rs:11:2
   |
11 | }
   |  ^ consider adding a `main` function to `/tmp/icemaker_global_tempdir.11SBEZ78Tsfu/rustc_testrunner_tmpdir_reporting.GGifX7KAr0cn/mvce.rs`

thread 'rustc' panicked at /rustc/8681e077b8afa99d60acf8f8470a012a3ce709a5/compiler/rustc_privacy/src/lib.rs:1774:13:
assertion `left == right` failed
  left: AssocFn
 right: Closure
stack backtrace:
   0:     0x7f8891f8aedc - std::backtrace_rs::backtrace::libunwind::trace::h4aa006547432fe3e
                               at /rustc/8681e077b8afa99d60acf8f8470a012a3ce709a5/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5
   1:     0x7f8891f8aedc - std::backtrace_rs::backtrace::trace_unsynchronized::hf7201be28f6c27c2
                               at /rustc/8681e077b8afa99d60acf8f8470a012a3ce709a5/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f8891f8aedc - std::sys_common::backtrace::_print_fmt::h0cd8a143bef670cf
                               at /rustc/8681e077b8afa99d60acf8f8470a012a3ce709a5/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x7f8891f8aedc - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::ha91f52f52d796a13
                               at /rustc/8681e077b8afa99d60acf8f8470a012a3ce709a5/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7f8891fde040 - core::fmt::rt::Argument::fmt::hdde011a0bd4392af
                               at /rustc/8681e077b8afa99d60acf8f8470a012a3ce709a5/library/core/src/fmt/rt.rs:142:9
   5:     0x7f8891fde040 - core::fmt::write::h5e781cf6866d3648
                               at /rustc/8681e077b8afa99d60acf8f8470a012a3ce709a5/library/core/src/fmt/mod.rs:1120:17
   6:     0x7f8891f7edef - std::io::Write::write_fmt::h9a2947799664dd03
                               at /rustc/8681e077b8afa99d60acf8f8470a012a3ce709a5/library/std/src/io/mod.rs:1810:15
   7:     0x7f8891f8acc4 - std::sys_common::backtrace::_print::h3370570567099b55
                               at /rustc/8681e077b8afa99d60acf8f8470a012a3ce709a5/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7f8891f8acc4 - std::sys_common::backtrace::print::h7d65fcf71bbcffc2
                               at /rustc/8681e077b8afa99d60acf8f8470a012a3ce709a5/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7f8891f8d987 - std::panicking::default_hook::{{closure}}::h5494d1902ea01b77
  10:     0x7f8891f8d6ef - std::panicking::default_hook::h41df66896c085068
                               at /rustc/8681e077b8afa99d60acf8f8470a012a3ce709a5/library/std/src/panicking.rs:292:9
  11:     0x7f8894d68210 - std[25382dfd2a072775]::panicking::update_hook::<alloc[280cef7c9c4d1494]::boxed::Box<rustc_driver_impl[ee97d3c831c39013]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7f8891f8e0a6 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::hd7509255f1dd7a52
                               at /rustc/8681e077b8afa99d60acf8f8470a012a3ce709a5/library/alloc/src/boxed.rs:2029:9
  13:     0x7f8891f8e0a6 - std::panicking::rust_panic_with_hook::h4df8865e41d9c2e8
                               at /rustc/8681e077b8afa99d60acf8f8470a012a3ce709a5/library/std/src/panicking.rs:783:13
  14:     0x7f8891f8ddfc - std::panicking::begin_panic_handler::{{closure}}::hd37df855ea4500f6
                               at /rustc/8681e077b8afa99d60acf8f8470a012a3ce709a5/library/std/src/panicking.rs:657:13
  15:     0x7f8891f8b396 - std::sys_common::backtrace::__rust_end_short_backtrace::ha411a8bd9892a2d7
                               at /rustc/8681e077b8afa99d60acf8f8470a012a3ce709a5/library/std/src/sys_common/backtrace.rs:171:18
  16:     0x7f8891f8db80 - rust_begin_unwind
                               at /rustc/8681e077b8afa99d60acf8f8470a012a3ce709a5/library/std/src/panicking.rs:645:5
  17:     0x7f8891fda745 - core::panicking::panic_fmt::hd7672b9c583e7546
                               at /rustc/8681e077b8afa99d60acf8f8470a012a3ce709a5/library/core/src/panicking.rs:72:14
  18:     0x7f8891fdacdb - core::panicking::assert_failed_inner::hcd6e15a64204aba0
                               at /rustc/8681e077b8afa99d60acf8f8470a012a3ce709a5/library/core/src/panicking.rs:342:17
  19:     0x7f889509aa0b - core[f0c809df4423c390]::panicking::assert_failed::<rustc_hir[f0d1db495241573d]::def::DefKind, rustc_hir[f0d1db495241573d]::def::DefKind>
  20:     0x7f88974f5e96 - <rustc_privacy[457c624ad30a0311]::provide::{closure#0} as core[f0c809df4423c390]::ops::function::FnOnce<(rustc_middle[6d381e14aaf01dd0]::ty::context::TyCtxt, rustc_span[bacf4852791a5a5]::def_id::LocalDefId)>>::call_once.llvm.14723649760234986019.cold.0
  21:     0x7f88969503f0 - rustc_query_impl[e7cdedc87b77f1f0]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[e7cdedc87b77f1f0]::query_impl::visibility::dynamic_query::{closure#2}::{closure#0}, rustc_middle[6d381e14aaf01dd0]::query::erase::Erased<[u8; 8usize]>>
  22:     0x7f8896088014 - rustc_query_system[5b4abf955d71912a]::query::plumbing::try_execute_query::<rustc_query_impl[e7cdedc87b77f1f0]::DynamicConfig<rustc_query_system[5b4abf955d71912a]::query::caches::DefaultCache<rustc_span[bacf4852791a5a5]::def_id::DefId, rustc_middle[6d381e14aaf01dd0]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[e7cdedc87b77f1f0]::plumbing::QueryCtxt, false>
  23:     0x7f889608761e - rustc_query_impl[e7cdedc87b77f1f0]::query_impl::visibility::get_query_non_incr::__rust_end_short_backtrace
  24:     0x7f88960cb283 - <rustc_privacy[457c624ad30a0311]::DefIdVisitorSkeleton<rustc_privacy[457c624ad30a0311]::ReachEverythingInTheInterfaceVisitor> as rustc_type_ir[44dc4dc99f375282]::visit::TypeVisitor<rustc_middle[6d381e14aaf01dd0]::ty::context::TyCtxt>>::visit_ty
  25:     0x7f889645eff5 - <rustc_privacy[457c624ad30a0311]::EmbargoVisitor as rustc_hir[f0d1db495241573d]::intravisit::Visitor>::visit_item
  26:     0x7f889645cb8a - <rustc_middle[6d381e14aaf01dd0]::hir::map::Map>::visit_all_item_likes_in_crate::<rustc_privacy[457c624ad30a0311]::EmbargoVisitor>
  27:     0x7f8896040810 - rustc_privacy[457c624ad30a0311]::effective_visibilities
  28:     0x7f8896040675 - rustc_query_impl[e7cdedc87b77f1f0]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[e7cdedc87b77f1f0]::query_impl::effective_visibilities::dynamic_query::{closure#2}::{closure#0}, rustc_middle[6d381e14aaf01dd0]::query::erase::Erased<[u8; 8usize]>>
  29:     0x7f8896ce0572 - rustc_query_system[5b4abf955d71912a]::query::plumbing::try_execute_query::<rustc_query_impl[e7cdedc87b77f1f0]::DynamicConfig<rustc_query_system[5b4abf955d71912a]::query::caches::SingleCache<rustc_middle[6d381e14aaf01dd0]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[e7cdedc87b77f1f0]::plumbing::QueryCtxt, false>
  30:     0x7f8896ce0e86 - rustc_query_impl[e7cdedc87b77f1f0]::query_impl::effective_visibilities::get_query_non_incr::__rust_end_short_backtrace
  31:     0x7f88966e3e7b - rustc_hir_analysis[2c57fcbec73706b1]::check::check::check_impl_items_against_trait
  32:     0x7f8896ce7dcc - rustc_hir_analysis[2c57fcbec73706b1]::check::check::check_mod_item_types
  33:     0x7f8896ce7a31 - rustc_query_impl[e7cdedc87b77f1f0]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[e7cdedc87b77f1f0]::query_impl::check_mod_item_types::dynamic_query::{closure#2}::{closure#0}, rustc_middle[6d381e14aaf01dd0]::query::erase::Erased<[u8; 0usize]>>
  34:     0x7f8896c5253b - rustc_query_system[5b4abf955d71912a]::query::plumbing::try_execute_query::<rustc_query_impl[e7cdedc87b77f1f0]::DynamicConfig<rustc_query_system[5b4abf955d71912a]::query::caches::DefaultCache<rustc_span[bacf4852791a5a5]::def_id::LocalModDefId, rustc_middle[6d381e14aaf01dd0]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[e7cdedc87b77f1f0]::plumbing::QueryCtxt, false>
  35:     0x7f8896c51f17 - rustc_query_impl[e7cdedc87b77f1f0]::query_impl::check_mod_item_types::get_query_non_incr::__rust_end_short_backtrace
  36:     0x7f8896a0f823 - rustc_hir_analysis[2c57fcbec73706b1]::check_crate
  37:     0x7f889694bbd7 - rustc_interface[dbc5283c63a93da8]::passes::analysis
  38:     0x7f889694b81d - rustc_query_impl[e7cdedc87b77f1f0]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[e7cdedc87b77f1f0]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[6d381e14aaf01dd0]::query::erase::Erased<[u8; 1usize]>>
  39:     0x7f8896fb4db2 - rustc_query_system[5b4abf955d71912a]::query::plumbing::try_execute_query::<rustc_query_impl[e7cdedc87b77f1f0]::DynamicConfig<rustc_query_system[5b4abf955d71912a]::query::caches::SingleCache<rustc_middle[6d381e14aaf01dd0]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[e7cdedc87b77f1f0]::plumbing::QueryCtxt, false>
  40:     0x7f8896fb4bb9 - rustc_query_impl[e7cdedc87b77f1f0]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  41:     0x7f889704cef1 - rustc_interface[dbc5283c63a93da8]::interface::run_compiler::<core[f0c809df4423c390]::result::Result<(), rustc_span[bacf4852791a5a5]::ErrorGuaranteed>, rustc_driver_impl[ee97d3c831c39013]::run_compiler::{closure#0}>::{closure#0}
  42:     0x7f8896fc11c6 - std[25382dfd2a072775]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[dbc5283c63a93da8]::util::run_in_thread_with_globals<rustc_interface[dbc5283c63a93da8]::util::run_in_thread_pool_with_globals<rustc_interface[dbc5283c63a93da8]::interface::run_compiler<core[f0c809df4423c390]::result::Result<(), rustc_span[bacf4852791a5a5]::ErrorGuaranteed>, rustc_driver_impl[ee97d3c831c39013]::run_compiler::{closure#0}>::{closure#0}, core[f0c809df4423c390]::result::Result<(), rustc_span[bacf4852791a5a5]::ErrorGuaranteed>>::{closure#0}, core[f0c809df4423c390]::result::Result<(), rustc_span[bacf4852791a5a5]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[f0c809df4423c390]::result::Result<(), rustc_span[bacf4852791a5a5]::ErrorGuaranteed>>
  43:     0x7f8896fc0ff3 - <<std[25382dfd2a072775]::thread::Builder>::spawn_unchecked_<rustc_interface[dbc5283c63a93da8]::util::run_in_thread_with_globals<rustc_interface[dbc5283c63a93da8]::util::run_in_thread_pool_with_globals<rustc_interface[dbc5283c63a93da8]::interface::run_compiler<core[f0c809df4423c390]::result::Result<(), rustc_span[bacf4852791a5a5]::ErrorGuaranteed>, rustc_driver_impl[ee97d3c831c39013]::run_compiler::{closure#0}>::{closure#0}, core[f0c809df4423c390]::result::Result<(), rustc_span[bacf4852791a5a5]::ErrorGuaranteed>>::{closure#0}, core[f0c809df4423c390]::result::Result<(), rustc_span[bacf4852791a5a5]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[f0c809df4423c390]::result::Result<(), rustc_span[bacf4852791a5a5]::ErrorGuaranteed>>::{closure#1} as core[f0c809df4423c390]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  44:     0x7f8891f97ef5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::haedb32b43ffd86c4
                               at /rustc/8681e077b8afa99d60acf8f8470a012a3ce709a5/library/alloc/src/boxed.rs:2015:9
  45:     0x7f8891f97ef5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::had410a5deb0b83ea
                               at /rustc/8681e077b8afa99d60acf8f8470a012a3ce709a5/library/alloc/src/boxed.rs:2015:9
  46:     0x7f8891f97ef5 - std::sys::unix::thread::Thread::new::thread_start::h84df661860ff711f
                               at /rustc/8681e077b8afa99d60acf8f8470a012a3ce709a5/library/std/src/sys/unix/thread.rs:108:17
  47:     0x7f8891d849eb - <unknown>
  48:     0x7f8891e087cc - <unknown>
  49:                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.76.0-nightly (8681e077b 2023-12-18) running on x86_64-unknown-linux-gnu

query stack during panic:
#0 [visibility] computing visibility of `<impl at /tmp/icemaker_global_tempdir.11SBEZ78Tsfu/rustc_testrunner_tmpdir_reporting.GGifX7KAr0cn/mvce.rs:5:1: 5:21>::MyTrait`
#1 [effective_visibilities] checking effective visibilities
#2 [check_mod_item_types] checking item types in top-level module
#3 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 5 previous errors

Some errors have detailed explanations: E0407, E0601, E0670.
For more information about an error, try `rustc --explain E0407`.

```

</p>
</details>

<!--
query stack:
#0 [visibility] computing visibility of `<impl at /tmp/icemaker_global_tempdir.11SBEZ78Tsfu/rustc_testrunner_tmpdir_reporting.GGifX7KAr0cn/mvce.rs:5:1: 5:21>::MyTrait`
#1 [effective_visibilities] checking effective visibilities
#2 [check_mod_item_types] checking item types in top-level module
#3 [analysis] running analysis passes on this crate

-->



Discussion: Regression in  #118657 ping @petrochenkov 
Fixed in https://github.com/rust-lang/rust/pull/119134.



Fix (patch):
diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs
index 037179350f044..cf50f630bf2a9 100644
--- a/compiler/rustc_resolve/src/late.rs
+++ b/compiler/rustc_resolve/src/late.rs
@@ -3078,17 +3078,25 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
             binding = self.r.resolution(module, key).try_borrow().ok().and_then(|r| r.binding);
             debug!(?binding);
         }
+
+        let feed_visibility = |this: &mut Self, def_id| {
+            let vis = this.r.tcx.visibility(def_id).expect_local();
+            this.r.feed_visibility(this.r.local_def_id(id), vis);
+        };
+
         let Some(binding) = binding else {
             // We could not find the method: report an error.
             let candidate = self.find_similarly_named_assoc_item(ident.name, kind);
             let path = &self.current_trait_ref.as_ref().unwrap().1.path;
             let path_names = path_names_to_string(path);
             self.report_error(span, err(ident, path_names, candidate));
+            feed_visibility(self, module.def_id());
             return;
         };
 
         let res = binding.res();
         let Res::Def(def_kind, id_in_trait) = res else { bug!() };
+        feed_visibility(self, id_in_trait);
 
         match seen_trait_items.entry(id_in_trait) {
             Entry::Occupied(entry) => {
@@ -3112,8 +3120,6 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
             | (DefKind::AssocFn, AssocItemKind::Fn(..))
             | (DefKind::AssocConst, AssocItemKind::Const(..)) => {
                 self.r.record_partial_res(id, PartialRes::new(res));
-                let vis = self.r.tcx.visibility(id_in_trait).expect_local();
-                self.r.feed_visibility(self.r.local_def_id(id), vis);
                 return;
             }
             _ => {}