Skip to content

Commit acfc84d

Browse files
authored
Rollup merge of rust-lang#138376 - nnethercote:hir-ItemKind-ident-precursors, r=compiler-errors
Item-related cleanups I have been looking at `hir::Item` closely and found a few minor cleanup opportunities. r? ``@spastorino``
2 parents ae5ccbb + ee9ef82 commit acfc84d

File tree

3 files changed

+24
-56
lines changed

3 files changed

+24
-56
lines changed

compiler/rustc_hir/src/hir.rs

-10
Original file line numberDiff line numberDiff line change
@@ -4335,16 +4335,6 @@ pub enum OwnerNode<'hir> {
43354335
}
43364336

43374337
impl<'hir> OwnerNode<'hir> {
4338-
pub fn ident(&self) -> Option<Ident> {
4339-
match self {
4340-
OwnerNode::Item(Item { ident, .. })
4341-
| OwnerNode::ForeignItem(ForeignItem { ident, .. })
4342-
| OwnerNode::ImplItem(ImplItem { ident, .. })
4343-
| OwnerNode::TraitItem(TraitItem { ident, .. }) => Some(*ident),
4344-
OwnerNode::Crate(..) | OwnerNode::Synthetic => None,
4345-
}
4346-
}
4347-
43484338
pub fn span(&self) -> Span {
43494339
match self {
43504340
OwnerNode::Item(Item { span, .. })

compiler/rustc_hir_pretty/src/lib.rs

+11-22
Original file line numberDiff line numberDiff line change
@@ -552,24 +552,6 @@ impl<'a> State<'a> {
552552
self.word(";")
553553
}
554554

555-
fn print_item_type(
556-
&mut self,
557-
item: &hir::Item<'_>,
558-
generics: &hir::Generics<'_>,
559-
inner: impl Fn(&mut Self),
560-
) {
561-
self.head("type");
562-
self.print_ident(item.ident);
563-
self.print_generic_params(generics.params);
564-
self.end(); // end the inner ibox
565-
566-
self.print_where_clause(generics);
567-
self.space();
568-
inner(self);
569-
self.word(";");
570-
self.end(); // end the outer ibox
571-
}
572-
573555
fn print_item(&mut self, item: &hir::Item<'_>) {
574556
self.hardbreak_if_not_bol();
575557
self.maybe_print_comment(item.span.lo());
@@ -682,10 +664,17 @@ impl<'a> State<'a> {
682664
self.end()
683665
}
684666
hir::ItemKind::TyAlias(ty, generics) => {
685-
self.print_item_type(item, generics, |state| {
686-
state.word_space("=");
687-
state.print_type(ty);
688-
});
667+
self.head("type");
668+
self.print_ident(item.ident);
669+
self.print_generic_params(generics.params);
670+
self.end(); // end the inner ibox
671+
672+
self.print_where_clause(generics);
673+
self.space();
674+
self.word_space("=");
675+
self.print_type(ty);
676+
self.word(";");
677+
self.end(); // end the outer ibox
689678
}
690679
hir::ItemKind::Enum(ref enum_definition, params) => {
691680
self.print_enum_def(enum_definition, params, item.ident.name, item.span);

compiler/rustc_parse/src/parser/item.rs

+13-24
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ impl<'a> Parser<'a> {
646646

647647
let impl_items = self.parse_item_list(attrs, |p| p.parse_impl_item(ForceCollect::No))?;
648648

649-
let item_kind = match ty_second {
649+
let (of_trait, self_ty) = match ty_second {
650650
Some(ty_second) => {
651651
// impl Trait for Type
652652
if !has_for {
@@ -679,31 +679,20 @@ impl<'a> Parser<'a> {
679679
};
680680
let trait_ref = TraitRef { path, ref_id: ty_first.id };
681681

682-
ItemKind::Impl(Box::new(Impl {
683-
safety,
684-
polarity,
685-
defaultness,
686-
constness,
687-
generics,
688-
of_trait: Some(trait_ref),
689-
self_ty: ty_second,
690-
items: impl_items,
691-
}))
692-
}
693-
None => {
694-
// impl Type
695-
ItemKind::Impl(Box::new(Impl {
696-
safety,
697-
polarity,
698-
defaultness,
699-
constness,
700-
generics,
701-
of_trait: None,
702-
self_ty: ty_first,
703-
items: impl_items,
704-
}))
682+
(Some(trait_ref), ty_second)
705683
}
684+
None => (None, ty_first), // impl Type
706685
};
686+
let item_kind = ItemKind::Impl(Box::new(Impl {
687+
safety,
688+
polarity,
689+
defaultness,
690+
constness,
691+
generics,
692+
of_trait,
693+
self_ty,
694+
items: impl_items,
695+
}));
707696

708697
Ok((Ident::empty(), item_kind))
709698
}

0 commit comments

Comments
 (0)