Skip to content

Cannot call method that requires a non-array unsized coercion, contrary to the reference #122708

Open
@kupiakos

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 eb45c84). Appears to occur in every version of Rust.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)C-bugCategory: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions