Skip to content

Commit fca3264

Browse files
authored
Rollup merge of rust-lang#68204 - ecstatic-morse:item-kind-impl, r=oli-obk
Use named fields for `{ast,hir}::ItemKind::Impl` Currently, the widely used `ItemKind::Impl` variant is a tuple with seven fields. I want to add an eighth in rust-lang#68140, which means I have to update most matches on this variant anyways. Giving a name to each field improves readability and makes future changes of this nature much simpler. This change will cause several tools to break. I will fix them once this is merged.
2 parents bafe089 + 4743995 commit fca3264

File tree

48 files changed

+296
-255
lines changed

Some content is hidden

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

48 files changed

+296
-255
lines changed

src/librustc/hir/check_attr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl Target {
112112
ItemKind::Union(..) => Target::Union,
113113
ItemKind::Trait(..) => Target::Trait,
114114
ItemKind::TraitAlias(..) => Target::TraitAlias,
115-
ItemKind::Impl(..) => Target::Impl,
115+
ItemKind::Impl { .. } => Target::Impl,
116116
}
117117
}
118118

@@ -144,7 +144,7 @@ impl Target {
144144
let parent_hir_id = tcx.hir().get_parent_item(impl_item.hir_id);
145145
let containing_item = tcx.hir().expect_item(parent_hir_id);
146146
let containing_impl_is_for_trait = match &containing_item.kind {
147-
hir::ItemKind::Impl(_, _, _, _, tr, _, _) => tr.is_some(),
147+
hir::ItemKind::Impl { ref of_trait, .. } => of_trait.is_some(),
148148
_ => bug!("parent of an ImplItem must be an Impl"),
149149
};
150150
if containing_impl_is_for_trait {

src/librustc/hir/map/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ impl<'hir> Map<'hir> {
341341
| ItemKind::Use(..)
342342
| ItemKind::ForeignMod(..)
343343
| ItemKind::GlobalAsm(..)
344-
| ItemKind::Impl(..) => return None,
344+
| ItemKind::Impl { .. } => return None,
345345
},
346346
Node::ForeignItem(item) => match item.kind {
347347
ForeignItemKind::Fn(..) => DefKind::Fn,
@@ -604,7 +604,7 @@ impl<'hir> Map<'hir> {
604604
| ItemKind::Union(_, ref generics)
605605
| ItemKind::Trait(_, _, ref generics, ..)
606606
| ItemKind::TraitAlias(ref generics, _)
607-
| ItemKind::Impl(_, _, _, ref generics, ..) => Some(generics),
607+
| ItemKind::Impl { ref generics, .. } => Some(generics),
608608
_ => None,
609609
},
610610
_ => None,
@@ -821,7 +821,7 @@ impl<'hir> Map<'hir> {
821821
| ItemKind::Struct(..)
822822
| ItemKind::Union(..)
823823
| ItemKind::Trait(..)
824-
| ItemKind::Impl(..) => true,
824+
| ItemKind::Impl { .. } => true,
825825
_ => false,
826826
},
827827
Node::ForeignItem(fi) => match fi.kind {
@@ -1332,7 +1332,7 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
13321332
ItemKind::Union(..) => "union",
13331333
ItemKind::Trait(..) => "trait",
13341334
ItemKind::TraitAlias(..) => "trait alias",
1335-
ItemKind::Impl(..) => "impl",
1335+
ItemKind::Impl { .. } => "impl",
13361336
};
13371337
format!("{} {}{}", item_str, path_str(), id_str)
13381338
}

src/librustc/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ fn emit_msg_span(
254254

255255
fn item_scope_tag(item: &hir::Item<'_>) -> &'static str {
256256
match item.kind {
257-
hir::ItemKind::Impl(..) => "impl",
257+
hir::ItemKind::Impl { .. } => "impl",
258258
hir::ItemKind::Struct(..) => "struct",
259259
hir::ItemKind::Union(..) => "union",
260260
hir::ItemKind::Enum(..) => "enum",

src/librustc/traits/error_reporting/suggestions.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
8888
..
8989
})
9090
| hir::Node::Item(hir::Item {
91-
kind: hir::ItemKind::Impl(_, _, _, generics, ..),
92-
..
91+
kind: hir::ItemKind::Impl { generics, .. }, ..
9392
}) if projection.is_some() => {
9493
// Missing associated type bound.
9594
suggest_restriction(&generics, "the associated type", err);
@@ -115,7 +114,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
115114
..
116115
})
117116
| hir::Node::Item(hir::Item {
118-
kind: hir::ItemKind::Impl(_, _, _, generics, ..),
117+
kind: hir::ItemKind::Impl { generics, .. },
119118
span,
120119
..
121120
})

src/librustc/traits/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ pub fn impl_is_default(tcx: TyCtxt<'_>, node_item_def_id: DefId) -> bool {
651651
match tcx.hir().as_local_hir_id(node_item_def_id) {
652652
Some(hir_id) => {
653653
let item = tcx.hir().expect_item(hir_id);
654-
if let hir::ItemKind::Impl(_, _, defaultness, ..) = item.kind {
654+
if let hir::ItemKind::Impl { defaultness, .. } = item.kind {
655655
defaultness.is_default()
656656
} else {
657657
false

src/librustc/traits/wf.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
229229
// |
230230
// = note: expected type `u32`
231231
// found type `()`
232-
if let Some(hir::ItemKind::Impl(.., impl_items)) = item.map(|i| &i.kind) {
232+
if let Some(hir::ItemKind::Impl { items, .. }) = item.map(|i| &i.kind) {
233233
let trait_assoc_item = tcx.associated_item(proj.projection_def_id());
234-
if let Some(impl_item) = impl_items
234+
if let Some(impl_item) = items
235235
.iter()
236236
.filter(|item| item.ident == trait_assoc_item.ident)
237237
.next()
@@ -279,14 +279,14 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
279279
// | ^^^^^^^^^^^^^^^^^^ the trait `Bar` is not implemented for `bool`
280280
if let (
281281
ty::Projection(ty::ProjectionTy { item_def_id, .. }),
282-
Some(hir::ItemKind::Impl(.., impl_items)),
282+
Some(hir::ItemKind::Impl { items, .. }),
283283
) = (&proj.skip_binder().self_ty().kind, item.map(|i| &i.kind))
284284
{
285285
if let Some((impl_item, trait_assoc_item)) = trait_assoc_items
286286
.filter(|i| i.def_id == *item_def_id)
287287
.next()
288288
.and_then(|trait_assoc_item| {
289-
impl_items
289+
items
290290
.iter()
291291
.filter(|i| i.ident == trait_assoc_item.ident)
292292
.next()

src/librustc_ast_lowering/item.rs

+18-17
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,15 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
6767
if let Some(hir_id) = item_hir_id {
6868
self.lctx.with_parent_item_lifetime_defs(hir_id, |this| {
6969
let this = &mut ItemLowerer { lctx: this };
70-
if let ItemKind::Impl(.., ref opt_trait_ref, _, _) = item.kind {
71-
if opt_trait_ref.as_ref().map(|tr| tr.constness.is_some()).unwrap_or(false) {
70+
if let ItemKind::Impl { ref of_trait, .. } = item.kind {
71+
if of_trait.as_ref().map(|tr| tr.constness.is_some()).unwrap_or(false) {
72+
this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item));
7273
this.lctx
7374
.diagnostic()
7475
.span_err(item.span, "const trait impls are not yet implemented");
7576
}
7677

77-
this.with_trait_impl_ref(opt_trait_ref, |this| visit::walk_item(this, item));
78+
this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item));
7879
} else {
7980
visit::walk_item(this, item);
8081
}
@@ -118,7 +119,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
118119
let old_len = self.in_scope_lifetimes.len();
119120

120121
let parent_generics = match self.items.get(&parent_hir_id).unwrap().kind {
121-
hir::ItemKind::Impl(_, _, _, ref generics, ..)
122+
hir::ItemKind::Impl { ref generics, .. }
122123
| hir::ItemKind::Trait(_, _, ref generics, ..) => &generics.params[..],
123124
_ => &[],
124125
};
@@ -173,7 +174,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
173174
vec
174175
}
175176
ItemKind::MacroDef(..) => SmallVec::new(),
176-
ItemKind::Fn(..) | ItemKind::Impl(.., None, _, _) => smallvec![i.id],
177+
ItemKind::Fn(..) | ItemKind::Impl { of_trait: None, .. } => smallvec![i.id],
177178
ItemKind::Static(ref ty, ..) => {
178179
let mut ids = smallvec![i.id];
179180
if self.sess.features_untracked().impl_trait_in_bindings {
@@ -361,15 +362,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
361362
self.lower_generics(generics, ImplTraitContext::disallowed()),
362363
)
363364
}
364-
ItemKind::Impl(
365+
ItemKind::Impl {
365366
unsafety,
366367
polarity,
367368
defaultness,
368-
ref ast_generics,
369-
ref trait_ref,
370-
ref ty,
371-
ref impl_items,
372-
) => {
369+
generics: ref ast_generics,
370+
of_trait: ref trait_ref,
371+
self_ty: ref ty,
372+
items: ref impl_items,
373+
} => {
373374
let def_id = self.resolver.definitions().local_def_id(id);
374375

375376
// Lower the "impl header" first. This ordering is important
@@ -417,15 +418,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
417418
)
418419
});
419420

420-
hir::ItemKind::Impl(
421+
hir::ItemKind::Impl {
421422
unsafety,
422423
polarity,
423-
self.lower_defaultness(defaultness, true /* [1] */),
424+
defaultness: self.lower_defaultness(defaultness, true /* [1] */),
424425
generics,
425-
trait_ref,
426-
lowered_ty,
427-
new_impl_items,
428-
)
426+
of_trait: trait_ref,
427+
self_ty: lowered_ty,
428+
items: new_impl_items,
429+
}
429430
}
430431
ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref items) => {
431432
let bounds = self.lower_param_bounds(bounds, ImplTraitContext::disallowed());

src/librustc_ast_passes/ast_validation.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -612,9 +612,17 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
612612
}
613613

614614
match item.kind {
615-
ItemKind::Impl(unsafety, polarity, _, _, Some(..), ref ty, ref impl_items) => {
615+
ItemKind::Impl {
616+
unsafety,
617+
polarity,
618+
defaultness: _,
619+
generics: _,
620+
of_trait: Some(_),
621+
ref self_ty,
622+
ref items,
623+
} => {
616624
self.invalid_visibility(&item.vis, None);
617-
if let TyKind::Err = ty.kind {
625+
if let TyKind::Err = self_ty.kind {
618626
self.err_handler()
619627
.struct_span_err(item.span, "`impl Trait for .. {}` is an obsolete syntax")
620628
.help("use `auto trait Trait {}` instead")
@@ -629,15 +637,23 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
629637
)
630638
.emit();
631639
}
632-
for impl_item in impl_items {
640+
for impl_item in items {
633641
self.invalid_visibility(&impl_item.vis, None);
634642
if let AssocItemKind::Fn(ref sig, _) = impl_item.kind {
635643
self.check_trait_fn_not_const(sig.header.constness);
636644
self.check_trait_fn_not_async(impl_item.span, sig.header.asyncness.node);
637645
}
638646
}
639647
}
640-
ItemKind::Impl(unsafety, polarity, defaultness, _, None, _, _) => {
648+
ItemKind::Impl {
649+
unsafety,
650+
polarity,
651+
defaultness,
652+
generics: _,
653+
of_trait: None,
654+
self_ty: _,
655+
items: _,
656+
} => {
641657
self.invalid_visibility(
642658
&item.vis,
643659
Some("place qualifiers on individual impl items instead"),

src/librustc_ast_passes/feature_gate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
339339
}
340340
}
341341

342-
ast::ItemKind::Impl(_, polarity, defaultness, ..) => {
342+
ast::ItemKind::Impl { polarity, defaultness, .. } => {
343343
if polarity == ast::ImplPolarity::Negative {
344344
gate_feature_post!(
345345
&self,

src/librustc_builtin_macros/deriving/generic/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -705,15 +705,15 @@ impl<'a> TraitDef<'a> {
705705
self.span,
706706
Ident::invalid(),
707707
a,
708-
ast::ItemKind::Impl(
708+
ast::ItemKind::Impl {
709709
unsafety,
710-
ast::ImplPolarity::Positive,
711-
ast::Defaultness::Final,
712-
trait_generics,
713-
opt_trait_ref,
714-
self_type,
715-
methods.into_iter().chain(associated_types).collect(),
716-
),
710+
polarity: ast::ImplPolarity::Positive,
711+
defaultness: ast::Defaultness::Final,
712+
generics: trait_generics,
713+
of_trait: opt_trait_ref,
714+
self_ty: self_type,
715+
items: methods.into_iter().chain(associated_types).collect(),
716+
},
717717
)
718718
}
719719

src/librustc_builtin_macros/deriving/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,15 @@ fn inject_impl_of_structural_trait(
156156
span,
157157
ast::Ident::invalid(),
158158
attrs,
159-
ItemKind::Impl(
160-
ast::Unsafety::Normal,
161-
ast::ImplPolarity::Positive,
162-
ast::Defaultness::Final,
159+
ItemKind::Impl {
160+
unsafety: ast::Unsafety::Normal,
161+
polarity: ast::ImplPolarity::Positive,
162+
defaultness: ast::Defaultness::Final,
163163
generics,
164-
Some(trait_ref),
165-
self_type,
166-
Vec::new(),
167-
),
164+
of_trait: Some(trait_ref),
165+
self_ty: self_type,
166+
items: Vec::new(),
167+
},
168168
);
169169

170170
push(Annotatable::Item(newitem));

src/librustc_hir/hir.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -2436,15 +2436,18 @@ pub enum ItemKind<'hir> {
24362436
TraitAlias(Generics<'hir>, GenericBounds<'hir>),
24372437

24382438
/// An implementation, e.g., `impl<A> Trait for Foo { .. }`.
2439-
Impl(
2440-
Unsafety,
2441-
ImplPolarity,
2442-
Defaultness,
2443-
Generics<'hir>,
2444-
Option<TraitRef<'hir>>, // (optional) trait this impl implements
2445-
&'hir Ty<'hir>, // self
2446-
&'hir [ImplItemRef<'hir>],
2447-
),
2439+
Impl {
2440+
unsafety: Unsafety,
2441+
polarity: ImplPolarity,
2442+
defaultness: Defaultness,
2443+
generics: Generics<'hir>,
2444+
2445+
/// The trait being implemented, if any.
2446+
of_trait: Option<TraitRef<'hir>>,
2447+
2448+
self_ty: &'hir Ty<'hir>,
2449+
items: &'hir [ImplItemRef<'hir>],
2450+
},
24482451
}
24492452

24502453
impl ItemKind<'_> {
@@ -2465,7 +2468,7 @@ impl ItemKind<'_> {
24652468
ItemKind::Union(..) => "union",
24662469
ItemKind::Trait(..) => "trait",
24672470
ItemKind::TraitAlias(..) => "trait alias",
2468-
ItemKind::Impl(..) => "impl",
2471+
ItemKind::Impl { .. } => "impl",
24692472
}
24702473
}
24712474

@@ -2478,7 +2481,7 @@ impl ItemKind<'_> {
24782481
| ItemKind::Struct(_, ref generics)
24792482
| ItemKind::Union(_, ref generics)
24802483
| ItemKind::Trait(_, _, ref generics, _, _)
2481-
| ItemKind::Impl(_, _, _, ref generics, _, _, _) => generics,
2484+
| ItemKind::Impl { ref generics, .. } => generics,
24822485
_ => return None,
24832486
})
24842487
}

src/librustc_hir/intravisit.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -566,12 +566,20 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
566566
// `visit_enum_def()` takes care of visiting the `Item`'s `HirId`.
567567
visitor.visit_enum_def(enum_definition, generics, item.hir_id, item.span)
568568
}
569-
ItemKind::Impl(.., ref generics, ref opt_trait_reference, ref typ, impl_item_refs) => {
569+
ItemKind::Impl {
570+
unsafety: _,
571+
defaultness: _,
572+
polarity: _,
573+
ref generics,
574+
ref of_trait,
575+
ref self_ty,
576+
items,
577+
} => {
570578
visitor.visit_id(item.hir_id);
571579
visitor.visit_generics(generics);
572-
walk_list!(visitor, visit_trait_ref, opt_trait_reference);
573-
visitor.visit_ty(typ);
574-
walk_list!(visitor, visit_impl_item_ref, impl_item_refs);
580+
walk_list!(visitor, visit_trait_ref, of_trait);
581+
visitor.visit_ty(self_ty);
582+
walk_list!(visitor, visit_impl_item_ref, items);
575583
}
576584
ItemKind::Struct(ref struct_definition, ref generics)
577585
| ItemKind::Union(ref struct_definition, ref generics) => {

0 commit comments

Comments
 (0)