Skip to content

Trait object coercion does not work with return objects #12744

New issue

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

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

Already on GitHub? # to your account

Closed
jmgrosen opened this issue Mar 7, 2014 · 2 comments · Fixed by #19780
Closed

Trait object coercion does not work with return objects #12744

jmgrosen opened this issue Mar 7, 2014 · 2 comments · Fixed by #19780
Labels
A-trait-system Area: Trait system E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@jmgrosen
Copy link
Contributor

jmgrosen commented Mar 7, 2014

fn foo() -> ~std::any::Any {
    ~3u32
}
test_trait.rs:4:5: 4:10 error: mismatched types: expected `~std::any::Any:Send` but found `~u32` (expected trait std::any::Any but found ~-ptr)
test_trait.rs:4     ~3u32
                    ^~~~~
error: aborting due to previous error

I guess #11156 missed it?

@aturon
Copy link
Member

aturon commented Jul 23, 2014

In more detail:

trait Foo {}
impl Foo for u8 {}

// Coercions work in argument position:

fn take_box(foo: Box<Foo>) {}
fn take_ref(foo: &Foo) {}

fn use_takes() {
    let x: u8 = 0;
    take_box(box x);
    take_ref(&x);
}

// But do not work in return position:

fn make_box() -> Box<Foo> {
    // The following fails with:
    //   error: mismatched types: expected `Box<Foo>` but found `Box<u8>`
    //   (expected trait Foo but found u8)
    // box 0u8

    box 0u8 as Box<Foo>
}

fn make_ref(u: &u8) -> &Foo {
    // The following fails with:
    //   error: mismatched types: expected `&Foo` but found `&u8`
    //   (expected trait Foo but found u8)
    // u

    u as &Foo
}

// They do work via ascription:

fn main() {
    let b: Box<Foo> = box 0u8;
    let r: &Foo = &0u8;
}

@bluss
Copy link
Member

bluss commented Nov 29, 2014

I think it's fixed.

fn test() -> Box<std::any::Any + 'static> { box 1i }
println!("{}", test())

@alexcrichton alexcrichton added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Nov 30, 2014
bors added a commit that referenced this issue Dec 18, 2014
Closes #5988.
Closes #10176.
Closes #10456.
Closes #12744.
Closes #13264.
Closes #13324.
Closes #14182.
Closes #15381.
Closes #15444.
Closes #15480.
Closes #15756.
Closes #16822.
Closes #16966.
Closes #17351.
Closes #17503.
Closes #17545.
Closes #17771.
Closes #17816.
Closes #17897.
Closes #17905.
Closes #18188.
Closes #18232.
Closes #18345.
Closes #18389.
Closes #18400.
Closes #18502.
Closes #18611.
Closes #18783.
Closes #19009.
Closes #19081.
Closes #19098.
Closes #19127.
Closes #19135.
bors added a commit to rust-lang-ci/rust that referenced this issue Jul 25, 2022
Fix typos

Signed-off-by: hi-rustin <rustin.liu@gmail.com>
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-trait-system Area: Trait system E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants