Skip to content

Conversation

@tmandry
Copy link
Member

@tmandry tmandry commented Jan 22, 2026

A small example to illustrate the difference between array and trait unsized coercions: (playground)

fn check_dyn(t: &Type) {
    check_is_unsize::<Type, dyn Trait>(); // Success
    t.method(); //~ERROR
}

fn check_array(t: &[i32; 3]) {
    check_is_unsize::<[i32; 3], [i32]>(); // Success
    t.len(); // Success
    // ^ Note that len is only defined on [T].
}

fn check_is_unsize<From: ?Sized, To: ?Sized>()
where
    From: std::marker::Unsize<To>,
{}

struct Type;

trait Trait {}

impl dyn Trait {
    fn method(&self) {}
}

impl Trait for Type {}

This came up during a discussion about autoref in zulip: #t-lang/custom-refs > Autoref. Thanks to @BennoLossin for the original example.

The rustc dev guide describes this algorithm similarly to the way the reference does today; it mentions unsized coercions in general, but only uses arrays as an example.

@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Jan 22, 2026
@tmandry
Copy link
Member Author

tmandry commented Jan 24, 2026

@BennoLossin and @Nadrieril pointed out that we could remove this step entirely with a simple Deref impl for arrays:

impl<T, const N: usize> Deref for [T; N] {
    type Target = [T];
    fn deref(&self) -> &[T] { &*self }
}

Similarly for DerefMut. I wonder why those impls don't exist.

@rustbot
Copy link
Collaborator

rustbot commented Jan 27, 2026

☔ The latest upstream changes (possibly 141b1af) made this pull request unmergeable. Please resolve the merge conflicts.

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

Labels

S-waiting-on-review Status: The marked PR is awaiting review from a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants