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 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 | Use(e, _)
112 | Binary(_, e, _)
113 | Call(e, _)
114 | Cast(e, _)
115 | Field(e, _)
116 | Index(e, _, _)
117 | Match(e, _, MatchKind::Postfix)
118 | Range(Some(e), _, _)
119 | Try(e) => {
120 expr = e;
121 }
122 MethodCall(method_call) => {
123 expr = &method_call.receiver;
124 }
125
126 AddrOf(..)
127 | Array(..)
128 | Become(..)
129 | Break(..)
130 | Closure(..)
131 | ConstBlock(..)
132 | Continue(..)
133 | FormatArgs(..)
134 | Gen(..)
135 | If(..)
136 | IncludedBytes(..)
137 | InlineAsm(..)
138 | Let(..)
139 | Lit(..)
140 | MacCall(..)
141 | Match(_, _, MatchKind::Prefix)
142 | OffsetOf(..)
143 | Paren(..)
144 | Path(..)
145 | Range(None, _, _)
146 | Repeat(..)
147 | Ret(..)
148 | Struct(..)
149 | TryBlock(..)
150 | Tup(..)
151 | Type(..)
152 | Unary(..)
153 | Underscore
154 | Yeet(..)
155 | Yield(..)
156 | UnsafeBinderCast(..)
157 | Err(..)
158 | Dummy => return false,
159 }
160 }
161}
162
163pub enum TrailingBrace<'a> {
164 /// Trailing brace in a macro call, like the one in `x as *const brace! {}`.
165 /// We will suggest changing the macro call to a different delimiter.
166 MacCall(&'a ast::MacCall),
167 /// Trailing brace in any other expression, such as `a + B {}`. We will
168 /// suggest wrapping the innermost expression in parentheses: `a + (B {})`.
169 Expr(&'a ast::Expr),
170}
171
172/// If an expression ends with `}`, returns the innermost expression ending in the `}`
173pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<TrailingBrace<'_>> {
174 loop {
175 match &expr.kind {
176 AddrOf(_, _, e)
177 | Assign(_, e, _)
178 | AssignOp(_, _, e)
179 | Binary(_, _, e)
180 | Break(_, Some(e))
181 | Let(_, e, _, _)
182 | Range(_, Some(e), _)
183 | Ret(Some(e))
184 | Unary(_, e)
185 | Yeet(Some(e))
186 | Become(e) => {
187 expr = e;
188 }
189 Yield(kind) => match kind.expr() {
190 Some(e) => expr = e,
191 None => break None,
192 },
193 Closure(closure) => {
194 expr = &closure.body;
195 }
196 Gen(..)
197 | Block(..)
198 | ForLoop { .. }
199 | If(..)
200 | Loop(..)
201 | Match(..)
202 | Struct(..)
203 | TryBlock(..)
204 | While(..)
205 | ConstBlock(_) => break Some(TrailingBrace::Expr(expr)),
206
207 Cast(_, ty) => {
208 break type_trailing_braced_mac_call(ty).map(TrailingBrace::MacCall);
209 }
210
211 MacCall(mac) => {
212 break (mac.args.delim == Delimiter::Brace).then_some(TrailingBrace::MacCall(mac));
213 }
214
215 InlineAsm(_) | OffsetOf(_, _) | IncludedBytes(_) | FormatArgs(_) => {
216 // These should have been denied pre-expansion.
217 break None;
218 }
219
220 Break(_, None)
221 | Range(_, None, _)
222 | Ret(None)
223 | Array(_)
224 | Call(_, _)
225 | MethodCall(_)
226 | Tup(_)
227 | Lit(_)
228 | Type(_, _)
229 | Await(_, _)
230 | Use(_, _)
231 | Field(_, _)
232 | Index(_, _, _)
233 | Underscore
234 | Path(_, _)
235 | Continue(_)
236 | Repeat(_, _)
237 | Paren(_)
238 | Try(_)
239 | Yeet(None)
240 | UnsafeBinderCast(..)
241 | Err(_)
242 | Dummy => {
243 break None;
244 }
245 }
246 }
247}
248
249/// If the type's last token is `}`, it must be due to a braced macro call, such
250/// as in `*const brace! { ... }`. Returns that trailing macro call.
251fn type_trailing_braced_mac_call(mut ty: &ast::Ty) -> Option<&ast::MacCall> {
252 loop {
253 match &ty.kind {
254 ast::TyKind::MacCall(mac) => {
255 break (mac.args.delim == Delimiter::Brace).then_some(mac);
256 }
257
258 ast::TyKind::Ptr(mut_ty)
259 | ast::TyKind::Ref(_, mut_ty)
260 | ast::TyKind::PinnedRef(_, mut_ty) => {
261 ty = &mut_ty.ty;
262 }
263
264 ast::TyKind::UnsafeBinder(binder) => {
265 ty = &binder.inner_ty;
266 }
267
268 ast::TyKind::BareFn(fn_ty) => match &fn_ty.decl.output {
269 ast::FnRetTy::Default(_) => break None,
270 ast::FnRetTy::Ty(ret) => ty = ret,
271 },
272
273 ast::TyKind::Path(_, path) => match path_return_type(path) {
274 Some(trailing_ty) => ty = trailing_ty,
275 None => break None,
276 },
277
278 ast::TyKind::TraitObject(bounds, _) | ast::TyKind::ImplTrait(_, bounds) => {
279 match bounds.last() {
280 Some(ast::GenericBound::Trait(bound)) => {
281 match path_return_type(&bound.trait_ref.path) {
282 Some(trailing_ty) => ty = trailing_ty,
283 None => break None,
284 }
285 }
286 Some(ast::GenericBound::Outlives(_) | ast::GenericBound::Use(..)) | None => {
287 break None;
288 }
289 }
290 }
291
292 ast::TyKind::Slice(..)
293 | ast::TyKind::Array(..)
294 | ast::TyKind::Never
295 | ast::TyKind::Tup(..)
296 | ast::TyKind::Paren(..)
297 | ast::TyKind::Typeof(..)
298 | ast::TyKind::Infer
299 | ast::TyKind::ImplicitSelf
300 | ast::TyKind::CVarArgs
301 | ast::TyKind::Pat(..)
302 | ast::TyKind::Dummy
303 | ast::TyKind::Err(..) => break None,
304 }
305 }
306}
307
308/// Returns the trailing return type in the given path, if it has one.
309///
310/// ```ignore (illustrative)
311/// ::std::ops::FnOnce(&str) -> fn() -> *const c_void
312/// ^^^^^^^^^^^^^^^^^^^^^
313/// ```
314fn path_return_type(path: &ast::Path) -> Option<&ast::Ty> {
315 let last_segment = path.segments.last()?;
316 let args = last_segment.args.as_ref()?;
317 match &**args {
318 ast::GenericArgs::Parenthesized(args) => match &args.output {
319 ast::FnRetTy::Default(_) => None,
320 ast::FnRetTy::Ty(ret) => Some(ret),
321 },
322 ast::GenericArgs::AngleBracketed(_) | ast::GenericArgs::ParenthesizedElided(_) => None,
323 }
324}