Cannot call method that requires a non-array unsized coercion, contrary to the reference
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
In short: why can't I call Cell::new([1, 2, 3]).as_slice_of_cells()?
Minimal example:
struct Foo<T: ?Sized>(T);
impl<T> Foo<[T]> {
fn foo_generic_slice(&self) {}
}
Foo([1u8, 2, 3]).foo_generic_slice();
I expected to see this happen: this should compile. As documented by the reference, these candidates should be considered in the above method call:
Foo<[u8; 3]>&Foo<[u8; 3]>&mut Foo<[u8; 3]>Foo<[u8]>(by unsized coercion)&Foo<[u8]>&mut Foo<[u8]>
The foo_generic_slice method should be called via the unsized coercion to Foo<[u8]>.
Instead, this happened: I get a compile error, stating that I must perform the unsized coercion manually:
error[E0599]: no method named `foo_generic_slice` found for struct `Foo<[u8; 3]>` in the current scope
--> src/main.rs:34:9
|
6 | struct Foo<T: ?Sized>(T);
| --------------------- method `foo_generic_slice` not found for this struct
...
34 | foo.foo_generic_slice();
| ^^^^^^^^^^^^^^^^^ method not found in `Foo<[u8; 3]>`
|
= note: the method was found for
- `Foo<[T]>`
In contrast, if the unsized coercion is from [T; N] to [T] rather than for a custom DST, the method can be called. This happens every time .len() is called on an array. See this playground which tests other broken and working scenarios.
There's also another unsized coercion that seems to be ignored entirely - the coercion from T to dyn Trait is ignored for inherent methods implemented on dyn Trait - it seems that actually looking up all of these candidates as described by the reference could be incredibly costly, especially for the Foo<T> to Foo<dyn Trait> coercion.
Meta
Happens in Rust 1.76.0 and 1.79.0-nightly (2024-03-17 eb45c844407968ea54df). Appears to occur in every version of Rust.
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 by compiling the minimal example in src/main.rs and compare the observed method lookup with the candidate list in the linked Rust Reference section. Use the linked playground to check the custom DST, array-to-slice, and dyn Trait cases; done means the behavior or its documented limitation is clearly established and covered by an appropriate regression test.
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
- Mostly clear
- Newbie friendliness
- 35/100