Repository: rust
Issue: Passing pointer to value of type alias with `impl Trait` through function breaks this type
Labels: C-bug,F-type_alias_impl_trait,T-types

<!--
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.
-->

I tried this code:

(1) I originally encountered this issue with pointers
```rust
#![feature(type_alias_impl_trait)]
struct Buffered<T>(T);
struct Inner<T, U>(T, U);
struct Regular;

trait Tr {}

type Stream = Buffered<Inner<Regular, impl Tr>>;

struct Ext(u64);

fn transform_to_ext(s: *mut Stream) -> *mut Ext {
    s.cast()
}

fn opaque() -> impl Tr + 'static {
    struct O(u64);
    impl Tr for O {}

    O(3)
}

fn wrap<R, T: Tr>(r: R, o: T) -> Inner<R, T> {
    Inner(r, o)
}

fn bug() -> *mut Ext {
    let inner = wrap(Regular, opaque());
    let boxed = Box::new(Buffered(inner));
    // *mut Stream -> *mut Ext, this should be fine
    transform_to_ext(Box::leak(boxed) as *mut Stream)
}

// fn works() -> *mut Stream {
//     let inner = wrap(Regular, opaque());
//     let boxed = Box::new(Buffered(inner));
//     // *mut Stream -> *mut Ext, this should be fine
//     Box::leak(boxed) as *mut Stream
// }
```

(2) If you remove pointers, there is the same-ish error, but with more (pointless for me though) diagnostics:
```rust
#![feature(type_alias_impl_trait)]
struct Buffered<T>(T);
struct Inner<T, U>(T, U);
struct Regular;

trait Tr {}

type Stream = Buffered<Inner<Regular, impl Tr>>;

struct Ext(u64);

fn transform_to_ext(s: Stream) -> Ext {
    unimplemented!()
}

fn opaque() -> impl Tr {
    struct O(u64);
    impl Tr for O {}

    O(3)
}

fn wrap<R, T: Tr>(r: R, o: T) -> Inner<R, T> {
    Inner(r, o)
}

fn bug() -> Ext {
    let inner = wrap(Regular, opaque());
    transform_to_ext(Buffered(inner))
}
```

I expected to see this happen: no errors

Instead, this happened:
(1)
```
error[E0606]: casting `&mut Buffered<Inner<Regular, impl Tr + 'static>>` as `*mut Buffered<Inner<Regular, Stream::{opaque#0}>>` is invalid
  --> src/main.rs:31:26
   |
31 |         transform_to_ext(Box::leak(boxed) as *mut Stream)
   |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0606`.
```
(2)
```
error[E0308]: mismatched types
  --> src/main.rs:29:31
   |
8  | type Stream = Buffered<Inner<Regular, impl Tr>>;
   |                                       ------- the expected opaque type
...
16 | fn opaque() -> impl Tr {
   |                ------- the found opaque type
...
29 |     transform_to_ext(Buffered(inner))
   |                      -------- ^^^^^ expected `Inner<Regular, Stream::{opaque#0}>`, found `Inner<Regular, impl Tr>`
   |                      |
   |                      arguments to this struct are incorrect
   |
   = note: expected struct `Inner<_, Stream::{opaque#0}>`
              found struct `Inner<_, impl Tr>`
   = note: distinct uses of `impl Trait` result in different opaque types
help: the type constructed contains `Inner<Regular, impl Tr>` due to the type of the argument passed
  --> src/main.rs:29:22
   |
29 |     transform_to_ext(Buffered(inner))
   |                      ^^^^^^^^^-----^
   |                               |
   |                               this argument influences the type of `Buffered`
note: tuple struct defined here
  --> src/main.rs:2:8
   |
2  | struct Buffered<T>(T);
   |        ^^^^^^^^
```

### 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.79.0-nightly (0d8b3346a 2024-04-14)
binary: rustc
commit-hash: 0d8b3346a3992ab11ea35ff0fb95a6864b91f797
commit-date: 2024-04-14
host: x86_64-pc-windows-msvc
release: 1.79.0-nightly
LLVM version: 18.1.3
```