Skip to content

Commit caee195

Browse files
committed
Invert early exit conditions in collect_tokens_trailing_token.
This has been bugging me for a while. I find complex "if any of these are true" conditions easier to think about than complex "if all of these are true" conditions, because you can stop as soon as one is true.
1 parent 6106b05 commit caee195

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

compiler/rustc_parse/src/parser/attr_wrapper.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -199,20 +199,20 @@ impl<'a> Parser<'a> {
199199
force_collect: ForceCollect,
200200
f: impl FnOnce(&mut Self, ast::AttrVec) -> PResult<'a, (R, bool)>,
201201
) -> PResult<'a, R> {
202-
// Skip collection when nothing could observe the collected tokens, i.e.
203-
// all of the following conditions hold.
204-
// - We are not force collecting tokens (because force collection
205-
// requires tokens by definition).
206-
if matches!(force_collect, ForceCollect::No)
207-
// - None of our outer attributes require tokens.
208-
&& attrs.is_complete()
209-
// - Our target doesn't support custom inner attributes (custom
202+
// We must collect if anything could observe the collected tokens, i.e.
203+
// if any of the following conditions hold.
204+
// - We are force collecting tokens (because force collection requires
205+
// tokens by definition).
206+
let needs_collection = matches!(force_collect, ForceCollect::Yes)
207+
// - Any of our outer attributes require tokens.
208+
|| !attrs.is_complete()
209+
// - Our target supports custom inner attributes (custom
210210
// inner attribute invocation might require token capturing).
211-
&& !R::SUPPORTS_CUSTOM_INNER_ATTRS
212-
// - We are not in `capture_cfg` mode (which requires tokens if
211+
|| R::SUPPORTS_CUSTOM_INNER_ATTRS
212+
// - We are in `capture_cfg` mode (which requires tokens if
213213
// the parsed node has `#[cfg]` or `#[cfg_attr]` attributes).
214-
&& !self.capture_cfg
215-
{
214+
|| self.capture_cfg;
215+
if !needs_collection {
216216
return Ok(f(self, attrs.attrs)?.0);
217217
}
218218

@@ -250,28 +250,28 @@ impl<'a> Parser<'a> {
250250
return Ok(ret);
251251
}
252252

253-
// This is similar to the "skip collection" check at the start of this
254-
// function, but now that we've parsed an AST node we have more
253+
// This is similar to the `needs_collection` check at the start of this
254+
// function, but now that we've parsed an AST node we have complete
255255
// information available. (If we return early here that means the
256256
// setup, such as cloning the token cursor, was unnecessary. That's
257257
// hard to avoid.)
258258
//
259-
// Skip collection when nothing could observe the collected tokens, i.e.
260-
// all of the following conditions hold.
261-
// - We are not force collecting tokens.
262-
if matches!(force_collect, ForceCollect::No)
263-
// - None of our outer *or* inner attributes require tokens.
259+
// We must collect if anything could observe the collected tokens, i.e.
260+
// if any of the following conditions hold.
261+
// - We are force collecting tokens.
262+
let needs_collection = matches!(force_collect, ForceCollect::Yes)
263+
// - Any of our outer *or* inner attributes require tokens.
264264
// (`attrs` was just outer attributes, but `ret.attrs()` is outer
265-
// and inner attributes. That makes this check more precise than
266-
// `attrs.is_complete()` at the start of the function, and we can
267-
// skip the subsequent check on `R::SUPPORTS_CUSTOM_INNER_ATTRS`.
268-
&& crate::parser::attr::is_complete(ret.attrs())
269-
// - We are not in `capture_cfg` mode, or we are but there are no
270-
// `#[cfg]` or `#[cfg_attr]` attributes. (During normal
271-
// non-`capture_cfg` parsing, we don't need any special capturing
272-
// for those attributes, because they're builtin.)
273-
&& (!self.capture_cfg || !has_cfg_or_cfg_attr(ret.attrs()))
274-
{
265+
// and inner attributes. So this check is more precise than the
266+
// earlier `attrs.is_complete()` check, and we don't need to
267+
// check `R::SUPPORTS_CUSTOM_INNER_ATTRS`.)
268+
|| !crate::parser::attr::is_complete(ret.attrs())
269+
// - We are in `capture_cfg` mode and there are `#[cfg]` or
270+
// `#[cfg_attr]` attributes. (During normal non-`capture_cfg`
271+
// parsing, we don't need any special capturing for those
272+
// attributes, because they're builtin.)
273+
|| (self.capture_cfg && has_cfg_or_cfg_attr(ret.attrs()));
274+
if !needs_collection {
275275
return Ok(ret);
276276
}
277277

0 commit comments

Comments
 (0)