Store associated item defaultness in impl_defaultness.

This commit is contained in:
Camille GILLOT 2022-03-12 19:36:11 +01:00
parent c9e134e1b6
commit 110f0656cb
25 changed files with 89 additions and 107 deletions

View File

@ -323,7 +323,7 @@ fn visit_assoc_type_binding(&mut self, type_binding: &'hir TypeBinding<'hir>) {
fn visit_trait_item_ref(&mut self, ii: &'hir TraitItemRef) { fn visit_trait_item_ref(&mut self, ii: &'hir TraitItemRef) {
// Do not visit the duplicate information in TraitItemRef. We want to // Do not visit the duplicate information in TraitItemRef. We want to
// map the actual nodes, not the duplicate ones in the *Ref. // 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); self.visit_nested_trait_item(id);
} }
@ -331,8 +331,7 @@ fn visit_trait_item_ref(&mut self, ii: &'hir TraitItemRef) {
fn visit_impl_item_ref(&mut self, ii: &'hir ImplItemRef) { fn visit_impl_item_ref(&mut self, ii: &'hir ImplItemRef) {
// Do not visit the duplicate information in ImplItemRef. We want to // Do not visit the duplicate information in ImplItemRef. We want to
// map the actual nodes, not the duplicate ones in the *Ref. // map the actual nodes, not the duplicate ones in the *Ref.
let ImplItemRef { id, ident: _, kind: _, span: _, defaultness: _, trait_item_def_id: _ } = let ImplItemRef { id, ident: _, kind: _, span: _, trait_item_def_id: _ } = *ii;
*ii;
self.visit_nested_impl_item(id); self.visit_nested_impl_item(id);
} }

View File

@ -755,17 +755,17 @@ fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
let hir_id = self.lower_node_id(i.id); let hir_id = self.lower_node_id(i.id);
let trait_item_def_id = hir_id.expect_owner(); 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) => { AssocItemKind::Const(_, ref ty, ref default) => {
let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type)); 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))); 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, .. }) => { AssocItemKind::Fn(box Fn { ref sig, ref generics, body: None, .. }) => {
let names = self.lower_fn_params_to_names(&sig.decl); let names = self.lower_fn_params_to_names(&sig.decl);
let (generics, sig) = let (generics, sig) =
self.lower_method_sig(generics, sig, i.id, FnDeclKind::Trait, None); 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), .. }) => { AssocItemKind::Fn(box Fn { ref sig, ref generics, body: Some(ref body), .. }) => {
let asyncness = sig.header.asyncness; let asyncness = sig.header.asyncness;
@ -778,7 +778,7 @@ fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
FnDeclKind::Trait, FnDeclKind::Trait,
asyncness.opt_return_id(), 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 { AssocItemKind::TyAlias(box TyAlias {
ref generics, ref generics,
@ -789,7 +789,7 @@ fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
}) => { }) => {
let mut generics = generics.clone(); let mut generics = generics.clone();
add_ty_alias_where_clause(&mut generics, where_clauses, false); add_ty_alias_where_clause(&mut generics, where_clauses, false);
self.lower_generics( let (generics, kind) = self.lower_generics(
&generics, &generics,
i.id, i.id,
ImplTraitContext::Disallowed(ImplTraitPosition::Generic), ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
@ -805,7 +805,8 @@ fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
ty, ty,
) )
}, },
) );
(generics, kind, ty.is_some())
} }
AssocItemKind::MacCall(..) => panic!("macro item shouldn't exist at this point"), AssocItemKind::MacCall(..) => panic!("macro item shouldn't exist at this point"),
}; };
@ -817,28 +818,25 @@ fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
generics, generics,
kind, kind,
span: self.lower_span(i.span), span: self.lower_span(i.span),
defaultness: hir::Defaultness::Default { has_value: has_default },
}; };
self.arena.alloc(item) self.arena.alloc(item)
} }
fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef { fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef {
let (kind, has_default) = match &i.kind { let kind = match &i.kind {
AssocItemKind::Const(_, _, default) => (hir::AssocItemKind::Const, default.is_some()), AssocItemKind::Const(..) => hir::AssocItemKind::Const,
AssocItemKind::TyAlias(box TyAlias { ty, .. }) => { AssocItemKind::TyAlias(..) => hir::AssocItemKind::Type,
(hir::AssocItemKind::Type, ty.is_some()) AssocItemKind::Fn(box Fn { sig, .. }) => {
} hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }
AssocItemKind::Fn(box Fn { sig, body, .. }) => {
(hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }, body.is_some())
} }
AssocItemKind::MacCall(..) => unimplemented!(), AssocItemKind::MacCall(..) => unimplemented!(),
}; };
let id = hir::TraitItemId { def_id: self.local_def_id(i.id) }; let id = hir::TraitItemId { def_id: self.local_def_id(i.id) };
let defaultness = hir::Defaultness::Default { has_value: has_default };
hir::TraitItemRef { hir::TraitItemRef {
id, id,
ident: self.lower_ident(i.ident), ident: self.lower_ident(i.ident),
span: self.lower_span(i.span), span: self.lower_span(i.span),
defaultness,
kind, kind,
} }
} }
@ -849,6 +847,10 @@ pub(crate) fn expr_err(&mut self, span: Span) -> hir::Expr<'hir> {
} }
fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'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 { let (generics, kind) = match &i.kind {
AssocItemKind::Const(_, ty, expr) => { AssocItemKind::Const(_, ty, expr) => {
let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type)); let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
@ -903,19 +905,16 @@ fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
kind, kind,
vis_span: self.lower_span(i.vis.span), vis_span: self.lower_span(i.vis.span),
span: self.lower_span(i.span), span: self.lower_span(i.span),
defaultness,
}; };
self.arena.alloc(item) self.arena.alloc(item)
} }
fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemRef { 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 { hir::ImplItemRef {
id: hir::ImplItemId { def_id: self.local_def_id(i.id) }, id: hir::ImplItemId { def_id: self.local_def_id(i.id) },
ident: self.lower_ident(i.ident), ident: self.lower_ident(i.ident),
span: self.lower_span(i.span), span: self.lower_span(i.span),
defaultness,
kind: match &i.kind { kind: match &i.kind {
AssocItemKind::Const(..) => hir::AssocItemKind::Const, AssocItemKind::Const(..) => hir::AssocItemKind::Const,
AssocItemKind::TyAlias(..) => hir::AssocItemKind::Type, AssocItemKind::TyAlias(..) => hir::AssocItemKind::Type,

View File

@ -2222,6 +2222,7 @@ pub struct TraitItem<'hir> {
pub generics: &'hir Generics<'hir>, pub generics: &'hir Generics<'hir>,
pub kind: TraitItemKind<'hir>, pub kind: TraitItemKind<'hir>,
pub span: Span, pub span: Span,
pub defaultness: Defaultness,
} }
impl TraitItem<'_> { impl TraitItem<'_> {
@ -2281,6 +2282,7 @@ pub struct ImplItem<'hir> {
pub def_id: LocalDefId, pub def_id: LocalDefId,
pub generics: &'hir Generics<'hir>, pub generics: &'hir Generics<'hir>,
pub kind: ImplItemKind<'hir>, pub kind: ImplItemKind<'hir>,
pub defaultness: Defaultness,
pub span: Span, pub span: Span,
pub vis_span: Span, pub vis_span: Span,
} }
@ -3083,7 +3085,6 @@ pub struct TraitItemRef {
pub ident: Ident, pub ident: Ident,
pub kind: AssocItemKind, pub kind: AssocItemKind,
pub span: Span, pub span: Span,
pub defaultness: Defaultness,
} }
/// A reference from an impl to one of its associated items. This /// A reference from an impl to one of its associated items. This
@ -3098,7 +3099,6 @@ pub struct ImplItemRef {
pub ident: Ident, pub ident: Ident,
pub kind: AssocItemKind, pub kind: AssocItemKind,
pub span: Span, pub span: Span,
pub defaultness: Defaultness,
/// When we are in a trait impl, link to the trait-item's id. /// When we are in a trait impl, link to the trait-item's id.
pub trait_item_def_id: Option<DefId>, pub trait_item_def_id: Option<DefId>,
} }
@ -3496,11 +3496,11 @@ mod size_asserts {
rustc_data_structures::static_assert_size!(ForeignItem<'static>, 72); rustc_data_structures::static_assert_size!(ForeignItem<'static>, 72);
rustc_data_structures::static_assert_size!(GenericBound<'_>, 48); rustc_data_structures::static_assert_size!(GenericBound<'_>, 48);
rustc_data_structures::static_assert_size!(Generics<'static>, 56); 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!(Impl<'static>, 80);
rustc_data_structures::static_assert_size!(Item<'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!(Pat<'static>, 88);
rustc_data_structures::static_assert_size!(QPath<'static>, 24); 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); rustc_data_structures::static_assert_size!(Ty<'static>, 72);
} }

View File

@ -948,6 +948,7 @@ 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>) { pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem<'v>) {
visitor.visit_ident(trait_item.ident); visitor.visit_ident(trait_item.ident);
visitor.visit_generics(&trait_item.generics); visitor.visit_generics(&trait_item.generics);
visitor.visit_defaultness(&trait_item.defaultness);
match trait_item.kind { match trait_item.kind {
TraitItemKind::Const(ref ty, default) => { TraitItemKind::Const(ref ty, default) => {
visitor.visit_id(trait_item.hir_id()); visitor.visit_id(trait_item.hir_id());
@ -980,19 +981,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) { 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. // 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_nested_trait_item(id);
visitor.visit_ident(ident); visitor.visit_ident(ident);
visitor.visit_associated_item_kind(kind); 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>) { 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. // 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_ident(ident);
visitor.visit_generics(generics); visitor.visit_generics(generics);
visitor.visit_defaultness(defaultness);
match *kind { match *kind {
ImplItemKind::Const(ref ty, body) => { ImplItemKind::Const(ref ty, body) => {
visitor.visit_id(impl_item.hir_id()); visitor.visit_id(impl_item.hir_id());
@ -1027,12 +1036,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) { 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. // 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: _ } = let ImplItemRef { id, ident, ref kind, span: _, trait_item_def_id: _ } = *impl_item_ref;
*impl_item_ref;
visitor.visit_nested_impl_item(id); visitor.visit_nested_impl_item(id);
visitor.visit_ident(ident); visitor.visit_ident(ident);
visitor.visit_associated_item_kind(kind); visitor.visit_associated_item_kind(kind);
visitor.visit_defaultness(defaultness);
} }
pub fn walk_struct_def<'v, V: Visitor<'v>>( pub fn walk_struct_def<'v, V: Visitor<'v>>(

View File

@ -1152,7 +1152,6 @@ fn get_associated_item(self, id: DefIndex) -> ty::AssocItem {
name, name,
kind, kind,
vis: self.get_visibility(id), vis: self.get_visibility(id),
defaultness: container.defaultness(),
def_id: self.local_def_id(id), def_id: self.local_def_id(id),
trait_item_def_id: self.get_trait_item_def_id(id), trait_item_def_id: self.get_trait_item_def_id(id),
container: container.with_def_id(parent), container: container.with_def_id(parent),

View File

@ -1212,14 +1212,9 @@ fn encode_info_for_trait_item(&mut self, def_id: DefId) {
let tcx = self.tcx; let tcx = self.tcx;
let ast_item = tcx.hir().expect_trait_item(def_id.expect_local()); let ast_item = tcx.hir().expect_trait_item(def_id.expect_local());
self.tables.impl_defaultness.set(def_id.index, ast_item.defaultness);
let trait_item = tcx.associated_item(def_id); let trait_item = tcx.associated_item(def_id);
let container = match trait_item.defaultness {
hir::Defaultness::Default { has_value: true } => AssocContainer::TraitWithDefault,
hir::Defaultness::Default { has_value: false } => AssocContainer::TraitRequired,
hir::Defaultness::Final => span_bug!(ast_item.span, "traits cannot have final items"),
};
match trait_item.kind { match trait_item.kind {
ty::AssocKind::Const => { ty::AssocKind::Const => {
let rendered = rustc_hir_pretty::to_string( let rendered = rustc_hir_pretty::to_string(
@ -1227,7 +1222,7 @@ fn encode_info_for_trait_item(&mut self, def_id: DefId) {
|s| s.print_trait_item(ast_item), |s| s.print_trait_item(ast_item),
); );
record!(self.tables.kind[def_id] <- EntryKind::AssocConst(container)); record!(self.tables.kind[def_id] <- EntryKind::AssocConst(AssocContainer::Trait));
record!(self.tables.mir_const_qualif[def_id] <- mir::ConstQualifs::default()); record!(self.tables.mir_const_qualif[def_id] <- mir::ConstQualifs::default());
record!(self.tables.rendered_const[def_id] <- rendered); record!(self.tables.rendered_const[def_id] <- rendered);
} }
@ -1244,13 +1239,13 @@ fn encode_info_for_trait_item(&mut self, def_id: DefId) {
self.tables.asyncness.set(def_id.index, m_sig.header.asyncness); self.tables.asyncness.set(def_id.index, m_sig.header.asyncness);
self.tables.constness.set(def_id.index, hir::Constness::NotConst); self.tables.constness.set(def_id.index, hir::Constness::NotConst);
record!(self.tables.kind[def_id] <- EntryKind::AssocFn(self.lazy(AssocFnData { record!(self.tables.kind[def_id] <- EntryKind::AssocFn(self.lazy(AssocFnData {
container, container:AssocContainer::Trait,
has_self: trait_item.fn_has_self_parameter, has_self: trait_item.fn_has_self_parameter,
}))); })));
} }
ty::AssocKind::Type => { ty::AssocKind::Type => {
self.encode_explicit_item_bounds(def_id); self.encode_explicit_item_bounds(def_id);
record!(self.tables.kind[def_id] <- EntryKind::AssocType(container)); record!(self.tables.kind[def_id] <- EntryKind::AssocType(AssocContainer::Trait));
} }
} }
match trait_item.kind { match trait_item.kind {
@ -1258,7 +1253,7 @@ fn encode_info_for_trait_item(&mut self, def_id: DefId) {
self.encode_item_type(def_id); self.encode_item_type(def_id);
} }
ty::AssocKind::Type => { ty::AssocKind::Type => {
if trait_item.defaultness.has_value() { if ast_item.defaultness.has_value() {
self.encode_item_type(def_id); self.encode_item_type(def_id);
} }
} }
@ -1273,23 +1268,16 @@ fn encode_info_for_impl_item(&mut self, def_id: DefId) {
let tcx = self.tcx; let tcx = self.tcx;
let ast_item = self.tcx.hir().expect_impl_item(def_id.expect_local()); let ast_item = self.tcx.hir().expect_impl_item(def_id.expect_local());
self.tables.impl_defaultness.set(def_id.index, ast_item.defaultness);
let impl_item = self.tcx.associated_item(def_id); let impl_item = self.tcx.associated_item(def_id);
let container = match impl_item.defaultness {
hir::Defaultness::Default { has_value: true } => AssocContainer::ImplDefault,
hir::Defaultness::Final => AssocContainer::ImplFinal,
hir::Defaultness::Default { has_value: false } => {
span_bug!(ast_item.span, "impl items always have values (currently)")
}
};
match impl_item.kind { match impl_item.kind {
ty::AssocKind::Const => { ty::AssocKind::Const => {
if let hir::ImplItemKind::Const(_, body_id) = ast_item.kind { if let hir::ImplItemKind::Const(_, body_id) = ast_item.kind {
let qualifs = self.tcx.at(ast_item.span).mir_const_qualif(def_id); let qualifs = self.tcx.at(ast_item.span).mir_const_qualif(def_id);
let const_data = self.encode_rendered_const_for_body(body_id); let const_data = self.encode_rendered_const_for_body(body_id);
record!(self.tables.kind[def_id] <- EntryKind::AssocConst(container)); record!(self.tables.kind[def_id] <- EntryKind::AssocConst(AssocContainer::Impl));
record!(self.tables.mir_const_qualif[def_id] <- qualifs); record!(self.tables.mir_const_qualif[def_id] <- qualifs);
record!(self.tables.rendered_const[def_id] <- const_data); record!(self.tables.rendered_const[def_id] <- const_data);
} else { } else {
@ -1308,12 +1296,12 @@ fn encode_info_for_impl_item(&mut self, def_id: DefId) {
}; };
self.tables.constness.set(def_id.index, constness); self.tables.constness.set(def_id.index, constness);
record!(self.tables.kind[def_id] <- EntryKind::AssocFn(self.lazy(AssocFnData { record!(self.tables.kind[def_id] <- EntryKind::AssocFn(self.lazy(AssocFnData {
container, container:AssocContainer::Impl,
has_self: impl_item.fn_has_self_parameter, has_self: impl_item.fn_has_self_parameter,
}))); })));
} }
ty::AssocKind::Type => { ty::AssocKind::Type => {
record!(self.tables.kind[def_id] <- EntryKind::AssocType(container)); record!(self.tables.kind[def_id] <- EntryKind::AssocType(AssocContainer::Impl));
} }
} }
self.encode_item_type(def_id); self.encode_item_type(def_id);

View File

@ -439,32 +439,15 @@ struct VariantData {
/// a default, or an in impl, whether it's marked "default". /// a default, or an in impl, whether it's marked "default".
#[derive(Copy, Clone, TyEncodable, TyDecodable)] #[derive(Copy, Clone, TyEncodable, TyDecodable)]
enum AssocContainer { enum AssocContainer {
TraitRequired, Trait,
TraitWithDefault, Impl,
ImplDefault,
ImplFinal,
} }
impl AssocContainer { impl AssocContainer {
fn with_def_id(&self, def_id: DefId) -> ty::AssocItemContainer { fn with_def_id(&self, def_id: DefId) -> ty::AssocItemContainer {
match *self { match *self {
AssocContainer::TraitRequired | AssocContainer::TraitWithDefault => { AssocContainer::Trait => ty::TraitContainer(def_id),
ty::TraitContainer(def_id) AssocContainer::Impl => ty::ImplContainer(def_id),
}
AssocContainer::ImplDefault | AssocContainer::ImplFinal => ty::ImplContainer(def_id),
}
}
fn defaultness(&self) -> hir::Defaultness {
match *self {
AssocContainer::TraitRequired => hir::Defaultness::Default { has_value: false },
AssocContainer::TraitWithDefault | AssocContainer::ImplDefault => {
hir::Defaultness::Default { has_value: true }
}
AssocContainer::ImplFinal => hir::Defaultness::Final,
} }
} }
} }

View File

@ -217,7 +217,7 @@ pub fn leaf_def(mut self, tcx: TyCtxt<'tcx>, trait_item_def_id: DefId) -> Option
self.find_map(|node| { self.find_map(|node| {
if let Some(item) = node.item(tcx, trait_item_def_id) { if let Some(item) = node.item(tcx, trait_item_def_id) {
if finalizing_node.is_none() { if finalizing_node.is_none() {
let is_specializable = item.defaultness.is_default() let is_specializable = item.defaultness(tcx).is_default()
|| tcx.impl_defaultness(node.def_id()).is_default(); || tcx.impl_defaultness(node.def_id()).is_default();
if !is_specializable { if !is_specializable {

View File

@ -47,7 +47,6 @@ pub struct AssocItem {
pub name: Symbol, pub name: Symbol,
pub kind: AssocKind, pub kind: AssocKind,
pub vis: Visibility, pub vis: Visibility,
pub defaultness: hir::Defaultness,
pub container: AssocItemContainer, pub container: AssocItemContainer,
/// If this is an item in an impl of a trait then this is the `DefId` of /// If this is an item in an impl of a trait then this is the `DefId` of
@ -64,6 +63,10 @@ pub fn ident(&self, tcx: TyCtxt<'_>) -> Ident {
Ident::new(self.name, tcx.def_ident_span(self.def_id).unwrap()) Ident::new(self.name, tcx.def_ident_span(self.def_id).unwrap())
} }
pub fn defaultness(&self, tcx: TyCtxt<'_>) -> hir::Defaultness {
tcx.impl_defaultness(self.def_id)
}
pub fn signature(&self, tcx: TyCtxt<'_>) -> String { pub fn signature(&self, tcx: TyCtxt<'_>) -> String {
match self.kind { match self.kind {
ty::AssocKind::Fn => { ty::AssocKind::Fn => {

View File

@ -844,7 +844,8 @@ fn point_at_associated_type(
hir::AssocItemKind::Type => { hir::AssocItemKind::Type => {
// FIXME: account for returning some type in a trait fn impl that has // FIXME: account for returning some type in a trait fn impl that has
// an assoc type as a return type (#72076). // an assoc type as a return type (#72076).
if let hir::Defaultness::Default { has_value: true } = item.defaultness if let hir::Defaultness::Default { has_value: true } =
self.impl_defaultness(item.id.def_id)
{ {
if self.type_of(item.id.def_id) == found { if self.type_of(item.id.def_id) == found {
diag.span_label( diag.span_label(

View File

@ -1945,7 +1945,7 @@ pub fn typeck_body(self, body: hir::BodyId) -> &'tcx TypeckResults<'tcx> {
pub fn provided_trait_methods(self, id: DefId) -> impl 'tcx + Iterator<Item = &'tcx AssocItem> { pub fn provided_trait_methods(self, id: DefId) -> impl 'tcx + Iterator<Item = &'tcx AssocItem> {
self.associated_items(id) self.associated_items(id)
.in_definition_order() .in_definition_order()
.filter(|item| item.kind == AssocKind::Fn && item.defaultness.has_value()) .filter(move |item| item.kind == AssocKind::Fn && item.defaultness(self).has_value())
} }
/// Look up the name of a definition across crates. This does not look at HIR. /// Look up the name of a definition across crates. This does not look at HIR.

View File

@ -734,11 +734,12 @@ fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
self.reach(item.def_id, item_level).generics().predicates(); self.reach(item.def_id, item_level).generics().predicates();
for trait_item_ref in trait_item_refs { for trait_item_ref in trait_item_refs {
let tcx = self.tcx;
let mut reach = self.reach(trait_item_ref.id.def_id, item_level); let mut reach = self.reach(trait_item_ref.id.def_id, item_level);
reach.generics().predicates(); reach.generics().predicates();
if trait_item_ref.kind == AssocItemKind::Type if trait_item_ref.kind == AssocItemKind::Type
&& !trait_item_ref.defaultness.has_value() && !tcx.impl_defaultness(trait_item_ref.id.def_id).has_value()
{ {
// No type to visit. // No type to visit.
} else { } else {
@ -1839,14 +1840,13 @@ fn check_assoc_item(
&self, &self,
def_id: LocalDefId, def_id: LocalDefId,
assoc_item_kind: AssocItemKind, assoc_item_kind: AssocItemKind,
defaultness: hir::Defaultness,
vis: ty::Visibility, vis: ty::Visibility,
) { ) {
let mut check = self.check(def_id, vis); let mut check = self.check(def_id, vis);
let (check_ty, is_assoc_ty) = match assoc_item_kind { let (check_ty, is_assoc_ty) = match assoc_item_kind {
AssocItemKind::Const | AssocItemKind::Fn { .. } => (true, false), AssocItemKind::Const | AssocItemKind::Fn { .. } => (true, false),
AssocItemKind::Type => (defaultness.has_value(), true), AssocItemKind::Type => (self.tcx.impl_defaultness(def_id).has_value(), true),
}; };
check.in_assoc_ty = is_assoc_ty; check.in_assoc_ty = is_assoc_ty;
check.generics().predicates(); check.generics().predicates();
@ -1878,7 +1878,6 @@ pub fn check_item(&mut self, id: ItemId) {
self.check_assoc_item( self.check_assoc_item(
trait_item_ref.id.def_id, trait_item_ref.id.def_id,
trait_item_ref.kind, trait_item_ref.kind,
trait_item_ref.defaultness,
item_visibility, item_visibility,
); );
@ -1951,7 +1950,6 @@ pub fn check_item(&mut self, id: ItemId) {
self.check_assoc_item( self.check_assoc_item(
impl_item_ref.id.def_id, impl_item_ref.id.def_id,
impl_item_ref.kind, impl_item_ref.kind,
impl_item_ref.defaultness,
impl_item_vis, impl_item_vis,
); );
} }

View File

@ -697,7 +697,7 @@ fn fn_type(seg: &hir::PathSegment<'_>) -> bool {
} }
Res::Def(HirDefKind::AssocFn, decl_id) => { Res::Def(HirDefKind::AssocFn, decl_id) => {
let def_id = if decl_id.is_local() { let def_id = if decl_id.is_local() {
if self.tcx.associated_item(decl_id).defaultness.has_value() { if self.tcx.impl_defaultness(decl_id).has_value() {
Some(decl_id) Some(decl_id)
} else { } else {
None None

View File

@ -1988,7 +1988,7 @@ fn confirm_impl_candidate<'cx, 'tcx>(
return Progress { term: tcx.ty_error().into(), obligations: nested }; return Progress { term: tcx.ty_error().into(), obligations: nested };
}; };
if !assoc_ty.item.defaultness.has_value() { if !assoc_ty.item.defaultness(tcx).has_value() {
// This means that the impl is missing a definition for the // This means that the impl is missing a definition for the
// associated type. This error will be reported by the type // associated type. This error will be reported by the type
// checker method `check_impl_items_against_trait`, so here we // checker method `check_impl_items_against_trait`, so here we
@ -2089,7 +2089,11 @@ fn assoc_def(
return Ok(specialization_graph::LeafDef { return Ok(specialization_graph::LeafDef {
item: *item, item: *item,
defining_node: impl_node, defining_node: impl_node,
finalizing_node: if item.defaultness.is_default() { None } else { Some(impl_node) }, finalizing_node: if item.defaultness(tcx).is_default() {
None
} else {
Some(impl_node)
},
}); });
} }

View File

@ -358,7 +358,8 @@ pub fn generator_trait_ref_and_outputs<'tcx>(
} }
pub fn impl_item_is_final(tcx: TyCtxt<'_>, assoc_item: &ty::AssocItem) -> bool { pub fn impl_item_is_final(tcx: TyCtxt<'_>, assoc_item: &ty::AssocItem) -> bool {
assoc_item.defaultness.is_final() && tcx.impl_defaultness(assoc_item.container.id()).is_final() assoc_item.defaultness(tcx).is_final()
&& tcx.impl_defaultness(assoc_item.container.id()).is_final()
} }
pub enum TupleArgumentsFlag { pub enum TupleArgumentsFlag {

View File

@ -103,7 +103,6 @@ fn associated_item_from_trait_item_ref(
name: trait_item_ref.ident.name, name: trait_item_ref.ident.name,
kind, kind,
vis: tcx.visibility(def_id), vis: tcx.visibility(def_id),
defaultness: trait_item_ref.defaultness,
def_id: def_id.to_def_id(), def_id: def_id.to_def_id(),
trait_item_def_id: Some(def_id.to_def_id()), trait_item_def_id: Some(def_id.to_def_id()),
container: ty::TraitContainer(parent_def_id.to_def_id()), container: ty::TraitContainer(parent_def_id.to_def_id()),
@ -127,7 +126,6 @@ fn associated_item_from_impl_item_ref(
name: impl_item_ref.ident.name, name: impl_item_ref.ident.name,
kind, kind,
vis: tcx.visibility(def_id), vis: tcx.visibility(def_id),
defaultness: impl_item_ref.defaultness,
def_id: def_id.to_def_id(), def_id: def_id.to_def_id(),
trait_item_def_id: impl_item_ref.trait_item_def_id, trait_item_def_id: impl_item_ref.trait_item_def_id,
container: ty::ImplContainer(parent_def_id.to_def_id()), container: ty::ImplContainer(parent_def_id.to_def_id()),

View File

@ -281,7 +281,7 @@ fn resolve_associated_item<'tcx>(
} }
// If the item does not have a value, then we cannot return an instance. // If the item does not have a value, then we cannot return an instance.
if !leaf_def.item.defaultness.has_value() { if !leaf_def.item.defaultness(tcx).has_value() {
return Ok(None); return Ok(None);
} }

View File

@ -70,11 +70,13 @@ fn sized_constraint_for_ty<'tcx>(
} }
fn impl_defaultness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Defaultness { fn impl_defaultness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Defaultness {
let item = tcx.hir().expect_item(def_id.expect_local()); match tcx.hir().get_by_def_id(def_id.expect_local()) {
if let hir::ItemKind::Impl(impl_) = &item.kind { hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(impl_), .. }) => impl_.defaultness,
impl_.defaultness hir::Node::ImplItem(hir::ImplItem { defaultness, .. })
} else { | hir::Node::TraitItem(hir::TraitItem { defaultness, .. }) => *defaultness,
bug!("`impl_defaultness` called on {:?}", item); node => {
bug!("`impl_defaultness` called on {:?}", node);
}
} }
} }

View File

@ -1098,7 +1098,7 @@ fn check_impl_items_against_trait<'tcx>(
for &trait_item_id in tcx.associated_item_def_ids(impl_trait_ref.def_id) { for &trait_item_id in tcx.associated_item_def_ids(impl_trait_ref.def_id) {
let is_implemented = ancestors let is_implemented = ancestors
.leaf_def(tcx, trait_item_id) .leaf_def(tcx, trait_item_id)
.map_or(false, |node_item| node_item.item.defaultness.has_value()); .map_or(false, |node_item| node_item.item.defaultness(tcx).has_value());
if !is_implemented && tcx.impl_defaultness(impl_id).is_final() { if !is_implemented && tcx.impl_defaultness(impl_id).is_final() {
missing_items.push(tcx.associated_item(trait_item_id)); missing_items.push(tcx.associated_item(trait_item_id));

View File

@ -1007,7 +1007,7 @@ fn check_associated_item(
if let ty::AssocItemContainer::TraitContainer(_) = item.container { if let ty::AssocItemContainer::TraitContainer(_) = item.container {
check_associated_type_bounds(wfcx, item, span) check_associated_type_bounds(wfcx, item, span)
} }
if item.defaultness.has_value() { if item.defaultness(tcx).has_value() {
let ty = tcx.type_of(item.def_id); let ty = tcx.type_of(item.def_id);
let ty = wfcx.normalize(span, Some(WellFormedLoc::Ty(item_id)), ty); let ty = wfcx.normalize(span, Some(WellFormedLoc::Ty(item_id)), ty);
wfcx.register_wf_obligation(span, loc, ty.into()); wfcx.register_wf_obligation(span, loc, ty.into());

View File

@ -1234,7 +1234,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: DefId) -> ty::TraitDef {
match item { match item {
Some(item) if matches!(item.kind, hir::AssocItemKind::Fn { .. }) => { Some(item) if matches!(item.kind, hir::AssocItemKind::Fn { .. }) => {
if !item.defaultness.has_value() { if !tcx.impl_defaultness(item.id.def_id).has_value() {
tcx.sess tcx.sess
.struct_span_err( .struct_span_err(
item.span, item.span,

View File

@ -106,7 +106,7 @@ fn enforce_impl_params_are_constrained(tcx: TyCtxt<'_>, impl_def_id: LocalDefId)
let item = tcx.associated_item(def_id); let item = tcx.associated_item(def_id);
match item.kind { match item.kind {
ty::AssocKind::Type => { ty::AssocKind::Type => {
if item.defaultness.has_value() { if item.defaultness(tcx).has_value() {
cgp::parameters_for(&tcx.type_of(def_id), true) cgp::parameters_for(&tcx.type_of(def_id), true)
} else { } else {
Vec::new() Vec::new()

View File

@ -1101,7 +1101,7 @@ fn clean(&self, cx: &mut DocContext<'tcx>) -> Item {
} }
hir::ImplItemKind::Fn(ref sig, body) => { hir::ImplItemKind::Fn(ref sig, body) => {
let m = clean_function(cx, sig, self.generics, body); let m = clean_function(cx, sig, self.generics, body);
let defaultness = cx.tcx.associated_item(self.def_id).defaultness; let defaultness = cx.tcx.impl_defaultness(self.def_id);
MethodItem(m, Some(defaultness)) MethodItem(m, Some(defaultness))
} }
hir::ImplItemKind::TyAlias(hir_ty) => { hir::ImplItemKind::TyAlias(hir_ty) => {
@ -1140,7 +1140,7 @@ fn clean(&self, cx: &mut DocContext<'tcx>) -> Item {
let provided = match self.container { let provided = match self.container {
ty::ImplContainer(_) => true, ty::ImplContainer(_) => true,
ty::TraitContainer(_) => self.defaultness.has_value(), ty::TraitContainer(_) => tcx.impl_defaultness(self.def_id).has_value(),
}; };
if provided { if provided {
AssocConstItem(ty, ConstantKind::Extern { def_id: self.def_id }) AssocConstItem(ty, ConstantKind::Extern { def_id: self.def_id })
@ -1179,11 +1179,11 @@ fn clean(&self, cx: &mut DocContext<'tcx>) -> Item {
let provided = match self.container { let provided = match self.container {
ty::ImplContainer(_) => true, ty::ImplContainer(_) => true,
ty::TraitContainer(_) => self.defaultness.has_value(), ty::TraitContainer(_) => self.defaultness(tcx).has_value(),
}; };
if provided { if provided {
let defaultness = match self.container { let defaultness = match self.container {
ty::ImplContainer(_) => Some(self.defaultness), ty::ImplContainer(_) => Some(self.defaultness(tcx)),
ty::TraitContainer(_) => None, ty::TraitContainer(_) => None,
}; };
MethodItem(Box::new(Function { generics, decl }), defaultness) MethodItem(Box::new(Function { generics, decl }), defaultness)
@ -1280,7 +1280,7 @@ fn param_eq_arg(param: &GenericParamDef, arg: &GenericArg) -> bool {
None => bounds.push(GenericBound::maybe_sized(cx)), None => bounds.push(GenericBound::maybe_sized(cx)),
} }
if self.defaultness.has_value() { if tcx.impl_defaultness(self.def_id).has_value() {
AssocTypeItem( AssocTypeItem(
Box::new(Typedef { Box::new(Typedef {
type_: clean_middle_ty( type_: clean_middle_ty(

View File

@ -234,7 +234,7 @@ pub(crate) fn render(&self, s: &mut String, tcx: TyCtxt<'_>) {
&UrlFragment::Item(def_id) => { &UrlFragment::Item(def_id) => {
let kind = match tcx.def_kind(def_id) { let kind = match tcx.def_kind(def_id) {
DefKind::AssocFn => { DefKind::AssocFn => {
if tcx.associated_item(def_id).defaultness.has_value() { if tcx.impl_defaultness(def_id).has_value() {
"method." "method."
} else { } else {
"tymethod." "tymethod."

View File

@ -105,7 +105,7 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {
match tit_.kind { match tit_.kind {
hir::TraitItemKind::Const(..) | hir::TraitItemKind::Type(..) => {}, hir::TraitItemKind::Const(..) | hir::TraitItemKind::Type(..) => {},
hir::TraitItemKind::Fn(..) => { hir::TraitItemKind::Fn(..) => {
if tit.defaultness.has_value() { if cx.tcx.impl_defaultness(tit.id.def_id).has_value() {
// trait method with default body needs inline in case // trait method with default body needs inline in case
// an impl is not provided // an impl is not provided
let desc = "a default trait method"; let desc = "a default trait method";