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

Unhelpful Error with #[project] when ommitting dummy attribute on nightly and forgetting mut on self #63

Closed
kbleeke opened this issue Sep 3, 2019 · 2 comments
Labels
A-diagnostics Area: Messages for errors and warnings C-upstream-bug Category: This is a bug of compiler or dependencies (the fix may require action in the upstream)

Comments

@kbleeke
Copy link

kbleeke commented Sep 3, 2019

Apologies for the long title, this issue is hard to put in a sentence. Basically I upgraded from a previous alpha to alpha 7 and ended up with something like this modified example code (from the docs on #[project]):

    use pin_project::{project, pin_project};
    use std::pin::Pin;

    #[pin_project]
    enum Foo<A, B, C> {
        Tuple(#[pin] A, B),
        Struct { field: C },
        Unit,
    }

    impl<A, B, C> Foo<A, B, C> {
        // no dummy attribute on nightly
        // forgot 'mut' self here
        fn baz(self: Pin<&mut Self>) {
            #[project]
            match self.project() {
                Foo::Tuple(x, y) => {
                    let _: Pin<&mut A> = x;
                    let _: &mut B = y;
                }
                Foo::Struct { field } => {
                    let _: &mut C = field;
                }
                Foo::Unit => {}
            }
        }
    }

As I am on nightly (tested on latest 2019-09-02 und 2019-08-21), I didn't put the dummy #[project] on the function itself. Then my code didn't have the mut self (I think this wasn't required in a previous version?). With the code in question rustc literally just outputs
error[E0596]: cannot borrow self as mutable, as it is not declared as mutable
without any further information in what file or line the error actually occurs.

Putting the #[project] attribute on the function produces a proper error message:

error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable               
--> libs.rs:148:19
    |                                                                                               
146 |         fn baz(self: Pin<&mut Self>) {                                                     
    |                ---- help: consider changing this to be mutable: `mut self`                       
147 |             #[project]                                                                         
148 |             match self.project() {       
    |                   ^^^^ cannot borrow as mutable    

I don't know enough about proc macros to tell whether anything can be done about this or if its just a rustc issue, so I'm posting it here for now.

@taiki-e
Copy link
Owner

taiki-e commented Sep 3, 2019

@pluth Thanks for the reporting!

without any further information in what file or line the error actually occurs.

Unfortunately, this is a rustc issue (rust-lang/rust#43081) and cannot be fixed on the proc-macro side. (I think this is one of the reasons why stmt_expr_attributes or proc_macro_hygiene is an unstable feature.)

I think this wasn't required in a previous version?

Yeah, this was changed in 0.4.0-alpha.3 (see #47 for more).

@kbleeke
Copy link
Author

kbleeke commented Sep 3, 2019

Thanks for the Reply. Yeah, I figured this was a possibility. So this issue can be closed, I guess.

@kbleeke kbleeke closed this as completed Sep 3, 2019
@taiki-e taiki-e added the C-bug Category: related to a bug. label Sep 23, 2019
@taiki-e taiki-e added A-diagnostics Area: Messages for errors and warnings C-upstream-bug Category: This is a bug of compiler or dependencies (the fix may require action in the upstream) and removed C-bug Category: related to a bug. labels Sep 23, 2019
@taiki-e taiki-e added C-question Category: A question and removed C-question Category: A question labels Oct 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors and warnings C-upstream-bug Category: This is a bug of compiler or dependencies (the fix may require action in the upstream)
Projects
None yet
Development

No branches or pull requests

2 participants