Skip to content

Commit 1b15638

Browse files
committed
Auto merge of #11990 - jonas-schievink:improve-itemtree-prettyprint, r=jonas-schievink
internal: Improve ItemTree pretty print output Expand `FnFlags` into what they actually mean and omit `AttrId`s, since those are sequential numbers in the item tree
2 parents 4cee507 + fbded17 commit 1b15638

File tree

2 files changed

+50
-38
lines changed

2 files changed

+50
-38
lines changed

crates/hir_def/src/item_tree/pretty.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,10 @@ impl<'a> Printer<'a> {
9090
for attr in &**attrs {
9191
wln!(
9292
self,
93-
"#{}[{}{}] // {:?}",
93+
"#{}[{}{}]",
9494
inner,
9595
attr.path,
9696
attr.input.as_ref().map(|it| it.to_string()).unwrap_or_default(),
97-
attr.id,
9897
);
9998
}
10099
}
@@ -242,10 +241,19 @@ impl<'a> Printer<'a> {
242241
ast_id: _,
243242
flags,
244243
} = &self.tree[it];
245-
if flags.bits != 0 {
246-
wln!(self, "// flags = 0x{:X}", flags.bits);
247-
}
248244
self.print_visibility(*visibility);
245+
if flags.contains(FnFlags::HAS_DEFAULT_KW) {
246+
w!(self, "default ");
247+
}
248+
if flags.contains(FnFlags::HAS_CONST_KW) {
249+
w!(self, "const ");
250+
}
251+
if flags.contains(FnFlags::HAS_ASYNC_KW) {
252+
w!(self, "async ");
253+
}
254+
if flags.contains(FnFlags::HAS_UNSAFE_KW) {
255+
w!(self, "unsafe ");
256+
}
249257
if let Some(abi) = abi {
250258
w!(self, "extern \"{}\" ", abi);
251259
}
@@ -254,7 +262,7 @@ impl<'a> Printer<'a> {
254262
w!(self, "(");
255263
if !params.is_empty() {
256264
self.indented(|this| {
257-
for param in params.clone() {
265+
for (i, param) in params.clone().enumerate() {
258266
this.print_attrs_of(param);
259267
match &this.tree[param] {
260268
Param::Normal(name, ty) => {
@@ -263,7 +271,12 @@ impl<'a> Printer<'a> {
263271
None => w!(this, "_: "),
264272
}
265273
this.print_type_ref(ty);
266-
wln!(this, ",");
274+
w!(this, ",");
275+
if flags.contains(FnFlags::HAS_SELF_PARAM) && i == 0 {
276+
wln!(this, " // self");
277+
} else {
278+
wln!(this);
279+
}
267280
}
268281
Param::Varargs => {
269282
wln!(this, "...");
@@ -275,7 +288,11 @@ impl<'a> Printer<'a> {
275288
w!(self, ") -> ");
276289
self.print_type_ref(ret_type);
277290
self.print_where_clause(explicit_generic_params);
278-
wln!(self, ";");
291+
if flags.contains(FnFlags::HAS_BODY) {
292+
wln!(self, " {{ ... }}");
293+
} else {
294+
wln!(self, ";");
295+
}
279296
}
280297
ModItem::Struct(it) => {
281298
let Struct { visibility, name, fields, generic_params, ast_id: _ } = &self.tree[it];

crates/hir_def/src/item_tree/tests.rs

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ use crate::{A, B};
3030
use a::{c, d::{e}};
3131
"#,
3232
expect![[r##"
33-
#![doc = " file comment"] // AttrId { ast_index: 0 }
34-
#![no_std] // AttrId { ast_index: 1 }
35-
#![doc = " another file comment"] // AttrId { ast_index: 2 }
33+
#![doc = " file comment"]
34+
#![no_std]
35+
#![doc = " another file comment"]
3636
3737
pub(self) extern crate self as renamed;
3838
@@ -42,7 +42,7 @@ use a::{c, d::{e}};
4242
4343
pub(self) use globs::*;
4444
45-
#[doc = " docs on import"] // AttrId { ast_index: 0 }
45+
#[doc = " docs on import"]
4646
pub(self) use crate::{A, B};
4747
4848
pub(self) use a::{c, d::{e}};
@@ -67,15 +67,15 @@ extern "C" {
6767
}
6868
"#,
6969
expect![[r##"
70-
#[on_extern_block] // AttrId { ast_index: 0 }
70+
#[on_extern_block]
7171
extern "C" {
72-
#[on_extern_type] // AttrId { ast_index: 0 }
72+
#[on_extern_type]
7373
pub(self) type ExType;
7474
75-
#[on_extern_static] // AttrId { ast_index: 0 }
75+
#[on_extern_static]
7676
pub(self) static EX_STATIC: u8 = _;
7777
78-
#[on_extern_fn] // AttrId { ast_index: 0 }
78+
#[on_extern_fn]
7979
pub(self) fn ex_fn() -> ();
8080
}
8181
"##]],
@@ -115,14 +115,14 @@ enum E {
115115
expect![[r##"
116116
pub(self) struct Unit;
117117
118-
#[derive(Debug)] // AttrId { ast_index: 0 }
118+
#[derive(Debug)]
119119
pub(self) struct Struct {
120-
#[doc = " fld docs"] // AttrId { ast_index: 0 }
120+
#[doc = " fld docs"]
121121
pub(self) fld: (),
122122
}
123123
124124
pub(self) struct Tuple(
125-
#[attr] // AttrId { ast_index: 0 }
125+
#[attr]
126126
pub(self) 0: u8,
127127
);
128128
@@ -132,14 +132,14 @@ enum E {
132132
}
133133
134134
pub(self) enum E {
135-
#[doc = " comment on Unit"] // AttrId { ast_index: 0 }
135+
#[doc = " comment on Unit"]
136136
Unit,
137-
#[doc = " comment on Tuple"] // AttrId { ast_index: 0 }
137+
#[doc = " comment on Tuple"]
138138
Tuple(
139139
pub(self) 0: u8,
140140
),
141141
Struct {
142-
#[doc = " comment on a: u8"] // AttrId { ast_index: 0 }
142+
#[doc = " comment on a: u8"]
143143
pub(self) a: u8,
144144
},
145145
}
@@ -170,14 +170,13 @@ trait Tr: SuperTrait + 'lifetime {
170170
171171
pub(self) const _: Anon = _;
172172
173-
#[attr] // AttrId { ast_index: 0 }
174-
#[inner_attr_in_fn] // AttrId { ast_index: 1 }
175-
// flags = 0x2
173+
#[attr]
174+
#[inner_attr_in_fn]
176175
pub(self) fn f(
177-
#[attr] // AttrId { ast_index: 0 }
176+
#[attr]
178177
arg: u8,
179178
_: (),
180-
) -> ();
179+
) -> () { ... }
181180
182181
pub(self) trait Tr<Self>
183182
where
@@ -186,9 +185,8 @@ trait Tr: SuperTrait + 'lifetime {
186185
{
187186
pub(self) type Assoc: AssocBound = Default;
188187
189-
// flags = 0x1
190188
pub(self) fn method(
191-
_: &Self,
189+
_: &Self, // self
192190
) -> ();
193191
}
194192
"##]],
@@ -211,13 +209,12 @@ mod inline {
211209
mod outline;
212210
"#,
213211
expect![[r##"
214-
#[doc = " outer"] // AttrId { ast_index: 0 }
215-
#[doc = " inner"] // AttrId { ast_index: 1 }
212+
#[doc = " outer"]
213+
#[doc = " inner"]
216214
pub(self) mod inline {
217215
pub(self) use super::*;
218216
219-
// flags = 0x2
220-
pub(self) fn fn_in_module() -> ();
217+
pub(self) fn fn_in_module() -> () { ... }
221218
}
222219
223220
pub(self) mod outline;
@@ -338,12 +335,11 @@ trait Tr<'a, T: 'a>: Super where Self: for<'a> Tr<'a, T> {}
338335
T: 'a,
339336
T: 'b
340337
{
341-
// flags = 0x2
342338
pub(self) fn f<G>(
343339
arg: impl Copy,
344340
) -> impl Copy
345341
where
346-
G: 'a;
342+
G: 'a { ... }
347343
}
348344
349345
pub(self) enum Enum<'a, T, const U: u8> {
@@ -392,10 +388,9 @@ pub(crate) trait Tr {
392388
pub(crate) trait Tr<Self> {
393389
pub(crate) fn f() -> ();
394390
395-
// flags = 0x3
396391
pub(crate) fn method(
397-
_: &Self,
398-
) -> ();
392+
_: &Self, // self
393+
) -> () { ... }
399394
}
400395
"#]],
401396
)

0 commit comments

Comments
 (0)