Skip to content

Commit

Permalink
Rollup merge of rust-lang#68204 - ecstatic-morse:item-kind-impl, r=ol…
Browse files Browse the repository at this point in the history
…i-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.
  • Loading branch information
tmandry authored Jan 18, 2020
2 parents bafe089 + 4743995 commit fca3264
Show file tree
Hide file tree
Showing 48 changed files with 296 additions and 255 deletions.
4 changes: 2 additions & 2 deletions src/librustc/hir/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl Target {
ItemKind::Union(..) => Target::Union,
ItemKind::Trait(..) => Target::Trait,
ItemKind::TraitAlias(..) => Target::TraitAlias,
ItemKind::Impl(..) => Target::Impl,
ItemKind::Impl { .. } => Target::Impl,
}
}

Expand Down Expand Up @@ -144,7 +144,7 @@ impl Target {
let parent_hir_id = tcx.hir().get_parent_item(impl_item.hir_id);
let containing_item = tcx.hir().expect_item(parent_hir_id);
let containing_impl_is_for_trait = match &containing_item.kind {
hir::ItemKind::Impl(_, _, _, _, tr, _, _) => tr.is_some(),
hir::ItemKind::Impl { ref of_trait, .. } => of_trait.is_some(),
_ => bug!("parent of an ImplItem must be an Impl"),
};
if containing_impl_is_for_trait {
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ impl<'hir> Map<'hir> {
| ItemKind::Use(..)
| ItemKind::ForeignMod(..)
| ItemKind::GlobalAsm(..)
| ItemKind::Impl(..) => return None,
| ItemKind::Impl { .. } => return None,
},
Node::ForeignItem(item) => match item.kind {
ForeignItemKind::Fn(..) => DefKind::Fn,
Expand Down Expand Up @@ -604,7 +604,7 @@ impl<'hir> Map<'hir> {
| ItemKind::Union(_, ref generics)
| ItemKind::Trait(_, _, ref generics, ..)
| ItemKind::TraitAlias(ref generics, _)
| ItemKind::Impl(_, _, _, ref generics, ..) => Some(generics),
| ItemKind::Impl { ref generics, .. } => Some(generics),
_ => None,
},
_ => None,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -1332,7 +1332,7 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
ItemKind::Union(..) => "union",
ItemKind::Trait(..) => "trait",
ItemKind::TraitAlias(..) => "trait alias",
ItemKind::Impl(..) => "impl",
ItemKind::Impl { .. } => "impl",
};
format!("{} {}{}", item_str, path_str(), id_str)
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ fn emit_msg_span(

fn item_scope_tag(item: &hir::Item<'_>) -> &'static str {
match item.kind {
hir::ItemKind::Impl(..) => "impl",
hir::ItemKind::Impl { .. } => "impl",
hir::ItemKind::Struct(..) => "struct",
hir::ItemKind::Union(..) => "union",
hir::ItemKind::Enum(..) => "enum",
Expand Down
5 changes: 2 additions & 3 deletions src/librustc/traits/error_reporting/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
..
})
| hir::Node::Item(hir::Item {
kind: hir::ItemKind::Impl(_, _, _, generics, ..),
..
kind: hir::ItemKind::Impl { generics, .. }, ..
}) if projection.is_some() => {
// Missing associated type bound.
suggest_restriction(&generics, "the associated type", err);
Expand All @@ -115,7 +114,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
..
})
| hir::Node::Item(hir::Item {
kind: hir::ItemKind::Impl(_, _, _, generics, ..),
kind: hir::ItemKind::Impl { generics, .. },
span,
..
})
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/traits/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ pub fn impl_is_default(tcx: TyCtxt<'_>, node_item_def_id: DefId) -> bool {
match tcx.hir().as_local_hir_id(node_item_def_id) {
Some(hir_id) => {
let item = tcx.hir().expect_item(hir_id);
if let hir::ItemKind::Impl(_, _, defaultness, ..) = item.kind {
if let hir::ItemKind::Impl { defaultness, .. } = item.kind {
defaultness.is_default()
} else {
false
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/traits/wf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
// |
// = note: expected type `u32`
// found type `()`
if let Some(hir::ItemKind::Impl(.., impl_items)) = item.map(|i| &i.kind) {
if let Some(hir::ItemKind::Impl { items, .. }) = item.map(|i| &i.kind) {
let trait_assoc_item = tcx.associated_item(proj.projection_def_id());
if let Some(impl_item) = impl_items
if let Some(impl_item) = items
.iter()
.filter(|item| item.ident == trait_assoc_item.ident)
.next()
Expand Down Expand Up @@ -279,14 +279,14 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
// | ^^^^^^^^^^^^^^^^^^ the trait `Bar` is not implemented for `bool`
if let (
ty::Projection(ty::ProjectionTy { item_def_id, .. }),
Some(hir::ItemKind::Impl(.., impl_items)),
Some(hir::ItemKind::Impl { items, .. }),
) = (&proj.skip_binder().self_ty().kind, item.map(|i| &i.kind))
{
if let Some((impl_item, trait_assoc_item)) = trait_assoc_items
.filter(|i| i.def_id == *item_def_id)
.next()
.and_then(|trait_assoc_item| {
impl_items
items
.iter()
.filter(|i| i.ident == trait_assoc_item.ident)
.next()
Expand Down
35 changes: 18 additions & 17 deletions src/librustc_ast_lowering/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -118,7 +119,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let old_len = self.in_scope_lifetimes.len();

let parent_generics = match self.items.get(&parent_hir_id).unwrap().kind {
hir::ItemKind::Impl(_, _, _, ref generics, ..)
hir::ItemKind::Impl { ref generics, .. }
| hir::ItemKind::Trait(_, _, ref generics, ..) => &generics.params[..],
_ => &[],
};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -417,15 +418,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
)
});

hir::ItemKind::Impl(
hir::ItemKind::Impl {
unsafety,
polarity,
self.lower_defaultness(defaultness, true /* [1] */),
defaultness: self.lower_defaultness(defaultness, true /* [1] */),
generics,
trait_ref,
lowered_ty,
new_impl_items,
)
of_trait: trait_ref,
self_ty: lowered_ty,
items: new_impl_items,
}
}
ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref items) => {
let bounds = self.lower_param_bounds(bounds, ImplTraitContext::disallowed());
Expand Down
24 changes: 20 additions & 4 deletions src/librustc_ast_passes/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -629,15 +637,23 @@ 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);
self.check_trait_fn_not_async(impl_item.span, sig.header.asyncness.node);
}
}
}
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"),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_ast_passes/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 8 additions & 8 deletions src/librustc_builtin_macros/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
)
}

Expand Down
16 changes: 8 additions & 8 deletions src/librustc_builtin_macros/deriving/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
25 changes: 14 additions & 11 deletions src/librustc_hir/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2436,15 +2436,18 @@ pub enum ItemKind<'hir> {
TraitAlias(Generics<'hir>, GenericBounds<'hir>),

/// An implementation, e.g., `impl<A> Trait for Foo { .. }`.
Impl(
Unsafety,
ImplPolarity,
Defaultness,
Generics<'hir>,
Option<TraitRef<'hir>>, // (optional) trait this impl implements
&'hir Ty<'hir>, // self
&'hir [ImplItemRef<'hir>],
),
Impl {
unsafety: Unsafety,
polarity: ImplPolarity,
defaultness: Defaultness,
generics: Generics<'hir>,

/// The trait being implemented, if any.
of_trait: Option<TraitRef<'hir>>,

self_ty: &'hir Ty<'hir>,
items: &'hir [ImplItemRef<'hir>],
},
}

impl ItemKind<'_> {
Expand All @@ -2465,7 +2468,7 @@ impl ItemKind<'_> {
ItemKind::Union(..) => "union",
ItemKind::Trait(..) => "trait",
ItemKind::TraitAlias(..) => "trait alias",
ItemKind::Impl(..) => "impl",
ItemKind::Impl { .. } => "impl",
}
}

Expand All @@ -2478,7 +2481,7 @@ impl ItemKind<'_> {
| ItemKind::Struct(_, ref generics)
| ItemKind::Union(_, ref generics)
| ItemKind::Trait(_, _, ref generics, _, _)
| ItemKind::Impl(_, _, _, ref generics, _, _, _) => generics,
| ItemKind::Impl { ref generics, .. } => generics,
_ => return None,
})
}
Expand Down
16 changes: 12 additions & 4 deletions src/librustc_hir/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,12 +566,20 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
// `visit_enum_def()` takes care of visiting the `Item`'s `HirId`.
visitor.visit_enum_def(enum_definition, generics, item.hir_id, item.span)
}
ItemKind::Impl(.., ref generics, ref opt_trait_reference, ref typ, impl_item_refs) => {
ItemKind::Impl {
unsafety: _,
defaultness: _,
polarity: _,
ref generics,
ref of_trait,
ref self_ty,
items,
} => {
visitor.visit_id(item.hir_id);
visitor.visit_generics(generics);
walk_list!(visitor, visit_trait_ref, opt_trait_reference);
visitor.visit_ty(typ);
walk_list!(visitor, visit_impl_item_ref, impl_item_refs);
walk_list!(visitor, visit_trait_ref, of_trait);
visitor.visit_ty(self_ty);
walk_list!(visitor, visit_impl_item_ref, items);
}
ItemKind::Struct(ref struct_definition, ref generics)
| ItemKind::Union(ref struct_definition, ref generics) => {
Expand Down
Loading

0 comments on commit fca3264

Please sign in to comment.