Passing pointer to value of type alias with `impl Trait` through function breaks this type
Open
Nobody has claimed this yet.
C-bug
F-type_alias_impl_trait
T-types
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
I tried this code:
(1) I originally encountered this issue with pointers
#![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:
#![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
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the two minimal reproductions in src/main.rs and run them with the nightly rustc version reported in the issue. Compare the pointer and non-pointer cases involving type_alias_impl_trait and opaque types. Done means both examples compile without the reported casting or mismatched-type errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100