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

Self not supported in fallible builders (as return type of constructor) #28

Closed
d4h0 opened this issue May 2, 2022 · 2 comments · Fixed by #41
Closed

Self not supported in fallible builders (as return type of constructor) #28

d4h0 opened this issue May 2, 2022 · 2 comments · Fixed by #41

Comments

@d4h0
Copy link

d4h0 commented May 2, 2022

Hi,

First of all, thanks for building buildstructor!

I like the chosen approach quite a lot.

I noticed, that the Self type alias isn't supported, when defining a fallible builder:

// Works:
pub struct Foo {
    foo: (),
}

#[buildstructor::builder]
impl Foo {
    pub fn new(foo: ()) -> Self { // <- `Self`
        Self { foo }
    }
}

// Doesn't work:
pub struct Bar {
    bar: (),
}

#[buildstructor::builder]
impl Bar {
    pub fn new(bar: ()) -> Result<Self, ()> { // <- `Self`
        Ok(Self { bar })
    }
}

// Works:
pub struct Baz {
    baz: (),
}

#[buildstructor::builder]
impl Baz {
    pub fn new(baz: ()) -> Result<Baz, ()> { // <- no `Self`
        Ok(Self { baz })
    }
}

The resulting error is as follows:

error[E0308]: mismatched types
  --> my_crate/main.rs:30:1
   |
30 | #[buildstructor::builder]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `__BarBuilder`, found struct `rustdoc::Bar`
31 | impl Bar {
32 |     pub fn new(bar: ()) -> Result<Self, ()> {
   |                            ---------------- expected `Result<__BarBuilder<(__P0,)>, ()>` because of return type
   |
   = note: expected enum `Result<__BarBuilder<(__P0,)>, _>`
              found enum `Result<rustdoc::Bar, _>`
   = note: this error originates in the attribute macro `buildstructor::builder` (in Nightly builds, run with -Z macro-backtrace for more info)

...which is a bit confusing, to be honest... ;)

@d4h0 d4h0 changed the title Self not supported in failable builders (as return type of constructor) Self not supported in fallible builders (as return type of constructor) May 2, 2022
@BrynCooke
Copy link
Owner

Hi.
Thanks for the detailed bug report!

I'll get round to this in the next couple of weeks.

Not sure what can be done about error messages, the generated code is not pleasant, and only testing and time is going to surface issues. Hopefully I'll get some time to improve things over the coming months.

@d4h0
Copy link
Author

d4h0 commented May 5, 2022

Sounds good. Thank you, @BrynCooke.

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

Successfully merging a pull request may close this issue.

2 participants