Repository: rust
Issue: Conflicting trait impls cause ICE with `generic_const_exprs`
Labels: I-ICE,T-compiler,C-bug,requires-nightly,F-generic_const_exprs,S-bug-has-test,requires-incomplete-features

<!--
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/
-->

### Code ([Playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=c3da9d31de73c379fe06009367c09738))

```Rust
#![allow(incomplete_features)]
#![feature(adt_const_params)]
#![feature(generic_const_exprs)]

use std::ops::Add;

#[derive(PartialEq, Eq, Clone, Debug, core::marker::ConstParamTy)]
pub struct Dimension;

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Default)]
pub struct Quantity<S, const D: Dimension>(pub(crate) S);

impl<const D: Dimension, LHS, RHS> Add<Quantity<RHS, D>> for Quantity<LHS, D>
where
    LHS: Add<RHS>,
{
    type Output = Quantity<<LHS as Add<RHS>>::Output, D>;
    fn add(self, rhs: Quantity<RHS, D>) -> Self::Output {
        Quantity(self.0 + rhs.0)
    }
}

impl<LHS, RHS> Add<RHS> for Quantity<LHS, { Dimension }>
where
    LHS: Add<RHS>,
{
    type Output = Quantity<<LHS as Add<RHS>>::Output, { Dimension }>;
    fn add(self, rhs: RHS) -> Self::Output {
        Quantity(self.0 + rhs)
    }
}

impl Add<Quantity<f32, { Dimension }>> for f32 {
    type Output = Quantity<f32, { Dimension }>;
    fn add(self, rhs: Quantity<f32, { Dimension }>) -> Self::Output {
        Quantity(self + rhs.0)
    }
}

pub fn add<const U: Dimension>(x: Quantity<f32, U>, y: Quantity<f32, U>) -> Quantity<f32, U> {
    x + y
}

fn main() {
    add(Quantity::<f32, {Dimension}>(1.0), Quantity(2.0));
}
```

The code should not compile, since the first two trait impls are conflicting. If `generic_const_exprs` is disabled (it is not needed code-wise in this reduced example, but was necessary in my original code), the compiler gives the correct error message:

```
error[E0119]: conflicting implementations of trait `Add<Quantity<_, Dimension>>` for type `Quantity<_, Dimension>`
  --> src/main.rs:22:1
   |
12 | / impl<const D: Dimension, LHS, RHS> Add<Quantity<RHS, D>> for Quantity<LHS, D>
13 | | where
14 | |     LHS: Add<RHS>,
   | |__________________- first implementation here
...
22 | / impl<LHS, RHS> Add<RHS> for Quantity<LHS, { Dimension }>
23 | | where
24 | |     LHS: Add<RHS>,
   | |__________________^ conflicting implementation for `Quantity<_, Dimension>`
```

Note that the even the third trait impl is needed in order to reproduce the ICE. The code compiles fine if either the second or the third trait impl is removed (the first one is actually used, so it cant be removed).

### 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.0-nightly (d6d7a9386 2023-12-22)
binary: rustc
commit-hash: d6d7a93866f2ffcfb51828b8859bdad760b54ce0
commit-date: 2023-12-22
host: x86_64-unknown-linux-gnu
release: 1.77.0-nightly
LLVM version: 17.0.6
```

### Error output

```
error: internal compiler error: compiler/rustc_middle/src/ty/instance.rs:414:25: failed to resolve instance for <Quantity<f32, Dimension> as Add>::add: Err(
                                    ErrorGuaranteed(
                                        (),
                                    ),
                                )

thread 'rustc' panicked at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/compiler/rustc_errors/src/lib.rs:1122:30:
Box<dyn Any>
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: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED]

query stack during panic:
#0 [collect_and_partition_mono_items] collect_and_partition_mono_items
end of query stack
```

<!--
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>

```
stack backtrace:
   0:     0x7f38a818b6f6 - std::backtrace_rs::backtrace::libunwind::trace::h4c39d39dfbe9783d
                               at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5
   1:     0x7f38a818b6f6 - std::backtrace_rs::backtrace::trace_unsynchronized::h4dc8ace54ae311e5
                               at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f38a818b6f6 - std::sys_common::backtrace::_print_fmt::hb93bb30078f88f5e
                               at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x7f38a818b6f6 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hc0a088b7e0825f6a
                               at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7f38a81ddf40 - core::fmt::rt::Argument::fmt::h3bf37701eb7e7bdb
                               at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/core/src/fmt/rt.rs:142:9
   5:     0x7f38a81ddf40 - core::fmt::write::h7e6ab5f6daf5c422
                               at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/core/src/fmt/mod.rs:1120:17
   6:     0x7f38a817f4af - std::io::Write::write_fmt::h08c3306cb5bf723d
                               at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/std/src/io/mod.rs:1810:15
   7:     0x7f38a818b4d4 - std::sys_common::backtrace::_print::hafed43d2fa285a46
                               at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7f38a818b4d4 - std::sys_common::backtrace::print::h39c23a58558ca2e2
                               at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7f38a818e267 - std::panicking::default_hook::{{closure}}::h33423deafbc8e615
  10:     0x7f38a818dfc9 - std::panicking::default_hook::h50ae27bf0009ee3b
                               at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/std/src/panicking.rs:292:9
  11:     0x7f38aaf82ccc - std[3a009c5aa3cd6e18]::panicking::update_hook::<alloc[7046a3554d0212f6]::boxed::Box<rustc_driver_impl[99e8362fbaa298ef]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7f38a818e9b6 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h5ddf83902817cab1
                               at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/alloc/src/boxed.rs:2029:9
  13:     0x7f38a818e9b6 - std::panicking::rust_panic_with_hook::hbc586ac9df6b73a8
                               at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/std/src/panicking.rs:783:13
  14:     0x7f38aafbbcd4 - std[3a009c5aa3cd6e18]::panicking::begin_panic::<rustc_errors[f488520a7868b61e]::ExplicitBug>::{closure#0}
  15:     0x7f38aafb8506 - std[3a009c5aa3cd6e18]::sys_common::backtrace::__rust_end_short_backtrace::<std[3a009c5aa3cd6e18]::panicking::begin_panic<rustc_errors[f488520a7868b61e]::ExplicitBug>::{closure#0}, !>
  16:     0x7f38aafb7eb6 - std[3a009c5aa3cd6e18]::panicking::begin_panic::<rustc_errors[f488520a7868b61e]::ExplicitBug>
  17:     0x7f38aafc6f01 - <rustc_errors[f488520a7868b61e]::diagnostic_builder::BugAbort as rustc_errors[f488520a7868b61e]::diagnostic_builder::EmissionGuarantee>::emit_producing_guarantee
  18:     0x7f38ab39ee8f - <rustc_errors[f488520a7868b61e]::DiagCtxt>::bug::<alloc[7046a3554d0212f6]::string::String>
  19:     0x7f38ab43aa1b - rustc_middle[8402766bc0d86440]::util::bug::opt_span_bug_fmt::<rustc_span[fa12e73b1270738]::span_encoding::Span>::{closure#0}
  20:     0x7f38ab423d9a - rustc_middle[8402766bc0d86440]::ty::context::tls::with_opt::<rustc_middle[8402766bc0d86440]::util::bug::opt_span_bug_fmt<rustc_span[fa12e73b1270738]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  21:     0x7f38ab423c38 - rustc_middle[8402766bc0d86440]::ty::context::tls::with_context_opt::<rustc_middle[8402766bc0d86440]::ty::context::tls::with_opt<rustc_middle[8402766bc0d86440]::util::bug::opt_span_bug_fmt<rustc_span[fa12e73b1270738]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  22:     0x7f38a939e400 - rustc_middle[8402766bc0d86440]::util::bug::bug_fmt
  23:     0x7f38ac4778b8 - <rustc_middle[8402766bc0d86440]::ty::instance::Instance>::expect_resolve
  24:     0x7f38aa11a260 - rustc_monomorphize[87b7e1563872e463]::collector::collect_items_rec::{closure#0}
  25:     0x7f38ad116156 - rustc_monomorphize[87b7e1563872e463]::collector::collect_items_rec
  26:     0x7f38ad116654 - rustc_monomorphize[87b7e1563872e463]::collector::collect_items_rec
  27:     0x7f38ad111c3e - rustc_monomorphize[87b7e1563872e463]::partitioning::collect_and_partition_mono_items
  28:     0x7f38ad1111b0 - rustc_query_impl[a5b594d180e67741]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a5b594d180e67741]::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2}::{closure#0}, rustc_middle[8402766bc0d86440]::query::erase::Erased<[u8; 24usize]>>
  29:     0x7f38ad111195 - <rustc_query_impl[a5b594d180e67741]::query_impl::collect_and_partition_mono_items::dynamic_query::{closure#2} as core[2b984803ee3f5e21]::ops::function::FnOnce<(rustc_middle[8402766bc0d86440]::ty::context::TyCtxt, ())>>::call_once
  30:     0x7f38ad10f9b7 - rustc_query_system[f0f2c4fcd6daa9a]::query::plumbing::try_execute_query::<rustc_query_impl[a5b594d180e67741]::DynamicConfig<rustc_query_system[f0f2c4fcd6daa9a]::query::caches::SingleCache<rustc_middle[8402766bc0d86440]::query::erase::Erased<[u8; 24usize]>>, false, false, false>, rustc_query_impl[a5b594d180e67741]::plumbing::QueryCtxt, true>
  31:     0x7f38ad2b5db3 - rustc_query_impl[a5b594d180e67741]::query_impl::collect_and_partition_mono_items::get_query_incr::__rust_end_short_backtrace
  32:     0x7f38acbab696 - rustc_codegen_ssa[daa1c997743eea6f]::base::codegen_crate::<rustc_codegen_llvm[e6316feb9c2ceeb]::LlvmCodegenBackend>
  33:     0x7f38acbab335 - <rustc_codegen_llvm[e6316feb9c2ceeb]::LlvmCodegenBackend as rustc_codegen_ssa[daa1c997743eea6f]::traits::backend::CodegenBackend>::codegen_crate
  34:     0x7f38acba95ed - rustc_interface[dfee6d40a3df20bb]::passes::start_codegen
  35:     0x7f38acba8d78 - <rustc_interface[dfee6d40a3df20bb]::queries::Queries>::codegen_and_build_linker
  36:     0x7f38ad19f2a8 - rustc_interface[dfee6d40a3df20bb]::interface::run_compiler::<core[2b984803ee3f5e21]::result::Result<(), rustc_span[fa12e73b1270738]::ErrorGuaranteed>, rustc_driver_impl[99e8362fbaa298ef]::run_compiler::{closure#0}>::{closure#0}
  37:     0x7f38ad125346 - std[3a009c5aa3cd6e18]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[dfee6d40a3df20bb]::util::run_in_thread_with_globals<rustc_interface[dfee6d40a3df20bb]::util::run_in_thread_pool_with_globals<rustc_interface[dfee6d40a3df20bb]::interface::run_compiler<core[2b984803ee3f5e21]::result::Result<(), rustc_span[fa12e73b1270738]::ErrorGuaranteed>, rustc_driver_impl[99e8362fbaa298ef]::run_compiler::{closure#0}>::{closure#0}, core[2b984803ee3f5e21]::result::Result<(), rustc_span[fa12e73b1270738]::ErrorGuaranteed>>::{closure#0}, core[2b984803ee3f5e21]::result::Result<(), rustc_span[fa12e73b1270738]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[2b984803ee3f5e21]::result::Result<(), rustc_span[fa12e73b1270738]::ErrorGuaranteed>>
  38:     0x7f38ad125173 - <<std[3a009c5aa3cd6e18]::thread::Builder>::spawn_unchecked_<rustc_interface[dfee6d40a3df20bb]::util::run_in_thread_with_globals<rustc_interface[dfee6d40a3df20bb]::util::run_in_thread_pool_with_globals<rustc_interface[dfee6d40a3df20bb]::interface::run_compiler<core[2b984803ee3f5e21]::result::Result<(), rustc_span[fa12e73b1270738]::ErrorGuaranteed>, rustc_driver_impl[99e8362fbaa298ef]::run_compiler::{closure#0}>::{closure#0}, core[2b984803ee3f5e21]::result::Result<(), rustc_span[fa12e73b1270738]::ErrorGuaranteed>>::{closure#0}, core[2b984803ee3f5e21]::result::Result<(), rustc_span[fa12e73b1270738]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[2b984803ee3f5e21]::result::Result<(), rustc_span[fa12e73b1270738]::ErrorGuaranteed>>::{closure#1} as core[2b984803ee3f5e21]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  39:     0x7f38a81988e5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h446fa4c55b1bf8c4
                               at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/alloc/src/boxed.rs:2015:9
  40:     0x7f38a81988e5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hb661ed53115c5033
                               at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/alloc/src/boxed.rs:2015:9
  41:     0x7f38a81988e5 - std::sys::unix::thread::Thread::new::thread_start::hbd74c47d36330101
                               at /rustc/d6d7a93866f2ffcfb51828b8859bdad760b54ce0/library/std/src/sys/unix/thread.rs:108:17
  42:     0x7f38a7f5a9eb - <unknown>
  43:     0x7f38a7fde7cc - <unknown>
  44:                0x0 - <unknown>
```

</p>
</details>

[rustc-ice-2024-01-07T13_58_40-55799.txt](https://github.com/rust-lang/rust/files/13853697/rustc-ice-2024-01-07T13_58_40-55799.txt)