Repository: rust
Issue: Trait bounds of `<[T; MYSIZE] as SomeTrait>::AssociatedType` are not deduplicated
Labels: A-associated-items,C-bug,A-const-generics

Original issue: https://github.com/rkyv/rkyv/issues/332

I tried this code:
~~_(I have not managed to make this example any more 'minimal' than this unfortunately)_~~ 
EDIT: See [second comment](https://github.com/rust-lang/rust/issues/107209#issuecomment-1399626243) for a more minimal example.

```
[package]
name = "array_traits_bug"
version = "0.1.0"
edition = "2021"

[dependencies]
rkyv = "0.7.39"
```

```rust
use rkyv::{Archive, Deserialize};

pub const MYSIZE: usize = 10;

#[derive(Archive, Deserialize)]
pub enum Happy<K>
where
    K: rkyv::Archive,
{
    A([K; 10]),
    B([K; 10]),
}

#[derive(Archive, Deserialize)]
pub enum Sad<K>
where
    K: rkyv::Archive,
{
    A([K; MYSIZE]),
    B([K; MYSIZE]),
}
```

[implementation of rkyv::{Archive, Deserialize} for [T; N]](https://github.com/rkyv/rkyv/blob/master/rkyv/src/impls/core/mod.rs#L128-L176)

I expected this code to compile, with the enums 'Happy' and 'Sad' behaving in the same way.

Instead, a compiler error occurs while deriving Deserialize for Sad.
(Deriving for Happy works without problems however!)

<details><summary>Backtrace</summary>


```rust
error[E0284]: type annotations needed
  --> src/main.rs:14:19
   |
14 | #[derive(Archive, Deserialize)]
   |                   ^^^^^^^^^^^ cannot infer type
   |
   = note: cannot satisfy `<[K; 10] as Archive>::Archived == _`
   = note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0283]: type annotations needed: cannot satisfy `[K; 10]: Archive`
  --> src/main.rs:14:19
   |
14 | #[derive(Archive, Deserialize)]
   |                   ^^^^^^^^^^^
   |
note: multiple `impl`s or `where` clauses satisfying `[K; 10]: Archive` found
  --> src/main.rs:14:19
   |
14 | #[derive(Archive, Deserialize)]
   |                   ^^^^^^^^^^^
   = note: and another `impl` found in the `rkyv` crate:
           - impl<T, N> Archive for [T; N]
             where T: Archive;
note: required for `Sad<K>` to implement `Archive`
  --> src/main.rs:14:10
   |
14 | #[derive(Archive, Deserialize)]
   |          ^^^^^^^ unsatisfied trait bound introduced in this `derive` macro
15 | pub enum Sad<K>
   |          ^^^^^^
   = note: this error originates in the derive macro `Deserialize` which comes from the expansion of the derive macro `Archive` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0283]: type annotations needed
  --> src/main.rs:14:19
   |
14 | #[derive(Archive, Deserialize)]
   |                   ^^^^^^^^^^^ cannot infer type
   |
note: multiple `impl`s or `where` clauses satisfying `_: Deserialize<[K; 10], __D>` found
  --> src/main.rs:14:19
   |
14 | #[derive(Archive, Deserialize)]
   |                   ^^^^^^^^^^^
   = note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)

Some errors have detailed explanations: E0283, E0284.
For more information about an error, try `rustc --explain E0283`.

```

</details>

This problem occurs both on stable and on nightly.

`rustc --version --verbose`:
```
rustc 1.68.0-nightly (52372f9c7 2023-01-21)
binary: rustc
commit-hash: 52372f9c71d8ade4cb815524f179119656f0aa2e
commit-date: 2023-01-21
host: aarch64-apple-darwin
release: 1.68.0-nightly
LLVM version: 15.0.6
```

---

It seems like the compiler treats `[K; 10]` and `pub const MYSIZE: usize = 10; ... [K; MYSIZE]` differently when looking up trait implementations / deducing trait bounds.

Curiously, the code compiles fine:
- When a struct is used rather than an enum.
- When an enum containing only a single variant is used. (e.g. `Sad` without `Sad::B`)
- When `Sad::B` would not also contain an ArrayVec (e.g. replace `B([K; MYSIZE]),` by `B(usize)`)
- When `Sad::B`'s ArrayVec would have a different size (e.g. replace `B([K; MYSIZE]),` by `B([K; MYSIZE + 1]),`

This makes me think that the compiler maybe realizes that `[K; 10]` is the same when it is reused in multiple enum variants but does not do the same deduplication for `[K; MYSIZE]`.

I did not manage to make the minimal example any smaller. It might be the case that the bug only surfaces when multiple complex compiler features interact.
 Help in this regard is also greatly appreciated.