Skip to content

ICE: next solver: failed to resolve instance for <e<FilterFn<{[email protected]:31:15: 31:17}> #140577

Open
@matthiaskrgr

Description

@matthiaskrgr

Code

original:

use std::future::{self, Future};
use std::marker::PhantomData;
use std::pin::Pin;
use std::sync::Arc;
use std::task::Context;
use std::task::Poll;

trait Acquire<'c> {
    type Connection;
    fn acquire(self) -> Pin<Box<dyn Future<Output = Result<Self::Connection, ()>> + Send + 'c>>;
}
struct PgConnection;
impl<'c> Acquire<'c> for &'c mut PgConnection {
    type Connection = ();
    fn acquire(self) -> Pin<Box<dyn Future<Output = Result<Self::Connection, ()>> + Send + 'c>> {
        unimplemented!()
    }
}
fn login<'a>(db: impl Acquire<'a> + Send + 'a) -> impl Future<Output = ()> + Send + 'a {
    async move {
        let _ = db.acquire().await;
    }
}
fn main() {
    path()
        .then(|| async {
            let mut conn = PgConnection;
            login(&mut conn).await;
        })
        .then(|_| async { unimplemented!() })
        .boxed();
}

fn path() -> impl Filter<Extract = (), Error = ()> {
    filter_fn(move || future::ready(Err(())))
}

struct Then<T, F> {
    _marker: PhantomData<(T, F)>,
}
impl<T, F> FilterBase for Then<T, F>
where
    T: Filter,
    F: Func<T::Extract> + Clone + Send,
    F::Output: Future + Send,
{
    type Extract = (<F::Output as Future>::Output,);
    type Error = T::Error;
    type Future = ThenFuture<T, F>;
}
struct ThenFuture<T, F>
where
    T: Filter,
    F: Func<T::Extract>,
    F::Output: Future + Send,
{
    _state: State<T::Future, F>,
}
enum State<T, F>
where
    T: TryFuture,
    F: Func<T::Ok>,
    F::Output: Future + Send,
{
    Second(F::Output),
}
impl<T, F> Future for ThenFuture<T, F>
where
    T: Filter,
    F: Func<T::Extract>,
    F::Output: Future + Send,
{
    type Output = Result<(<F::Output as Future>::Output,), T::Error>;
    fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
        unimplemented!()
    }
}
struct MapErr<T, F> {
    _filter: T,
    _callback: F,
}
impl<T, F, E> FilterBase for MapErr<T, F>
where
    T: Filter,
    F: Fn(T::Error) -> E + Clone + Send,
{
    type Extract = T::Extract;
    type Error = E;
    type Future = MapErrFuture<T, F>;
}
struct MapErrFuture<T: Filter, F> {
    _extract: T::Future,
    _callback: F,
}
impl<T, F, E> Future for MapErrFuture<T, F>
where
    T: Filter,
    F: Fn(T::Error) -> E,
{
    type Output = Result<T::Extract, E>;
    fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
        unimplemented!()
    }
}

struct BoxedFilter<T> {
    _filter: Arc<
        dyn Filter<
                Extract = T,
                Error = (),
                Future = Pin<Box<dyn Future<Output = Result<T, ()>> + Send>>,
            > + Send
            + Sync,
    >,
}
impl<T: Send> BoxedFilter<T> {
    fn new<F>(filter: F) -> BoxedFilter<T>
    where
        F: Filter<Extract = T> + Send + Sync + 'static,
        F::Error: Into<()>,
    {
        let filter = Arc::new(BoxingFilter {
            filter: filter.map_err(Internal, Into::into),
        });
        BoxedFilter { _filter: filter }
    }
}
struct BoxingFilter<F> {
    filter: F,
}
impl<F> FilterBase for BoxingFilter<F>
where
    F: Filter,
    F::Future: Send + 'static,
{
    type Extract = F::Extract;
    type Error = F::Error;
    type Future = Pin<Box<dyn Future<Output = Result<Self::Extract, Self::Error>> + Send>>;
    fn filter(&self, _: Internal) -> Self::Future {
        Box::pin(self.filter.filter(Internal).into_future())
    }
}
trait FilterBase {
    type Extract;
    type Error;
    type Future: Future<Output = Result<Self::Extract, Self::Error>> + Send;
    fn filter(&self, _internal: Internal) -> Self::Future {
        unimplemented!()
    }
    fn map_err<F, E>(self, _internal: Internal, _fun: F) -> MapErr<Self, F>
    where
        Self: Sized,
        F: Fn(Self::Error) -> E + Clone,
        E: Send,
    {
        unimplemented!()
    }
}
struct Internal;
trait Filter: FilterBase {
    fn then<F>(self, _fun: F) -> Then<Self, F>
    where
        Self: Sized,
        F: Func<Self::Extract> + Clone,
        F::Output: Future + Send,
    {
        unimplemented!()
    }
    fn boxed(self) -> BoxedFilter<Self::Extract>
    where
        Self: Sized + Send + Sync + 'static,
        Self::Extract: Send,
        Self::Error: Into<()>,
    {
        BoxedFilter::new(self)
    }
}
impl<T: FilterBase> Filter for T {}
fn filter_fn<F, U>(_func: F) -> FilterFn<F>
where
    F: Fn() -> U,
    U: TryFuture,
{
    unimplemented!()
}
struct FilterFn<F> {
    _func: F,
}
impl<F, U> FilterBase for FilterFn<F>
where
    F: Fn() -> U,
    U: TryFuture + Send + 'static,
{
    type Extract = U::Ok;
    type Error = U::Error;
    type Future = IntoFuture<U>;
}

trait Func<Args> {
    type Output;
}
impl<F, R> Func<()> for F
where
    F: Fn() -> R,
{
    type Output = R;
}
impl<F, R, T2> Func<(T2,)> for F
where
    F: Fn(T2) -> R,
{
    type Output = R;
}
trait TryFuture: Future {
    type Ok;
    type Error;
    fn into_future(self) -> IntoFuture<Self>
    where
        Self: Sized,
    {
        unimplemented!()
    }
}
impl<F, T, E> TryFuture for F
where
    F: ?Sized + Future<Output = Result<T, E>>,
{
    type Ok = T;
    type Error = E;
}
struct IntoFuture<Fut> {
    _future: Fut,
}
impl<Fut: TryFuture> Future for IntoFuture<Fut> {
    type Output = Result<Fut::Ok, Fut::Error>;
    fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
        unimplemented!()
    }
}

auto-reduced:

use std::future::{self, Future};
use std::marker::PhantomData;
use std::pin::Pin;
use std::sync::Arc;
use std::task::Context;
use std::task::Poll;
trait Acquire<'c> {
    type Connection;
    fn acquire(self) -> Pin<Box<dyn Future<Output = Result<Self::Connection, ()>> + Send + 'c>>;
}
struct PgConnection;
impl<'c> Acquire<'c> for &'c mut PgConnection {
    type Connection = ();
    fn acquire(self) -> Pin<Box<dyn Future<Output = Result<Self::Connection, ()>> + Send + 'c>> {
        unimplemented!()
    }
}
fn b<'a>(db: impl Acquire<'a> + Send + 'a) -> impl Future + Send + 'a {
    {
        db.acquire()
    }
}
fn main() {
    d().t(|| async {
        let mut conn = PgConnection;
        b(&mut conn).await;
    })
    .aa();
}
fn d() -> impl Filter<ab = (), Error = ()> {
    filter_fn(|| future::ready(Err(())))
}
struct e<f, F> {
    u: PhantomData<(f, F)>,
}
impl<f, F> FilterBase for e<f, F>
where
    f: Filter,
    F: g<f::ab>,
    F::Output: Future + Send,
{
    type ab = (<F::Output as Future>::Output,);
    type Error = f::Error;
    type Future = h<f, F>;
}
struct h<f, F>
where
    f: Filter,
    F: g<f::ab>,
{
    i: v<f::Future, F>,
}
enum v<f, F>
where
    f: TryFuture,
    F: g<f::j>,
{
    k(F::Output),
}
impl<f, F> Future for h<f, F>
where
    f: Filter,
    F: g<f::ab>,
    F::Output: Future,
{
    type Output = Result<(<F::Output as Future>::Output,), f::Error>;
    fn poll(self: Pin<&mut Self>, l: &mut Context) -> Poll<Self::Output> {
        unimplemented!()
    }
}
struct w<f, F> {
    _filter: f,
    m: F,
}
impl<f, F, x> FilterBase for w<f, F>
where
    f: Filter,
    F: Fn(f::Error) -> x + Send,
{
    type ab = f::ab;
    type Error = x;
    type Future = y<f, F>;
}
struct y<f: Filter, F> {
    n: f::Future,
    m: F,
}
impl<f, F, x> Future for y<f, F>
where
    f: Filter,
    F: Fn(f::Error) -> x,
{
    type Output = Result<f::ab, x>;
    fn poll(self: Pin<&mut Self>, l: &mut Context) -> Poll<Self::Output> {
        unimplemented!()
    }
}
struct BoxedFilter<f> {
    _filter: Arc<
        dyn Filter<
            ab = f,
            Error = (),
            Future = Pin<Box<dyn Future<Output = Result<f, ()>> + Send>>,
        >,
    >,
}
impl<f> BoxedFilter<f> {
    fn new<F>(filter: F) -> BoxedFilter<f>
    where
        F: Filter<ab = f> + 'static,
        F::Error: Into<()>,
    {
        let filter = Arc::new(BoxingFilter {
            filter: filter.map_err(Internal, Into::into),
        });
        BoxedFilter { _filter: filter }
    }
}
struct BoxingFilter<F> {
    filter: F,
}
impl<F> FilterBase for BoxingFilter<F>
where
    F: Filter,
    F::Future: Send + 'static,
{
    type ab = F::ab;
    type Error = F::Error;
    type Future = Pin<Box<dyn Future<Output = Result<Self::ab, Self::Error>> + Send>>;
}
trait FilterBase {
    type ab;
    type Error;
    type Future: Future<Output = Result<Self::ab, Self::Error>> + Send;
    fn map_err<F, x>(self, o: Internal, p: F) -> w<Self, F>
    where
        Self: Sized,
        F: Fn(Self::Error) -> x,
    {
        unimplemented!()
    }
}
struct Internal;
trait Filter: FilterBase {
    fn t<F>(self, p: F) -> e<Self, F>
    where
        Self: Sized,
    {
        unimplemented!()
    }
    fn aa(self) -> BoxedFilter<Self::ab>
    where
        Self: Sized + 'static,
        Self::Error: Into<()>,
    {
        BoxedFilter::new(self)
    }
}
impl<T: FilterBase> Filter for T {}
fn filter_fn<F, U>(_func: F) -> FilterFn<F>
where
    F: Fn() -> U,
{
    unimplemented!()
}
struct FilterFn<F> {
    _func: F,
}
impl<F, U> FilterBase for FilterFn<F>
where
    F: Fn() -> U,
    U: TryFuture + Send + 'static,
{
    type ab = U::j;
    type Error = U::Error;
    type Future = q<U>;
}
trait g<r> {
    type Output;
}
impl<F, z> g<()> for F
where
    F: Fn() -> z,
{
    type Output = z;
}
trait TryFuture {
    type j;
    type Error;
}
impl<F, f, x> TryFuture for F
where
    F: Future<Output = Result<f, x>>,
{
    type j = f;
    type Error = x;
}
struct q<s> {
    ac: s,
}
impl<s: TryFuture> Future for q<s> {
    type Output = Result<s::j, s::Error>;
    fn poll(self: Pin<&mut Self>, l: &mut Context) -> Poll<Self::Output> {
        unimplemented!()
    }
}

This compiles with vanilla rustc code.rs --edition=2024 but ICEs with rustc code.rs --edition=2024 -Znext-solver=globally

Meta

rustc --version --verbose:

rustc 1.88.0-nightly (3350c1eb3 2025-05-01)
binary: rustc
commit-hash: 3350c1eb3fd8fe1bee1ed4c76944d707bd256876
commit-date: 2025-05-01
host: x86_64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.2

Error output

bunch of unused/var / var never used warnings
Backtrace

error: internal compiler error: compiler/rustc_middle/src/ty/instance.rs:625:21: failed to resolve instance for <e<FilterFn<{[email protected]:31:15: 31:17}>, {[email protected]:24:11: 24:13}> as Filter>::aa
  --> code.rs:24:5
   |
24 | /     d().t(|| async {
25 | |         let mut conn = PgConnection;
26 | |         b(&mut conn).await;
27 | |     })
28 | |     .aa();
   | |_________^


thread 'rustc' panicked at compiler/rustc_middle/src/ty/instance.rs:625:21:
Box<dyn Any>
stack backtrace:
   0:     0x7a66a052fec3 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h542d155fb00771a6
   1:     0x7a66a0c059c7 - core::fmt::write::h5c20ef0aafe2b529
   2:     0x7a66a1c85751 - std::io::Write::write_fmt::hac11ffd732faa171
   3:     0x7a66a052fd22 - std::sys::backtrace::BacktraceLock::print::h47f90f237b73cbf4
   4:     0x7a66a0533b6a - std::panicking::default_hook::{{closure}}::h6282c72a1387d25f
   5:     0x7a66a05336ef - std::panicking::default_hook::he6b494ff3c83ead4
   6:     0x7a669f57c3c3 - std[fa717b9852406dd]::panicking::update_hook::<alloc[a770b99575d6623a]::boxed::Box<rustc_driver_impl[24e7910ed411b030]::install_ice_hook::{closure#1}>>::{closure#0}
   7:     0x7a66a05343e3 - std::panicking::rust_panic_with_hook::h149b08272aeb16bc
   8:     0x7a669f5b8641 - std[fa717b9852406dd]::panicking::begin_panic::<rustc_errors[c9d2c4465b08520b]::ExplicitBug>::{closure#0}
   9:     0x7a669f5ac516 - std[fa717b9852406dd]::sys::backtrace::__rust_end_short_backtrace::<std[fa717b9852406dd]::panicking::begin_panic<rustc_errors[c9d2c4465b08520b]::ExplicitBug>::{closure#0}, !>
  10:     0x7a669f5a8d29 - std[fa717b9852406dd]::panicking::begin_panic::<rustc_errors[c9d2c4465b08520b]::ExplicitBug>
  11:     0x7a669f5c2cb1 - <rustc_errors[c9d2c4465b08520b]::diagnostic::BugAbort as rustc_errors[c9d2c4465b08520b]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  12:     0x7a669fb7f52c - <rustc_errors[c9d2c4465b08520b]::DiagCtxtHandle>::span_bug::<rustc_span[a74cfce499341739]::span_encoding::Span, alloc[a770b99575d6623a]::string::String>
  13:     0x7a669fc1aeb7 - rustc_middle[2743830fcd413abb]::util::bug::opt_span_bug_fmt::<rustc_span[a74cfce499341739]::span_encoding::Span>::{closure#0}
  14:     0x7a669fbf376a - rustc_middle[2743830fcd413abb]::ty::context::tls::with_opt::<rustc_middle[2743830fcd413abb]::util::bug::opt_span_bug_fmt<rustc_span[a74cfce499341739]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  15:     0x7a669fbf35db - rustc_middle[2743830fcd413abb]::ty::context::tls::with_context_opt::<rustc_middle[2743830fcd413abb]::ty::context::tls::with_opt<rustc_middle[2743830fcd413abb]::util::bug::opt_span_bug_fmt<rustc_span[a74cfce499341739]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  16:     0x7a669e5aee27 - rustc_middle[2743830fcd413abb]::util::bug::span_bug_fmt::<rustc_span[a74cfce499341739]::span_encoding::Span>
  17:     0x7a66a1329989 - <rustc_middle[2743830fcd413abb]::ty::instance::Instance>::expect_resolve
  18:     0x7a669da8941a - rustc_monomorphize[e46901a8dfc3224a]::collector::items_of_instance
  19:     0x7a66a0c15af2 - rustc_query_impl[72cbea546227b7ba]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[72cbea546227b7ba]::query_impl::items_of_instance::dynamic_query::{closure#2}::{closure#0}, rustc_middle[2743830fcd413abb]::query::erase::Erased<[u8; 32usize]>>
  20:     0x7a66a17c4d31 - rustc_query_system[f33c5473bf6e507c]::query::plumbing::try_execute_query::<rustc_query_impl[72cbea546227b7ba]::DynamicConfig<rustc_query_system[f33c5473bf6e507c]::query::caches::DefaultCache<(rustc_middle[2743830fcd413abb]::ty::instance::Instance, rustc_middle[2743830fcd413abb]::mir::mono::CollectionMode), rustc_middle[2743830fcd413abb]::query::erase::Erased<[u8; 32usize]>>, false, false, false>, rustc_query_impl[72cbea546227b7ba]::plumbing::QueryCtxt, false>
  21:     0x7a66a17c4981 - rustc_query_impl[72cbea546227b7ba]::query_impl::items_of_instance::get_query_non_incr::__rust_end_short_backtrace
  22:     0x7a66a17c72f1 - rustc_monomorphize[e46901a8dfc3224a]::collector::collect_items_rec::{closure#0}
  23:     0x7a66a17c8209 - rustc_monomorphize[e46901a8dfc3224a]::collector::collect_items_rec
  24:     0x7a66a110ce58 - rustc_monomorphize[e46901a8dfc3224a]::partitioning::collect_and_partition_mono_items
  25:     0x7a66a1d596ea - rustc_query_impl[72cbea546227b7ba]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[72cbea546227b7ba]::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2}::{closure#0}, rustc_middle[2743830fcd413abb]::query::erase::Erased<[u8; 40usize]>>
  26:     0x7a66a1d596cf - <rustc_query_impl[72cbea546227b7ba]::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2} as core[2e44cabe4492cfed]::ops::function::FnOnce<(rustc_middle[2743830fcd413abb]::ty::context::TyCtxt, ())>>::call_once
  27:     0x7a66a1d591f4 - rustc_query_system[f33c5473bf6e507c]::query::plumbing::try_execute_query::<rustc_query_impl[72cbea546227b7ba]::DynamicConfig<rustc_query_system[f33c5473bf6e507c]::query::caches::SingleCache<rustc_middle[2743830fcd413abb]::query::erase::Erased<[u8; 40usize]>>, false, false, false>, rustc_query_impl[72cbea546227b7ba]::plumbing::QueryCtxt, false>
  28:     0x7a66a1d58f84 - rustc_query_impl[72cbea546227b7ba]::query_impl::collect_and_partition_mono_items::get_query_non_incr::__rust_end_short_backtrace
  29:     0x7a66a1d5fa1e - <rustc_codegen_llvm[925356ce9595d98e]::LlvmCodegenBackend as rustc_codegen_ssa[6e46795bd2475e1e]::traits::backend::CodegenBackend>::codegen_crate
  30:     0x7a66a1b31aa3 - <rustc_interface[9732b83b5fcd21d3]::queries::Linker>::codegen_and_build_linker
  31:     0x7a66a1b2aa2f - rustc_interface[9732b83b5fcd21d3]::passes::create_and_enter_global_ctxt::<core[2e44cabe4492cfed]::option::Option<rustc_interface[9732b83b5fcd21d3]::queries::Linker>, rustc_driver_impl[24e7910ed411b030]::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  32:     0x7a66a1c5931c - rustc_interface[9732b83b5fcd21d3]::interface::run_compiler::<(), rustc_driver_impl[24e7910ed411b030]::run_compiler::{closure#0}>::{closure#1}
  33:     0x7a66a1c7c23e - std[fa717b9852406dd]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[9732b83b5fcd21d3]::util::run_in_thread_with_globals<rustc_interface[9732b83b5fcd21d3]::util::run_in_thread_pool_with_globals<rustc_interface[9732b83b5fcd21d3]::interface::run_compiler<(), rustc_driver_impl[24e7910ed411b030]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
  34:     0x7a66a1c7c6b4 - <<std[fa717b9852406dd]::thread::Builder>::spawn_unchecked_<rustc_interface[9732b83b5fcd21d3]::util::run_in_thread_with_globals<rustc_interface[9732b83b5fcd21d3]::util::run_in_thread_pool_with_globals<rustc_interface[9732b83b5fcd21d3]::interface::run_compiler<(), rustc_driver_impl[24e7910ed411b030]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[2e44cabe4492cfed]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  35:     0x7a66a1c7daab - std::sys::pal::unix::thread::Thread::new::thread_start::h91e3ac4e59384259
  36:     0x7a669baa370a - <unknown>
  37:     0x7a669bb27aac - <unknown>
  38:                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: please attach the file at `/home/matthias/vcs/github/CRED/rustc-ice-2025-05-02T10_30_48-524199.txt` to your bug report

note: compiler flags: -Z next-solver=globally

query stack during panic:
#0 [items_of_instance] collecting items used by `main`
#1 [collect_and_partition_mono_items] collect_and_partition_mono_items
end of query stack
error: aborting due to 1 previous error; 42 warnings emitted

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️S-bug-has-testStatus: This bug is tracked inside the repo by a `known-bug` test.S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-trait-system-refactorThe Rustc Trait System Refactor Initiative (-Znext-solver)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions