Skip to content

Commit d461e6d

Browse files
Use named fields for ast::ItemKind::Impl
1 parent 689fca0 commit d461e6d

File tree

19 files changed

+130
-94
lines changed

19 files changed

+130
-94
lines changed

src/librustc/hir/map/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -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 {

src/librustc_ast_lowering/item.rs

+11-10
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
}
@@ -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

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_lint/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ impl EarlyLintPass for UnsafeCode {
251251
self.report_unsafe(cx, it.span, "declaration of an `unsafe` trait")
252252
}
253253

254-
ast::ItemKind::Impl(ast::Unsafety::Unsafe, ..) => {
254+
ast::ItemKind::Impl { unsafety: ast::Unsafety::Unsafe, .. } => {
255255
self.report_unsafe(cx, it.span, "implementation of an `unsafe` trait")
256256
}
257257

src/librustc_lint/internal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ declare_lint_pass!(LintPassImpl => [LINT_PASS_IMPL_WITHOUT_MACRO]);
221221

222222
impl EarlyLintPass for LintPassImpl {
223223
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
224-
if let ItemKind::Impl(_, _, _, _, Some(lint_pass), _, _) = &item.kind {
224+
if let ItemKind::Impl { of_trait: Some(lint_pass), .. } = &item.kind {
225225
if let Some(last) = lint_pass.path.segments.last() {
226226
if last.ident.name == sym::LintPass {
227227
let expn_data = lint_pass.path.span.ctxt().outer_expn_data();

src/librustc_parse/parser/item.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -634,15 +634,15 @@ impl<'a> Parser<'a> {
634634
let constness = constness.map(|c| c.node);
635635
let trait_ref = TraitRef { path, constness, ref_id: ty_first.id };
636636

637-
ItemKind::Impl(
637+
ItemKind::Impl {
638638
unsafety,
639639
polarity,
640640
defaultness,
641641
generics,
642-
Some(trait_ref),
643-
ty_second,
644-
impl_items,
645-
)
642+
of_trait: Some(trait_ref),
643+
self_ty: ty_second,
644+
items: impl_items,
645+
}
646646
}
647647
None => {
648648
// Reject `impl const Type {}` here
@@ -653,15 +653,15 @@ impl<'a> Parser<'a> {
653653
}
654654

655655
// impl Type
656-
ItemKind::Impl(
656+
ItemKind::Impl {
657657
unsafety,
658658
polarity,
659659
defaultness,
660660
generics,
661-
None,
662-
ty_first,
663-
impl_items,
664-
)
661+
of_trait: None,
662+
self_ty: ty_first,
663+
items: impl_items,
664+
}
665665
}
666666
};
667667

src/librustc_resolve/build_reduced_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
815815
}
816816

817817
// These items do not add names to modules.
818-
ItemKind::Impl(..) | ItemKind::ForeignMod(..) | ItemKind::GlobalAsm(..) => {}
818+
ItemKind::Impl { .. } | ItemKind::ForeignMod(..) | ItemKind::GlobalAsm(..) => {}
819819

820820
ItemKind::MacroDef(..) | ItemKind::Mac(_) => unreachable!(),
821821
}

src/librustc_resolve/def_collector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
104104
// Pick the def data. This need not be unique, but the more
105105
// information we encapsulate into, the better
106106
let def_data = match &i.kind {
107-
ItemKind::Impl(..) => DefPathData::Impl,
107+
ItemKind::Impl { .. } => DefPathData::Impl,
108108
ItemKind::Mod(..) if i.ident.name == kw::Invalid => {
109109
return visit::walk_item(self, i);
110110
}

src/librustc_resolve/late.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -797,14 +797,14 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
797797
self.resolve_adt(item, generics);
798798
}
799799

800-
ItemKind::Impl(.., ref generics, ref opt_trait_ref, ref self_type, ref impl_items) => {
801-
self.resolve_implementation(
802-
generics,
803-
opt_trait_ref,
804-
&self_type,
805-
item.id,
806-
impl_items,
807-
)
800+
ItemKind::Impl {
801+
ref generics,
802+
ref of_trait,
803+
ref self_ty,
804+
items: ref impl_items,
805+
..
806+
} => {
807+
self.resolve_implementation(generics, of_trait, &self_ty, item.id, impl_items);
808808
}
809809

810810
ItemKind::Trait(.., ref generics, ref bounds, ref trait_items) => {

src/librustc_save_analysis/dump_visitor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1300,8 +1300,8 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
13001300
self.process_struct(item, def, ty_params)
13011301
}
13021302
Enum(ref def, ref ty_params) => self.process_enum(item, def, ty_params),
1303-
Impl(.., ref ty_params, ref trait_ref, ref typ, ref impl_items) => {
1304-
self.process_impl(item, ty_params, trait_ref, &typ, impl_items)
1303+
Impl { ref generics, ref of_trait, ref self_ty, ref items, .. } => {
1304+
self.process_impl(item, generics, of_trait, &self_ty, items)
13051305
}
13061306
Trait(_, _, ref generics, ref trait_refs, ref methods) => {
13071307
self.process_trait(item, generics, trait_refs, methods)

src/librustc_save_analysis/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
305305
attributes: lower_attributes(item.attrs.clone(), self),
306306
}))
307307
}
308-
ast::ItemKind::Impl(.., ref trait_ref, ref typ, ref impls) => {
309-
if let ast::TyKind::Path(None, ref path) = typ.kind {
308+
ast::ItemKind::Impl { ref of_trait, ref self_ty, ref items, .. } => {
309+
if let ast::TyKind::Path(None, ref path) = self_ty.kind {
310310
// Common case impl for a struct or something basic.
311311
if generated_code(path.span) {
312312
return None;
@@ -317,29 +317,29 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
317317
let impl_id = self.next_impl_id();
318318
let span = self.span_from_span(sub_span);
319319

320-
let type_data = self.lookup_def_id(typ.id);
320+
let type_data = self.lookup_def_id(self_ty.id);
321321
type_data.map(|type_data| {
322322
Data::RelationData(
323323
Relation {
324324
kind: RelationKind::Impl { id: impl_id },
325325
span: span.clone(),
326326
from: id_from_def_id(type_data),
327-
to: trait_ref
327+
to: of_trait
328328
.as_ref()
329329
.and_then(|t| self.lookup_def_id(t.ref_id))
330330
.map(id_from_def_id)
331331
.unwrap_or_else(|| null_id()),
332332
},
333333
Impl {
334334
id: impl_id,
335-
kind: match *trait_ref {
335+
kind: match *of_trait {
336336
Some(_) => ImplKind::Direct,
337337
None => ImplKind::Inherent,
338338
},
339339
span: span,
340340
value: String::new(),
341341
parent: None,
342-
children: impls
342+
children: items
343343
.iter()
344344
.map(|i| id_from_node_id(i.id, self))
345345
.collect(),

src/librustc_save_analysis/sig.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -482,15 +482,15 @@ impl Sig for ast::Item {
482482

483483
Ok(sig)
484484
}
485-
ast::ItemKind::Impl(
485+
ast::ItemKind::Impl {
486486
unsafety,
487487
polarity,
488488
defaultness,
489489
ref generics,
490-
ref opt_trait,
491-
ref ty,
492-
_,
493-
) => {
490+
ref of_trait,
491+
ref self_ty,
492+
items: _,
493+
} => {
494494
let mut text = String::new();
495495
if let ast::Defaultness::Default = defaultness {
496496
text.push_str("default ");
@@ -505,7 +505,7 @@ impl Sig for ast::Item {
505505

506506
text.push(' ');
507507

508-
let trait_sig = if let Some(ref t) = *opt_trait {
508+
let trait_sig = if let Some(ref t) = *of_trait {
509509
if polarity == ast::ImplPolarity::Negative {
510510
text.push('!');
511511
}
@@ -517,7 +517,7 @@ impl Sig for ast::Item {
517517
text_sig(String::new())
518518
};
519519

520-
let ty_sig = ty.make(offset + text.len(), id, scx)?;
520+
let ty_sig = self_ty.make(offset + text.len(), id, scx)?;
521521
text.push_str(&ty_sig.text);
522522

523523
text.push_str(" {}");

src/libsyntax/ast.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -2614,15 +2614,18 @@ pub enum ItemKind {
26142614
/// An implementation.
26152615
///
26162616
/// E.g., `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`.
2617-
Impl(
2618-
Unsafety,
2619-
ImplPolarity,
2620-
Defaultness,
2621-
Generics,
2622-
Option<TraitRef>, // (optional) trait this impl implements
2623-
P<Ty>, // self
2624-
Vec<AssocItem>,
2625-
),
2617+
Impl {
2618+
unsafety: Unsafety,
2619+
polarity: ImplPolarity,
2620+
defaultness: Defaultness,
2621+
generics: Generics,
2622+
2623+
/// The trait being implemented, if any.
2624+
of_trait: Option<TraitRef>,
2625+
2626+
self_ty: P<Ty>,
2627+
items: Vec<AssocItem>,
2628+
},
26262629
/// A macro invocation.
26272630
///
26282631
/// E.g., `foo!(..)`.
@@ -2649,7 +2652,7 @@ impl ItemKind {
26492652
ItemKind::Union(..) => "union",
26502653
ItemKind::Trait(..) => "trait",
26512654
ItemKind::TraitAlias(..) => "trait alias",
2652-
ItemKind::Mac(..) | ItemKind::MacroDef(..) | ItemKind::Impl(..) => "item",
2655+
ItemKind::Mac(..) | ItemKind::MacroDef(..) | ItemKind::Impl { .. } => "item",
26532656
}
26542657
}
26552658
}

0 commit comments

Comments
 (0)