Skip to content

Commit fe460ac

Browse files
committed
Add some attribute stringify! tests.
A couple of these are marked `FIXME` because they demonstrate existing bugs with token collection.
1 parent 5aaa2f9 commit fe460ac

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

Diff for: tests/ui/macros/stringify.rs

+53-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ macro_rules! stmt { ($stmt:stmt) => { stringify!($stmt) }; }
3333
macro_rules! ty { ($ty:ty) => { stringify!($ty) }; }
3434
macro_rules! vis { ($vis:vis) => { stringify!($vis) }; }
3535

36-
// Use this when AST pretty-printing and TokenStream pretty-printing give
37-
// the same result (which is preferable.)
3836
macro_rules! c1 {
3937
($frag:ident, [$($tt:tt)*], $s:literal) => {
4038
// Prior to #125174:
@@ -53,6 +51,14 @@ macro_rules! c1 {
5351
};
5452
}
5553

54+
// FIXME: temporary
55+
macro_rules! c2 {
56+
($frag:ident, [$($tt:tt)*], $s1:literal, $s2:literal) => {
57+
assert_eq!($frag!($($tt)*), $s1);
58+
assert_eq!(stringify!($($tt)*), $s2);
59+
};
60+
}
61+
5662
#[test]
5763
fn test_block() {
5864
c1!(block, [ {} ], "{}");
@@ -66,6 +72,8 @@ fn test_block() {
6672
} ],
6773
"{ let _; true }"
6874
);
75+
76+
// Attributes are not allowed on vanilla blocks.
6977
}
7078

7179
#[test]
@@ -332,6 +340,20 @@ fn test_expr() {
332340
// ExprKind::FormatArgs: untestable because this test works pre-expansion.
333341

334342
// ExprKind::Err: untestable.
343+
344+
// Ones involving attributes.
345+
c1!(expr, [ #[aa] 1 ], "#[aa] 1");
346+
c1!(expr, [ #[aa] #[bb] x ], "#[aa] #[bb] x");
347+
c2!(expr, [ #[aa] 1 + 2 ], "1 + 2", "#[aa] 1 + 2"); // FIXME
348+
c2!(expr, [ #[aa] x + 2 ], "x + 2", "#[aa] x + 2"); // FIXME
349+
c2!(expr, [ #[aa] 1 / #[bb] 2 ], "1 / #[bb] 2", "#[aa] 1 / #[bb] 2"); // FIXME
350+
c2!(expr, [ #[aa] x / #[bb] 2 ], "x / #[bb] 2", "#[aa] x / #[bb] 2"); // FIXME
351+
c1!(expr, [ 1 << #[bb] 2 ], "1 << #[bb] 2");
352+
c1!(expr, [ x << #[bb] 2 ], "x << #[bb] 2");
353+
c1!(expr, [ #[aa] (1 + 2) ], "#[aa] (1 + 2)");
354+
c1!(expr, [ #[aa] #[bb] (x + 2) ], "#[aa] #[bb] (x + 2)");
355+
c1!(expr, [ #[aa] x[0].p ], "#[aa] x[0].p");
356+
c1!(expr, [ #[aa] { #![bb] 0 } ], "#[aa] { #![bb] 0 }");
335357
}
336358

337359
#[test]
@@ -484,6 +506,11 @@ fn test_item() {
484506
"macro_rules! stringify { () => {}; }"
485507
);
486508
c1!(item, [ pub macro stringify() {} ], "pub macro stringify() {}");
509+
510+
// Ones involving attributes.
511+
c1!(item, [ #[aa] mod m; ], "#[aa] mod m;");
512+
c1!(item, [ mod m { #![bb] } ], "mod m { #![bb] }");
513+
c1!(item, [ #[aa] mod m { #![bb] } ], "#[aa] mod m { #![bb] }");
487514
}
488515

489516
#[test]
@@ -492,6 +519,8 @@ fn test_meta() {
492519
c1!(meta, [ k = "v" ], "k = \"v\"");
493520
c1!(meta, [ list(k1, k2 = "v") ], "list(k1, k2 = \"v\")");
494521
c1!(meta, [ serde::k ], "serde::k");
522+
523+
// Attributes are not allowed on metas.
495524
}
496525

497526
#[test]
@@ -580,6 +609,8 @@ fn test_pat() {
580609
c1!(pat, [ mac!(...) ], "mac!(...)");
581610
c1!(pat, [ mac![...] ], "mac![...]");
582611
c1!(pat, [ mac! { ... } ], "mac! { ... }");
612+
613+
// Attributes are not allowed on patterns.
583614
}
584615

585616
#[test]
@@ -593,6 +624,8 @@ fn test_path() {
593624
c1!(path, [ Self::<'static> ], "Self::<'static>");
594625
c1!(path, [ Self() ], "Self()");
595626
c1!(path, [ Self() -> () ], "Self() -> ()");
627+
628+
// Attributes are not allowed on paths.
596629
}
597630

598631
#[test]
@@ -622,6 +655,20 @@ fn test_stmt() {
622655
c1!(stmt, [ mac!(...) ], "mac!(...)");
623656
c1!(stmt, [ mac![...] ], "mac![...]");
624657
c1!(stmt, [ mac! { ... } ], "mac! { ... }");
658+
659+
// Ones involving attributes.
660+
c1!(stmt, [ #[aa] 1 ], "#[aa] 1");
661+
c1!(stmt, [ #[aa] #[bb] x ], "#[aa] #[bb] x");
662+
c2!(stmt, [ #[aa] 1 as u32 ], "1 as u32", "#[aa] 1 as u32"); // FIXME
663+
c2!(stmt, [ #[aa] x as u32 ], "x as u32", "#[aa] x as u32"); // FIXME
664+
c2!(stmt, [ #[aa] 1 .. #[bb] 2 ], "1 .. #[bb] 2", "#[aa] 1 .. #[bb] 2"); // FIXME
665+
c2!(stmt, [ #[aa] x .. #[bb] 2 ], "x .. #[bb] 2", "#[aa] x .. #[bb] 2"); // FIXME
666+
c1!(stmt, [ 1 || #[bb] 2 ], "1 || #[bb] 2");
667+
c1!(stmt, [ x || #[bb] 2 ], "x || #[bb] 2");
668+
c1!(stmt, [ #[aa] (1 + 2) ], "#[aa] (1 + 2)");
669+
c1!(stmt, [ #[aa] #[bb] (x + 2) ], "#[aa] #[bb] (x + 2)");
670+
c1!(stmt, [ #[aa] x[0].p ], "#[aa] x[0].p");
671+
c1!(stmt, [ #[aa] { #![bb] 0 } ], "#[aa] { #![bb] 0 }");
625672
}
626673

627674
#[test]
@@ -708,6 +755,8 @@ fn test_ty() {
708755

709756
// TyKind::CVarArgs
710757
// FIXME: todo
758+
759+
// Attributes are not allowed on types.
711760
}
712761

713762
#[test]
@@ -732,6 +781,8 @@ fn test_vis() {
732781
macro_rules! inherited_vis { ($vis:vis struct) => { vis!($vis) }; }
733782
assert_eq!(inherited_vis!(struct), "");
734783
assert_eq!(stringify!(), "");
784+
785+
// Attributes are not allowed on visibilities.
735786
}
736787

737788
macro_rules! p {

0 commit comments

Comments
 (0)