Repository: rust
Issue: `generic_const_exprs` and generics inferring lead to false impls resolving across files
Labels: T-compiler,C-bug,A-const-generics,F-generic_const_exprs

<!--
Thank you for filing a bug report! 🐛 Please provide a short summary of the bug,
along with any information you feel relevant to replicating the bug.
-->

Please consider these two code files:

`lib.rs`
```rust
#![feature(generic_const_exprs)]

pub fn hash<H, T>(a: T)
where
    H: Digest,
    [(); H::OutputSize::SIZE]:,
    ArrayWrapper: From<[u8; H::OutputSize::SIZE]>,
{
}

pub trait Digest {
    type OutputSize: TypeNumValue;
}

pub struct Hasher {}

impl Digest for Hasher {
    type OutputSize = TypeNum32;
}

pub struct TypeNum32;

pub trait TypeNumValue {
    const SIZE: usize;
}

impl TypeNumValue for TypeNum32 {
    const SIZE: usize = 32;
}

pub struct ArrayWrapper {}

impl From<[u8; 32]> for ArrayWrapper {
    fn from(_value: [u8; 32]) -> Self {
        todo!()
    }
}
```

`main.rs`
```rust
use rust_basic::{hash, Hasher};

fn main() {
    hash::<Hasher, _>(0_u8);
}

```

I expect this to compile, but instead the compiler complains:
```console
error[E0277]: the trait bound `ArrayWrapper: From<[u8; rust_basic::::hash::{constant#1}]>` is not satisfied
 --> src/main.rs:4:12
  |
4 |     hash::<Hasher, _>(0_u8);
  |            ^^^^^^ the trait `From<[u8; rust_basic::::hash::{constant#1}]>` is not implemented for `ArrayWrapper`
  |
  = help: the trait `From<[u8; 32]>` is implemented for `ArrayWrapper`
note: required by a bound in `rust_basic::hash`
 --> /home/bczhc/code/rust-basic/src/lib.rs:7:19
  |
3 | pub fn hash<H, T>(a: T)
  |        ---- required by a bound in this function
...
7 |     ArrayWrapper: From<[u8; H::OutputSize::SIZE]>,
  |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `hash`

```

However, this can be solved by basically three ways so far:
- Fix 1

  Explicitly add the second generic type on `hash` call in `main.rs`, as:
  ```rust
  hash::<Hasher, u8>(0_u8);
  ```

- Fix 2

  Just delete the second generic `T` on both `hash` function definition and its call.

- Fix 3

  Add `#![feature(generic_const_exprs)]` at the top of `main.rs`.


There's another way: move `hash` to `main.rs`; merge them in one file. (`#![feature(generic_const_exprs)]` is required)

After applying one of the fixes, the code compiles. Basically the second generic *shouldn't* make a change here, but it does. The compiler's trait-bound-not-satisfied error message is quite confusing! For the real-world case it's me today coding with crate `digest`, and encountering this bug. The code is like this for a comparison and reference:
```rust
pub fn fixed_output_hash<H, R>(
    mut reader: R,
    iter_num: u64,
) -> io::Result<[u8; H::OutputSize::USIZE]>
where
    H: FixedOutput,
    R: Read,
    H: Write,
    [(); H::OutputSize::USIZE]:,
    [u8; H::OutputSize::USIZE]: From<GenericArray<u8, H::OutputSize>>,
{
    todo!()
}
```

### 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.73.0-nightly (8131b9774 2023-08-02)
binary: rustc
commit-hash: 8131b9774ebcb6c162fcac71545a13543ec369e7
commit-date: 2023-08-02
host: x86_64-unknown-linux-gnu
release: 1.73.0-nightly
LLVM version: 16.0.5
```