Repository: rust
Issue: ICE: `error performing ParamEnvAnd { ... }` possibly related to (lack of) HRTBs in closures
Labels: I-ICE,A-trait-system,A-closures,T-compiler,C-bug,S-bug-has-test,fixed-by-next-solver,A-higher-ranked

<!--
Thank you for finding an Internal Compiler Error! 🧊  If possible, try to provide
a minimal verifiable example. You can read "Rust Bug Minimization Patterns" for
how to create smaller examples.
http://blog.pnkfx.org/blog/2019/11/18/rust-bug-minimization-patterns/
-->

Credits to @kvverti for finding the following ICE while experimenting with some of my code that was producing HRTB-related errors.

### Code

```rust
#![allow(warnings)]

trait Foo {}

impl<F> Foo for F where F: FnMut(&()) {}

struct Bar<F> {
    f: F,
}

impl<F> Foo for Bar<F> where F: Foo {}

fn assert_foo<F>(_: F)
where
    Bar<F>: Foo,
{
}

fn main() {
    assert_foo(|_| ());
}
```

### Meta
<!--
If you're using the stable version of the compiler, you should also check if the
bug also exists in the beta or nightly versions.
-->

`rustc --version --verbose`:
```
rustc 1.77.2 (25ef9e3d8 2024-04-09)
binary: rustc
commit-hash: 25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04
commit-date: 2024-04-09
host: x86_64-pc-windows-msvc
release: 1.77.2
LLVM version: 17.0.6
```

### Error output

Command: `cargo build`

```
note: no errors encountered even though `span_delayed_bug` issued

note: those delayed bugs will now be shown as internal compiler errors

error: internal compiler error: error performing ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: UserFacing }, value: ProvePredicate { predicate: Binder { value: TraitPredicate(<Bar<{closure@src\main.rs:20:16: 20:19}> as Foo>, polarity:Positive), bound_vars: [] } } }
  --> src\main.rs:20:5
   |
20 |     assert_foo(|_| ());
   |     ^^^^^^^^^^^^^^^^^^
   |
note: delayed at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04\compiler\rustc_trait_selection\src\traits\query\type_op\mod.rs:164:29 - disabled backtrace    
  --> src\main.rs:20:5
   |
20 |     assert_foo(|_| ());
   |     ^^^^^^^^^^^^^^^^^^

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.77.2 (25ef9e3d8 2024-04-09) running on x86_64-pc-windows-msvc

note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED]

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack
error: could not compile `rustc-ice-mre` (bin "rustc-ice-mre")
```

<!--
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>Backtrace</strong></summary>
<p>

```
note: no errors encountered even though `span_delayed_bug` issued

note: those delayed bugs will now be shown as internal compiler errors

error: internal compiler error: error performing ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: UserFacing }, value: ProvePredicate { predicate: Binder { value: TraitPredicate(<Bar<{closure@src\main.rs:20:16: 20:19}> as Foo>, polarity:Positive), bound_vars: [] } } }
  --> src\main.rs:20:5
   |
20 |     assert_foo(|_| ());
   |     ^^^^^^^^^^^^^^^^^^
   |
note: delayed at /rustc/25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04\compiler\rustc_trait_selection\src\traits\query\type_op\mod.rs:164:29
         0: std::backtrace::Backtrace::force_capture
         1: std::backtrace::Backtrace::capture
         2: <rustc_errors::DiagCtxt>::flush_delayed
         3: <rustc_errors::DiagCtxt>::emit_diagnostic
         4: <rustc_errors::diagnostic::Diagnostic as core::cmp::PartialEq>::eq
         5: <rustc_borrowck::type_check::liveness::local_use_map::LocalUseMapBuild as rustc_middle::mir::visit::Visitor>::visit_local
         6: <rustc_borrowck::type_check::TypeVerifier as rustc_middle::mir::visit::Visitor>::visit_body
         7: <rustc_borrowck::type_check::TypeVerifier as rustc_middle::mir::visit::Visitor>::visit_constant
         8: <rustc_borrowck::type_check::TypeVerifier as rustc_middle::mir::visit::Visitor>::visit_body
         9: <rustc_borrowck::type_check::relate_tys::NllTypeRelatingDelegate as rustc_infer::infer::relate::nll::TypeRelatingDelegate>::push_outlives
        10: <rustc_borrowck::member_constraints::MemberConstraintSet<rustc_middle::ty::sty::RegionVid> as core::default::Default>::default
        11: <rustc_borrowck::MirBorrowckCtxt>::consume_operand
        12: rustc_borrowck::mir_borrowck
        13: <rustc_query_impl::plumbing::QueryCtxt as rustc_query_system::query::QueryContext>::load_side_effects
        14: rustc_query_impl::query_callbacks
        15: <rustc_query_impl::plumbing::QueryCtxt as rustc_query_system::query::QueryContext>::load_side_effects
        16: <rustc_query_impl::plumbing::QueryCtxt as rustc_query_system::query::QueryContext>::load_side_effects
        17: rustc_interface::proc_macro_decls::proc_macro_decls_static
        18: <time::date_time::DateTime<time::date_time::offset_kind::Fixed> as core::convert::From<std::time::SystemTime>>::from
        19: rustc_interface::passes::analysis
        20: rustc_query_impl::profiling_support::alloc_self_profile_query_strings
        21: rustc_query_impl::profiling_support::alloc_self_profile_query_strings
        22: rustc_query_impl::profiling_support::alloc_self_profile_query_strings
        23: rustc_query_impl::profiling_support::alloc_self_profile_query_strings
        24: rustc_query_impl::profiling_support::alloc_self_profile_query_strings
        25: rustc_driver_impl::args::arg_expand_all
        26: rustc_driver_impl::args::arg_expand_all
        27: <rustc_middle::ty::SymbolName as core::fmt::Debug>::fmt
        28: <rustc_middle::ty::SymbolName as core::fmt::Debug>::fmt
        29: <rustc_middle::ty::SymbolName as core::fmt::Debug>::fmt
        30: std::sys::pal::windows::thread::Thread::new
        31: BaseThreadInitThunk
        32: RtlUserThreadStart
  --> src\main.rs:20:5
   |
20 |     assert_foo(|_| ());
   |     ^^^^^^^^^^^^^^^^^^

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.77.2 (25ef9e3d8 2024-04-09) running on x86_64-pc-windows-msvc

note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED]

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack
error: could not compile `rustc-ice-mre` (bin "rustc-ice-mre")
```

</p>
</details>

### Notes

The code compiles successfully if the closure is explicitly typed like so:

```rust
assert_foo(|_: &()| ()); // OK
```

Or if an identify function is used like so:

```rust
fn id<F: FnMut(&())>(f: F) -> F { f }

assert_foo(id(|_| ())); // OK
```

My original code produces an error (not ICE) that seems to be a diagnostics bug, complaining about trait bounds not being satisfied when it should really state something along the lines of `implementation of FnMut is not general enough`.
An MRE is provided below.

<details><summary><strong>Original code and error</strong></summary>
<p>

```rust
trait Foo {}

impl<F> Foo for F where F: FnMut(&()) {}

struct Bar<F> {
    f: F,
}

impl<F> Foo for Bar<F> where F: Foo {}

fn main() -> impl Foo {
    Bar { f: (|a| ()) }
}
```

```
error[E0277]: the trait bound `Bar<{closure@src\main.rs:12:15: 12:18}>: Foo` is not satisfied
  --> src\main.rs:11:14
   |
11 | fn main() -> impl Foo {
   |              ^^^^^^^^ the trait `Foo` is not implemented for `Bar<{closure@src\main.rs:12:15: 12:18}>`
   |
   = help: the trait `Foo` is implemented for `Bar<{closure@src\main.rs:12:15: 12:18}>`

error[E0277]: `main` has invalid return type `impl Foo`
  --> src\main.rs:11:14
   |
11 | fn main() -> impl Foo {
   |              ^^^^^^^^ `main` can only return types that implement `Termination`
   |
   = help: consider using `()`, or a `Result`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `rustc-ice-mre` (bin "rustc-ice-mre") due to 2 previous errors
```
</p>
</details>