• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! Tests for user-defined procedural macros.
2 //!
3 //! Note `//- proc_macros: identity` fixture metas in tests -- we don't use real
4 //! proc-macros here, as that would be slow. Instead, we use several hard-coded
5 //! in-memory macros.
6 use expect_test::expect;
7 
8 use crate::macro_expansion_tests::check;
9 
10 #[test]
attribute_macro_attr_censoring()11 fn attribute_macro_attr_censoring() {
12     cov_mark::check!(attribute_macro_attr_censoring);
13     check(
14         r#"
15 //- proc_macros: identity
16 #[attr1] #[proc_macros::identity] #[attr2]
17 struct S;
18 "#,
19         expect![[r##"
20 #[attr1] #[proc_macros::identity] #[attr2]
21 struct S;
22 
23 #[attr1]
24 #[attr2] struct S;"##]],
25     );
26 }
27 
28 #[test]
derive_censoring()29 fn derive_censoring() {
30     cov_mark::check!(derive_censoring);
31     check(
32         r#"
33 //- proc_macros: derive_identity
34 //- minicore:derive
35 #[attr1]
36 #[derive(Foo)]
37 #[derive(proc_macros::DeriveIdentity)]
38 #[derive(Bar)]
39 #[attr2]
40 struct S;
41 "#,
42         expect![[r##"
43 #[attr1]
44 #[derive(Foo)]
45 #[derive(proc_macros::DeriveIdentity)]
46 #[derive(Bar)]
47 #[attr2]
48 struct S;
49 
50 #[attr1]
51 #[derive(Bar)]
52 #[attr2] struct S;"##]],
53     );
54 }
55 
56 #[test]
attribute_macro_syntax_completion_1()57 fn attribute_macro_syntax_completion_1() {
58     // this is just the case where the input is actually valid
59     check(
60         r#"
61 //- proc_macros: identity_when_valid
62 #[proc_macros::identity_when_valid]
63 fn foo() { bar.baz(); blub }
64 "#,
65         expect![[r##"
66 #[proc_macros::identity_when_valid]
67 fn foo() { bar.baz(); blub }
68 
69 fn foo() {
70     bar.baz();
71     blub
72 }"##]],
73     );
74 }
75 
76 #[test]
attribute_macro_syntax_completion_2()77 fn attribute_macro_syntax_completion_2() {
78     // common case of dot completion while typing
79     check(
80         r#"
81 //- proc_macros: identity_when_valid
82 #[proc_macros::identity_when_valid]
83 fn foo() { bar.; blub }
84 "#,
85         expect![[r#"
86 #[proc_macros::identity_when_valid]
87 fn foo() { bar.; blub }
88 
89 fn foo() {
90     bar. ;
91     blub
92 }"#]],
93     );
94 }
95 
96 #[test]
float_parsing_panic()97 fn float_parsing_panic() {
98     // Regression test for https://github.com/rust-lang/rust-analyzer/issues/12211
99     check(
100         r#"
101 //- proc_macros: identity
102 macro_rules! id {
103     ($($t:tt)*) => {
104         $($t)*
105     };
106 }
107 id! {
108     #[proc_macros::identity]
109     impl Foo for WrapBj {
110         async fn foo(&self) {
111             self.0. id().await;
112         }
113     }
114 }
115 "#,
116         expect![[r#"
117 macro_rules! id {
118     ($($t:tt)*) => {
119         $($t)*
120     };
121 }
122 #[proc_macros::identity] impl Foo for WrapBj {
123     async fn foo(&self ) {
124         self .0.id().await ;
125     }
126 }
127 "#]],
128     );
129 }
130