]> git.lizzy.rs Git - rust.git/blob - tests/ui/proc-macro/allowed-attr-stmt-expr.rs
Rollup merge of #106717 - klensy:typo, r=lcnr
[rust.git] / tests / ui / proc-macro / allowed-attr-stmt-expr.rs
1 // aux-build:attr-stmt-expr.rs
2 // aux-build:test-macros.rs
3 // compile-flags: -Z span-debug
4 // check-pass
5
6 #![feature(proc_macro_hygiene)]
7 #![feature(stmt_expr_attributes)]
8 #![feature(rustc_attrs)]
9 #![allow(dead_code)]
10
11 #![no_std] // Don't load unnecessary hygiene information from std
12 extern crate std;
13
14 extern crate attr_stmt_expr;
15 extern crate test_macros;
16 use attr_stmt_expr::{expect_let, expect_my_macro_stmt, expect_expr, expect_my_macro_expr};
17 use test_macros::print_attr;
18
19 // We don't use `std::println` so that we avoid loading hygiene
20 // information from libstd, which would affect the SyntaxContext ids
21 macro_rules! my_macro {
22     ($($tt:tt)*) => { () }
23 }
24
25
26 fn print_str(string: &'static str) {
27     // macros are handled a bit differently
28     #[expect_my_macro_expr]
29     my_macro!("{}", string)
30 }
31
32 macro_rules! make_stmt {
33     ($stmt:stmt) => {
34         #[print_attr]
35         #[rustc_dummy]
36         $stmt; // This semicolon is *not* passed to the macro,
37                // since `$stmt` is already a statement.
38     }
39 }
40
41 macro_rules! second_make_stmt {
42     ($stmt:stmt) => {
43         make_stmt!($stmt);
44     }
45 }
46
47 // The macro will see a semicolon here
48 #[print_attr]
49 struct ItemWithSemi;
50
51
52 fn main() {
53     make_stmt!(struct Foo {});
54
55     #[print_attr]
56     #[expect_let]
57     let string = "Hello, world!";
58
59     #[print_attr]
60     #[expect_my_macro_stmt]
61     my_macro!("{}", string);
62
63     #[print_attr]
64     second_make_stmt!(#[allow(dead_code)] struct Bar {});
65
66     #[print_attr]
67     #[rustc_dummy]
68     struct Other {};
69
70     // The macro also sees a semicolon,
71     // for consistency with the `ItemWithSemi` case above.
72     #[print_attr]
73     #[rustc_dummy]
74     struct NonBracedStruct;
75
76     #[expect_expr]
77     print_str("string")
78 }