Skip to content

box try!(MyStruct::new()) as Box<MyTrait> no longer works without being put on separate lines #20128

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

Closed
daboross opened this issue Dec 22, 2014 · 4 comments

Comments

@daboross
Copy link
Contributor

This code worked on the nightly from the 18th, and is broken by the nightly on the 21st.

Last nightly working version: rustc 0.13.0-nightly (2a231594c 2014-12-18 12:21:57 +0000)
First nightly broken version: rustc 0.13.0-nightly (cc19e3380 2014-12-20 20:00:36 +0000)

Here's the full error:

<std macros>:4:24: 4:27 error: mismatched types: expected `MyTrait + 'static`, found `MyStruct` (expected trait MyTrait, found struct MyStruct)
<std macros>:4             Ok(val) => val,
                                      ^~~
<std macros>:1:1: 8:2 note: in expansion of try!
src/main.rs:16:21: 16:45 note: expansion site
src/main.rs:16:17: 16:68 error: the trait `core::kinds::Sized` is not implemented for the type `MyTrait + 'static`
src/main.rs:16     let boxed = box try!(MyStruct::new()) as Box<MyTrait + 'static>;
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/main.rs:16:17: 16:68 note: only sized types can be made into objects
src/main.rs:16     let boxed = box try!(MyStruct::new()) as Box<MyTrait + 'static>;
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/main.rs:16:17: 16:68 error: the trait `MyTrait` is not implemented for the type `MyTrait + 'static`
src/main.rs:16     let boxed = box try!(MyStruct::new()) as Box<MyTrait + 'static>;
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/main.rs:16:17: 16:68 note: required for the cast to the object type `MyTrait + 'static`
src/main.rs:16     let boxed = box try!(MyStruct::new()) as Box<MyTrait + 'static>;
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 3 previous errors

Here's the code producing the above error:

trait MyTrait {}

struct MyStruct;

impl MyStruct {
    fn new() -> Result<MyStruct, String> {
        return Ok(MyStruct);
    }
}

impl MyTrait for MyStruct {}

/// This used to work, and doesn't work anymore
fn get1() -> Result<Box<MyTrait + 'static>, String> {
    let boxed = box try!(MyStruct::new()) as Box<MyTrait + 'static>;

    return Ok(boxed);
}

/// This works fine
fn get2() -> Result<Box<MyTrait + 'static>, String> {
    let thing = box try!(MyStruct::new());
    let boxed = thing as Box<MyTrait + 'static>;

    return Ok(boxed);
}

fn main() {}

Rust playpen: http://is.gd/JNqrmN

@oli-obk
Copy link
Contributor

oli-obk commented Dec 22, 2014

related to #20093 and might be fixed by #20099

@ftxqxd
Copy link
Contributor

ftxqxd commented Dec 22, 2014

I don’t think this is related to #20093. It’s not a parsing issue: it gets past the parsing stage without issues, and adding delimiters ({}) doesn’t affect it. Also, #20093 only affects macro invocations that begin statements.

@daboross
Copy link
Contributor Author

I don't think it's related to #20099, as it fails even when using a match statement instead of try!().

trait MyTrait {}

struct MyStruct;

impl MyStruct {
    fn new() -> Result<MyStruct, String> {
        return Ok(MyStruct);
    }
}

impl MyTrait for MyStruct {}

fn get() -> Result<(), String> {

    box match MyStruct::new() {
        Ok(v) => v,
        Err(e) => return Err(e),
    } as Box<MyTrait>;

    return Ok(());
}

fn main() {
}

@daboross
Copy link
Contributor Author

Closing as this seems to work in the latest nightly! (rustc 0.13.0-nightly (96a3c7c6a 2014-12-23 22:21:10 +0000))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants