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
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