rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
99.08k stars 12.79k forks source link

ICE during async closure experiments #127331

Open chadaustin opened 5 months ago

chadaustin commented 5 months ago

rustc-ice-2024-07-04T18_10_25-10188.txt

Code

Minimizing the code is nontrivial. Let me know if it is required to make a branch that reproduces the issue.

Meta

nightly-x86_64-pc-windows-msvc unchanged - rustc 1.81.0-nightly (aa1d4f682 2024-07-03)

Error output

See attached rust-ice.

chadaustin commented 5 months ago

Oh, here's the error message:

error: internal compiler error: compiler\rustc_middle\src\ty\mod.rs:1588:13: item_name: no name for DefPath { data: [DisambiguatedDefPathData { data: TypeNs("ChannelSender"), disambiguator: 0 }, DisambiguatedDefPathData { data: TypeNs(""
), disambiguator: 0 }], krate: crate0 }
compiler-errors commented 4 months ago

Minimizing the code is nontrivial. Let me know if it is required to make a branch that reproduces the issue.

Yep, I can't fix this with no code. It's not clear what the issue is from the ICE backtrace alone.

matthiaskrgr commented 4 months ago

you could probably fix https://github.com/rust-lang/rust/issues/125099 and it would fix this one as well?

chadaustin commented 4 months ago

Clone the batch-channel project and check out the async-closure-autobatch:

git clone https://github.com/chadaustin/batch-channel.git
cd batch-channel
git checkout async-closure-autobatch

Then cargo check the throughput benchmark:

cargo +nightly check --bench throughput
theemathas commented 4 months ago

Self-contained reproduction (probably can be minimized further):

use std::future::Future;

trait AsyncCallback<A>: FnOnce(A) -> Self::Fut {
    type Fut: Future<Output = ()>;
}

impl<A, Out: Future<Output = ()>, F: FnOnce(A) -> Out> AsyncCallback<A> for F {
    type Fut = Out;
}

struct BatchSender<T>(T);

trait ChannelSender<T>: Clone + Send {
    type BatchSender;

    fn autobatch<F>(self, f: F) -> impl Future<Output = ()> + Send
    where
        F: for<'a> AsyncCallback<&'a mut Self::BatchSender>;
}

struct Sender<T>(T);

impl<T: Send> ChannelSender<T> for Sender<T> {
    type BatchSender = BatchSender<T>;

    fn autobatch<F>(self, f: F) -> impl Future<Output = ()> + Send
    where
        F: for<'a> AsyncCallback<&'a mut Self::BatchSender>,
    {
        async {}
    }
}
Error output ``` Checking batch-channel v0.0.0 (/Users/timch/Documents/batch-channel) error[E0277]: expected a `FnOnce(&'a mut BatchSender)` closure, found `F` --> src/lib.rs:26:5 | 26 | / fn autobatch(self, f: F) -> impl Future + Send 27 | | where 28 | | F: for<'a> AsyncCallback<&'a mut Self::BatchSender>, | |____________________________________________________________^ expected an `FnOnce(&'a mut BatchSender)` closure, found `F` | = note: expected a closure with arguments `(&mut as ChannelSender>::BatchSender,)` found a closure with arguments `(&'a mut BatchSender,)` note: required for `F` to implement `for<'a> AsyncCallback<&'a mut BatchSender>` --> src/lib.rs:7:56 | 7 | impl, F: FnOnce(A) -> Out> AsyncCallback for F { | ---------------- ^^^^^^^^^^^^^^^^ ^ | | | unsatisfied trait bound introduced here error[E0277]: expected a `FnOnce(&'a mut BatchSender)` closure, found `F` --> src/lib.rs:26:36 | 26 | fn autobatch(self, f: F) -> impl Future + Send | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an `FnOnce(&'a mut BatchSender)` closure, found `F` | = note: expected a closure with arguments `(&mut as ChannelSender>::BatchSender,)` found a closure with arguments `(&'a mut BatchSender,)` note: required for `F` to implement `for<'a> AsyncCallback<&'a mut BatchSender>` --> src/lib.rs:7:56 | 7 | impl, F: FnOnce(A) -> Out> AsyncCallback for F { | ---------------- ^^^^^^^^^^^^^^^^ ^ | | | unsatisfied trait bound introduced here note: required by a bound in ` as ChannelSender>::autobatch` --> src/lib.rs:28:12 | 26 | fn autobatch(self, f: F) -> impl Future + Send | --------- required by a bound in this associated function 27 | where 28 | F: for<'a> AsyncCallback<&'a mut Self::BatchSender>, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in ` as ChannelSender>::autobatch` error[E0277]: the trait bound `Sender: Clone` is not satisfied --> src/lib.rs:23:36 | 23 | impl ChannelSender for Sender { | ^^^^^^^^^ the trait `Clone` is not implemented for `Sender` | note: required by a bound in `ChannelSender` --> src/lib.rs:13:25 | 13 | trait ChannelSender: Clone + Send { | ^^^^^ required by this bound in `ChannelSender` error[E0277]: the trait bound `for<'a> F: AsyncCallback<&'a mut BatchSender>` is not satisfied --> src/lib.rs:28:12 | 28 | F: for<'a> AsyncCallback<&'a mut Self::BatchSender>, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `for<'a> FnOnce(&'a mut BatchSender)` is not implemented for `F`, which is required by `for<'a> F: AsyncCallback<&'a mut BatchSender>` | = note: expected a closure with arguments `(&mut as ChannelSender>::BatchSender,)` found a closure with arguments `(&'a mut BatchSender,)` note: required for `F` to implement `for<'a> AsyncCallback<&'a mut BatchSender>` --> src/lib.rs:7:56 | 7 | impl, F: FnOnce(A) -> Out> AsyncCallback for F { | ---------------- ^^^^^^^^^^^^^^^^ ^ | | | unsatisfied trait bound introduced here note: the requirement `for<'a> F: AsyncCallback<&'a mut BatchSender>` appears on the `impl`'s method `autobatch` but not on the corresponding trait's method --> src/lib.rs:16:8 | 13 | trait ChannelSender: Clone + Send { | ------------- in this trait ... 16 | fn autobatch(self, f: F) -> impl Future + Send | ^^^^^^^^^ this trait's method doesn't have the requirement `for<'a> F: AsyncCallback<&'a mut BatchSender>` error[E0277]: the trait bound `for<'a> F: AsyncCallback<&'a mut BatchSender>` is not satisfied --> src/lib.rs:26:5 | 26 | / fn autobatch(self, f: F) -> impl Future + Send 27 | | where 28 | | F: for<'a> AsyncCallback<&'a mut Self::BatchSender>, | |____________________________________________________________^ the trait `for<'a> FnOnce(&'a mut BatchSender)` is not implemented for `F`, which is required by `for<'a> F: AsyncCallback<&'a mut BatchSender>` | = note: expected a closure with arguments `(&mut as ChannelSender>::BatchSender,)` found a closure with arguments `(&'a mut BatchSender,)` note: required for `F` to implement `for<'a> AsyncCallback<&'a mut BatchSender>` --> src/lib.rs:7:56 | 7 | impl, F: FnOnce(A) -> Out> AsyncCallback for F { | ---------------- ^^^^^^^^^^^^^^^^ ^ | | | unsatisfied trait bound introduced here note: required by a bound in `ChannelSender::{synthetic#0}` --> src/lib.rs:18:12 | 18 | F: for<'a> AsyncCallback<&'a mut Self::BatchSender>; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `ChannelSender::{synthetic#0}` error[E0277]: expected a `FnOnce(&'a mut BatchSender)` closure, found `F` --> src/lib.rs:26:36 | 26 | fn autobatch(self, f: F) -> impl Future + Send | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an `FnOnce(&'a mut BatchSender)` closure, found `F` | = note: expected a closure with arguments `(&mut as ChannelSender>::BatchSender,)` found a closure with arguments `(&'a mut BatchSender,)` note: required for `F` to implement `for<'a> AsyncCallback<&'a mut BatchSender>` --> src/lib.rs:7:56 | 7 | impl, F: FnOnce(A) -> Out> AsyncCallback for F { | ---------------- ^^^^^^^^^^^^^^^^ ^ | | | unsatisfied trait bound introduced here error: internal compiler error: compiler/rustc_middle/src/ty/mod.rs:1588:13: item_name: no name for DefPath { data: [DisambiguatedDefPathData { data: TypeNs("ChannelSender"), disambiguator: 0 }, DisambiguatedDefPathData { data: TypeNs(""), disambiguator: 0 }], krate: crate0 } thread 'rustc' panicked at compiler/rustc_middle/src/ty/mod.rs:1588:13: Box stack backtrace: 0: 0x1052b6e30 - ::fmt::he7fc263c3ae60281 1: 0x1052fa124 - core::fmt::write::he0bb149810b76b94 2: 0x1052acf10 - std::io::Write::write_fmt::h961f49b732a1ce21 3: 0x1052b6c88 - std::sys::backtrace::print::h805bc4620f3151a6 4: 0x1052b9298 - std::panicking::default_hook::{{closure}}::h1847e4937d8814b8 5: 0x1052b8f64 - std::panicking::default_hook::hd04f2dc85931a3af 6: 0x10f15bb04 - as core[4a89e3a3d2972735]::ops::function::Fn<(&dyn for<'a, 'b> core[4a89e3a3d2972735]::ops::function::Fn<(&'a std[791662d3d9623b74]::panic::PanicHookInfo<'b>,), Output = ()> + core[4a89e3a3d2972735]::marker::Sync + core[4a89e3a3d2972735]::marker::Send, &std[791662d3d9623b74]::panic::PanicHookInfo)>>::call 7: 0x1052b9e28 - std::panicking::rust_panic_with_hook::hfb7f8fd8d6ca280d 8: 0x10f1e7dd0 - std[791662d3d9623b74]::panicking::begin_panic::::{closure#0} 9: 0x10f1e46f0 - std[791662d3d9623b74]::sys::backtrace::__rust_end_short_backtrace::::{closure#0}, !> 10: 0x11355b278 - std[791662d3d9623b74]::panicking::begin_panic:: 11: 0x10f1c40cc - ::emit_producing_guarantee 12: 0x10fe69648 - rustc_middle[8bc529f0e8dcc906]::util::bug::opt_span_bug_fmt::::{closure#0} 13: 0x10fe69588 - rustc_middle[8bc529f0e8dcc906]::ty::context::tls::with_opt::::{closure#0}, !>::{closure#0} 14: 0x10fe69554 - rustc_middle[8bc529f0e8dcc906]::ty::context::tls::with_context_opt::::{closure#0}, !>::{closure#0}, !> 15: 0x1135f9e00 - rustc_middle[8bc529f0e8dcc906]::util::bug::bug_fmt 16: 0x10fd0cfec - ::item_name 17: 0x110bec424 - ::note_obligation_cause_code::>> 18: 0x110bf3a9c - ::note_obligation_cause_code::>>::{closure#6} 19: 0x110bf1b38 - ::note_obligation_cause_code:: 20: 0x110c04f60 - ::note_obligation_cause 21: 0x110bfce1c - ::report_selection_error 22: 0x110c0abfc - ::report_fulfillment_error 23: 0x110bfb85c - ::report_fulfillment_errors 24: 0x10f54ae9c - rustc_hir_analysis[dbec75d63dfe474d]::check::check::check_impl_items_against_trait 25: 0x10f54339c - rustc_hir_analysis[dbec75d63dfe474d]::check::check::check_item_type 26: 0x10f521c64 - rustc_hir_analysis[dbec75d63dfe474d]::check::wfcheck::check_well_formed 27: 0x1106225cc - rustc_query_impl[f1d8f2bcea61389e]::plumbing::__rust_begin_short_backtrace::> 28: 0x1107016ac - >::call_once 29: 0x1105d8268 - rustc_query_system[8ffd5b92dfc530c8]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[f1d8f2bcea61389e]::plumbing::QueryCtxt, true> 30: 0x1107c0f34 - rustc_query_impl[f1d8f2bcea61389e]::query_impl::check_well_formed::get_query_incr::__rust_end_short_backtrace 31: 0x10f50ba4c - rustc_middle[8bc529f0e8dcc906]::query::plumbing::query_ensure_error_guaranteed::>, ()> 32: 0x10f4ff304 - ::run::, rustc_data_structures[288cf1eb93b81d30]::sync::parallel::enabled::try_par_for_each_in<&[rustc_hir[7a2e70a68ac31600]::hir::ImplItemId], rustc_span[68cecb55101cab86]::ErrorGuaranteed, ::par_impl_items::{closure#0}>::{closure#0}::{closure#0}::{closure#0}> 33: 0x10f3b6fa0 - ::par_items:: 34: 0x10f527b5c - rustc_hir_analysis[dbec75d63dfe474d]::check::wfcheck::check_mod_type_wf 35: 0x110622580 - rustc_query_impl[f1d8f2bcea61389e]::plumbing::__rust_begin_short_backtrace::> 36: 0x110701430 - >::call_once 37: 0x11057e474 - rustc_query_system[8ffd5b92dfc530c8]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[f1d8f2bcea61389e]::plumbing::QueryCtxt, true> 38: 0x1107b1f30 - rustc_query_impl[f1d8f2bcea61389e]::query_impl::check_mod_type_wf::get_query_incr::__rust_end_short_backtrace 39: 0x10f4ff8a8 - ::run::<(), rustc_data_structures[288cf1eb93b81d30]::sync::parallel::enabled::par_for_each_in<&rustc_hir[7a2e70a68ac31600]::hir_id::OwnerId, &[rustc_hir[7a2e70a68ac31600]::hir_id::OwnerId], ::par_for_each_module::{closure#0}>::{closure#0}::{closure#0}::{closure#0}> 40: 0x10f4e2b68 - ::time::<(), rustc_hir_analysis[dbec75d63dfe474d]::check_crate::{closure#0}> 41: 0x10f4e0e8c - rustc_hir_analysis[dbec75d63dfe474d]::check_crate 42: 0x10fa6b85c - rustc_interface[ff3e7bd95de52456]::passes::analysis 43: 0x1106277d4 - rustc_query_impl[f1d8f2bcea61389e]::plumbing::__rust_begin_short_backtrace::> 44: 0x1106c91d0 - >::call_once 45: 0x110542040 - rustc_query_system[8ffd5b92dfc530c8]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[f1d8f2bcea61389e]::plumbing::QueryCtxt, true> 46: 0x1107a3778 - rustc_query_impl[f1d8f2bcea61389e]::query_impl::analysis::get_query_incr::__rust_end_short_backtrace 47: 0x10f0f1e80 - ::enter::> 48: 0x10f123578 - ::enter::, rustc_span[68cecb55101cab86]::ErrorGuaranteed>> 49: 0x10f10f684 - rustc_span[68cecb55101cab86]::create_session_globals_then::, rustc_interface[ff3e7bd95de52456]::util::run_in_thread_with_globals, rustc_driver_impl[c003af3b489365f2]::run_compiler::{closure#0}>::{closure#1}, core[4a89e3a3d2972735]::result::Result<(), rustc_span[68cecb55101cab86]::ErrorGuaranteed>>::{closure#0}, core[4a89e3a3d2972735]::result::Result<(), rustc_span[68cecb55101cab86]::ErrorGuaranteed>>::{closure#0}::{closure#0}::{closure#0}> 50: 0x10f0f37f8 - std[791662d3d9623b74]::sys::backtrace::__rust_begin_short_backtrace::, rustc_driver_impl[c003af3b489365f2]::run_compiler::{closure#0}>::{closure#1}, core[4a89e3a3d2972735]::result::Result<(), rustc_span[68cecb55101cab86]::ErrorGuaranteed>>::{closure#0}, core[4a89e3a3d2972735]::result::Result<(), rustc_span[68cecb55101cab86]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[4a89e3a3d2972735]::result::Result<(), rustc_span[68cecb55101cab86]::ErrorGuaranteed>> 51: 0x10f0f0c6c - <::spawn_unchecked_, rustc_driver_impl[c003af3b489365f2]::run_compiler::{closure#0}>::{closure#1}, core[4a89e3a3d2972735]::result::Result<(), rustc_span[68cecb55101cab86]::ErrorGuaranteed>>::{closure#0}, core[4a89e3a3d2972735]::result::Result<(), rustc_span[68cecb55101cab86]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[4a89e3a3d2972735]::result::Result<(), rustc_span[68cecb55101cab86]::ErrorGuaranteed>>::{closure#2} as core[4a89e3a3d2972735]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} 52: 0x1052c2374 - std::sys::pal::unix::thread::Thread::new::thread_start::h3cbd0bca5805ff8b 53: 0x19a536f94 - __pthread_joiner_wake 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: please attach the file at `/Users/timch/Documents/batch-channel/rustc-ice-2024-07-05T17_15_43-26060.txt` to your bug report note: compiler flags: --crate-type lib -C embed-bitcode=no -C debuginfo=2 -C split-debuginfo=unpacked -C incremental=[REDACTED] note: some of the compiler flags provided by cargo are hidden query stack during panic: #0 [check_well_formed] checking that `` is well-formed #1 [check_mod_type_wf] checking that types are well-formed in top-level module end of query stack error[E0277]: expected a `FnOnce(&'a mut BatchSender)` closure, found `F` --> src/lib.rs:30:9 | 30 | async {} | ^^^^^ expected an `FnOnce(&'a mut BatchSender)` closure, found `F` | = note: expected a closure with arguments `(&mut as ChannelSender>::BatchSender,)` found a closure with arguments `(&'a mut BatchSender,)` note: required for `F` to implement `for<'a> AsyncCallback<&'a mut BatchSender>` --> src/lib.rs:7:56 | 7 | impl, F: FnOnce(A) -> Out> AsyncCallback for F { | ---------------- ^^^^^^^^^^^^^^^^ ^ | | | unsatisfied trait bound introduced here For more information about this error, try `rustc --explain E0277`. error: could not compile `batch-channel` (lib) due to 8 previous errors ```

Notably doesn't require the async_closure feature.

@rustbot label -F-async_closure

compiler-errors commented 4 months ago

Thanks!!

theemathas commented 4 months ago

Minimized (I can't get it smaller than this) and got rid of dependence on std:

trait MyFn<T> {
    type Output;
}

trait MyFnAlias<A>: MyFn<A, Output = Self::OutputAlias> {
    type OutputAlias;
}

impl<A, F: MyFn<A>> MyFnAlias<A> for F {
    type OutputAlias = F::Output;
}

struct Thing;
trait Trait {}
impl Trait for Thing {}

trait ChannelSender {
    type Arg;

    fn autobatch<F>(self) -> impl Trait
    where
        F: MyFnAlias<Self::Arg>;
}

struct Sender;

impl ChannelSender for Sender {
    type Arg = i32;

    fn autobatch<F>(self) -> impl Trait
    where
        F: MyFnAlias<Self::Arg>,
    {
        Thing
    }
}

I believe this code should compile.

compiler-errors commented 4 months ago

Oh yeah, this has nothing to do with async closures at all.

This is a shortcoming of the old trait solver having to do with -- and pardon for my technical jargon -- not being able to normalize param envs correctly. I think there should be other issues like this, probably labeled fixed-by-next-solver...

I'll look into the ICE though, since I think we crash the compiler when trying to report the error lol.