Skip to main content

rustc_ast/util/
classify.rs

1//! Routines the parser and pretty-printer use to classify AST nodes.
2
3use crate::ast::ExprKind::*;
4use crate::ast::{self, MatchKind};
5use crate::token::Delimiter;
6
7/// This classification determines whether various syntactic positions break out
8/// of parsing the current expression (true) or continue parsing more of the
9/// same expression (false).
10///
11/// For example, it's relevant in the parsing of match arms:
12///
13/// ```ignore (illustrative)
14/// match ... {
15///     // Is this calling $e as a function, or is it the start of a new arm
16///     // with a tuple pattern?
17///     _ => $e (
18///             ^                                                          )
19///
20///     // Is this an Index operation, or new arm with a slice pattern?
21///     _ => $e [
22///             ^                                                          ]
23///
24///     // Is this a binary operator, or leading vert in a new arm? Same for
25///     // other punctuation which can either be a binary operator in
26///     // expression or unary operator in pattern, such as `&` and `-`.
27///     _ => $e |
28///             ^
29/// }
30/// ```
31///
32/// If $e is something like `{}` or `if … {}`, then terminate the current
33/// arm and parse a new arm.
34///
35/// If $e is something like `path::to` or `(…)`, continue parsing the same
36/// arm.
37///
38/// *Almost* the same classification is used as an early bail-out for parsing
39/// statements. See `expr_requires_semi_to_be_stmt`.
40pub fn expr_is_complete(e: &ast::Expr) -> bool {
41    #[allow(non_exhaustive_omitted_patterns)] match e.kind {
    If(..) | Match(..) | Block(..) | While(..) | Loop(..) | ForLoop { .. } |
        TryBlock(..) | ConstBlock(..) => true,
    _ => false,
}matches!(
42        e.kind,
43        If(..)
44            | Match(..)
45            | Block(..)
46            | While(..)
47            | Loop(..)
48            | ForLoop { .. }
49            | TryBlock(..)
50            | ConstBlock(..)
51    )
52}
53
54/// Does this expression require a semicolon to be treated as a statement?
55///
56/// The negation of this: "can this expression be used as a statement without a
57/// semicolon" -- is used as an early bail-out when parsing statements so that,
58/// for instance,
59///
60/// ```ignore (illustrative)
61/// if true {...} else {...}
62/// |x| 5
63/// ```
64///
65/// isn't parsed as `(if true {...} else {...} | x) | 5`.
66///
67/// Surprising special case: even though braced macro calls like `m! {}`
68/// normally do not introduce a boundary when found at the head of a match arm,
69/// they do terminate the parsing of a statement.
70///
71/// ```ignore (illustrative)
72/// match ... {
73///     _ => m! {} (),  // macro that expands to a function, which is then called
74/// }
75///
76/// let _ = { m! {} () };  // macro call followed by unit
77/// ```
78pub fn expr_requires_semi_to_be_stmt(e: &ast::Expr) -> bool {
79    match &e.kind {
80        MacCall(mac_call) => mac_call.args.delim != Delimiter::Brace,
81        _ => !expr_is_complete(e),
82    }
83}
84
85/// Returns whether the leftmost token of the given expression is the label of a
86/// labeled loop or block, such as in `'inner: loop { break 'inner 1 } + 1`.
87///
88/// Such expressions are not allowed as the value of an unlabeled break.
89///
90/// ```ignore (illustrative)
91/// 'outer: {
92///     break 'inner: loop { break 'inner 1 } + 1;  // invalid syntax
93///
94///     break 'outer 'inner: loop { break 'inner 1 } + 1;  // okay
95///
96///     break ('inner: loop { break 'inner 1 } + 1);  // okay
97///
98///     break ('inner: loop { break 'inner 1 }) + 1;  // okay
99/// }
100/// ```
101pub fn leading_labeled_expr(mut expr: &ast::Expr) -> bool {
102    loop {
103        match &expr.kind {
104            Block(_, label) | ForLoop { label, .. } | Loop(_, label, _) | While(_, _, label) => {
105                return label.is_some();
106            }
107
108            Assign(e, _, _)
109            | AssignOp(_, e, _)
110            | Await(e, _)
111            | Move(e, _)
112            | Use(e, _)
113            | Binary(_, e, _)
114            | Call(e, _)
115            | Cast(e, _)
116            | Field(e, _)
117            | Index(e, _, _)
118            | Match(e, _, MatchKind::Postfix)
119            | Range(Some(e), _, _)
120            | Try(e) => {
121                expr = e;
122            }
123            MethodCall(method_call) => {
124                expr = &method_call.receiver;
125            }
126
127            AddrOf(..)
128            | Array(..)
129            | Become(..)
130            | Break(..)
131            | Closure(..)
132            | ConstBlock(..)
133            | Continue(..)
134            | FormatArgs(..)
135            | Gen(..)
136            | If(..)
137            | IncludedBytes(..)
138            | InlineAsm(..)
139            | Let(..)
140            | Lit(..)
141            | MacCall(..)
142            | Match(_, _, MatchKind::Prefix)
143            | OffsetOf(..)
144            | Paren(..)
145            | Path(..)
146            | Range(None, _, _)
147            | Repeat(..)
148            | Ret(..)
149            | Struct(..)
150            | TryBlock(..)
151            | Tup(..)
152            | Type(..)
153            | Unary(..)
154            | Underscore
155            | Yeet(..)
156            | Yield(..)
157            | UnsafeBinderCast(..)
158            | Err(..)
159            | Dummy => return false,
160        }
161    }
162}
163
164pub enum TrailingBrace<'a> {
165    /// Trailing brace in a macro call, like the one in `x as *const brace! {}`.
166    /// We will suggest changing the macro call to a different delimiter.
167    MacCall(&'a ast::MacCall),
168    /// Trailing brace in any other expression, such as `a + B {}`. We will
169    /// suggest wrapping the innermost expression in parentheses: `a + (B {})`.
170    Expr(&'a ast::Expr),
171}
172
173/// If an expression ends with `}`, returns the innermost expression ending in the `}`
174pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<TrailingBrace<'_>> {
175    loop {
176        match &expr.kind {
177            AddrOf(_, _, e)
178            | Assign(_, e, _)
179            | AssignOp(_, _, e)
180            | Binary(_, _, e)
181            | Break(_, Some(e))
182            | Let(_, e, _, _)
183            | Range(_, Some(e), _)
184            | Ret(Some(e))
185            | Unary(_, e)
186            | Yeet(Some(e))
187            | Move(e, _)
188            | Become(e) => {
189                expr = e;
190            }
191            Yield(kind) => match kind.expr() {
192                Some(e) => expr = e,
193                None => break None,
194            },
195            Closure(closure) => {
196                expr = &closure.body;
197            }
198            Gen(..)
199            | Block(..)
200            | ForLoop { .. }
201            | If(..)
202            | Loop(..)
203            | Match(..)
204            | Struct(..)
205            | TryBlock(..)
206            | While(..)
207            | ConstBlock(_) => break Some(TrailingBrace::Expr(expr)),
208
209            Cast(_, ty) => {
210                break type_trailing_braced_mac_call(ty).map(TrailingBrace::MacCall);
211            }
212
213            MacCall(mac) => {
214                break (mac.args.delim == Delimiter::Brace).then_some(TrailingBrace::MacCall(mac));
215            }
216
217            InlineAsm(_) | OffsetOf(_, _) | IncludedBytes(_) | FormatArgs(_) => {
218                // These should have been denied pre-expansion.
219                break None;
220            }
221
222            Break(_, None)
223            | Range(_, None, _)
224            | Ret(None)
225            | Array(_)
226            | Call(_, _)
227            | MethodCall(_)
228            | Tup(_)
229            | Lit(_)
230            | Type(_, _)
231            | Await(_, _)
232            | Use(_, _)
233            | Field(_, _)
234            | Index(_, _, _)
235            | Underscore
236            | Path(_, _)
237            | Continue(_)
238            | Repeat(_, _)
239            | Paren(_)
240            | Try(_)
241            | Yeet(None)
242            | UnsafeBinderCast(..)
243            | Err(_)
244            | Dummy => {
245                break None;
246            }
247        }
248    }
249}
250
251/// If the type's last token is `}`, it must be due to a braced macro call, such
252/// as in `*const brace! { ... }`. Returns that trailing macro call.
253fn type_trailing_braced_mac_call(mut ty: &ast::Ty) -> Option<&ast::MacCall> {
254    loop {
255        match &ty.kind {
256            ast::TyKind::MacCall(mac) => {
257                break (mac.args.delim == Delimiter::Brace).then_some(mac);
258            }
259
260            ast::TyKind::Ptr(mut_ty)
261            | ast::TyKind::Ref(_, mut_ty)
262            | ast::TyKind::PinnedRef(_, mut_ty) => {
263                ty = &mut_ty.ty;
264            }
265
266            ast::TyKind::UnsafeBinder(binder) => {
267                ty = &binder.inner_ty;
268            }
269
270            ast::TyKind::FnPtr(fn_ty) => match &fn_ty.decl.output {
271                ast::FnRetTy::Default(_) => break None,
272                ast::FnRetTy::Ty(ret) => ty = ret,
273            },
274
275            ast::TyKind::Path(_, path) => match path_return_type(path) {
276                Some(trailing_ty) => ty = trailing_ty,
277                None => break None,
278            },
279
280            ast::TyKind::TraitObject(bounds, _) | ast::TyKind::ImplTrait(_, bounds) => {
281                match bounds.last() {
282                    Some(ast::GenericBound::Trait(bound)) => {
283                        match path_return_type(&bound.trait_ref.path) {
284                            Some(trailing_ty) => ty = trailing_ty,
285                            None => break None,
286                        }
287                    }
288                    Some(ast::GenericBound::Outlives(_) | ast::GenericBound::Use(..)) | None => {
289                        break None;
290                    }
291                }
292            }
293
294            ast::TyKind::Slice(..)
295            | ast::TyKind::Array(..)
296            | ast::TyKind::Never
297            | ast::TyKind::Tup(..)
298            | ast::TyKind::Paren(..)
299            | ast::TyKind::Infer
300            | ast::TyKind::ImplicitSelf
301            | ast::TyKind::CVarArgs
302            | ast::TyKind::Pat(..)
303            | ast::TyKind::FieldOf(..)
304            | ast::TyKind::Dummy
305            | ast::TyKind::Err(..) => break None,
306        }
307    }
308}
309
310/// Returns the trailing return type in the given path, if it has one.
311///
312/// ```ignore (illustrative)
313/// ::std::ops::FnOnce(&str) -> fn() -> *const c_void
314///                             ^^^^^^^^^^^^^^^^^^^^^
315/// ```
316fn path_return_type(path: &ast::Path) -> Option<&ast::Ty> {
317    let last_segment = path.segments.last()?;
318    let args = last_segment.args.as_ref()?;
319    match &**args {
320        ast::GenericArgs::Parenthesized(args) => match &args.output {
321            ast::FnRetTy::Default(_) => None,
322            ast::FnRetTy::Ty(ret) => Some(ret),
323        },
324        ast::GenericArgs::AngleBracketed(_) | ast::GenericArgs::ParenthesizedElided(_) => None,
325    }
326}