Commit 77ed39c
committed
Auto merge of #29850 - Kimundi:attributes_that_make_a_statement, r=pnkfelix
See rust-lang/rfcs#16 and #15701
- Added syntax support for attributes on expressions and all syntax nodes in statement position.
- Extended `#[cfg]` folder to allow removal of statements, and
of expressions in optional positions like expression lists and trailing
block expressions.
- Extended lint checker to recognize lint levels on expressions and
locals.
- As per RFC, attributes are not yet accepted on `if` expressions.
Examples:
```rust
let x = y;
{
...
}
assert_eq!((1, #[cfg(unset)] 2, 3), (1, 3));
let FOO = 0;
```
Implementation wise, there are a few rough corners and open questions:
- The parser work ended up a bit ugly.
- The pretty printer change was based mostly on guessing.
- Similar to the `if` case, there are some places in the grammar where a new `Expr` node starts,
but where it seemed weird to accept attributes and hence the parser doesn't. This includes:
- const expressions in patterns
- in the middle of an postfix operator chain (that is, after `.`, before indexing, before calls)
- on range expressions, since `#[attr] x .. y` parses as `(#[attr] x) .. y`, which is inconsistent with
`#[attr] .. y` which would parse as `#[attr] (.. y)`
- Attributes are added as additional `Option<Box<Vec<Attribute>>>` fields in expressions and locals.
- Memory impact has not been measured yet.
- A cfg-away trailing expression in a block does not currently promote the previous `StmtExpr` in a block to a new trailing expr. That is to say, this won't work:
```rust
let x = {
#[cfg(foo)]
Foo { data: x }
#[cfg(not(foo))]
Foo { data: y }
};
```
- One-element tuples can have their inner expression removed to become Unit, but just Parenthesis can't. Eg, `(#[cfg(unset)] x,) == ()` but `(#[cfg(unset)] x) == error`. This seemed reasonable to me since tuples and unit are type constructors, but could probably be argued either way.
- Attributes on macro nodes are currently unconditionally dropped during macro expansion, which seemed fine since macro disappear at that point?
- Attributes on `ast::ExprParens` will be prepend-ed to the inner expression in the hir folder.
- The work on pretty printer tests for this did trigger, but not fix errors regarding macros:
- expression `foo![]` prints as `foo!()`
- expression `foo!{}` prints as `foo!()`
- statement `foo![];` prints as `foo!();`
- statement `foo!{};` prints as `foo!();`
- statement `foo!{}` triggers a `None` unwrap ICE.File tree
45 files changed
+2283
-586
lines changed- src
- doc
- librustc_driver
- librustc_front
- librustc_lint
- librustc
- lint
- middle
- libsyntax
- ext
- deriving
- generic
- parse
- print
- util
- test
- compile-fail
- parse-fail
- pretty
- run-pass-fulldeps
- run-pass
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
45 files changed
+2283
-586
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2368 | 2368 | | |
2369 | 2369 | | |
2370 | 2370 | | |
| 2371 | + | |
| 2372 | + | |
| 2373 | + | |
2371 | 2374 | | |
2372 | 2375 | | |
2373 | 2376 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| 45 | + | |
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
| |||
674 | 675 | | |
675 | 676 | | |
676 | 677 | | |
677 | | - | |
678 | | - | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
679 | 682 | | |
680 | 683 | | |
681 | 684 | | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
682 | 690 | | |
683 | 691 | | |
684 | 692 | | |
| |||
730 | 738 | | |
731 | 739 | | |
732 | 740 | | |
733 | | - | |
734 | | - | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
735 | 745 | | |
736 | 746 | | |
737 | 747 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
409 | 409 | | |
410 | 410 | | |
411 | 411 | | |
412 | | - | |
| 412 | + | |
| 413 | + | |
413 | 414 | | |
414 | 415 | | |
415 | 416 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
654 | 654 | | |
655 | 655 | | |
656 | 656 | | |
| 657 | + | |
657 | 658 | | |
658 | 659 | | |
659 | 660 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
| 23 | + | |
50 | 24 | | |
51 | 25 | | |
52 | 26 | | |
| |||
332 | 306 | | |
333 | 307 | | |
334 | 308 | | |
335 | | - | |
| 309 | + | |
336 | 310 | | |
337 | 311 | | |
338 | 312 | | |
| |||
501 | 475 | | |
502 | 476 | | |
503 | 477 | | |
504 | | - | |
| 478 | + | |
505 | 479 | | |
506 | 480 | | |
507 | 481 | | |
508 | 482 | | |
509 | 483 | | |
510 | 484 | | |
| 485 | + | |
511 | 486 | | |
512 | 487 | | |
513 | 488 | | |
| |||
769 | 744 | | |
770 | 745 | | |
771 | 746 | | |
772 | | - | |
| 747 | + | |
773 | 748 | | |
774 | 749 | | |
775 | 750 | | |
| |||
816 | 791 | | |
817 | 792 | | |
818 | 793 | | |
819 | | - | |
820 | | - | |
821 | | - | |
| 794 | + | |
| 795 | + | |
822 | 796 | | |
823 | 797 | | |
824 | 798 | | |
| |||
834 | 808 | | |
835 | 809 | | |
836 | 810 | | |
837 | | - | |
838 | | - | |
839 | | - | |
| 811 | + | |
840 | 812 | | |
841 | 813 | | |
842 | 814 | | |
| |||
892 | 864 | | |
893 | 865 | | |
894 | 866 | | |
895 | | - | |
| 867 | + | |
896 | 868 | | |
897 | 869 | | |
898 | 870 | | |
| |||
1048 | 1020 | | |
1049 | 1021 | | |
1050 | 1022 | | |
1051 | | - | |
| 1023 | + | |
1052 | 1024 | | |
1053 | 1025 | | |
1054 | 1026 | | |
| |||
1171 | 1143 | | |
1172 | 1144 | | |
1173 | 1145 | | |
| 1146 | + | |
1174 | 1147 | | |
1175 | 1148 | | |
1176 | 1149 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| 44 | + | |
44 | 45 | | |
45 | 46 | | |
46 | 47 | | |
| |||
558 | 559 | | |
559 | 560 | | |
560 | 561 | | |
| 562 | + | |
561 | 563 | | |
562 | 564 | | |
563 | 565 | | |
| |||
609 | 611 | | |
610 | 612 | | |
611 | 613 | | |
| 614 | + | |
612 | 615 | | |
613 | 616 | | |
614 | 617 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
38 | | - | |
39 | 38 | | |
40 | 39 | | |
41 | 40 | | |
| |||
0 commit comments