Skip to content

Thin AssocItem #95884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions compiler/rustc_ast_lowering/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,16 +323,15 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
fn visit_trait_item_ref(&mut self, ii: &'hir TraitItemRef) {
// Do not visit the duplicate information in TraitItemRef. We want to
// map the actual nodes, not the duplicate ones in the *Ref.
let TraitItemRef { id, ident: _, kind: _, span: _, defaultness: _ } = *ii;
let TraitItemRef { id, ident: _, kind: _, span: _ } = *ii;

self.visit_nested_trait_item(id);
}

fn visit_impl_item_ref(&mut self, ii: &'hir ImplItemRef) {
// Do not visit the duplicate information in ImplItemRef. We want to
// map the actual nodes, not the duplicate ones in the *Ref.
let ImplItemRef { id, ident: _, kind: _, span: _, defaultness: _, trait_item_def_id: _ } =
*ii;
let ImplItemRef { id, ident: _, kind: _, span: _, trait_item_def_id: _ } = *ii;

self.visit_nested_impl_item(id);
}
Expand Down
37 changes: 18 additions & 19 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,17 +755,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
let hir_id = self.lower_node_id(i.id);
let trait_item_def_id = hir_id.expect_owner();

let (generics, kind) = match i.kind {
let (generics, kind, has_default) = match i.kind {
AssocItemKind::Const(_, ref ty, ref default) => {
let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
let body = default.as_ref().map(|x| self.lower_const_body(i.span, Some(x)));
(hir::Generics::empty(), hir::TraitItemKind::Const(ty, body))
(hir::Generics::empty(), hir::TraitItemKind::Const(ty, body), body.is_some())
}
AssocItemKind::Fn(box Fn { ref sig, ref generics, body: None, .. }) => {
let names = self.lower_fn_params_to_names(&sig.decl);
let (generics, sig) =
self.lower_method_sig(generics, sig, i.id, FnDeclKind::Trait, None);
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Required(names)))
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Required(names)), false)
}
AssocItemKind::Fn(box Fn { ref sig, ref generics, body: Some(ref body), .. }) => {
let asyncness = sig.header.asyncness;
Expand All @@ -778,7 +778,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
FnDeclKind::Trait,
asyncness.opt_return_id(),
);
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id)))
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id)), true)
}
AssocItemKind::TyAlias(box TyAlias {
ref generics,
Expand All @@ -789,7 +789,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}) => {
let mut generics = generics.clone();
add_ty_alias_where_clause(&mut generics, where_clauses, false);
self.lower_generics(
let (generics, kind) = self.lower_generics(
&generics,
i.id,
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
Expand All @@ -805,7 +805,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
ty,
)
},
)
);
(generics, kind, ty.is_some())
}
AssocItemKind::MacCall(..) => panic!("macro item shouldn't exist at this point"),
};
Expand All @@ -817,28 +818,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
generics,
kind,
span: self.lower_span(i.span),
defaultness: hir::Defaultness::Default { has_value: has_default },
};
self.arena.alloc(item)
}

fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef {
let (kind, has_default) = match &i.kind {
AssocItemKind::Const(_, _, default) => (hir::AssocItemKind::Const, default.is_some()),
AssocItemKind::TyAlias(box TyAlias { ty, .. }) => {
(hir::AssocItemKind::Type, ty.is_some())
}
AssocItemKind::Fn(box Fn { sig, body, .. }) => {
(hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }, body.is_some())
let kind = match &i.kind {
AssocItemKind::Const(..) => hir::AssocItemKind::Const,
AssocItemKind::TyAlias(..) => hir::AssocItemKind::Type,
AssocItemKind::Fn(box Fn { sig, .. }) => {
hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }
}
AssocItemKind::MacCall(..) => unimplemented!(),
};
let id = hir::TraitItemId { def_id: self.local_def_id(i.id) };
let defaultness = hir::Defaultness::Default { has_value: has_default };
hir::TraitItemRef {
id,
ident: self.lower_ident(i.ident),
span: self.lower_span(i.span),
defaultness,
kind,
}
}
Expand All @@ -849,6 +847,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
// Since `default impl` is not yet implemented, this is always true in impls.
let has_value = true;
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);

let (generics, kind) = match &i.kind {
AssocItemKind::Const(_, ty, expr) => {
let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
Expand Down Expand Up @@ -903,19 +905,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
kind,
vis_span: self.lower_span(i.vis.span),
span: self.lower_span(i.span),
defaultness,
};
self.arena.alloc(item)
}

fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemRef {
// Since `default impl` is not yet implemented, this is always true in impls.
let has_value = true;
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
hir::ImplItemRef {
id: hir::ImplItemId { def_id: self.local_def_id(i.id) },
ident: self.lower_ident(i.ident),
span: self.lower_span(i.span),
defaultness,
kind: match &i.kind {
AssocItemKind::Const(..) => hir::AssocItemKind::Const,
AssocItemKind::TyAlias(..) => hir::AssocItemKind::Type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
let mut nonconst_call_permission = false;
if let Some(callee_trait) = tcx.trait_of_item(callee)
&& tcx.has_attr(callee_trait, sym::const_trait)
&& Some(callee_trait) == tcx.trait_of_item(caller)
&& Some(callee_trait) == tcx.trait_of_item(caller.to_def_id())
// Can only call methods when it's `<Self as TheTrait>::f`.
&& tcx.types.self_param == substs.type_at(0)
{
Expand Down
9 changes: 6 additions & 3 deletions compiler/rustc_const_eval/src/util/call_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@ pub fn call_kind<'tcx>(
from_hir_call: bool,
self_arg: Option<Ident>,
) -> CallKind<'tcx> {
let parent = tcx.opt_associated_item(method_did).and_then(|assoc| match assoc.container {
AssocItemContainer::ImplContainer(impl_did) => tcx.trait_id_of_impl(impl_did),
AssocItemContainer::TraitContainer(trait_did) => Some(trait_did),
let parent = tcx.opt_associated_item(method_did).and_then(|assoc| {
let container_id = assoc.container_id(tcx);
match assoc.container {
AssocItemContainer::ImplContainer => tcx.trait_id_of_impl(container_id),
AssocItemContainer::TraitContainer => Some(container_id),
}
});

let fn_call = parent
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2222,6 +2222,7 @@ pub struct TraitItem<'hir> {
pub generics: &'hir Generics<'hir>,
pub kind: TraitItemKind<'hir>,
pub span: Span,
pub defaultness: Defaultness,
}

impl TraitItem<'_> {
Expand Down Expand Up @@ -2281,6 +2282,7 @@ pub struct ImplItem<'hir> {
pub def_id: LocalDefId,
pub generics: &'hir Generics<'hir>,
pub kind: ImplItemKind<'hir>,
pub defaultness: Defaultness,
pub span: Span,
pub vis_span: Span,
}
Expand Down Expand Up @@ -3083,7 +3085,6 @@ pub struct TraitItemRef {
pub ident: Ident,
pub kind: AssocItemKind,
pub span: Span,
pub defaultness: Defaultness,
}

/// A reference from an impl to one of its associated items. This
Expand All @@ -3098,7 +3099,6 @@ pub struct ImplItemRef {
pub ident: Ident,
pub kind: AssocItemKind,
pub span: Span,
pub defaultness: Defaultness,
/// When we are in a trait impl, link to the trait-item's id.
pub trait_item_def_id: Option<DefId>,
}
Expand Down Expand Up @@ -3496,11 +3496,11 @@ mod size_asserts {
rustc_data_structures::static_assert_size!(ForeignItem<'static>, 72);
rustc_data_structures::static_assert_size!(GenericBound<'_>, 48);
rustc_data_structures::static_assert_size!(Generics<'static>, 56);
rustc_data_structures::static_assert_size!(ImplItem<'static>, 80);
rustc_data_structures::static_assert_size!(ImplItem<'static>, 88);
rustc_data_structures::static_assert_size!(Impl<'static>, 80);
rustc_data_structures::static_assert_size!(Item<'static>, 80);
rustc_data_structures::static_assert_size!(Pat<'static>, 88);
rustc_data_structures::static_assert_size!(QPath<'static>, 24);
rustc_data_structures::static_assert_size!(TraitItem<'static>, 88);
rustc_data_structures::static_assert_size!(TraitItem<'static>, 96);
rustc_data_structures::static_assert_size!(Ty<'static>, 72);
}
42 changes: 23 additions & 19 deletions compiler/rustc_hir/src/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,32 +946,30 @@ pub fn walk_fn<'v, V: Visitor<'v>>(
}

pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem<'v>) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might be nice to also exhaustively match on TraitItem here?

visitor.visit_ident(trait_item.ident);
visitor.visit_generics(&trait_item.generics);
match trait_item.kind {
// N.B., deliberately force a compilation error if/when new fields are added.
let TraitItem { ident, generics, ref defaultness, ref kind, span, def_id: _ } = *trait_item;
let hir_id = trait_item.hir_id();
visitor.visit_ident(ident);
visitor.visit_generics(&generics);
visitor.visit_defaultness(&defaultness);
match *kind {
TraitItemKind::Const(ref ty, default) => {
visitor.visit_id(trait_item.hir_id());
visitor.visit_id(hir_id);
visitor.visit_ty(ty);
walk_list!(visitor, visit_nested_body, default);
}
TraitItemKind::Fn(ref sig, TraitFn::Required(param_names)) => {
visitor.visit_id(trait_item.hir_id());
visitor.visit_id(hir_id);
visitor.visit_fn_decl(&sig.decl);
for &param_name in param_names {
visitor.visit_ident(param_name);
}
}
TraitItemKind::Fn(ref sig, TraitFn::Provided(body_id)) => {
visitor.visit_fn(
FnKind::Method(trait_item.ident, sig),
&sig.decl,
body_id,
trait_item.span,
trait_item.hir_id(),
);
visitor.visit_fn(FnKind::Method(ident, sig), &sig.decl, body_id, span, hir_id);
}
TraitItemKind::Type(bounds, ref default) => {
visitor.visit_id(trait_item.hir_id());
visitor.visit_id(hir_id);
walk_list!(visitor, visit_param_bound, bounds);
walk_list!(visitor, visit_ty, default);
}
Expand All @@ -980,19 +978,27 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v Trai

pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref: &'v TraitItemRef) {
// N.B., deliberately force a compilation error if/when new fields are added.
let TraitItemRef { id, ident, ref kind, span: _, ref defaultness } = *trait_item_ref;
let TraitItemRef { id, ident, ref kind, span: _ } = *trait_item_ref;
visitor.visit_nested_trait_item(id);
visitor.visit_ident(ident);
visitor.visit_associated_item_kind(kind);
visitor.visit_defaultness(defaultness);
}

pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem<'v>) {
// N.B., deliberately force a compilation error if/when new fields are added.
let ImplItem { def_id: _, ident, ref generics, ref kind, span: _, vis_span: _ } = *impl_item;
let ImplItem {
def_id: _,
ident,
ref generics,
ref kind,
ref defaultness,
span: _,
vis_span: _,
} = *impl_item;

visitor.visit_ident(ident);
visitor.visit_generics(generics);
visitor.visit_defaultness(defaultness);
match *kind {
ImplItemKind::Const(ref ty, body) => {
visitor.visit_id(impl_item.hir_id());
Expand Down Expand Up @@ -1027,12 +1033,10 @@ pub fn walk_foreign_item_ref<'v, V: Visitor<'v>>(

pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'v ImplItemRef) {
// N.B., deliberately force a compilation error if/when new fields are added.
let ImplItemRef { id, ident, ref kind, span: _, ref defaultness, trait_item_def_id: _ } =
*impl_item_ref;
let ImplItemRef { id, ident, ref kind, span: _, trait_item_def_id: _ } = *impl_item_ref;
visitor.visit_nested_impl_item(id);
visitor.visit_ident(ident);
visitor.visit_associated_item_kind(kind);
visitor.visit_defaultness(defaultness);
}

pub fn walk_struct_def<'v, V: Visitor<'v>>(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_incremental/src/persist/dirty_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const BASE_STRUCT: &[&str] =
/// Extra `DepNode`s for functions and methods.
const EXTRA_ASSOCIATED: &[&str] = &[label_strs::associated_item];

const EXTRA_TRAIT: &[&str] = &[label_strs::trait_of_item];
const EXTRA_TRAIT: &[&str] = &[];

// Fully Built Labels

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
"...is used and required to live as long as `'static` here \
because of an implicit lifetime bound on the {}",
match ctxt.assoc_item.container {
AssocItemContainer::TraitContainer(id) =>
format!("`impl` of `{}`", tcx.def_path_str(id)),
AssocItemContainer::ImplContainer(_) =>
"inherent `impl`".to_string(),
AssocItemContainer::TraitContainer => {
let id = ctxt.assoc_item.container_id(tcx);
format!("`impl` of `{}`", tcx.def_path_str(id))
}
AssocItemContainer::ImplContainer => "inherent `impl`".to_string(),
},
),
);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_lint/src/nonstandard_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ pub fn method_context(cx: &LateContext<'_>, id: hir::HirId) -> MethodLateContext
let def_id = cx.tcx.hir().local_def_id(id);
let item = cx.tcx.associated_item(def_id);
match item.container {
ty::TraitContainer(..) => MethodLateContext::TraitAutoImpl,
ty::ImplContainer(cid) => match cx.tcx.impl_trait_ref(cid) {
ty::TraitContainer => MethodLateContext::TraitAutoImpl,
ty::ImplContainer => match cx.tcx.impl_trait_ref(item.container_id(cx.tcx)) {
Some(_) => MethodLateContext::TraitImpl,
None => MethodLateContext::PlainImpl,
},
Expand Down
28 changes: 4 additions & 24 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {

fn get_fn_has_self_parameter(self, id: DefIndex) -> bool {
match self.kind(id) {
EntryKind::AssocFn(data) => data.decode(self).has_self,
EntryKind::AssocFn { has_self, .. } => has_self,
_ => false,
}
}
Expand All @@ -1134,28 +1134,21 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
}

fn get_associated_item(self, id: DefIndex) -> ty::AssocItem {
let def_key = self.def_key(id);
let parent = self.local_def_id(def_key.parent.unwrap());
let name = self.item_name(id);

let (kind, container, has_self) = match self.kind(id) {
EntryKind::AssocConst(container) => (ty::AssocKind::Const, container, false),
EntryKind::AssocFn(data) => {
let data = data.decode(self);
(ty::AssocKind::Fn, data.container, data.has_self)
}
EntryKind::AssocFn { container, has_self } => (ty::AssocKind::Fn, container, has_self),
EntryKind::AssocType(container) => (ty::AssocKind::Type, container, false),
_ => bug!("cannot get associated-item of `{:?}`", def_key),
_ => bug!("cannot get associated-item of `{:?}`", id),
};

ty::AssocItem {
name,
kind,
vis: self.get_visibility(id),
defaultness: container.defaultness(),
def_id: self.local_def_id(id),
trait_item_def_id: self.get_trait_item_def_id(id),
container: container.with_def_id(parent),
container,
fn_has_self_parameter: has_self,
}
}
Expand Down Expand Up @@ -1310,19 +1303,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
}
}

fn get_trait_of_item(self, id: DefIndex) -> Option<DefId> {
let def_key = self.def_key(id);
match def_key.disambiguated_data.data {
DefPathData::TypeNs(..) | DefPathData::ValueNs(..) => (),
// Not an associated item
_ => return None,
}
def_key.parent.and_then(|parent_index| match self.kind(parent_index) {
EntryKind::Trait | EntryKind::TraitAlias => Some(self.local_def_id(parent_index)),
_ => None,
})
}

fn get_native_libraries(self, sess: &'a Session) -> impl Iterator<Item = NativeLib> + 'a {
self.root.native_libraries.decode((self, sess))
}
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ provide! { <'tcx> tcx, def_id, other, cdata,
inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) }
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
item_attrs => { tcx.arena.alloc_from_iter(cdata.get_item_attrs(def_id.index, tcx.sess)) }
trait_of_item => { cdata.get_trait_of_item(def_id.index) }
is_mir_available => { cdata.is_item_mir_available(def_id.index) }
is_ctfe_mir_available => { cdata.is_ctfe_mir_available(def_id.index) }

Expand Down
Loading