rust/tests/ui/conditional-compilation
Nicholas Nethercote 1ae521e9d5 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 #129166. Note: these invalid ranges have been happening for a long
time. #128725 looks like it's at fault only because it introduced the
assertion that catches the invalid ranges.
2024-08-23 14:40:08 +10:00
..
auxiliary
cfg_accessible-bugs.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
cfg_accessible-bugs.stderr
cfg_accessible-input-validation.rs
cfg_accessible-input-validation.stderr
cfg_accessible-not_sure.edition2015.stderr
cfg_accessible-not_sure.edition2021.stderr
cfg_accessible-not_sure.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
cfg_accessible-private.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
cfg_accessible-stuck.rs
cfg_accessible-stuck.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
cfg_accessible-unstable.rs
cfg_accessible-unstable.stderr Bless tests 2024-01-13 12:46:58 -05:00
cfg_accessible.rs
cfg_accessible.stderr
cfg_attr_path.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
cfg-arg-invalid-1.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
cfg-arg-invalid-1.stderr
cfg-arg-invalid-2.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
cfg-arg-invalid-2.stderr
cfg-arg-invalid-3.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
cfg-arg-invalid-3.stderr
cfg-arg-invalid-4.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
cfg-arg-invalid-4.stderr
cfg-arg-invalid-5.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
cfg-arg-invalid-5.stderr
cfg-arg-invalid-6.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
cfg-arg-invalid-6.stderr
cfg-arg-invalid-7.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
cfg-arg-invalid-7.stderr
cfg-arg-invalid-8.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
cfg-arg-invalid-8.stderr
cfg-arg-invalid-9.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
cfg-arg-invalid-9.stderr
cfg-attr-cfg-2.rs compiletest: add enable-by-default check-cfg 2024-05-04 11:30:38 +02:00
cfg-attr-cfg-2.stderr compiletest: add enable-by-default check-cfg 2024-05-04 11:30:38 +02:00
cfg-attr-crate-2.rs compiletest: add enable-by-default check-cfg 2024-05-04 11:30:38 +02:00
cfg-attr-crate-2.stderr Bless tests 2024-01-13 12:46:58 -05:00
cfg-attr-empty-is-unused.rs Unify all the always-false cfgs under the FALSE cfg 2024-04-07 01:16:45 +02:00
cfg-attr-empty-is-unused.stderr Unify all the always-false cfgs under the FALSE cfg 2024-04-07 01:16:45 +02:00
cfg-attr-invalid-predicate.rs
cfg-attr-invalid-predicate.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
cfg-attr-multi-false.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
cfg-attr-multi-invalid-1.rs compiletest: add enable-by-default check-cfg 2024-05-04 11:30:38 +02:00
cfg-attr-multi-invalid-1.stderr Bless tests 2024-01-13 12:46:58 -05:00
cfg-attr-multi-invalid-2.rs compiletest: add enable-by-default check-cfg 2024-05-04 11:30:38 +02:00
cfg-attr-multi-invalid-2.stderr Bless tests 2024-01-13 12:46:58 -05:00
cfg-attr-multi-true.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
cfg-attr-multi-true.stderr error-msg: expand suggestion for unused lint 2023-03-15 23:30:12 +13:00
cfg-attr-parse.rs
cfg-attr-parse.stderr Make parse error suggestions verbose and fix spans 2024-07-12 03:02:57 +00:00
cfg-attr-syntax-validation.rs Fix parse error message for meta items 2024-05-10 09:16:27 +02:00
cfg-attr-syntax-validation.stderr Fix parse error message for meta items 2024-05-10 09:16:27 +02:00
cfg-attr-unknown-attribute-macro-expansion.rs
cfg-attr-unknown-attribute-macro-expansion.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
cfg-empty-codemap.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
cfg-empty-codemap.stderr
cfg-generic-params.rs compiletest: add enable-by-default check-cfg 2024-05-04 11:30:38 +02:00
cfg-generic-params.stderr Further cleanup cfgs in the UI test suite 2024-04-09 23:58:18 +02:00
cfg-in-crate-1.rs Unify all the always-false cfgs under the FALSE cfg 2024-04-07 01:16:45 +02:00
cfg-in-crate-1.stderr Unify all the always-false cfgs under the FALSE cfg 2024-04-07 01:16:45 +02:00
cfg-non-opt-expr.rs Unify all the always-false cfgs under the FALSE cfg 2024-04-07 01:16:45 +02:00
cfg-non-opt-expr.stderr Unify all the always-false cfgs under the FALSE cfg 2024-04-07 01:16:45 +02:00
inner-cfg-non-inline-mod.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
invalid-node-range-issue-129166.rs Return earlier in some cases in collect_token. 2024-08-23 14:40:08 +10:00
invalid-node-range-issue-129166.stderr Return earlier in some cases in collect_token. 2024-08-23 14:40:08 +10:00
issue-34028.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
module_with_cfg.rs [AUTO-GENERATED] Migrate ui tests from // to //@ directives 2024-02-16 20:02:50 +00:00
test-cfg.rs compiletest: add enable-by-default check-cfg 2024-05-04 11:30:38 +02:00
test-cfg.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00