]> git.lizzy.rs Git - rust.git/commitdiff
Improve ItemTree pretty print output
authorJonas Schievink <jonas.schievink@ferrous-systems.com>
Thu, 14 Apr 2022 12:24:27 +0000 (14:24 +0200)
committerJonas Schievink <jonas.schievink@ferrous-systems.com>
Thu, 14 Apr 2022 12:24:27 +0000 (14:24 +0200)
crates/hir_def/src/item_tree/pretty.rs
crates/hir_def/src/item_tree/tests.rs

index b24ba61ea069fd49d30807a60c4d50aa0a29b0b9..f12d9a1273cc16bba0c6e67c3e0cbcbbeafb4bd9 100644 (file)
@@ -90,11 +90,10 @@ fn print_attrs(&mut self, attrs: &RawAttrs, inner: bool) {
         for attr in &**attrs {
             wln!(
                 self,
-                "#{}[{}{}]  // {:?}",
+                "#{}[{}{}]",
                 inner,
                 attr.path,
                 attr.input.as_ref().map(|it| it.to_string()).unwrap_or_default(),
-                attr.id,
             );
         }
     }
@@ -242,10 +241,19 @@ fn print_mod_item(&mut self, item: ModItem) {
                     ast_id: _,
                     flags,
                 } = &self.tree[it];
-                if flags.bits != 0 {
-                    wln!(self, "// flags = 0x{:X}", flags.bits);
-                }
                 self.print_visibility(*visibility);
+                if flags.contains(FnFlags::HAS_DEFAULT_KW) {
+                    w!(self, "default ");
+                }
+                if flags.contains(FnFlags::HAS_CONST_KW) {
+                    w!(self, "const ");
+                }
+                if flags.contains(FnFlags::HAS_ASYNC_KW) {
+                    w!(self, "async ");
+                }
+                if flags.contains(FnFlags::HAS_UNSAFE_KW) {
+                    w!(self, "unsafe ");
+                }
                 if let Some(abi) = abi {
                     w!(self, "extern \"{}\" ", abi);
                 }
@@ -254,7 +262,7 @@ fn print_mod_item(&mut self, item: ModItem) {
                 w!(self, "(");
                 if !params.is_empty() {
                     self.indented(|this| {
-                        for param in params.clone() {
+                        for (i, param) in params.clone().enumerate() {
                             this.print_attrs_of(param);
                             match &this.tree[param] {
                                 Param::Normal(name, ty) => {
@@ -263,7 +271,12 @@ fn print_mod_item(&mut self, item: ModItem) {
                                         None => w!(this, "_: "),
                                     }
                                     this.print_type_ref(ty);
-                                    wln!(this, ",");
+                                    w!(this, ",");
+                                    if flags.contains(FnFlags::HAS_SELF_PARAM) && i == 0 {
+                                        wln!(this, "  // self");
+                                    } else {
+                                        wln!(this);
+                                    }
                                 }
                                 Param::Varargs => {
                                     wln!(this, "...");
@@ -275,7 +288,11 @@ fn print_mod_item(&mut self, item: ModItem) {
                 w!(self, ") -> ");
                 self.print_type_ref(ret_type);
                 self.print_where_clause(explicit_generic_params);
-                wln!(self, ";");
+                if flags.contains(FnFlags::HAS_BODY) {
+                    wln!(self, " {{ ... }}");
+                } else {
+                    wln!(self, ";");
+                }
             }
             ModItem::Struct(it) => {
                 let Struct { visibility, name, fields, generic_params, ast_id: _ } = &self.tree[it];
index 65352e74440917f9b2e0021d6f602c1a1bfa7711..fb3811dbd56b6cbd3894d69549c8333dfc7f0e76 100644 (file)
@@ -30,9 +30,9 @@ fn imports() {
 use a::{c, d::{e}};
         "#,
         expect![[r##"
-            #![doc = " file comment"]  // AttrId { ast_index: 0 }
-            #![no_std]  // AttrId { ast_index: 1 }
-            #![doc = " another file comment"]  // AttrId { ast_index: 2 }
+            #![doc = " file comment"]
+            #![no_std]
+            #![doc = " another file comment"]
 
             pub(self) extern crate self as renamed;
 
@@ -42,7 +42,7 @@ fn imports() {
 
             pub(self) use globs::*;
 
-            #[doc = " docs on import"]  // AttrId { ast_index: 0 }
+            #[doc = " docs on import"]
             pub(self) use crate::{A, B};
 
             pub(self) use a::{c, d::{e}};
@@ -67,15 +67,15 @@ fn extern_blocks() {
 }
         "#,
         expect![[r##"
-            #[on_extern_block]  // AttrId { ast_index: 0 }
+            #[on_extern_block]
             extern "C" {
-                #[on_extern_type]  // AttrId { ast_index: 0 }
+                #[on_extern_type]
                 pub(self) type ExType;
 
-                #[on_extern_static]  // AttrId { ast_index: 0 }
+                #[on_extern_static]
                 pub(self) static EX_STATIC: u8 = _;
 
-                #[on_extern_fn]  // AttrId { ast_index: 0 }
+                #[on_extern_fn]
                 pub(self) fn ex_fn() -> ();
             }
         "##]],
@@ -115,14 +115,14 @@ enum E {
         expect![[r##"
             pub(self) struct Unit;
 
-            #[derive(Debug)]  // AttrId { ast_index: 0 }
+            #[derive(Debug)]
             pub(self) struct Struct {
-                #[doc = " fld docs"]  // AttrId { ast_index: 0 }
+                #[doc = " fld docs"]
                 pub(self) fld: (),
             }
 
             pub(self) struct Tuple(
-                #[attr]  // AttrId { ast_index: 0 }
+                #[attr]
                 pub(self) 0: u8,
             );
 
@@ -132,14 +132,14 @@ pub(self) union Ize {
             }
 
             pub(self) enum E {
-                #[doc = " comment on Unit"]  // AttrId { ast_index: 0 }
+                #[doc = " comment on Unit"]
                 Unit,
-                #[doc = " comment on Tuple"]  // AttrId { ast_index: 0 }
+                #[doc = " comment on Tuple"]
                 Tuple(
                     pub(self) 0: u8,
                 ),
                 Struct {
-                    #[doc = " comment on a: u8"]  // AttrId { ast_index: 0 }
+                    #[doc = " comment on a: u8"]
                     pub(self) a: u8,
                 },
             }
@@ -170,14 +170,13 @@ trait Tr: SuperTrait + 'lifetime {
 
             pub(self) const _: Anon = _;
 
-            #[attr]  // AttrId { ast_index: 0 }
-            #[inner_attr_in_fn]  // AttrId { ast_index: 1 }
-            // flags = 0x2
+            #[attr]
+            #[inner_attr_in_fn]
             pub(self) fn f(
-                #[attr]  // AttrId { ast_index: 0 }
+                #[attr]
                 arg: u8,
                 _: (),
-            ) -> ();
+            ) -> () { ... }
 
             pub(self) trait Tr<Self>
             where
@@ -186,9 +185,8 @@ pub(self) trait Tr<Self>
             {
                 pub(self) type Assoc: AssocBound = Default;
 
-                // flags = 0x1
                 pub(self) fn method(
-                    _: &Self,
+                    _: &Self,  // self
                 ) -> ();
             }
         "##]],
@@ -211,13 +209,12 @@ fn fn_in_module() {}
 mod outline;
         "#,
         expect![[r##"
-            #[doc = " outer"]  // AttrId { ast_index: 0 }
-            #[doc = " inner"]  // AttrId { ast_index: 1 }
+            #[doc = " outer"]
+            #[doc = " inner"]
             pub(self) mod inline {
                 pub(self) use super::*;
 
-                // flags = 0x2
-                pub(self) fn fn_in_module() -> ();
+                pub(self) fn fn_in_module() -> () { ... }
             }
 
             pub(self) mod outline;
@@ -338,12 +335,11 @@ impl<'a, 'b, T, const K: u8> S<'a, 'b, T, K>
                 T: 'a,
                 T: 'b
             {
-                // flags = 0x2
                 pub(self) fn f<G>(
                     arg: impl Copy,
                 ) -> impl Copy
                 where
-                    G: 'a;
+                    G: 'a { ... }
             }
 
             pub(self) enum Enum<'a, T, const U: u8> {
@@ -392,10 +388,9 @@ pub(crate) enum En {
             pub(crate) trait Tr<Self> {
                 pub(crate) fn f() -> ();
 
-                // flags = 0x3
                 pub(crate) fn method(
-                    _: &Self,
-                ) -> ();
+                    _: &Self,  // self
+                ) -> () { ... }
             }
         "#]],
     )