syn/
stmt.rs

1use crate::attr::Attribute;
2use crate::expr::Expr;
3use crate::item::Item;
4use crate::mac::Macro;
5use crate::pat::Pat;
6use crate::token;
7
8ast_struct! {
9    /// A braced block containing Rust statements.
10    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
11    pub struct Block {
12        pub brace_token: token::Brace,
13        /// Statements in a block
14        pub stmts: Vec<Stmt>,
15    }
16}
17
18ast_enum! {
19    /// A statement, usually ending in a semicolon.
20    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
21    pub enum Stmt {
22        /// A local (let) binding.
23        Local(Local),
24
25        /// An item definition.
26        Item(Item),
27
28        /// Expression, with or without trailing semicolon.
29        Expr(Expr, Option<Token![;]>),
30
31        /// A macro invocation in statement position.
32        ///
33        /// Syntactically it's ambiguous which other kind of statement this
34        /// macro would expand to. It can be any of local variable (`let`),
35        /// item, or expression.
36        Macro(StmtMacro),
37    }
38}
39
40ast_struct! {
41    /// A local `let` binding: `let x: u64 = s.parse()?`.
42    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
43    pub struct Local {
44        pub attrs: Vec<Attribute>,
45        pub let_token: Token![let],
46        pub pat: Pat,
47        pub init: Option<LocalInit>,
48        pub semi_token: Token![;],
49    }
50}
51
52ast_struct! {
53    /// The expression assigned in a local `let` binding, including optional
54    /// diverging `else` block.
55    ///
56    /// `LocalInit` represents `= s.parse()?` in `let x: u64 = s.parse()?` and
57    /// `= r else { return }` in `let Ok(x) = r else { return }`.
58    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
59    pub struct LocalInit {
60        pub eq_token: Token![=],
61        pub expr: Box<Expr>,
62        pub diverge: Option<(Token![else], Box<Expr>)>,
63    }
64}
65
66ast_struct! {
67    /// A macro invocation in statement position.
68    ///
69    /// Syntactically it's ambiguous which other kind of statement this macro
70    /// would expand to. It can be any of local variable (`let`), item, or
71    /// expression.
72    #[cfg_attr(docsrs, doc(cfg(feature = "full")))]
73    pub struct StmtMacro {
74        pub attrs: Vec<Attribute>,
75        pub mac: Macro,
76        pub semi_token: Option<Token![;]>,
77    }
78}
79
80#[cfg(feature = "parsing")]
81pub(crate) mod parsing {
82    use crate::attr::Attribute;
83    use crate::classify;
84    use crate::error::Result;
85    use crate::expr::{Expr, ExprBlock, ExprMacro};
86    use crate::ident::Ident;
87    use crate::item;
88    use crate::mac::{self, Macro};
89    use crate::parse::discouraged::Speculative as _;
90    use crate::parse::{Parse, ParseStream};
91    use crate::pat::{Pat, PatType};
92    use crate::path::Path;
93    use crate::stmt::{Block, Local, LocalInit, Stmt, StmtMacro};
94    use crate::token;
95    use crate::ty::Type;
96    use proc_macro2::TokenStream;
97
98    struct AllowNoSemi(bool);
99
100    impl Block {
101        /// Parse the body of a block as zero or more statements, possibly
102        /// including one trailing expression.
103        ///
104        /// # Example
105        ///
106        /// ```
107        /// use syn::{braced, token, Attribute, Block, Ident, Result, Stmt, Token};
108        /// use syn::parse::{Parse, ParseStream};
109        ///
110        /// // Parse a function with no generics or parameter list.
111        /// //
112        /// //     fn playground {
113        /// //         let mut x = 1;
114        /// //         x += 1;
115        /// //         println!("{}", x);
116        /// //     }
117        /// struct MiniFunction {
118        ///     attrs: Vec<Attribute>,
119        ///     fn_token: Token![fn],
120        ///     name: Ident,
121        ///     brace_token: token::Brace,
122        ///     stmts: Vec<Stmt>,
123        /// }
124        ///
125        /// impl Parse for MiniFunction {
126        ///     fn parse(input: ParseStream) -> Result<Self> {
127        ///         let outer_attrs = input.call(Attribute::parse_outer)?;
128        ///         let fn_token: Token![fn] = input.parse()?;
129        ///         let name: Ident = input.parse()?;
130        ///
131        ///         let content;
132        ///         let brace_token = braced!(content in input);
133        ///         let inner_attrs = content.call(Attribute::parse_inner)?;
134        ///         let stmts = content.call(Block::parse_within)?;
135        ///
136        ///         Ok(MiniFunction {
137        ///             attrs: {
138        ///                 let mut attrs = outer_attrs;
139        ///                 attrs.extend(inner_attrs);
140        ///                 attrs
141        ///             },
142        ///             fn_token,
143        ///             name,
144        ///             brace_token,
145        ///             stmts,
146        ///         })
147        ///     }
148        /// }
149        /// ```
150        #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
151        pub fn parse_within(input: ParseStream) -> Result<Vec<Stmt>> {
152            let mut stmts = Vec::new();
153            loop {
154                while let semi @ Some(_) = input.parse()? {
155                    stmts.push(Stmt::Expr(Expr::Verbatim(TokenStream::new()), semi));
156                }
157                if input.is_empty() {
158                    break;
159                }
160                let stmt = parse_stmt(input, AllowNoSemi(true))?;
161                let requires_semicolon = match &stmt {
162                    Stmt::Expr(stmt, None) => classify::requires_semi_to_be_stmt(stmt),
163                    Stmt::Macro(stmt) => {
164                        stmt.semi_token.is_none() && !stmt.mac.delimiter.is_brace()
165                    }
166                    Stmt::Local(_) | Stmt::Item(_) | Stmt::Expr(_, Some(_)) => false,
167                };
168                stmts.push(stmt);
169                if input.is_empty() {
170                    break;
171                } else if requires_semicolon {
172                    return Err(input.error("unexpected token, expected `;`"));
173                }
174            }
175            Ok(stmts)
176        }
177    }
178
179    #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
180    impl Parse for Block {
181        fn parse(input: ParseStream) -> Result<Self> {
182            let content;
183            Ok(Block {
184                brace_token: braced!(content in input),
185                stmts: content.call(Block::parse_within)?,
186            })
187        }
188    }
189
190    #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
191    impl Parse for Stmt {
192        fn parse(input: ParseStream) -> Result<Self> {
193            let allow_nosemi = AllowNoSemi(false);
194            parse_stmt(input, allow_nosemi)
195        }
196    }
197
198    fn parse_stmt(input: ParseStream, allow_nosemi: AllowNoSemi) -> Result<Stmt> {
199        let begin = input.fork();
200        let attrs = input.call(Attribute::parse_outer)?;
201
202        // brace-style macros; paren and bracket macros get parsed as
203        // expression statements.
204        let ahead = input.fork();
205        let mut is_item_macro = false;
206        if let Ok(path) = ahead.call(Path::parse_mod_style) {
207            if ahead.peek(Token![!]) {
208                if ahead.peek2(Ident) || ahead.peek2(Token![try]) {
209                    is_item_macro = true;
210                } else if ahead.peek2(token::Brace)
211                    && !(ahead.peek3(Token![.]) || ahead.peek3(Token![?]))
212                {
213                    input.advance_to(&ahead);
214                    return stmt_mac(input, attrs, path).map(Stmt::Macro);
215                }
216            }
217        }
218
219        if input.peek(Token![let]) && !input.peek(token::Group) {
220            stmt_local(input, attrs).map(Stmt::Local)
221        } else if input.peek(Token![pub])
222            || input.peek(Token![crate]) && !input.peek2(Token![::])
223            || input.peek(Token![extern])
224            || input.peek(Token![use])
225            || input.peek(Token![static])
226                && (input.peek2(Token![mut])
227                    || input.peek2(Ident)
228                        && !(input.peek2(Token![async])
229                            && (input.peek3(Token![move]) || input.peek3(Token![|]))))
230            || input.peek(Token![const])
231                && !(input.peek2(token::Brace)
232                    || input.peek2(Token![static])
233                    || input.peek2(Token![async])
234                        && !(input.peek3(Token![unsafe])
235                            || input.peek3(Token![extern])
236                            || input.peek3(Token![fn]))
237                    || input.peek2(Token![move])
238                    || input.peek2(Token![|]))
239            || input.peek(Token![unsafe]) && !input.peek2(token::Brace)
240            || input.peek(Token![async])
241                && (input.peek2(Token![unsafe])
242                    || input.peek2(Token![extern])
243                    || input.peek2(Token![fn]))
244            || input.peek(Token![fn])
245            || input.peek(Token![mod])
246            || input.peek(Token![type])
247            || input.peek(Token![struct])
248            || input.peek(Token![enum])
249            || input.peek(Token![union]) && input.peek2(Ident)
250            || input.peek(Token![auto]) && input.peek2(Token![trait])
251            || input.peek(Token![trait])
252            || input.peek(Token![default])
253                && (input.peek2(Token![unsafe]) || input.peek2(Token![impl]))
254            || input.peek(Token![impl])
255            || input.peek(Token![macro])
256            || is_item_macro
257        {
258            let item = item::parsing::parse_rest_of_item(begin, attrs, input)?;
259            Ok(Stmt::Item(item))
260        } else {
261            stmt_expr(input, allow_nosemi, attrs)
262        }
263    }
264
265    fn stmt_mac(input: ParseStream, attrs: Vec<Attribute>, path: Path) -> Result<StmtMacro> {
266        let bang_token: Token![!] = input.parse()?;
267        let (delimiter, tokens) = mac::parse_delimiter(input)?;
268        let semi_token: Option<Token![;]> = input.parse()?;
269
270        Ok(StmtMacro {
271            attrs,
272            mac: Macro {
273                path,
274                bang_token,
275                delimiter,
276                tokens,
277            },
278            semi_token,
279        })
280    }
281
282    fn stmt_local(input: ParseStream, attrs: Vec<Attribute>) -> Result<Local> {
283        let let_token: Token![let] = input.parse()?;
284
285        let mut pat = Pat::parse_single(input)?;
286        if input.peek(Token![:]) {
287            let colon_token: Token![:] = input.parse()?;
288            let ty: Type = input.parse()?;
289            pat = Pat::Type(PatType {
290                attrs: Vec::new(),
291                pat: Box::new(pat),
292                colon_token,
293                ty: Box::new(ty),
294            });
295        }
296
297        let init = if let Some(eq_token) = input.parse()? {
298            let eq_token: Token![=] = eq_token;
299            let expr: Expr = input.parse()?;
300
301            let diverge = if !classify::expr_trailing_brace(&expr) && input.peek(Token![else]) {
302                let else_token: Token![else] = input.parse()?;
303                let diverge = ExprBlock {
304                    attrs: Vec::new(),
305                    label: None,
306                    block: input.parse()?,
307                };
308                Some((else_token, Box::new(Expr::Block(diverge))))
309            } else {
310                None
311            };
312
313            Some(LocalInit {
314                eq_token,
315                expr: Box::new(expr),
316                diverge,
317            })
318        } else {
319            None
320        };
321
322        let semi_token: Token![;] = input.parse()?;
323
324        Ok(Local {
325            attrs,
326            let_token,
327            pat,
328            init,
329            semi_token,
330        })
331    }
332
333    fn stmt_expr(
334        input: ParseStream,
335        allow_nosemi: AllowNoSemi,
336        mut attrs: Vec<Attribute>,
337    ) -> Result<Stmt> {
338        let mut e = Expr::parse_with_earlier_boundary_rule(input)?;
339
340        let mut attr_target = &mut e;
341        loop {
342            attr_target = match attr_target {
343                Expr::Assign(e) => &mut e.left,
344                Expr::Binary(e) => &mut e.left,
345                Expr::Cast(e) => &mut e.expr,
346                Expr::Array(_)
347                | Expr::Async(_)
348                | Expr::Await(_)
349                | Expr::Block(_)
350                | Expr::Break(_)
351                | Expr::Call(_)
352                | Expr::Closure(_)
353                | Expr::Const(_)
354                | Expr::Continue(_)
355                | Expr::Field(_)
356                | Expr::ForLoop(_)
357                | Expr::Group(_)
358                | Expr::If(_)
359                | Expr::Index(_)
360                | Expr::Infer(_)
361                | Expr::Let(_)
362                | Expr::Lit(_)
363                | Expr::Loop(_)
364                | Expr::Macro(_)
365                | Expr::Match(_)
366                | Expr::MethodCall(_)
367                | Expr::Paren(_)
368                | Expr::Path(_)
369                | Expr::Range(_)
370                | Expr::RawAddr(_)
371                | Expr::Reference(_)
372                | Expr::Repeat(_)
373                | Expr::Return(_)
374                | Expr::Struct(_)
375                | Expr::Try(_)
376                | Expr::TryBlock(_)
377                | Expr::Tuple(_)
378                | Expr::Unary(_)
379                | Expr::Unsafe(_)
380                | Expr::While(_)
381                | Expr::Yield(_)
382                | Expr::Verbatim(_) => break,
383            };
384        }
385        attrs.extend(attr_target.replace_attrs(Vec::new()));
386        attr_target.replace_attrs(attrs);
387
388        let semi_token: Option<Token![;]> = input.parse()?;
389
390        match e {
391            Expr::Macro(ExprMacro { attrs, mac })
392                if semi_token.is_some() || mac.delimiter.is_brace() =>
393            {
394                return Ok(Stmt::Macro(StmtMacro {
395                    attrs,
396                    mac,
397                    semi_token,
398                }));
399            }
400            _ => {}
401        }
402
403        if semi_token.is_some() {
404            Ok(Stmt::Expr(e, semi_token))
405        } else if allow_nosemi.0 || !classify::requires_semi_to_be_stmt(&e) {
406            Ok(Stmt::Expr(e, None))
407        } else {
408            Err(input.error("expected semicolon"))
409        }
410    }
411}
412
413#[cfg(feature = "printing")]
414pub(crate) mod printing {
415    use crate::classify;
416    use crate::expr::{self, Expr};
417    use crate::fixup::FixupContext;
418    use crate::stmt::{Block, Local, Stmt, StmtMacro};
419    use crate::token;
420    use proc_macro2::TokenStream;
421    use quote::{ToTokens, TokenStreamExt};
422
423    #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
424    impl ToTokens for Block {
425        fn to_tokens(&self, tokens: &mut TokenStream) {
426            self.brace_token.surround(tokens, |tokens| {
427                tokens.append_all(&self.stmts);
428            });
429        }
430    }
431
432    #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
433    impl ToTokens for Stmt {
434        fn to_tokens(&self, tokens: &mut TokenStream) {
435            match self {
436                Stmt::Local(local) => local.to_tokens(tokens),
437                Stmt::Item(item) => item.to_tokens(tokens),
438                Stmt::Expr(expr, semi) => {
439                    expr::printing::print_expr(expr, tokens, FixupContext::new_stmt());
440                    semi.to_tokens(tokens);
441                }
442                Stmt::Macro(mac) => mac.to_tokens(tokens),
443            }
444        }
445    }
446
447    #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
448    impl ToTokens for Local {
449        fn to_tokens(&self, tokens: &mut TokenStream) {
450            expr::printing::outer_attrs_to_tokens(&self.attrs, tokens);
451            self.let_token.to_tokens(tokens);
452            self.pat.to_tokens(tokens);
453            if let Some(init) = &self.init {
454                init.eq_token.to_tokens(tokens);
455                if init.diverge.is_some() && classify::expr_trailing_brace(&init.expr) {
456                    token::Paren::default().surround(tokens, |tokens| init.expr.to_tokens(tokens));
457                } else {
458                    init.expr.to_tokens(tokens);
459                }
460                if let Some((else_token, diverge)) = &init.diverge {
461                    else_token.to_tokens(tokens);
462                    match &**diverge {
463                        Expr::Block(diverge) => diverge.to_tokens(tokens),
464                        _ => token::Brace::default().surround(tokens, |tokens| {
465                            expr::printing::print_expr(diverge, tokens, FixupContext::new_stmt());
466                        }),
467                    }
468                }
469            }
470            self.semi_token.to_tokens(tokens);
471        }
472    }
473
474    #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
475    impl ToTokens for StmtMacro {
476        fn to_tokens(&self, tokens: &mut TokenStream) {
477            expr::printing::outer_attrs_to_tokens(&self.attrs, tokens);
478            self.mac.to_tokens(tokens);
479            self.semi_token.to_tokens(tokens);
480        }
481    }
482}