taiki-e / pin-project

A crate for safe and ergonomic pin-projection.

Home Page:https://docs.rs/pin-project

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

kbleeke opened this issue · comments

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.

@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).

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