Skip to content

Commit 1ae521e

Browse files
committed
Return earlier in some cases in collect_token.
This example triggers an assertion failure: ``` fn f() -> u32 { #[cfg_eval] #[cfg(not(FALSE))] 0 } ``` The sequence of events: - `configure_annotatable` calls `parse_expr_force_collect`, which calls `collect_tokens`. - Within that, we end up in `parse_expr_dot_or_call`, which again calls `collect_tokens`. - The return value of the `f` call is the expression `0`. - This inner call collects tokens for `0` (parser range 10..11) and creates a replacement covering `#[cfg(not(FALSE))] 0` (parser range 0..11). - We return to the outer `collect_tokens` call. The return value of the `f` call is *again* the expression `0`, again with the range 10..11, but the replacement from earlier covers the range 0..11. The code mistakenly assumes that any attributes from an inner `collect_tokens` call fit entirely within the body of the result of an outer `collect_tokens` call. So it adjusts the replacement parser range 0..11 to a node range by subtracting 10, resulting in -10..1. This is an invalid range and triggers an assertion failure. It's tricky to follow, but basically things get complicated when an AST node is returned from an inner `collect_tokens` call and then returned again from an outer `collect_token` node without being wrapped in any kind of additional layer. This commit changes `collect_tokens` to return early in some extra cases, avoiding the construction of lazy tokens. In the example above, the outer `collect_tokens` returns earlier because the `0` token already has tokens and `self.capture_state.capturing` is `Capturing::No`. This early return avoids the creation of the invalid range and the assertion failure. Fixes rust-lang#129166. Note: these invalid ranges have been happening for a long time. rust-lang#128725 looks like it's at fault only because it introduced the assertion that catches the invalid ranges.
1 parent 312ecdb commit 1ae521e

File tree

4 files changed

+41
-24
lines changed

4 files changed

+41
-24
lines changed

compiler/rustc_parse/src/parser/attr_wrapper.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ impl<'a> Parser<'a> {
234234
force_collect: ForceCollect,
235235
f: impl FnOnce(&mut Self, AttrVec) -> PResult<'a, (R, Trailing, UsePreAttrPos)>,
236236
) -> PResult<'a, R> {
237+
let possible_capture_mode = self.capture_cfg;
238+
237239
// We must collect if anything could observe the collected tokens, i.e.
238240
// if any of the following conditions hold.
239241
// - We are force collecting tokens (because force collection requires
@@ -244,9 +246,9 @@ impl<'a> Parser<'a> {
244246
// - Our target supports custom inner attributes (custom
245247
// inner attribute invocation might require token capturing).
246248
|| R::SUPPORTS_CUSTOM_INNER_ATTRS
247-
// - We are in `capture_cfg` mode (which requires tokens if
249+
// - We are in "possible capture mode" (which requires tokens if
248250
// the parsed node has `#[cfg]` or `#[cfg_attr]` attributes).
249-
|| self.capture_cfg;
251+
|| possible_capture_mode;
250252
if !needs_collection {
251253
return Ok(f(self, attrs.attrs)?.0);
252254
}
@@ -267,7 +269,7 @@ impl<'a> Parser<'a> {
267269
res?
268270
};
269271

270-
// When we're not in `capture_cfg` mode, then skip collecting and
272+
// When we're not in "definite capture mode", then skip collecting and
271273
// return early if either of the following conditions hold.
272274
// - `None`: Our target doesn't support tokens at all (e.g. `NtIdent`).
273275
// - `Some(Some(_))`: Our target already has tokens set (e.g. we've
@@ -278,7 +280,10 @@ impl<'a> Parser<'a> {
278280
// Note that this check is independent of `force_collect`. There's no
279281
// need to collect tokens when we don't support tokens or already have
280282
// tokens.
281-
if !self.capture_cfg && matches!(ret.tokens_mut(), None | Some(Some(_))) {
283+
let definite_capture_mode = self.capture_cfg
284+
&& matches!(self.capture_state.capturing, Capturing::Yes)
285+
&& has_cfg_or_cfg_attr(ret.attrs());
286+
if !definite_capture_mode && matches!(ret.tokens_mut(), None | Some(Some(_))) {
282287
return Ok(ret);
283288
}
284289

@@ -298,11 +303,11 @@ impl<'a> Parser<'a> {
298303
// the earlier `needs_tokens` check, and we don't need to
299304
// check `R::SUPPORTS_CUSTOM_INNER_ATTRS`.)
300305
|| needs_tokens(ret.attrs())
301-
// - We are in `capture_cfg` mode and there are `#[cfg]` or
302-
// `#[cfg_attr]` attributes. (During normal non-`capture_cfg`
303-
// parsing, we don't need any special capturing for those
304-
// attributes, because they're builtin.)
305-
|| (self.capture_cfg && has_cfg_or_cfg_attr(ret.attrs()));
306+
// - We are in "definite capture mode", which requires that there
307+
// are `#[cfg]` or `#[cfg_attr]` attributes. (During normal
308+
// non-`capture_cfg` parsing, we don't need any special capturing
309+
// for those attributes, because they're builtin.)
310+
|| definite_capture_mode;
306311
if !needs_collection {
307312
return Ok(ret);
308313
}
@@ -399,20 +404,18 @@ impl<'a> Parser<'a> {
399404
break_last_token: self.break_last_token,
400405
node_replacements,
401406
});
407+
let mut tokens_used = false;
402408

403409
// If we support tokens and don't already have them, store the newly captured tokens.
404410
if let Some(target_tokens @ None) = ret.tokens_mut() {
411+
tokens_used = true;
405412
*target_tokens = Some(tokens.clone());
406413
}
407414

408-
// If `capture_cfg` is set and we're inside a recursive call to
409-
// `collect_tokens`, then we need to register a replace range if we
410-
// have `#[cfg]` or `#[cfg_attr]`. This allows us to run eager
411-
// cfg-expansion on the captured token stream.
412-
if self.capture_cfg
413-
&& matches!(self.capture_state.capturing, Capturing::Yes)
414-
&& has_cfg_or_cfg_attr(ret.attrs())
415-
{
415+
// If in "definite capture mode" we need to register a replace range
416+
// for the `#[cfg]` and/or `#[cfg_attr]` attrs. This allows us to run
417+
// eager cfg-expansion on the captured token stream.
418+
if definite_capture_mode {
416419
assert!(!self.break_last_token, "Should not have unglued last token with cfg attr");
417420

418421
// What is the status here when parsing the example code at the top of this method?
@@ -430,6 +433,7 @@ impl<'a> Parser<'a> {
430433
let start_pos =
431434
if has_outer_attrs { attrs.start_pos.unwrap() } else { collect_pos.start_pos };
432435
let target = AttrsTarget { attrs: ret.attrs().iter().cloned().collect(), tokens };
436+
tokens_used = true;
433437
self.capture_state
434438
.parser_replacements
435439
.push((ParserRange(start_pos..end_pos), Some(target)));
@@ -439,6 +443,7 @@ impl<'a> Parser<'a> {
439443
self.capture_state.parser_replacements.clear();
440444
self.capture_state.inner_attr_parser_ranges.clear();
441445
}
446+
assert!(tokens_used); // check we didn't create `tokens` unnecessarily
442447
Ok(ret)
443448
}
444449
}

tests/crashes/129166.rs

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// This was triggering an assertion failure in `NodeRange::new`.
2+
3+
#![feature(cfg_eval)]
4+
#![feature(stmt_expr_attributes)]
5+
6+
fn f() -> u32 {
7+
#[cfg_eval] #[cfg(not(FALSE))] 0
8+
//~^ ERROR removing an expression is not supported in this position
9+
}
10+
11+
fn main() {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: removing an expression is not supported in this position
2+
--> $DIR/invalid-node-range-issue-129166.rs:7:17
3+
|
4+
LL | #[cfg_eval] #[cfg(not(FALSE))] 0
5+
| ^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)