Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a8ff647

Browse files
committed
Separate out a hir::Impl struct
This makes it possible to pass the `Impl` directly to functions, instead of having to pass each of the many fields one at a time. It also simplifies matches in many cases.
1 parent fd34606 commit a8ff647

File tree

61 files changed

+260
-248
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+260
-248
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
134134
let old_len = self.in_scope_lifetimes.len();
135135

136136
let parent_generics = match self.items.get(&parent_hir_id).unwrap().kind {
137-
hir::ItemKind::Impl { ref generics, .. }
137+
hir::ItemKind::Impl(hir::Impl { ref generics, .. })
138138
| hir::ItemKind::Trait(_, _, ref generics, ..) => &generics.params[..],
139139
_ => &[],
140140
};
@@ -431,7 +431,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
431431
// to not cause an assertion failure inside the `lower_defaultness` function.
432432
let has_val = true;
433433
let (defaultness, defaultness_span) = self.lower_defaultness(defaultness, has_val);
434-
hir::ItemKind::Impl {
434+
hir::ItemKind::Impl(hir::Impl {
435435
unsafety: self.lower_unsafety(unsafety),
436436
polarity,
437437
defaultness,
@@ -441,7 +441,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
441441
of_trait: trait_ref,
442442
self_ty: lowered_ty,
443443
items: new_impl_items,
444-
}
444+
})
445445
}
446446
ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref items) => {
447447
let bounds = self.lower_param_bounds(bounds, ImplTraitContext::disallowed());

compiler/rustc_hir/src/hir.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2562,22 +2562,25 @@ pub enum ItemKind<'hir> {
25622562
TraitAlias(Generics<'hir>, GenericBounds<'hir>),
25632563

25642564
/// An implementation, e.g., `impl<A> Trait for Foo { .. }`.
2565-
Impl {
2566-
unsafety: Unsafety,
2567-
polarity: ImplPolarity,
2568-
defaultness: Defaultness,
2569-
// We do not put a `Span` in `Defaultness` because it breaks foreign crate metadata
2570-
// decoding as `Span`s cannot be decoded when a `Session` is not available.
2571-
defaultness_span: Option<Span>,
2572-
constness: Constness,
2573-
generics: Generics<'hir>,
2574-
2575-
/// The trait being implemented, if any.
2576-
of_trait: Option<TraitRef<'hir>>,
2577-
2578-
self_ty: &'hir Ty<'hir>,
2579-
items: &'hir [ImplItemRef<'hir>],
2580-
},
2565+
Impl(Impl<'hir>),
2566+
}
2567+
2568+
#[derive(Debug, HashStable_Generic)]
2569+
pub struct Impl<'hir> {
2570+
pub unsafety: Unsafety,
2571+
pub polarity: ImplPolarity,
2572+
pub defaultness: Defaultness,
2573+
// We do not put a `Span` in `Defaultness` because it breaks foreign crate metadata
2574+
// decoding as `Span`s cannot be decoded when a `Session` is not available.
2575+
pub defaultness_span: Option<Span>,
2576+
pub constness: Constness,
2577+
pub generics: Generics<'hir>,
2578+
2579+
/// The trait being implemented, if any.
2580+
pub of_trait: Option<TraitRef<'hir>>,
2581+
2582+
pub self_ty: &'hir Ty<'hir>,
2583+
pub items: &'hir [ImplItemRef<'hir>],
25812584
}
25822585

25832586
impl ItemKind<'_> {
@@ -2590,7 +2593,7 @@ impl ItemKind<'_> {
25902593
| ItemKind::Struct(_, ref generics)
25912594
| ItemKind::Union(_, ref generics)
25922595
| ItemKind::Trait(_, _, ref generics, _, _)
2593-
| ItemKind::Impl { ref generics, .. } => generics,
2596+
| ItemKind::Impl(Impl { ref generics, .. }) => generics,
25942597
_ => return None,
25952598
})
25962599
}

compiler/rustc_hir/src/intravisit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
611611
// `visit_enum_def()` takes care of visiting the `Item`'s `HirId`.
612612
visitor.visit_enum_def(enum_definition, generics, item.hir_id, item.span)
613613
}
614-
ItemKind::Impl {
614+
ItemKind::Impl(Impl {
615615
unsafety: _,
616616
defaultness: _,
617617
polarity: _,
@@ -621,7 +621,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
621621
ref of_trait,
622622
ref self_ty,
623623
items,
624-
} => {
624+
}) => {
625625
visitor.visit_id(item.hir_id);
626626
visitor.visit_generics(generics);
627627
walk_list!(visitor, visit_trait_ref, of_trait);

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ impl<'a> State<'a> {
684684
self.head(visibility_qualified(&item.vis, "union"));
685685
self.print_struct(struct_def, generics, item.ident.name, item.span, true);
686686
}
687-
hir::ItemKind::Impl {
687+
hir::ItemKind::Impl(hir::Impl {
688688
unsafety,
689689
polarity,
690690
defaultness,
@@ -694,7 +694,7 @@ impl<'a> State<'a> {
694694
ref of_trait,
695695
ref self_ty,
696696
items,
697-
} => {
697+
}) => {
698698
self.head("");
699699
self.print_visibility(&item.vis);
700700
self.print_defaultness(defaultness);

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,10 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
345345
match tcx.hir().get_if_local(def_id) {
346346
Some(Node::ImplItem(ImplItem { ident, hir_id, .. })) => {
347347
match tcx.hir().find(tcx.hir().get_parent_item(*hir_id)) {
348-
Some(Node::Item(Item { kind: ItemKind::Impl { self_ty, .. }, .. })) => {
349-
Some((*ident, self_ty))
350-
}
348+
Some(Node::Item(Item {
349+
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
350+
..
351+
})) => Some((*ident, self_ty)),
351352
_ => None,
352353
}
353354
}
@@ -367,7 +368,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
367368
let impl_did = tcx.hir().local_def_id(*impl_node);
368369
match tcx.hir().get_if_local(impl_did.to_def_id()) {
369370
Some(Node::Item(Item {
370-
kind: ItemKind::Impl { self_ty, .. },
371+
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
371372
..
372373
})) if trait_objects.iter().all(|did| {
373374
// FIXME: we should check `self_ty` against the receiver

compiler/rustc_lint/src/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
541541
return;
542542
}
543543
}
544-
hir::ItemKind::Impl { of_trait: Some(ref trait_ref), items, .. } => {
544+
hir::ItemKind::Impl(hir::Impl { of_trait: Some(ref trait_ref), items, .. }) => {
545545
// If the trait is private, add the impl items to `private_traits` so they don't get
546546
// reported for missing docs.
547547
let real_trait = trait_ref.path.res.def_id();

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,7 @@ impl EncodeContext<'a, 'tcx> {
12591259
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
12601260
}), adt_def.repr)
12611261
}
1262-
hir::ItemKind::Impl { defaultness, .. } => {
1262+
hir::ItemKind::Impl(hir::Impl { defaultness, .. }) => {
12631263
let trait_ref = self.tcx.impl_trait_ref(def_id);
12641264
let polarity = self.tcx.impl_polarity(def_id);
12651265
let parent = if let Some(trait_ref) = trait_ref {

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ impl<'hir> Map<'hir> {
505505
| ItemKind::Union(_, generics)
506506
| ItemKind::Trait(_, _, generics, ..)
507507
| ItemKind::TraitAlias(generics, _)
508-
| ItemKind::Impl { generics, .. },
508+
| ItemKind::Impl(Impl { generics, .. }),
509509
..
510510
}) => Some(generics),
511511
_ => None,

compiler/rustc_middle/src/ty/error.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,8 @@ fn foo(&self) -> Self::T { String::new() }
829829
}
830830
}
831831
Some(hir::Node::Item(hir::Item {
832-
kind: hir::ItemKind::Impl { items, .. }, ..
832+
kind: hir::ItemKind::Impl(hir::Impl { items, .. }),
833+
..
833834
})) => {
834835
for item in &items[..] {
835836
if let hir::AssocItemKind::Type = item.kind {

compiler/rustc_mir/src/const_eval/fn_queries.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ fn is_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
126126
matches!(
127127
node,
128128
hir::Node::Item(hir::Item {
129-
kind: hir::ItemKind::Impl { constness: hir::Constness::Const, .. },
129+
kind: hir::ItemKind::Impl(hir::Impl { constness: hir::Constness::Const, .. }),
130130
..
131131
})
132132
)

0 commit comments

Comments
 (0)