Skip to content

Commit 150d1fe

Browse files
committed
Auto merge of #79322 - jyn514:refactor-impl, r=estebank
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. See `rustc_save_analysis::dump_visitor::process_impl` or `rustdoc::clean::clean_impl` for a good example of how this makes `impl`s easier to work with. r? `@petrochenkov` maybe?
2 parents 058a710 + a8ff647 commit 150d1fe

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

+3-3
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

+20-17
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

+2-2
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

+2-2
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

+5-4
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

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

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,7 @@ impl EncodeContext<'a, 'tcx> {
12831283
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
12841284
}), adt_def.repr)
12851285
}
1286-
hir::ItemKind::Impl { defaultness, .. } => {
1286+
hir::ItemKind::Impl(hir::Impl { defaultness, .. }) => {
12871287
let trait_ref = self.tcx.impl_trait_ref(def_id);
12881288
let polarity = self.tcx.impl_polarity(def_id);
12891289
let parent = if let Some(trait_ref) = trait_ref {

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

+1-1
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

+2-1
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

+1-1
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
)

compiler/rustc_mir/src/monomorphize/collector.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1146,8 +1146,8 @@ fn create_mono_items_for_default_impls<'tcx>(
11461146
output: &mut Vec<Spanned<MonoItem<'tcx>>>,
11471147
) {
11481148
match item.kind {
1149-
hir::ItemKind::Impl { ref generics, ref items, .. } => {
1150-
for param in generics.params {
1149+
hir::ItemKind::Impl(ref impl_) => {
1150+
for param in impl_.generics.params {
11511151
match param.kind {
11521152
hir::GenericParamKind::Lifetime { .. } => {}
11531153
hir::GenericParamKind::Type { .. } | hir::GenericParamKind::Const { .. } => {
@@ -1167,7 +1167,7 @@ fn create_mono_items_for_default_impls<'tcx>(
11671167
let param_env = ty::ParamEnv::reveal_all();
11681168
let trait_ref = tcx.normalize_erasing_regions(param_env, trait_ref);
11691169
let overridden_methods: FxHashSet<_> =
1170-
items.iter().map(|iiref| iiref.ident.normalize_to_macros_2_0()).collect();
1170+
impl_.items.iter().map(|iiref| iiref.ident.normalize_to_macros_2_0()).collect();
11711171
for method in tcx.provided_trait_methods(trait_ref.def_id) {
11721172
if overridden_methods.contains(&method.ident.normalize_to_macros_2_0()) {
11731173
continue;

compiler/rustc_passes/src/check_attr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub(crate) fn target_from_impl_item<'tcx>(
3232
let parent_hir_id = tcx.hir().get_parent_item(impl_item.hir_id);
3333
let containing_item = tcx.hir().expect_item(parent_hir_id);
3434
let containing_impl_is_for_trait = match &containing_item.kind {
35-
hir::ItemKind::Impl { ref of_trait, .. } => of_trait.is_some(),
35+
hir::ItemKind::Impl(impl_) => impl_.of_trait.is_some(),
3636
_ => bug!("parent of an ImplItem must be an Impl"),
3737
};
3838
if containing_impl_is_for_trait {
@@ -343,7 +343,7 @@ impl CheckAttrVisitor<'tcx> {
343343
// We can't link to trait impl's consts.
344344
let err = "associated constant in trait implementation block";
345345
match containing_item.kind {
346-
ItemKind::Impl { of_trait: Some(_), .. } => Some(err),
346+
ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) => Some(err),
347347
_ => None,
348348
}
349349
}

compiler/rustc_passes/src/dead.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ impl<'v, 'k, 'tcx> ItemLikeVisitor<'v> for LifeSeeder<'k, 'tcx> {
396396
}
397397
}
398398
}
399-
hir::ItemKind::Impl { ref of_trait, items, .. } => {
399+
hir::ItemKind::Impl(hir::Impl { ref of_trait, items, .. }) => {
400400
if of_trait.is_some() {
401401
self.worklist.push(item.hir_id);
402402
}

compiler/rustc_passes/src/reachable.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,9 @@ impl<'a, 'tcx> ItemLikeVisitor<'tcx> for CollectPrivateImplItemsVisitor<'a, 'tcx
349349
}
350350

351351
// We need only trait impls here, not inherent impls, and only non-exported ones
352-
if let hir::ItemKind::Impl { of_trait: Some(ref trait_ref), ref items, .. } = item.kind {
352+
if let hir::ItemKind::Impl(hir::Impl { of_trait: Some(ref trait_ref), ref items, .. }) =
353+
item.kind
354+
{
353355
if !self.access_levels.is_reachable(item.hir_id) {
354356
// FIXME(#53488) remove `let`
355357
let tcx = self.tcx;

compiler/rustc_passes/src/stability.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,12 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
330330
// they don't have their own stability. They still can be annotated as unstable
331331
// and propagate this unstability to children, but this annotation is completely
332332
// optional. They inherit stability from their parents when unannotated.
333-
hir::ItemKind::Impl { of_trait: None, .. } | hir::ItemKind::ForeignMod { .. } => {
333+
hir::ItemKind::Impl(hir::Impl { of_trait: None, .. })
334+
| hir::ItemKind::ForeignMod { .. } => {
334335
self.in_trait_impl = false;
335336
kind = AnnotationKind::Container;
336337
}
337-
hir::ItemKind::Impl { of_trait: Some(_), .. } => {
338+
hir::ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) => {
338339
self.in_trait_impl = true;
339340
kind = AnnotationKind::DeprecationProhibited;
340341
}
@@ -503,7 +504,7 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
503504
// optional. They inherit stability from their parents when unannotated.
504505
if !matches!(
505506
i.kind,
506-
hir::ItemKind::Impl { of_trait: None, .. } | hir::ItemKind::ForeignMod { .. }
507+
hir::ItemKind::Impl(hir::Impl { of_trait: None, .. }) | hir::ItemKind::ForeignMod { .. }
507508
) {
508509
self.check_missing_stability(i.hir_id, i.span);
509510
}
@@ -672,7 +673,7 @@ impl Visitor<'tcx> for Checker<'tcx> {
672673
// For implementations of traits, check the stability of each item
673674
// individually as it's possible to have a stable trait with unstable
674675
// items.
675-
hir::ItemKind::Impl { of_trait: Some(ref t), self_ty, items, .. } => {
676+
hir::ItemKind::Impl(hir::Impl { of_trait: Some(ref t), self_ty, items, .. }) => {
676677
if self.tcx.features().staged_api {
677678
// If this impl block has an #[unstable] attribute, give an
678679
// error if all involved types and traits are stable, because

0 commit comments

Comments
 (0)