Use named fields for ast::ItemKind::Impl

This commit is contained in:
Dylan MacKenzie 2020-01-13 20:30:20 -08:00
parent 689fca01c5
commit d461e6d6cb
19 changed files with 130 additions and 94 deletions

View File

@ -821,7 +821,7 @@ impl<'hir> Map<'hir> {
| ItemKind::Struct(..)
| ItemKind::Union(..)
| ItemKind::Trait(..)
| ItemKind::Impl(..) => true,
| ItemKind::Impl { .. } => true,
_ => false,
},
Node::ForeignItem(fi) => match fi.kind {

View File

@ -67,14 +67,15 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
if let Some(hir_id) = item_hir_id {
self.lctx.with_parent_item_lifetime_defs(hir_id, |this| {
let this = &mut ItemLowerer { lctx: this };
if let ItemKind::Impl(.., ref opt_trait_ref, _, _) = item.kind {
if opt_trait_ref.as_ref().map(|tr| tr.constness.is_some()).unwrap_or(false) {
if let ItemKind::Impl { ref of_trait, .. } = item.kind {
if of_trait.as_ref().map(|tr| tr.constness.is_some()).unwrap_or(false) {
this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item));
this.lctx
.diagnostic()
.span_err(item.span, "const trait impls are not yet implemented");
}
this.with_trait_impl_ref(opt_trait_ref, |this| visit::walk_item(this, item));
this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item));
} else {
visit::walk_item(this, item);
}
@ -173,7 +174,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
vec
}
ItemKind::MacroDef(..) => SmallVec::new(),
ItemKind::Fn(..) | ItemKind::Impl(.., None, _, _) => smallvec![i.id],
ItemKind::Fn(..) | ItemKind::Impl { of_trait: None, .. } => smallvec![i.id],
ItemKind::Static(ref ty, ..) => {
let mut ids = smallvec![i.id];
if self.sess.features_untracked().impl_trait_in_bindings {
@ -361,15 +362,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.lower_generics(generics, ImplTraitContext::disallowed()),
)
}
ItemKind::Impl(
ItemKind::Impl {
unsafety,
polarity,
defaultness,
ref ast_generics,
ref trait_ref,
ref ty,
ref impl_items,
) => {
generics: ref ast_generics,
of_trait: ref trait_ref,
self_ty: ref ty,
items: ref impl_items,
} => {
let def_id = self.resolver.definitions().local_def_id(id);
// Lower the "impl header" first. This ordering is important

View File

@ -612,9 +612,17 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}
match item.kind {
ItemKind::Impl(unsafety, polarity, _, _, Some(..), ref ty, ref impl_items) => {
ItemKind::Impl {
unsafety,
polarity,
defaultness: _,
generics: _,
of_trait: Some(_),
ref self_ty,
ref items,
} => {
self.invalid_visibility(&item.vis, None);
if let TyKind::Err = ty.kind {
if let TyKind::Err = self_ty.kind {
self.err_handler()
.struct_span_err(item.span, "`impl Trait for .. {}` is an obsolete syntax")
.help("use `auto trait Trait {}` instead")
@ -629,7 +637,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
)
.emit();
}
for impl_item in impl_items {
for impl_item in items {
self.invalid_visibility(&impl_item.vis, None);
if let AssocItemKind::Fn(ref sig, _) = impl_item.kind {
self.check_trait_fn_not_const(sig.header.constness);
@ -637,7 +645,15 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}
}
}
ItemKind::Impl(unsafety, polarity, defaultness, _, None, _, _) => {
ItemKind::Impl {
unsafety,
polarity,
defaultness,
generics: _,
of_trait: None,
self_ty: _,
items: _,
} => {
self.invalid_visibility(
&item.vis,
Some("place qualifiers on individual impl items instead"),

View File

@ -339,7 +339,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
}
}
ast::ItemKind::Impl(_, polarity, defaultness, ..) => {
ast::ItemKind::Impl { polarity, defaultness, .. } => {
if polarity == ast::ImplPolarity::Negative {
gate_feature_post!(
&self,

View File

@ -705,15 +705,15 @@ impl<'a> TraitDef<'a> {
self.span,
Ident::invalid(),
a,
ast::ItemKind::Impl(
ast::ItemKind::Impl {
unsafety,
ast::ImplPolarity::Positive,
ast::Defaultness::Final,
trait_generics,
opt_trait_ref,
self_type,
methods.into_iter().chain(associated_types).collect(),
),
polarity: ast::ImplPolarity::Positive,
defaultness: ast::Defaultness::Final,
generics: trait_generics,
of_trait: opt_trait_ref,
self_ty: self_type,
items: methods.into_iter().chain(associated_types).collect(),
},
)
}

View File

@ -156,15 +156,15 @@ fn inject_impl_of_structural_trait(
span,
ast::Ident::invalid(),
attrs,
ItemKind::Impl(
ast::Unsafety::Normal,
ast::ImplPolarity::Positive,
ast::Defaultness::Final,
ItemKind::Impl {
unsafety: ast::Unsafety::Normal,
polarity: ast::ImplPolarity::Positive,
defaultness: ast::Defaultness::Final,
generics,
Some(trait_ref),
self_type,
Vec::new(),
),
of_trait: Some(trait_ref),
self_ty: self_type,
items: Vec::new(),
},
);
push(Annotatable::Item(newitem));

View File

@ -251,7 +251,7 @@ impl EarlyLintPass for UnsafeCode {
self.report_unsafe(cx, it.span, "declaration of an `unsafe` trait")
}
ast::ItemKind::Impl(ast::Unsafety::Unsafe, ..) => {
ast::ItemKind::Impl { unsafety: ast::Unsafety::Unsafe, .. } => {
self.report_unsafe(cx, it.span, "implementation of an `unsafe` trait")
}

View File

@ -221,7 +221,7 @@ declare_lint_pass!(LintPassImpl => [LINT_PASS_IMPL_WITHOUT_MACRO]);
impl EarlyLintPass for LintPassImpl {
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
if let ItemKind::Impl(_, _, _, _, Some(lint_pass), _, _) = &item.kind {
if let ItemKind::Impl { of_trait: Some(lint_pass), .. } = &item.kind {
if let Some(last) = lint_pass.path.segments.last() {
if last.ident.name == sym::LintPass {
let expn_data = lint_pass.path.span.ctxt().outer_expn_data();

View File

@ -634,15 +634,15 @@ impl<'a> Parser<'a> {
let constness = constness.map(|c| c.node);
let trait_ref = TraitRef { path, constness, ref_id: ty_first.id };
ItemKind::Impl(
ItemKind::Impl {
unsafety,
polarity,
defaultness,
generics,
Some(trait_ref),
ty_second,
impl_items,
)
of_trait: Some(trait_ref),
self_ty: ty_second,
items: impl_items,
}
}
None => {
// Reject `impl const Type {}` here
@ -653,15 +653,15 @@ impl<'a> Parser<'a> {
}
// impl Type
ItemKind::Impl(
ItemKind::Impl {
unsafety,
polarity,
defaultness,
generics,
None,
ty_first,
impl_items,
)
of_trait: None,
self_ty: ty_first,
items: impl_items,
}
}
};

View File

@ -815,7 +815,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
}
// These items do not add names to modules.
ItemKind::Impl(..) | ItemKind::ForeignMod(..) | ItemKind::GlobalAsm(..) => {}
ItemKind::Impl { .. } | ItemKind::ForeignMod(..) | ItemKind::GlobalAsm(..) => {}
ItemKind::MacroDef(..) | ItemKind::Mac(_) => unreachable!(),
}

View File

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

View File

@ -797,14 +797,14 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
self.resolve_adt(item, generics);
}
ItemKind::Impl(.., ref generics, ref opt_trait_ref, ref self_type, ref impl_items) => {
self.resolve_implementation(
generics,
opt_trait_ref,
&self_type,
item.id,
impl_items,
)
ItemKind::Impl {
ref generics,
ref of_trait,
ref self_ty,
items: ref impl_items,
..
} => {
self.resolve_implementation(generics, of_trait, &self_ty, item.id, impl_items);
}
ItemKind::Trait(.., ref generics, ref bounds, ref trait_items) => {

View File

@ -1300,8 +1300,8 @@ impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
self.process_struct(item, def, ty_params)
}
Enum(ref def, ref ty_params) => self.process_enum(item, def, ty_params),
Impl(.., ref ty_params, ref trait_ref, ref typ, ref impl_items) => {
self.process_impl(item, ty_params, trait_ref, &typ, impl_items)
Impl { ref generics, ref of_trait, ref self_ty, ref items, .. } => {
self.process_impl(item, generics, of_trait, &self_ty, items)
}
Trait(_, _, ref generics, ref trait_refs, ref methods) => {
self.process_trait(item, generics, trait_refs, methods)

View File

@ -305,8 +305,8 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
attributes: lower_attributes(item.attrs.clone(), self),
}))
}
ast::ItemKind::Impl(.., ref trait_ref, ref typ, ref impls) => {
if let ast::TyKind::Path(None, ref path) = typ.kind {
ast::ItemKind::Impl { ref of_trait, ref self_ty, ref items, .. } => {
if let ast::TyKind::Path(None, ref path) = self_ty.kind {
// Common case impl for a struct or something basic.
if generated_code(path.span) {
return None;
@ -317,14 +317,14 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
let impl_id = self.next_impl_id();
let span = self.span_from_span(sub_span);
let type_data = self.lookup_def_id(typ.id);
let type_data = self.lookup_def_id(self_ty.id);
type_data.map(|type_data| {
Data::RelationData(
Relation {
kind: RelationKind::Impl { id: impl_id },
span: span.clone(),
from: id_from_def_id(type_data),
to: trait_ref
to: of_trait
.as_ref()
.and_then(|t| self.lookup_def_id(t.ref_id))
.map(id_from_def_id)
@ -332,14 +332,14 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
},
Impl {
id: impl_id,
kind: match *trait_ref {
kind: match *of_trait {
Some(_) => ImplKind::Direct,
None => ImplKind::Inherent,
},
span: span,
value: String::new(),
parent: None,
children: impls
children: items
.iter()
.map(|i| id_from_node_id(i.id, self))
.collect(),

View File

@ -482,15 +482,15 @@ impl Sig for ast::Item {
Ok(sig)
}
ast::ItemKind::Impl(
ast::ItemKind::Impl {
unsafety,
polarity,
defaultness,
ref generics,
ref opt_trait,
ref ty,
_,
) => {
ref of_trait,
ref self_ty,
items: _,
} => {
let mut text = String::new();
if let ast::Defaultness::Default = defaultness {
text.push_str("default ");
@ -505,7 +505,7 @@ impl Sig for ast::Item {
text.push(' ');
let trait_sig = if let Some(ref t) = *opt_trait {
let trait_sig = if let Some(ref t) = *of_trait {
if polarity == ast::ImplPolarity::Negative {
text.push('!');
}
@ -517,7 +517,7 @@ impl Sig for ast::Item {
text_sig(String::new())
};
let ty_sig = ty.make(offset + text.len(), id, scx)?;
let ty_sig = self_ty.make(offset + text.len(), id, scx)?;
text.push_str(&ty_sig.text);
text.push_str(" {}");

View File

@ -2614,15 +2614,18 @@ pub enum ItemKind {
/// An implementation.
///
/// E.g., `impl<A> Foo<A> { .. }` or `impl<A> Trait for Foo<A> { .. }`.
Impl(
Unsafety,
ImplPolarity,
Defaultness,
Generics,
Option<TraitRef>, // (optional) trait this impl implements
P<Ty>, // self
Vec<AssocItem>,
),
Impl {
unsafety: Unsafety,
polarity: ImplPolarity,
defaultness: Defaultness,
generics: Generics,
/// The trait being implemented, if any.
of_trait: Option<TraitRef>,
self_ty: P<Ty>,
items: Vec<AssocItem>,
},
/// A macro invocation.
///
/// E.g., `foo!(..)`.
@ -2649,7 +2652,7 @@ impl ItemKind {
ItemKind::Union(..) => "union",
ItemKind::Trait(..) => "trait",
ItemKind::TraitAlias(..) => "trait alias",
ItemKind::Mac(..) | ItemKind::MacroDef(..) | ItemKind::Impl(..) => "item",
ItemKind::Mac(..) | ItemKind::MacroDef(..) | ItemKind::Impl { .. } => "item",
}
}
}

View File

@ -918,10 +918,18 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
vis.visit_variant_data(variant_data);
vis.visit_generics(generics);
}
ItemKind::Impl(_unsafety, _polarity, _defaultness, generics, trait_ref, ty, items) => {
ItemKind::Impl {
unsafety: _,
polarity: _,
defaultness: _,
generics,
of_trait,
self_ty,
items,
} => {
vis.visit_generics(generics);
visit_opt(trait_ref, |trait_ref| vis.visit_trait_ref(trait_ref));
vis.visit_ty(ty);
visit_opt(of_trait, |trait_ref| vis.visit_trait_ref(trait_ref));
vis.visit_ty(self_ty);
items.flat_map_in_place(|item| vis.flat_map_impl_item(item));
}
ItemKind::Trait(_is_auto, _unsafety, generics, bounds, items) => {

View File

@ -1226,15 +1226,15 @@ impl<'a> State<'a> {
self.head(visibility_qualified(&item.vis, "union"));
self.print_struct(struct_def, generics, item.ident, item.span, true);
}
ast::ItemKind::Impl(
ast::ItemKind::Impl {
unsafety,
polarity,
defaultness,
ref generics,
ref opt_trait,
ref ty,
ref impl_items,
) => {
ref of_trait,
ref self_ty,
ref items,
} => {
self.head("");
self.print_visibility(&item.vis);
self.print_defaultness(defaultness);
@ -1250,19 +1250,19 @@ impl<'a> State<'a> {
self.s.word("!");
}
if let Some(ref t) = *opt_trait {
if let Some(ref t) = *of_trait {
self.print_trait_ref(t);
self.s.space();
self.word_space("for");
}
self.print_type(ty);
self.print_type(self_ty);
self.print_where_clause(&generics.where_clause);
self.s.space();
self.bopen();
self.print_inner_attributes(&item.attrs);
for impl_item in impl_items {
for impl_item in items {
self.print_assoc_item(impl_item);
}
self.bclose(item.span);

View File

@ -308,11 +308,19 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
visitor.visit_generics(generics);
visitor.visit_enum_def(enum_definition, generics, item.id, item.span)
}
ItemKind::Impl(_, _, _, ref generics, ref opt_trait_reference, ref typ, ref impl_items) => {
ItemKind::Impl {
unsafety: _,
polarity: _,
defaultness: _,
ref generics,
ref of_trait,
ref self_ty,
ref items,
} => {
visitor.visit_generics(generics);
walk_list!(visitor, visit_trait_ref, opt_trait_reference);
visitor.visit_ty(typ);
walk_list!(visitor, visit_impl_item, impl_items);
walk_list!(visitor, visit_trait_ref, of_trait);
visitor.visit_ty(self_ty);
walk_list!(visitor, visit_impl_item, items);
}
ItemKind::Struct(ref struct_definition, ref generics)
| ItemKind::Union(ref struct_definition, ref generics) => {