]> git.proxmox.com Git - rustc.git/blame - src/tools/rust-analyzer/crates/hir-def/src/item_tree/tests.rs
New upstream version 1.72.1+dfsg1
[rustc.git] / src / tools / rust-analyzer / crates / hir-def / src / item_tree / tests.rs
CommitLineData
064997fb
FG
1use base_db::fixture::WithFixture;
2use expect_test::{expect, Expect};
3
4use crate::{db::DefDatabase, test_db::TestDB};
5
6fn check(ra_fixture: &str, expect: Expect) {
7 let (db, file_id) = TestDB::with_single_file(ra_fixture);
8 let item_tree = db.file_item_tree(file_id.into());
fe692bf9 9 let pretty = item_tree.pretty_print(&db);
064997fb
FG
10 expect.assert_eq(&pretty);
11}
12
13#[test]
14fn imports() {
15 check(
16 r#"
17//! file comment
18#![no_std]
19//! another file comment
20
21extern crate self as renamed;
22pub(super) extern crate bli;
23
24pub use crate::path::{nested, items as renamed, Trait as _};
25use globs::*;
26
27/// docs on import
28use crate::{A, B};
29
30use a::{c, d::{e}};
31 "#,
32 expect![[r##"
33 #![doc = " file comment"]
34 #![no_std]
35 #![doc = " another file comment"]
36
37 pub(self) extern crate self as renamed;
38
39 pub(super) extern crate bli;
40
41 pub use crate::path::{nested, items as renamed, Trait as _};
42
43 pub(self) use globs::*;
44
45 #[doc = " docs on import"]
46 pub(self) use crate::{A, B};
47
48 pub(self) use a::{c, d::{e}};
49 "##]],
50 );
51}
52
53#[test]
54fn extern_blocks() {
55 check(
56 r#"
57#[on_extern_block]
58extern "C" {
59 #[on_extern_type]
60 type ExType;
61
62 #[on_extern_static]
63 static EX_STATIC: u8;
64
65 #[on_extern_fn]
66 fn ex_fn();
67}
68 "#,
69 expect![[r##"
70 #[on_extern_block]
71 extern "C" {
72 #[on_extern_type]
73 pub(self) type ExType;
74
75 #[on_extern_static]
76 pub(self) static EX_STATIC: u8 = _;
77
78 #[on_extern_fn]
79 pub(self) fn ex_fn() -> ();
80 }
81 "##]],
82 );
83}
84
85#[test]
86fn adts() {
87 check(
88 r#"
89struct Unit;
90
91#[derive(Debug)]
92struct Struct {
93 /// fld docs
94 fld: (),
95}
96
97struct Tuple(#[attr] u8);
98
99union Ize {
100 a: (),
101 b: (),
102}
103
104enum E {
105 /// comment on Unit
106 Unit,
107 /// comment on Tuple
108 Tuple(u8),
109 Struct {
110 /// comment on a: u8
111 a: u8,
112 }
113}
114 "#,
115 expect![[r##"
116 pub(self) struct Unit;
117
118 #[derive(Debug)]
119 pub(self) struct Struct {
120 #[doc = " fld docs"]
121 pub(self) fld: (),
122 }
123
124 pub(self) struct Tuple(
125 #[attr]
126 pub(self) 0: u8,
127 );
128
129 pub(self) union Ize {
130 pub(self) a: (),
131 pub(self) b: (),
132 }
133
134 pub(self) enum E {
135 #[doc = " comment on Unit"]
136 Unit,
137 #[doc = " comment on Tuple"]
138 Tuple(
139 pub(self) 0: u8,
140 ),
141 Struct {
142 #[doc = " comment on a: u8"]
143 pub(self) a: u8,
144 },
145 }
146 "##]],
147 );
148}
149
150#[test]
151fn misc() {
152 check(
153 r#"
154pub static mut ST: () = ();
155
156const _: Anon = ();
157
158#[attr]
159fn f(#[attr] arg: u8, _: ()) {
160 #![inner_attr_in_fn]
161}
162
163trait Tr: SuperTrait + 'lifetime {
164 type Assoc: AssocBound = Default;
165 fn method(&self);
166}
167 "#,
fe692bf9 168 expect![[r#"
064997fb
FG
169 pub static mut ST: () = _;
170
171 pub(self) const _: Anon = _;
172
173 #[attr]
174 #[inner_attr_in_fn]
175 pub(self) fn f(
176 #[attr]
fe692bf9
FG
177 u8,
178 (),
064997fb
FG
179 ) -> () { ... }
180
181 pub(self) trait Tr<Self>
182 where
183 Self: SuperTrait,
184 Self: 'lifetime
185 {
186 pub(self) type Assoc: AssocBound = Default;
187
188 pub(self) fn method(
fe692bf9 189 self: &Self,
064997fb
FG
190 ) -> ();
191 }
fe692bf9 192 "#]],
064997fb
FG
193 );
194}
195
196#[test]
197fn modules() {
198 check(
199 r#"
200/// outer
201mod inline {
202 //! inner
203
204 use super::*;
205
206 fn fn_in_module() {}
207}
208
209mod outline;
210 "#,
211 expect![[r##"
212 #[doc = " outer"]
213 #[doc = " inner"]
214 pub(self) mod inline {
215 pub(self) use super::*;
216
217 pub(self) fn fn_in_module() -> () { ... }
218 }
219
220 pub(self) mod outline;
221 "##]],
222 );
223}
224
225#[test]
226fn macros() {
227 check(
228 r#"
229macro_rules! m {
230 () => {};
231}
232
233pub macro m2() {}
234
235m!();
236 "#,
237 expect![[r#"
238 macro_rules! m { ... }
239
240 pub macro m2 { ... }
241
242 m!(...);
243 "#]],
244 );
245}
246
247#[test]
248fn mod_paths() {
249 check(
250 r#"
251struct S {
252 a: self::Ty,
253 b: super::SuperTy,
254 c: super::super::SuperSuperTy,
255 d: ::abs::Path,
256 e: crate::Crate,
257 f: plain::path::Ty,
258}
259 "#,
260 expect![[r#"
261 pub(self) struct S {
262 pub(self) a: self::Ty,
263 pub(self) b: super::SuperTy,
264 pub(self) c: super::super::SuperSuperTy,
265 pub(self) d: ::abs::Path,
266 pub(self) e: crate::Crate,
267 pub(self) f: plain::path::Ty,
268 }
269 "#]],
270 )
271}
272
273#[test]
274fn types() {
275 check(
276 r#"
277struct S {
278 a: Mixed<'a, T, Item=(), OtherItem=u8>,
279 b: <Fully as Qualified>::Syntax,
280 c: <TypeAnchored>::Path::<'a>,
281 d: dyn for<'a> Trait<'a>,
282}
283 "#,
284 expect![[r#"
285 pub(self) struct S {
f2b60f7d
FG
286 pub(self) a: Mixed::<'a, T, Item = (), OtherItem = u8>,
287 pub(self) b: Qualified::<Self=Fully>::Syntax,
288 pub(self) c: <TypeAnchored>::Path::<'a>,
289 pub(self) d: dyn for<'a> Trait::<'a>,
064997fb
FG
290 }
291 "#]],
292 )
293}
294
295#[test]
296fn generics() {
297 check(
298 r#"
299struct S<'a, 'b: 'a, T: Copy + 'a + 'b, const K: u8 = 0> {
300 field: &'a &'b T,
301}
302
303struct Tuple<T: Copy, U: ?Sized>(T, U);
304
305impl<'a, 'b: 'a, T: Copy + 'a + 'b, const K: u8 = 0> S<'a, 'b, T, K> {
306 fn f<G: 'a>(arg: impl Copy) -> impl Copy {}
307}
308
309enum Enum<'a, T, const U: u8> {}
310union Union<'a, T, const U: u8> {}
311
312trait Tr<'a, T: 'a>: Super where Self: for<'a> Tr<'a, T> {}
313 "#,
314 expect![[r#"
315 pub(self) struct S<'a, 'b, T, const K: u8>
316 where
317 T: Copy,
318 T: 'a,
319 T: 'b
320 {
321 pub(self) field: &'a &'b T,
322 }
323
324 pub(self) struct Tuple<T, U>(
325 pub(self) 0: T,
326 pub(self) 1: U,
327 )
328 where
329 T: Copy,
330 U: ?Sized;
331
f2b60f7d 332 impl<'a, 'b, T, const K: u8> S::<'a, 'b, T, K>
064997fb
FG
333 where
334 T: Copy,
335 T: 'a,
336 T: 'b
337 {
338 pub(self) fn f<G>(
fe692bf9 339 impl Copy,
064997fb
FG
340 ) -> impl Copy
341 where
342 G: 'a { ... }
343 }
344
345 pub(self) enum Enum<'a, T, const U: u8> {
346 }
347
348 pub(self) union Union<'a, T, const U: u8> {
349 }
350
351 pub(self) trait Tr<'a, Self, T>
352 where
353 Self: Super,
354 T: 'a,
f2b60f7d 355 Self: for<'a> Tr::<'a, T>
064997fb
FG
356 {
357 }
358 "#]],
359 )
360}