Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kupiakos opened this issue Mar 18, 2024 · 1 comment
Labels
A-associated-items Area: Associated items such as associated types and consts. C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@kupiakos
Copy link
Contributor

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.

@kupiakos kupiakos added the C-bug Category: This is a bug. label Mar 18, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 18, 2024
@fmease fmease added C-discussion Category: Discussion or questions that doesn't represent real issues. T-types Relevant to the types team, which will review and decide on the PR/issue. A-associated-items Area: Associated items such as associated types and consts. C-bug Category: This is a bug. and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. C-discussion Category: Discussion or questions that doesn't represent real issues. labels Mar 18, 2024
@compiler-errors
Copy link
Member

compiler-errors commented Mar 18, 2024

The reference should probably be adjusted to stop saying "unsized coercion" and just say "coercion from array to slice".

I estimate it's going to be an infeasible amount of work to allow any kind of unsize coercion here, since unsize coercions are quite general and rustc isn't really equipped to walk through the space of all possible targets; and also, fixing this would necessarily induce breakage due to changing the order that methods are considered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items such as associated types and consts. C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants