From d7029d7e2dbd1b2b4f6578e632e2cef8bd4ba0cc Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 7 Mar 2025 16:38:39 +1100 Subject: [PATCH 1/3] Remove unused `OwnerNode::ident` method. --- compiler/rustc_hir/src/hir.rs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 53f0194091cdc..38af49971921b 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -4332,16 +4332,6 @@ pub enum OwnerNode<'hir> { } impl<'hir> OwnerNode<'hir> { - pub fn ident(&self) -> Option { - match self { - OwnerNode::Item(Item { ident, .. }) - | OwnerNode::ForeignItem(ForeignItem { ident, .. }) - | OwnerNode::ImplItem(ImplItem { ident, .. }) - | OwnerNode::TraitItem(TraitItem { ident, .. }) => Some(*ident), - OwnerNode::Crate(..) | OwnerNode::Synthetic => None, - } - } - pub fn span(&self) -> Span { match self { OwnerNode::Item(Item { span, .. }) From ed10418363ef580a08dc310b3108646d6742b470 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 7 Mar 2025 20:31:41 +1100 Subject: [PATCH 2/3] Inline and remove `State::print_item_type`. It has a single call site. The removal of the closure argument is a nice touch. --- compiler/rustc_hir_pretty/src/lib.rs | 33 ++++++++++------------------ 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index 163d9a1b9d975..ae624fc253f2f 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -553,24 +553,6 @@ impl<'a> State<'a> { self.word(";") } - fn print_item_type( - &mut self, - item: &hir::Item<'_>, - generics: &hir::Generics<'_>, - inner: impl Fn(&mut Self), - ) { - self.head("type"); - self.print_ident(item.ident); - self.print_generic_params(generics.params); - self.end(); // end the inner ibox - - self.print_where_clause(generics); - self.space(); - inner(self); - self.word(";"); - self.end(); // end the outer ibox - } - fn print_item(&mut self, item: &hir::Item<'_>) { self.hardbreak_if_not_bol(); self.maybe_print_comment(item.span.lo()); @@ -683,10 +665,17 @@ impl<'a> State<'a> { self.end() } hir::ItemKind::TyAlias(ty, generics) => { - self.print_item_type(item, generics, |state| { - state.word_space("="); - state.print_type(ty); - }); + self.head("type"); + self.print_ident(item.ident); + self.print_generic_params(generics.params); + self.end(); // end the inner ibox + + self.print_where_clause(generics); + self.space(); + self.word_space("="); + self.print_type(ty); + self.word(";"); + self.end(); // end the outer ibox } hir::ItemKind::Enum(ref enum_definition, params) => { self.print_enum_def(enum_definition, params, item.ident.name, item.span); From ee9ef8279572ef9e3b47aa446b812c6e02e41572 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 7 Mar 2025 20:41:31 +1100 Subject: [PATCH 3/3] Factor out some repeated code in `parse_item_impl`. --- compiler/rustc_parse/src/parser/item.rs | 37 +++++++++---------------- 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index e309f144b4f41..9e6cdfe59bba3 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -646,7 +646,7 @@ impl<'a> Parser<'a> { let impl_items = self.parse_item_list(attrs, |p| p.parse_impl_item(ForceCollect::No))?; - let item_kind = match ty_second { + let (of_trait, self_ty) = match ty_second { Some(ty_second) => { // impl Trait for Type if !has_for { @@ -679,31 +679,20 @@ impl<'a> Parser<'a> { }; let trait_ref = TraitRef { path, ref_id: ty_first.id }; - ItemKind::Impl(Box::new(Impl { - safety, - polarity, - defaultness, - constness, - generics, - of_trait: Some(trait_ref), - self_ty: ty_second, - items: impl_items, - })) - } - None => { - // impl Type - ItemKind::Impl(Box::new(Impl { - safety, - polarity, - defaultness, - constness, - generics, - of_trait: None, - self_ty: ty_first, - items: impl_items, - })) + (Some(trait_ref), ty_second) } + None => (None, ty_first), // impl Type }; + let item_kind = ItemKind::Impl(Box::new(Impl { + safety, + polarity, + defaultness, + constness, + generics, + of_trait, + self_ty, + items: impl_items, + })); Ok((Ident::empty(), item_kind)) }