1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{
    parse::{Parse, ParseStream},
    spanned::Spanned,
    Attribute, Error, Ident, Result, Token,
};

use super::PIN;
use crate::utils::{ParseBufferExt, SliceExt};

pub(super) fn parse_args(attrs: &[Attribute]) -> Result<Args> {
    // `(__private(<args>))` -> `<args>`
    struct Input(Option<TokenStream>);

    impl Parse for Input {
        fn parse(input: ParseStream<'_>) -> Result<Self> {
            Ok(Self((|| {
                let content = input.parenthesized().ok()?;
                let private = content.parse::<Ident>().ok()?;
                if private == "__private" {
                    content.parenthesized().ok()?.parse::<TokenStream>().ok()
                } else {
                    None
                }
            })()))
        }
    }

    if let Some(attr) = attrs.find("pin_project") {
        bail!(attr, "duplicate #[pin_project] attribute");
    }

    let mut attrs = attrs.iter().filter(|attr| attr.path.is_ident(PIN));

    let prev = if let Some(attr) = attrs.next() {
        (attr, syn::parse2::<Input>(attr.tokens.clone()).unwrap().0)
    } else {
        // This only fails if another macro removes `#[pin]`.
        bail!(TokenStream::new(), "#[pin_project] attribute has been removed");
    };

    if let Some(attr) = attrs.next() {
        let (prev_attr, prev_res) = &prev;
        // As the `#[pin]` attribute generated by `#[pin_project]`
        // has the same span as `#[pin_project]`, it is possible
        // that a useless error message will be generated.
        // So, use the span of `prev_attr` if it is not a valid attribute.
        let res = syn::parse2::<Input>(attr.tokens.clone()).unwrap().0;
        let span = match (prev_res, res) {
            (Some(_), _) => attr,
            (None, _) => prev_attr,
        };
        bail!(span, "duplicate #[pin] attribute");
    }
    // This `unwrap` only fails if another macro removes `#[pin]` and inserts own `#[pin]`.
    syn::parse2(prev.1.unwrap())
}

pub(super) struct Args {
    /// `PinnedDrop` argument.
    pub(super) pinned_drop: Option<Span>,
    /// `UnsafeUnpin` or `!Unpin` argument.
    pub(super) unpin_impl: UnpinImpl,
    /// `project = <ident>` argument.
    pub(super) project: Option<Ident>,
    /// `project_ref = <ident>` argument.
    pub(super) project_ref: Option<Ident>,
    /// `project_replace [= <ident>]` argument.
    pub(super) project_replace: ProjReplace,
}

impl Parse for Args {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        mod kw {
            syn::custom_keyword!(Unpin);
        }

        /// Parses `= <value>` in `<name> = <value>` and returns value and span of name-value pair.
        fn parse_value(
            input: ParseStream<'_>,
            name: &Ident,
            has_prev: bool,
        ) -> Result<(Ident, TokenStream)> {
            if input.is_empty() {
                bail!(name, "expected `{0} = <identifier>`, found `{0}`", name);
            }
            let eq_token: Token![=] = input.parse()?;
            if input.is_empty() {
                let span = quote!(#name #eq_token);
                bail!(span, "expected `{0} = <identifier>`, found `{0} =`", name);
            }
            let value: Ident = input.parse()?;
            let span = quote!(#name #value);
            if has_prev {
                bail!(span, "duplicate `{}` argument", name);
            }
            Ok((value, span))
        }

        let mut pinned_drop = None;
        let mut unsafe_unpin = None;
        let mut not_unpin = None;
        let mut project = None;
        let mut project_ref = None;
        let mut project_replace_value = None;
        let mut project_replace_span = None;

        while !input.is_empty() {
            if input.peek(Token![!]) {
                let bang: Token![!] = input.parse()?;
                if input.is_empty() {
                    bail!(bang, "expected `!Unpin`, found `!`");
                }
                let unpin: kw::Unpin = input.parse()?;
                let span = quote!(#bang #unpin);
                if not_unpin.replace(span.span()).is_some() {
                    bail!(span, "duplicate `!Unpin` argument");
                }
            } else {
                let token = input.parse::<Ident>()?;
                match &*token.to_string() {
                    "PinnedDrop" => {
                        if pinned_drop.replace(token.span()).is_some() {
                            bail!(token, "duplicate `PinnedDrop` argument");
                        }
                    }
                    "UnsafeUnpin" => {
                        if unsafe_unpin.replace(token.span()).is_some() {
                            bail!(token, "duplicate `UnsafeUnpin` argument");
                        }
                    }
                    "project" => {
                        project = Some(parse_value(input, &token, project.is_some())?.0);
                    }
                    "project_ref" => {
                        project_ref = Some(parse_value(input, &token, project_ref.is_some())?.0);
                    }
                    "project_replace" => {
                        if input.peek(Token![=]) {
                            let (value, span) =
                                parse_value(input, &token, project_replace_span.is_some())?;
                            project_replace_value = Some(value);
                            project_replace_span = Some(span.span());
                        } else if project_replace_span.is_some() {
                            bail!(token, "duplicate `project_replace` argument");
                        } else {
                            project_replace_span = Some(token.span());
                        }
                    }
                    "Replace" => {
                        bail!(
                            token,
                            "`Replace` argument was removed, use `project_replace` argument instead"
                        );
                    }
                    _ => bail!(token, "unexpected argument: {}", token),
                }
            }

            if input.is_empty() {
                break;
            }
            let _: Token![,] = input.parse()?;
        }

        if project.is_some() || project_ref.is_some() {
            if project == project_ref {
                bail!(
                    project_ref,
                    "name `{}` is already specified by `project` argument",
                    project_ref.as_ref().unwrap()
                );
            }
            if let Some(ident) = &project_replace_value {
                if project == project_replace_value {
                    bail!(ident, "name `{}` is already specified by `project` argument", ident);
                } else if project_ref == project_replace_value {
                    bail!(ident, "name `{}` is already specified by `project_ref` argument", ident);
                }
            }
        }

        if let Some(span) = pinned_drop {
            if project_replace_span.is_some() {
                return Err(Error::new(
                    span,
                    "arguments `PinnedDrop` and `project_replace` are mutually exclusive",
                ));
            }
        }
        let project_replace = match (project_replace_span, project_replace_value) {
            (None, _) => ProjReplace::None,
            (Some(span), Some(ident)) => ProjReplace::Named { ident, span },
            (Some(span), None) => ProjReplace::Unnamed { span },
        };
        let unpin_impl = match (unsafe_unpin, not_unpin) {
            (None, None) => UnpinImpl::Default,
            (Some(span), None) => UnpinImpl::Unsafe(span),
            (None, Some(span)) => UnpinImpl::Negative(span),
            (Some(span), Some(_)) => {
                return Err(Error::new(
                    span,
                    "arguments `UnsafeUnpin` and `!Unpin` are mutually exclusive",
                ));
            }
        };

        Ok(Self { pinned_drop, unpin_impl, project, project_ref, project_replace })
    }
}

/// `UnsafeUnpin` or `!Unpin` argument.
#[derive(Clone, Copy)]
pub(super) enum UnpinImpl {
    Default,
    /// `UnsafeUnpin`.
    Unsafe(Span),
    /// `!Unpin`.
    Negative(Span),
}

/// `project_replace [= <ident>]` argument.
pub(super) enum ProjReplace {
    None,
    /// `project_replace`.
    Unnamed {
        span: Span,
    },
    /// `project_replace = <ident>`.
    #[allow(dead_code)] // false positive that fixed in Rust 1.38
    Named {
        span: Span,
        ident: Ident,
    },
}

impl ProjReplace {
    /// Return the span of this argument.
    pub(super) fn span(&self) -> Option<Span> {
        match self {
            Self::None => None,
            Self::Named { span, .. } | Self::Unnamed { span, .. } => Some(*span),
        }
    }

    pub(super) fn ident(&self) -> Option<&Ident> {
        if let Self::Named { ident, .. } = self {
            Some(ident)
        } else {
            None
        }
    }
}