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

Deref coercion not working for Box<Trait> #22194

Open
aturon opened this issue Feb 11, 2015 · 8 comments
Open

Deref coercion not working for Box<Trait> #22194

aturon opened this issue Feb 11, 2015 · 8 comments
Labels
A-trait-system Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@aturon
Copy link
Member

aturon commented Feb 11, 2015

The following fails to compile:

trait Trait {}

fn takes_ref(t: &Trait) {}
fn takes_box(t: Box<Trait>) {
    takes_ref(&t)
}
fn main() { }

with

<anon>:5:15: 5:17 error: the trait `Trait` is not implemented for the type `Box<Trait>` [E0277]
<anon>:5     takes_ref(&t)
                       ^~

despite the Deref implementation here.

I believe this should work based on the RFC.

@aturon
Copy link
Member Author

aturon commented Feb 11, 2015

cc @nick29581 @wycats

@steveklabnik steveklabnik added the A-trait-system Area: Trait system label Feb 13, 2015
@emberian
Copy link
Member

emberian commented Jun 9, 2015

Bump!

@steveklabnik
Copy link
Member

Triage: today, this complains about a lack of 'static, and I'm not 100% sure how to fix it; I thought adding + 'static would, but it doesn't seem to help.

@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 22, 2017
@Michael-F-Bryan
Copy link

I also encountered this bug while trying to iterate over boxed trait objects and posted to the user forum.

@CAD97
Copy link
Contributor

CAD97 commented Oct 25, 2018

This came up again today.

Based on my reading, the unsize cooersion is only applying to the first level when it should pass through Deref cooersion as well.

@Spoonbender
Copy link

Triage: no change

@nielsle
Copy link
Contributor

nielsle commented Mar 23, 2022

This version seems to compile

trait Trait {}
fn takes_ref(t: &dyn Trait) {}
fn takes_box(t: Box<dyn Trait>) {
    takes_ref(&*t)
}

@Michael-F-Bryan
Copy link

@nielsle the problem is that takes_ref(&t) won't compile even though t dereferences to dyn Trait, which is what you would expect according to the deref coersion rules.

@Noratrieb Noratrieb added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trait-system Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

9 participants