diff options
Diffstat (limited to 'compiler')
27 files changed, 367 insertions, 563 deletions
diff --git a/compiler/rustc_ast_lowering/src/delegation.rs b/compiler/rustc_ast_lowering/src/delegation.rs index 42d25b512f5..9bfcd232221 100644 --- a/compiler/rustc_ast_lowering/src/delegation.rs +++ b/compiler/rustc_ast_lowering/src/delegation.rs @@ -47,6 +47,7 @@ use rustc_errors::ErrorGuaranteed; use rustc_hir::def_id::DefId; use rustc_middle::span_bug; use rustc_middle::ty::{Asyncness, ResolverAstLowering}; +use rustc_span::symbol::kw; use rustc_span::{Ident, Span, Symbol}; use {rustc_ast as ast, rustc_hir as hir}; @@ -61,21 +62,6 @@ pub(crate) struct DelegationResults<'hir> { } impl<'hir> LoweringContext<'_, 'hir> { - /// Defines whether the delegatee is an associated function whose first parameter is `self`. - pub(crate) fn delegatee_is_method( - &self, - item_id: NodeId, - path_id: NodeId, - span: Span, - is_in_trait_impl: bool, - ) -> bool { - let sig_id = self.get_delegation_sig_id(item_id, path_id, span, is_in_trait_impl); - let Ok(sig_id) = sig_id else { - return false; - }; - self.is_method(sig_id, span) - } - fn is_method(&self, def_id: DefId, span: Span) -> bool { match self.tcx.def_kind(def_id) { DefKind::Fn => false, @@ -101,10 +87,11 @@ impl<'hir> LoweringContext<'_, 'hir> { let sig_id = self.get_delegation_sig_id(item_id, delegation.id, span, is_in_trait_impl); match sig_id { Ok(sig_id) => { + let is_method = self.is_method(sig_id, span); let (param_count, c_variadic) = self.param_count(sig_id); let decl = self.lower_delegation_decl(sig_id, param_count, c_variadic, span); let sig = self.lower_delegation_sig(sig_id, decl, span); - let body_id = self.lower_delegation_body(delegation, param_count, span); + let body_id = self.lower_delegation_body(delegation, is_method, param_count, span); let ident = self.lower_ident(delegation.ident); let generics = self.lower_delegation_generics(span); DelegationResults { body_id, sig, ident, generics } @@ -234,10 +221,21 @@ impl<'hir> LoweringContext<'_, 'hir> { hir::FnSig { decl, header, span } } - fn generate_param(&mut self, idx: usize, span: Span) -> (hir::Param<'hir>, NodeId) { + fn generate_param( + &mut self, + is_method: bool, + idx: usize, + span: Span, + ) -> (hir::Param<'hir>, NodeId) { let pat_node_id = self.next_node_id(); let pat_id = self.lower_node_id(pat_node_id); - let ident = Ident::with_dummy_span(Symbol::intern(&format!("arg{idx}"))); + // FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`. + let name = if is_method && idx == 0 { + kw::SelfLower + } else { + Symbol::intern(&format!("arg{idx}")) + }; + let ident = Ident::with_dummy_span(name); let pat = self.arena.alloc(hir::Pat { hir_id: pat_id, kind: hir::PatKind::Binding(hir::BindingMode::NONE, pat_id, ident, None), @@ -248,9 +246,21 @@ impl<'hir> LoweringContext<'_, 'hir> { (hir::Param { hir_id: self.next_id(), pat, ty_span: span, span }, pat_node_id) } - fn generate_arg(&mut self, idx: usize, param_id: HirId, span: Span) -> hir::Expr<'hir> { + fn generate_arg( + &mut self, + is_method: bool, + idx: usize, + param_id: HirId, + span: Span, + ) -> hir::Expr<'hir> { + // FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`. + let name = if is_method && idx == 0 { + kw::SelfLower + } else { + Symbol::intern(&format!("arg{idx}")) + }; let segments = self.arena.alloc_from_iter(iter::once(hir::PathSegment { - ident: Ident::with_dummy_span(Symbol::intern(&format!("arg{idx}"))), + ident: Ident::with_dummy_span(name), hir_id: self.next_id(), res: Res::Local(param_id), args: None, @@ -264,6 +274,7 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_delegation_body( &mut self, delegation: &Delegation, + is_method: bool, param_count: usize, span: Span, ) -> BodyId { @@ -274,7 +285,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let mut args: Vec<hir::Expr<'_>> = Vec::with_capacity(param_count); for idx in 0..param_count { - let (param, pat_node_id) = this.generate_param(idx, span); + let (param, pat_node_id) = this.generate_param(is_method, idx, span); parameters.push(param); let arg = if let Some(block) = block @@ -290,7 +301,7 @@ impl<'hir> LoweringContext<'_, 'hir> { this.ident_and_label_to_local_id.insert(pat_node_id, param.pat.hir_id.local_id); this.lower_target_expr(&block) } else { - this.generate_arg(idx, param.pat.hir_id, span) + this.generate_arg(is_method, idx, param.pat.hir_id, span) }; args.push(arg); } diff --git a/compiler/rustc_ast_lowering/src/index.rs b/compiler/rustc_ast_lowering/src/index.rs index 956cb580d10..1ef64f5a352 100644 --- a/compiler/rustc_ast_lowering/src/index.rs +++ b/compiler/rustc_ast_lowering/src/index.rs @@ -381,28 +381,16 @@ 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: _ } = *ii; - - self.visit_nested_trait_item(id); + fn visit_trait_item_ref(&mut self, id: &'hir TraitItemId) { + 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: _, trait_item_def_id: _ } = *ii; - - self.visit_nested_impl_item(id); + fn visit_impl_item_ref(&mut self, id: &'hir ImplItemId) { + self.visit_nested_impl_item(*id); } - fn visit_foreign_item_ref(&mut self, fi: &'hir ForeignItemRef) { - // Do not visit the duplicate information in ForeignItemRef. We want to - // map the actual nodes, not the duplicate ones in the *Ref. - let ForeignItemRef { id, ident: _, span: _ } = *fi; - - self.visit_nested_foreign_item(id); + fn visit_foreign_item_ref(&mut self, id: &'hir ForeignItemId) { + self.visit_nested_foreign_item(*id); } fn visit_where_predicate(&mut self, predicate: &'hir WherePredicate<'hir>) { diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index bdcb750ba26..9d40a7386f6 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -393,11 +393,9 @@ impl<'hir> LoweringContext<'_, 'hir> { (trait_ref, lowered_ty) }); - let new_impl_items = self.arena.alloc_from_iter( - impl_items - .iter() - .map(|item| self.lower_impl_item_ref(item, trait_ref.is_some())), - ); + let new_impl_items = self + .arena + .alloc_from_iter(impl_items.iter().map(|item| self.lower_impl_item_ref(item))); // `defaultness.has_value()` is never called for an `impl`, always `true` in order // to not cause an assertion failure inside the `lower_defaultness` function. @@ -706,14 +704,8 @@ impl<'hir> LoweringContext<'_, 'hir> { self.arena.alloc(item) } - fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef { - hir::ForeignItemRef { - id: hir::ForeignItemId { owner_id: self.owner_id(i.id) }, - // `unwrap` is safe because `ForeignItemKind::MacCall` is the only foreign item kind - // without an identifier and it cannot reach here. - ident: self.lower_ident(i.kind.ident().unwrap()), - span: self.lower_span(i.span), - } + fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemId { + hir::ForeignItemId { owner_id: self.owner_id(i.id) } } fn lower_variant(&mut self, item_kind: &ItemKind, v: &Variant) -> hir::Variant<'hir> { @@ -972,32 +964,8 @@ impl<'hir> LoweringContext<'_, 'hir> { self.arena.alloc(item) } - fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef { - let (ident, kind) = match &i.kind { - AssocItemKind::Const(box ConstItem { ident, .. }) => { - (*ident, hir::AssocItemKind::Const) - } - AssocItemKind::Type(box TyAlias { ident, .. }) => (*ident, hir::AssocItemKind::Type), - AssocItemKind::Fn(box Fn { ident, sig, .. }) => { - (*ident, hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }) - } - AssocItemKind::Delegation(box delegation) => ( - delegation.ident, - hir::AssocItemKind::Fn { - has_self: self.delegatee_is_method(i.id, delegation.id, i.span, false), - }, - ), - AssocItemKind::MacCall(..) | AssocItemKind::DelegationMac(..) => { - panic!("macros should have been expanded by now") - } - }; - let id = hir::TraitItemId { owner_id: self.owner_id(i.id) }; - hir::TraitItemRef { - id, - ident: self.lower_ident(ident), - span: self.lower_span(i.span), - kind, - } + fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemId { + hir::TraitItemId { owner_id: self.owner_id(i.id) } } /// Construct `ExprKind::Err` for the given `span`. @@ -1128,41 +1096,17 @@ impl<'hir> LoweringContext<'_, 'hir> { span: self.lower_span(i.span), defaultness, has_delayed_lints: !self.delayed_lints.is_empty(), - }; - self.arena.alloc(item) - } - - fn lower_impl_item_ref(&mut self, i: &AssocItem, is_in_trait_impl: bool) -> hir::ImplItemRef { - hir::ImplItemRef { - id: hir::ImplItemId { owner_id: self.owner_id(i.id) }, - // `unwrap` is safe because `AssocItemKind::{MacCall,DelegationMac}` are the only - // assoc item kinds without an identifier and they cannot reach here. - ident: self.lower_ident(i.kind.ident().unwrap()), - span: self.lower_span(i.span), - kind: match &i.kind { - AssocItemKind::Const(..) => hir::AssocItemKind::Const, - AssocItemKind::Type(..) => hir::AssocItemKind::Type, - AssocItemKind::Fn(box Fn { sig, .. }) => { - hir::AssocItemKind::Fn { has_self: sig.decl.has_self() } - } - AssocItemKind::Delegation(box delegation) => hir::AssocItemKind::Fn { - has_self: self.delegatee_is_method( - i.id, - delegation.id, - i.span, - is_in_trait_impl, - ), - }, - AssocItemKind::MacCall(..) | AssocItemKind::DelegationMac(..) => { - panic!("macros should have been expanded by now") - } - }, trait_item_def_id: self .resolver .get_partial_res(i.id) .map(|r| r.expect_full_res().opt_def_id()) .unwrap_or(None), - } + }; + self.arena.alloc(item) + } + + fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemId { + hir::ImplItemId { owner_id: self.owner_id(i.id) } } fn lower_defaultness( diff --git a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs index 7c69baba62e..a06540f8325 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs @@ -681,46 +681,30 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { return (false, false, None); } let my_def = self.body.source.def_id(); - let my_hir = self.infcx.tcx.local_def_id_to_hir_id(my_def.as_local().unwrap()); let Some(td) = self.infcx.tcx.impl_of_method(my_def).and_then(|x| self.infcx.tcx.trait_id_of_impl(x)) else { return (false, false, None); }; + + let implemented_trait_item = self.infcx.tcx.associated_item(my_def).trait_item_def_id; + ( true, td.is_local(), - td.as_local().and_then(|tld| match self.infcx.tcx.hir_node_by_def_id(tld) { - Node::Item(hir::Item { - kind: hir::ItemKind::Trait(_, _, _, _, _, items), .. - }) => { - let mut f_in_trait_opt = None; - for hir::TraitItemRef { id: fi, kind: k, .. } in *items { - let hi = fi.hir_id(); - if !matches!(k, hir::AssocItemKind::Fn { .. }) { - continue; - } - if self.infcx.tcx.hir_name(hi) != self.infcx.tcx.hir_name(my_hir) { - continue; - } - f_in_trait_opt = Some(hi); - break; - } - f_in_trait_opt.and_then(|f_in_trait| { - if let Node::TraitItem(ti) = self.infcx.tcx.hir_node(f_in_trait) - && let hir::TraitItemKind::Fn(sig, _) = ti.kind - && let Some(ty) = sig.decl.inputs.get(local.index() - 1) - && let hir::TyKind::Ref(_, mut_ty) = ty.kind - && let hir::Mutability::Not = mut_ty.mutbl - && sig.decl.implicit_self.has_implicit_self() - { - Some(ty.span) - } else { - None - } - }) + implemented_trait_item.and_then(|f_in_trait| { + let f_in_trait = f_in_trait.as_local()?; + if let Node::TraitItem(ti) = self.infcx.tcx.hir_node_by_def_id(f_in_trait) + && let hir::TraitItemKind::Fn(sig, _) = ti.kind + && let Some(ty) = sig.decl.inputs.get(local.index() - 1) + && let hir::TyKind::Ref(_, mut_ty) = ty.kind + && let hir::Mutability::Not = mut_ty.mutbl + && sig.decl.implicit_self.has_implicit_self() + { + Some(ty.span) + } else { + None } - _ => None, }), ) } diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 0f6f81d7964..80e07af8a46 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -3172,6 +3172,8 @@ pub struct ImplItem<'hir> { pub span: Span, pub vis_span: Span, pub has_delayed_lints: bool, + /// When we are in a trait impl, link to the trait-item's id. + pub trait_item_def_id: Option<DefId>, } impl<'hir> ImplItem<'hir> { @@ -4136,7 +4138,7 @@ impl<'hir> Item<'hir> { expect_mod, (Ident, &'hir Mod<'hir>), ItemKind::Mod(ident, m), (*ident, m); - expect_foreign_mod, (ExternAbi, &'hir [ForeignItemRef]), + expect_foreign_mod, (ExternAbi, &'hir [ForeignItemId]), ItemKind::ForeignMod { abi, items }, (*abi, items); expect_global_asm, &'hir InlineAsm<'hir>, ItemKind::GlobalAsm { asm, .. }, asm; @@ -4160,7 +4162,7 @@ impl<'hir> Item<'hir> { Ident, &'hir Generics<'hir>, GenericBounds<'hir>, - &'hir [TraitItemRef] + &'hir [TraitItemId] ), ItemKind::Trait(is_auto, safety, ident, generics, bounds, items), (*is_auto, *safety, *ident, generics, bounds, items); @@ -4313,7 +4315,7 @@ pub enum ItemKind<'hir> { /// A module. Mod(Ident, &'hir Mod<'hir>), /// An external module, e.g. `extern { .. }`. - ForeignMod { abi: ExternAbi, items: &'hir [ForeignItemRef] }, + ForeignMod { abi: ExternAbi, items: &'hir [ForeignItemId] }, /// Module-level inline assembly (from `global_asm!`). GlobalAsm { asm: &'hir InlineAsm<'hir>, @@ -4333,7 +4335,7 @@ pub enum ItemKind<'hir> { /// A union definition, e.g., `union Foo<A, B> {x: A, y: B}`. Union(Ident, &'hir Generics<'hir>, VariantData<'hir>), /// A trait definition. - Trait(IsAuto, Safety, Ident, &'hir Generics<'hir>, GenericBounds<'hir>, &'hir [TraitItemRef]), + Trait(IsAuto, Safety, Ident, &'hir Generics<'hir>, GenericBounds<'hir>, &'hir [TraitItemId]), /// A trait alias. TraitAlias(Ident, &'hir Generics<'hir>, GenericBounds<'hir>), @@ -4360,7 +4362,7 @@ pub struct Impl<'hir> { pub of_trait: Option<TraitRef<'hir>>, pub self_ty: &'hir Ty<'hir>, - pub items: &'hir [ImplItemRef], + pub items: &'hir [ImplItemId], } impl ItemKind<'_> { @@ -4403,43 +4405,6 @@ impl ItemKind<'_> { } } -/// A reference from an trait to one of its associated items. This -/// contains the item's id, naturally, but also the item's name and -/// some other high-level details (like whether it is an associated -/// type or method, and whether it is public). This allows other -/// passes to find the impl they want without loading the ID (which -/// means fewer edges in the incremental compilation graph). -#[derive(Debug, Clone, Copy, HashStable_Generic)] -pub struct TraitItemRef { - pub id: TraitItemId, - pub ident: Ident, - pub kind: AssocItemKind, - pub span: Span, -} - -/// A reference from an impl to one of its associated items. This -/// contains the item's ID, naturally, but also the item's name and -/// some other high-level details (like whether it is an associated -/// type or method, and whether it is public). This allows other -/// passes to find the impl they want without loading the ID (which -/// means fewer edges in the incremental compilation graph). -#[derive(Debug, Clone, Copy, HashStable_Generic)] -pub struct ImplItemRef { - pub id: ImplItemId, - pub ident: Ident, - pub kind: AssocItemKind, - pub span: Span, - /// When we are in a trait impl, link to the trait-item's id. - pub trait_item_def_id: Option<DefId>, -} - -#[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)] -pub enum AssocItemKind { - Const, - Fn { has_self: bool }, - Type, -} - // The bodies for items are stored "out of line", in a separate // hashmap in the `Crate`. Here we just record the hir-id of the item // so it can fetched later. @@ -4456,19 +4421,6 @@ impl ForeignItemId { } } -/// A reference from a foreign block to one of its items. This -/// contains the item's ID, naturally, but also the item's name and -/// some other high-level details (like whether it is an associated -/// type or method, and whether it is public). This allows other -/// passes to find the impl they want without loading the ID (which -/// means fewer edges in the incremental compilation graph). -#[derive(Debug, Clone, Copy, HashStable_Generic)] -pub struct ForeignItemRef { - pub id: ForeignItemId, - pub ident: Ident, - pub span: Span, -} - #[derive(Debug, Clone, Copy, HashStable_Generic)] pub struct ForeignItem<'hir> { pub ident: Ident, @@ -4969,7 +4921,7 @@ mod size_asserts { static_assert_size!(GenericBound<'_>, 64); static_assert_size!(Generics<'_>, 56); static_assert_size!(Impl<'_>, 80); - static_assert_size!(ImplItem<'_>, 88); + static_assert_size!(ImplItem<'_>, 96); static_assert_size!(ImplItemKind<'_>, 40); static_assert_size!(Item<'_>, 88); static_assert_size!(ItemKind<'_>, 64); diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index 1bb8f7ad894..3edb94c28da 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -435,17 +435,17 @@ pub trait Visitor<'v>: Sized { fn visit_trait_item(&mut self, ti: &'v TraitItem<'v>) -> Self::Result { walk_trait_item(self, ti) } - fn visit_trait_item_ref(&mut self, ii: &'v TraitItemRef) -> Self::Result { - walk_trait_item_ref(self, ii) + fn visit_trait_item_ref(&mut self, ii: &'v TraitItemId) -> Self::Result { + walk_trait_item_ref(self, *ii) } fn visit_impl_item(&mut self, ii: &'v ImplItem<'v>) -> Self::Result { walk_impl_item(self, ii) } - fn visit_foreign_item_ref(&mut self, ii: &'v ForeignItemRef) -> Self::Result { - walk_foreign_item_ref(self, ii) + fn visit_foreign_item_ref(&mut self, ii: &'v ForeignItemId) -> Self::Result { + walk_foreign_item_ref(self, *ii) } - fn visit_impl_item_ref(&mut self, ii: &'v ImplItemRef) -> Self::Result { - walk_impl_item_ref(self, ii) + fn visit_impl_item_ref(&mut self, ii: &'v ImplItemId) -> Self::Result { + walk_impl_item_ref(self, *ii) } fn visit_trait_ref(&mut self, t: &'v TraitRef<'v>) -> Self::Result { walk_trait_ref(self, t) @@ -499,9 +499,6 @@ pub trait Visitor<'v>: Sized { fn visit_attribute(&mut self, _attr: &'v Attribute) -> Self::Result { Self::Result::output() } - fn visit_associated_item_kind(&mut self, kind: &'v AssocItemKind) -> Self::Result { - walk_associated_item_kind(self, kind) - } fn visit_defaultness(&mut self, defaultness: &'v Defaultness) -> Self::Result { walk_defaultness(self, defaultness) } @@ -1248,14 +1245,8 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>( V::Result::output() } -pub fn walk_trait_item_ref<'v, V: Visitor<'v>>( - visitor: &mut V, - trait_item_ref: &'v TraitItemRef, -) -> V::Result { - let TraitItemRef { id, ident, ref kind, span: _ } = *trait_item_ref; - try_visit!(visitor.visit_nested_trait_item(id)); - try_visit!(visitor.visit_ident(ident)); - visitor.visit_associated_item_kind(kind) +pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, id: TraitItemId) -> V::Result { + visitor.visit_nested_trait_item(id) } pub fn walk_impl_item<'v, V: Visitor<'v>>( @@ -1271,6 +1262,7 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>( span: _, vis_span: _, has_delayed_lints: _, + trait_item_def_id: _, } = *impl_item; try_visit!(visitor.visit_ident(ident)); @@ -1293,23 +1285,12 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>( } } -pub fn walk_foreign_item_ref<'v, V: Visitor<'v>>( - visitor: &mut V, - foreign_item_ref: &'v ForeignItemRef, -) -> V::Result { - let ForeignItemRef { id, ident, span: _ } = *foreign_item_ref; - try_visit!(visitor.visit_nested_foreign_item(id)); - visitor.visit_ident(ident) +pub fn walk_foreign_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, id: ForeignItemId) -> V::Result { + visitor.visit_nested_foreign_item(id) } -pub fn walk_impl_item_ref<'v, V: Visitor<'v>>( - visitor: &mut V, - impl_item_ref: &'v ImplItemRef, -) -> V::Result { - let ImplItemRef { id, ident, ref kind, span: _, trait_item_def_id: _ } = *impl_item_ref; - try_visit!(visitor.visit_nested_impl_item(id)); - try_visit!(visitor.visit_ident(ident)); - visitor.visit_associated_item_kind(kind) +pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, id: ImplItemId) -> V::Result { + visitor.visit_nested_impl_item(id) } pub fn walk_trait_ref<'v, V: Visitor<'v>>( @@ -1483,13 +1464,6 @@ pub fn walk_assoc_item_constraint<'v, V: Visitor<'v>>( V::Result::output() } -pub fn walk_associated_item_kind<'v, V: Visitor<'v>>(_: &mut V, _: &'v AssocItemKind) -> V::Result { - // No visitable content here: this fn exists so you can call it if - // the right thing to do, should content be added in the future, - // would be to walk it. - V::Result::output() -} - pub fn walk_defaultness<'v, V: Visitor<'v>>(_: &mut V, _: &'v Defaultness) -> V::Result { // No visitable content here: this fn exists so you can call it if // the right thing to do, should content be added in the future, diff --git a/compiler/rustc_hir_analysis/src/check/check.rs b/compiler/rustc_hir_analysis/src/check/check.rs index f4bbf03f0c2..70fbb8a543e 100644 --- a/compiler/rustc_hir_analysis/src/check/check.rs +++ b/compiler/rustc_hir_analysis/src/check/check.rs @@ -930,8 +930,8 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), check_abi(tcx, it.hir_id(), it.span, abi); - for item in items { - let def_id = item.id.owner_id.def_id; + for &item in items { + let def_id = item.owner_id.def_id; let generics = tcx.generics_of(def_id); let own_counts = generics.own_counts(); @@ -943,13 +943,14 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), (0, _) => ("const", "consts", None), _ => ("type or const", "types or consts", None), }; + let span = tcx.def_span(def_id); struct_span_code_err!( tcx.dcx(), - item.span, + span, E0044, "foreign items may not have {kinds} parameters", ) - .with_span_label(item.span, format!("can't have {kinds} parameters")) + .with_span_label(span, format!("can't have {kinds} parameters")) .with_help( // FIXME: once we start storing spans for type arguments, turn this // into a suggestion. @@ -963,22 +964,23 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), .emit(); } - let item = tcx.hir_foreign_item(item.id); - tcx.ensure_ok().generics_of(item.owner_id); - tcx.ensure_ok().type_of(item.owner_id); - tcx.ensure_ok().predicates_of(item.owner_id); + tcx.ensure_ok().generics_of(def_id); + tcx.ensure_ok().type_of(def_id); + tcx.ensure_ok().predicates_of(def_id); if tcx.is_conditionally_const(def_id) { tcx.ensure_ok().explicit_implied_const_bounds(def_id); tcx.ensure_ok().const_conditions(def_id); } - match item.kind { - hir::ForeignItemKind::Fn(sig, ..) => { - tcx.ensure_ok().codegen_fn_attrs(item.owner_id); - tcx.ensure_ok().fn_sig(item.owner_id); + match tcx.def_kind(def_id) { + DefKind::Fn => { + tcx.ensure_ok().codegen_fn_attrs(def_id); + tcx.ensure_ok().fn_sig(def_id); + let item = tcx.hir_foreign_item(item); + let hir::ForeignItemKind::Fn(sig, ..) = item.kind else { bug!() }; require_c_abi_if_c_variadic(tcx, sig.decl, abi, item.span); } - hir::ForeignItemKind::Static(..) => { - tcx.ensure_ok().codegen_fn_attrs(item.owner_id); + DefKind::Static { .. } => { + tcx.ensure_ok().codegen_fn_attrs(def_id); } _ => (), } diff --git a/compiler/rustc_hir_analysis/src/check/intrinsic.rs b/compiler/rustc_hir_analysis/src/check/intrinsic.rs index e3532ade32f..dcab6ef1c5a 100644 --- a/compiler/rustc_hir_analysis/src/check/intrinsic.rs +++ b/compiler/rustc_hir_analysis/src/check/intrinsic.rs @@ -59,7 +59,7 @@ fn equate_intrinsic_type<'tcx>( /// Returns the unsafety of the given intrinsic. fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -> hir::Safety { - let is_in_list = match tcx.item_name(intrinsic_id.into()) { + let is_in_list = match tcx.item_name(intrinsic_id) { // When adding a new intrinsic to this list, // it's usually worth updating that intrinsic's documentation // to note that it's safe to call, since @@ -144,7 +144,7 @@ fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -> hi tcx.def_span(intrinsic_id), DiagMessage::from(format!( "intrinsic safety mismatch between list of intrinsics within the compiler and core library intrinsics for intrinsic `{}`", - tcx.item_name(intrinsic_id.into()) + tcx.item_name(intrinsic_id) ) )).emit(); } diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index 431d99a572d..28a8758178f 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -844,11 +844,9 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> { fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef { let item = tcx.hir_expect_item(def_id); - let (is_alias, is_auto, safety, items) = match item.kind { - hir::ItemKind::Trait(is_auto, safety, .., items) => { - (false, is_auto == hir::IsAuto::Yes, safety, items) - } - hir::ItemKind::TraitAlias(..) => (true, false, hir::Safety::Safe, &[][..]), + let (is_alias, is_auto, safety) = match item.kind { + hir::ItemKind::Trait(is_auto, safety, ..) => (false, is_auto == hir::IsAuto::Yes, safety), + hir::ItemKind::TraitAlias(..) => (true, false, hir::Safety::Safe), _ => span_bug!(item.span, "trait_def_of_item invoked on non-trait"), }; @@ -911,13 +909,16 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef { // functions in the trait with default implementations .and_then(|(list, attr_span)| { let errors = list.iter().filter_map(|ident| { - let item = items.iter().find(|item| item.ident == *ident); + let item = tcx + .associated_items(def_id) + .filter_by_name_unhygienic(ident.name) + .find(|item| item.ident(tcx) == *ident); match item { - Some(item) if matches!(item.kind, hir::AssocItemKind::Fn { .. }) => { - if !tcx.defaultness(item.id.owner_id).has_value() { + Some(item) if matches!(item.kind, ty::AssocKind::Fn { .. }) => { + if !item.defaultness(tcx).has_value() { tcx.dcx().emit_err(errors::FunctionNotHaveDefaultImplementation { - span: item.span, + span: tcx.def_span(item.def_id), note_span: attr_span, }); @@ -928,7 +929,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef { } Some(item) => { tcx.dcx().emit_err(errors::MustImplementNotFunction { - span: item.span, + span: tcx.def_span(item.def_id), span_note: errors::MustImplementNotFunctionSpanNote { span: attr_span }, note: errors::MustImplementNotFunctionNote {}, }); diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index 3a525021f6f..2c13c9ef438 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -654,8 +654,8 @@ impl<'a> State<'a> { let (cb, ib) = self.head("extern"); self.word_nbsp(abi.to_string()); self.bopen(ib); - for item in items { - self.ann.nested(self, Nested::ForeignItem(item.id)); + for &foreign_item in items { + self.ann.nested(self, Nested::ForeignItem(foreign_item)); } self.bclose(item.span, cb); } @@ -730,8 +730,8 @@ impl<'a> State<'a> { self.space(); self.bopen(ib); - for impl_item in items { - self.ann.nested(self, Nested::ImplItem(impl_item.id)); + for &impl_item in items { + self.ann.nested(self, Nested::ImplItem(impl_item)); } self.bclose(item.span, cb); } @@ -746,8 +746,8 @@ impl<'a> State<'a> { self.print_where_clause(generics); self.word(" "); self.bopen(ib); - for trait_item in trait_items { - self.ann.nested(self, Nested::TraitItem(trait_item.id)); + for &trait_item in trait_items { + self.ann.nested(self, Nested::TraitItem(trait_item)); } self.bclose(item.span, cb); } diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 172f3372483..255ff56f62b 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -28,7 +28,7 @@ use rustc_hir as hir; use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId}; use rustc_hir::intravisit::FnKind as HirFnKind; -use rustc_hir::{Body, FnDecl, GenericParamKind, PatKind, PredicateOrigin}; +use rustc_hir::{Body, FnDecl, PatKind, PredicateOrigin}; use rustc_middle::bug; use rustc_middle::lint::LevelAndSource; use rustc_middle::ty::layout::LayoutOf; @@ -952,36 +952,34 @@ declare_lint! { declare_lint_pass!(InvalidNoMangleItems => [NO_MANGLE_CONST_ITEMS, NO_MANGLE_GENERIC_ITEMS]); +impl InvalidNoMangleItems { + fn check_no_mangle_on_generic_fn( + &self, + cx: &LateContext<'_>, + attr_span: Span, + def_id: LocalDefId, + ) { + let generics = cx.tcx.generics_of(def_id); + if generics.requires_monomorphization(cx.tcx) { + cx.emit_span_lint( + NO_MANGLE_GENERIC_ITEMS, + cx.tcx.def_span(def_id), + BuiltinNoMangleGeneric { suggestion: attr_span }, + ); + } + } +} + impl<'tcx> LateLintPass<'tcx> for InvalidNoMangleItems { fn check_item(&mut self, cx: &LateContext<'_>, it: &hir::Item<'_>) { let attrs = cx.tcx.hir_attrs(it.hir_id()); - let check_no_mangle_on_generic_fn = |attr_span: Span, - impl_generics: Option<&hir::Generics<'_>>, - generics: &hir::Generics<'_>, - span| { - for param in - generics.params.iter().chain(impl_generics.map(|g| g.params).into_iter().flatten()) - { - match param.kind { - GenericParamKind::Lifetime { .. } => {} - GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => { - cx.emit_span_lint( - NO_MANGLE_GENERIC_ITEMS, - span, - BuiltinNoMangleGeneric { suggestion: attr_span }, - ); - break; - } - } - } - }; match it.kind { - hir::ItemKind::Fn { generics, .. } => { + hir::ItemKind::Fn { .. } => { if let Some(attr_span) = find_attr!(attrs, AttributeKind::ExportName {span, ..} => *span) .or_else(|| find_attr!(attrs, AttributeKind::NoMangle(span) => *span)) { - check_no_mangle_on_generic_fn(attr_span, None, generics, it.span); + self.check_no_mangle_on_generic_fn(cx, attr_span, it.owner_id.def_id); } } hir::ItemKind::Const(..) => { @@ -1006,24 +1004,19 @@ impl<'tcx> LateLintPass<'tcx> for InvalidNoMangleItems { ); } } - hir::ItemKind::Impl(hir::Impl { generics, items, .. }) => { - for it in *items { - if let hir::AssocItemKind::Fn { .. } = it.kind { - let attrs = cx.tcx.hir_attrs(it.id.hir_id()); - if let Some(attr_span) = - find_attr!(attrs, AttributeKind::ExportName {span, ..} => *span) - .or_else( - || find_attr!(attrs, AttributeKind::NoMangle(span) => *span), - ) - { - check_no_mangle_on_generic_fn( - attr_span, - Some(generics), - cx.tcx.hir_get_generics(it.id.owner_id.def_id).unwrap(), - it.span, - ); - } - } + _ => {} + } + } + + fn check_impl_item(&mut self, cx: &LateContext<'_>, it: &hir::ImplItem<'_>) { + let attrs = cx.tcx.hir_attrs(it.hir_id()); + match it.kind { + hir::ImplItemKind::Fn { .. } => { + if let Some(attr_span) = + find_attr!(attrs, AttributeKind::ExportName {span, ..} => *span) + .or_else(|| find_attr!(attrs, AttributeKind::NoMangle(span) => *span)) + { + self.check_no_mangle_on_generic_fn(cx, attr_span, it.owner_id.def_id); } } _ => {} diff --git a/compiler/rustc_lint/src/deref_into_dyn_supertrait.rs b/compiler/rustc_lint/src/deref_into_dyn_supertrait.rs index 5989ef9519c..dd16117db1c 100644 --- a/compiler/rustc_lint/src/deref_into_dyn_supertrait.rs +++ b/compiler/rustc_lint/src/deref_into_dyn_supertrait.rs @@ -1,7 +1,7 @@ use rustc_hir::{self as hir, LangItem}; use rustc_middle::ty; use rustc_session::{declare_lint, declare_lint_pass}; -use rustc_span::sym; +use rustc_span::{Ident, sym}; use rustc_trait_selection::traits::supertraits; use crate::lints::{SupertraitAsDerefTarget, SupertraitAsDerefTargetLabel}; @@ -79,11 +79,15 @@ impl<'tcx> LateLintPass<'tcx> for DerefIntoDynSupertrait { // erase regions in self type for better diagnostic presentation let (self_ty, target_principal, supertrait_principal) = tcx.erase_regions((self_ty, target_principal, supertrait_principal)); - let label2 = impl_ - .items - .iter() - .find_map(|i| (i.ident.name == sym::Target).then_some(i.span)) - .map(|label| SupertraitAsDerefTargetLabel { label }); + let label2 = tcx + .associated_items(item.owner_id) + .find_by_ident_and_kind( + tcx, + Ident::with_dummy_span(sym::Target), + ty::AssocTag::Type, + item.owner_id.to_def_id(), + ) + .map(|label| SupertraitAsDerefTargetLabel { label: tcx.def_span(label.def_id) }); let span = tcx.def_span(item.owner_id.def_id); cx.emit_span_lint( DEREF_INTO_DYN_SUPERTRAIT, diff --git a/compiler/rustc_metadata/src/foreign_modules.rs b/compiler/rustc_metadata/src/foreign_modules.rs index 24689ea61d0..8a6b2027083 100644 --- a/compiler/rustc_metadata/src/foreign_modules.rs +++ b/compiler/rustc_metadata/src/foreign_modules.rs @@ -19,7 +19,7 @@ pub(crate) fn collect(tcx: TyCtxt<'_>, LocalCrate: LocalCrate) -> FxIndexMap<Def let item = tcx.hir_item(id); if let hir::ItemKind::ForeignMod { abi, items } = item.kind { - let foreign_items = items.iter().map(|it| it.id.owner_id.to_def_id()).collect(); + let foreign_items = items.iter().map(|it| it.owner_id.to_def_id()).collect(); modules.insert(def_id, ForeignModule { def_id, abi, foreign_items }); } } diff --git a/compiler/rustc_middle/src/ty/assoc.rs b/compiler/rustc_middle/src/ty/assoc.rs index 9534d45e495..1d15e4de7b6 100644 --- a/compiler/rustc_middle/src/ty/assoc.rs +++ b/compiler/rustc_middle/src/ty/assoc.rs @@ -258,6 +258,16 @@ impl AssocItems { } /// Returns the associated item with the given identifier and `AssocKind`, if one exists. + /// The identifier is ignoring hygiene. This is meant to be used for lints and diagnostics. + pub fn filter_by_name_unhygienic_and_kind( + &self, + name: Symbol, + assoc_tag: AssocTag, + ) -> impl '_ + Iterator<Item = &ty::AssocItem> { + self.filter_by_name_unhygienic(name).filter(move |item| item.as_tag() == assoc_tag) + } + + /// Returns the associated item with the given identifier and `AssocKind`, if one exists. /// The identifier is matched hygienically. pub fn find_by_ident_and_kind( &self, diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 0177a95498b..9df44bbc15e 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1590,7 +1590,8 @@ impl<'tcx> TyCtxt<'tcx> { } /// Look up the name of a definition across crates. This does not look at HIR. - pub fn opt_item_name(self, def_id: DefId) -> Option<Symbol> { + pub fn opt_item_name(self, def_id: impl IntoQueryParam<DefId>) -> Option<Symbol> { + let def_id = def_id.into_query_param(); if let Some(cnum) = def_id.as_crate_root() { Some(self.crate_name(cnum)) } else { @@ -1610,7 +1611,8 @@ impl<'tcx> TyCtxt<'tcx> { /// [`opt_item_name`] instead. /// /// [`opt_item_name`]: Self::opt_item_name - pub fn item_name(self, id: DefId) -> Symbol { + pub fn item_name(self, id: impl IntoQueryParam<DefId>) -> Symbol { + let id = id.into_query_param(); self.opt_item_name(id).unwrap_or_else(|| { bug!("item_name: no name for {:?}", self.def_path(id)); }) @@ -1619,7 +1621,8 @@ impl<'tcx> TyCtxt<'tcx> { /// Look up the name and span of a definition. /// /// See [`item_name`][Self::item_name] for more information. - pub fn opt_item_ident(self, def_id: DefId) -> Option<Ident> { + pub fn opt_item_ident(self, def_id: impl IntoQueryParam<DefId>) -> Option<Ident> { + let def_id = def_id.into_query_param(); let def = self.opt_item_name(def_id)?; let span = self .def_ident_span(def_id) @@ -1630,7 +1633,8 @@ impl<'tcx> TyCtxt<'tcx> { /// Look up the name and span of a definition. /// /// See [`item_name`][Self::item_name] for more information. - pub fn item_ident(self, def_id: DefId) -> Ident { + pub fn item_ident(self, def_id: impl IntoQueryParam<DefId>) -> Ident { + let def_id = def_id.into_query_param(); self.opt_item_ident(def_id).unwrap_or_else(|| { bug!("item_ident: no name for {:?}", self.def_path(def_id)); }) diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index fd80d85f198..174892c6f4d 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -1683,7 +1683,7 @@ pub fn intrinsic_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<ty::Intrinsi _ => true, }; Some(ty::IntrinsicDef { - name: tcx.item_name(def_id.into()), + name: tcx.item_name(def_id), must_be_overridden, const_stable: tcx.has_attr(def_id, sym::rustc_intrinsic_const_stable_indirect), }) diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index a49bfc1b8f4..7f47754f6bc 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -1084,7 +1084,7 @@ fn find_fallback_pattern_typo<'tcx>( && infcx.can_eq(param_env, ty, cx.tcx.type_of(item.owner_id).instantiate_identity()) { // Look for local consts. - let item_name = cx.tcx.item_name(item.owner_id.into()); + let item_name = cx.tcx.item_name(item.owner_id); let vis = cx.tcx.visibility(item.owner_id); if vis.is_accessible_from(parent, cx.tcx) { accessible.push(item_name); diff --git a/compiler/rustc_passes/src/abi_test.rs b/compiler/rustc_passes/src/abi_test.rs index b139ed6a66c..0ac42f03eb2 100644 --- a/compiler/rustc_passes/src/abi_test.rs +++ b/compiler/rustc_passes/src/abi_test.rs @@ -79,7 +79,7 @@ fn dump_abi_of_fn_item(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribut for meta_item in meta_items { match meta_item.name() { Some(sym::debug) => { - let fn_name = tcx.item_name(item_def_id.into()); + let fn_name = tcx.item_name(item_def_id); tcx.dcx().emit_err(AbiOf { span: tcx.def_span(item_def_id), fn_name, @@ -135,7 +135,7 @@ fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribut item_def_id, ); - let fn_name = tcx.item_name(item_def_id.into()); + let fn_name = tcx.item_name(item_def_id); tcx.dcx().emit_err(AbiOf { span, fn_name, fn_abi: format!("{:#?}", abi) }); } Some(sym::assert_eq) => { diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 9a96749f3e7..e7af615a715 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -20,8 +20,8 @@ use rustc_hir::def::DefKind; use rustc_hir::def_id::LocalModDefId; use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::{ - self as hir, self, AssocItemKind, Attribute, CRATE_HIR_ID, CRATE_OWNER_ID, FnSig, ForeignItem, - HirId, Item, ItemKind, MethodKind, Safety, Target, TraitItem, + self as hir, self, Attribute, CRATE_HIR_ID, CRATE_OWNER_ID, FnSig, ForeignItem, HirId, Item, + ItemKind, MethodKind, Safety, Target, TraitItem, }; use rustc_macros::LintDiagnostic; use rustc_middle::hir::nested_filter; @@ -1151,7 +1151,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> { if generics.params.len() != 0 => {} ItemKind::Trait(_, _, _, generics, _, items) if generics.params.len() != 0 - || items.iter().any(|item| matches!(item.kind, AssocItemKind::Type)) => {} + || items.iter().any(|item| { + matches!(self.tcx.def_kind(item.owner_id), DefKind::AssocTy) + }) => {} ItemKind::TyAlias(_, generics, _) if generics.params.len() != 0 => {} _ => { self.dcx().emit_err(errors::DocSearchUnboxInvalid { span: meta.span() }); diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index 35d6f22fd3b..d987041fe0e 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -418,8 +418,8 @@ impl<'tcx> MarkSymbolVisitor<'tcx> { hir::ItemKind::Trait(.., trait_item_refs) => { // mark assoc ty live if the trait is live for trait_item in trait_item_refs { - if matches!(trait_item.kind, hir::AssocItemKind::Type) { - self.check_def_id(trait_item.id.owner_id.to_def_id()); + if matches!(self.tcx.def_kind(trait_item.owner_id), DefKind::AssocTy) { + self.check_def_id(trait_item.owner_id.to_def_id()); } } intravisit::walk_item(self, item) diff --git a/compiler/rustc_passes/src/input_stats.rs b/compiler/rustc_passes/src/input_stats.rs index e38c7b2cbf1..6ee325dce03 100644 --- a/compiler/rustc_passes/src/input_stats.rs +++ b/compiler/rustc_passes/src/input_stats.rs @@ -467,9 +467,9 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> { hir_visit::walk_trait_item(self, ti) } - fn visit_trait_item_ref(&mut self, ti: &'v hir::TraitItemRef) { - self.record("TraitItemRef", Some(ti.id.hir_id()), ti); - hir_visit::walk_trait_item_ref(self, ti) + fn visit_trait_item_ref(&mut self, ti: &'v hir::TraitItemId) { + self.record("TraitItemId", Some(ti.hir_id()), ti); + hir_visit::walk_trait_item_ref(self, *ti) } fn visit_impl_item(&mut self, ii: &'v hir::ImplItem<'v>) { @@ -480,14 +480,14 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> { hir_visit::walk_impl_item(self, ii) } - fn visit_foreign_item_ref(&mut self, fi: &'v hir::ForeignItemRef) { - self.record("ForeignItemRef", Some(fi.id.hir_id()), fi); - hir_visit::walk_foreign_item_ref(self, fi) + fn visit_foreign_item_ref(&mut self, fi: &'v hir::ForeignItemId) { + self.record("ForeignItemId", Some(fi.hir_id()), fi); + hir_visit::walk_foreign_item_ref(self, *fi) } - fn visit_impl_item_ref(&mut self, ii: &'v hir::ImplItemRef) { - self.record("ImplItemRef", Some(ii.id.hir_id()), ii); - hir_visit::walk_impl_item_ref(self, ii) + fn visit_impl_item_ref(&mut self, ii: &'v hir::ImplItemId) { + self.record("ImplItemId", Some(ii.hir_id()), ii); + hir_visit::walk_impl_item_ref(self, *ii) } fn visit_param_bound(&mut self, b: &'v hir::GenericBound<'v>) { diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index a30655d32a7..adda94fda8f 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -880,11 +880,16 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> { } for impl_item_ref in *items { - let impl_item = self.tcx.associated_item(impl_item_ref.id.owner_id); + let impl_item = self.tcx.associated_item(impl_item_ref.owner_id); if let Some(def_id) = impl_item.trait_item_def_id { // Pass `None` to skip deprecation warnings. - self.tcx.check_stability(def_id, None, impl_item_ref.span, None); + self.tcx.check_stability( + def_id, + None, + self.tcx.def_span(impl_item_ref.owner_id), + None, + ); } } } diff --git a/compiler/rustc_privacy/src/lib.rs b/compiler/rustc_privacy/src/lib.rs index 963f4c77d80..ab2433234aa 100644 --- a/compiler/rustc_privacy/src/lib.rs +++ b/compiler/rustc_privacy/src/lib.rs @@ -26,7 +26,7 @@ use rustc_errors::{MultiSpan, listify}; use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId, LocalModDefId}; use rustc_hir::intravisit::{self, InferKind, Visitor}; -use rustc_hir::{AmbigArg, AssocItemKind, ForeignItemKind, ItemId, ItemKind, PatKind}; +use rustc_hir::{AmbigArg, ForeignItemKind, ItemId, ItemKind, PatKind}; use rustc_middle::middle::privacy::{EffectiveVisibilities, EffectiveVisibility, Level}; use rustc_middle::query::Providers; use rustc_middle::ty::print::PrintTraitRefExt as _; @@ -672,14 +672,14 @@ impl<'tcx> Visitor<'tcx> for EmbargoVisitor<'tcx> { self.reach(item.owner_id.def_id, item_ev).generics().predicates(); for trait_item_ref in trait_item_refs { - self.update(trait_item_ref.id.owner_id.def_id, item_ev, Level::Reachable); + self.update(trait_item_ref.owner_id.def_id, item_ev, Level::Reachable); let tcx = self.tcx; - let mut reach = self.reach(trait_item_ref.id.owner_id.def_id, item_ev); + let mut reach = self.reach(trait_item_ref.owner_id.def_id, item_ev); reach.generics().predicates(); - if trait_item_ref.kind == AssocItemKind::Type - && !tcx.defaultness(trait_item_ref.id.owner_id).has_value() + if let DefKind::AssocTy = tcx.def_kind(trait_item_ref.owner_id) + && !tcx.defaultness(trait_item_ref.owner_id).has_value() { // No type to visit. } else { @@ -715,7 +715,7 @@ impl<'tcx> Visitor<'tcx> for EmbargoVisitor<'tcx> { self.reach(item.owner_id.def_id, item_ev).generics().predicates().ty().trait_ref(); for impl_item_ref in impl_.items { - let def_id = impl_item_ref.id.owner_id.def_id; + let def_id = impl_item_ref.owner_id.def_id; let max_vis = impl_.of_trait.is_none().then(|| self.tcx.local_visibility(def_id)); self.update_eff_vis(def_id, item_ev, max_vis, Level::Direct); @@ -755,8 +755,8 @@ impl<'tcx> Visitor<'tcx> for EmbargoVisitor<'tcx> { } hir::ItemKind::ForeignMod { items, .. } => { for foreign_item in items { - if let Some(foreign_item_ev) = self.get(foreign_item.id.owner_id.def_id) { - self.reach(foreign_item.id.owner_id.def_id, foreign_item_ev) + if let Some(foreign_item_ev) = self.get(foreign_item.owner_id.def_id) { + self.reach(foreign_item.owner_id.def_id, foreign_item_ev) .generics() .predicates() .ty(); @@ -1576,16 +1576,15 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'_, 'tcx> { fn check_assoc_item( &self, - def_id: LocalDefId, - assoc_item_kind: AssocItemKind, + item: &ty::AssocItem, vis: ty::Visibility, effective_vis: Option<EffectiveVisibility>, ) { - let mut check = self.check(def_id, vis, effective_vis); + let mut check = self.check(item.def_id.expect_local(), vis, effective_vis); - let (check_ty, is_assoc_ty) = match assoc_item_kind { - AssocItemKind::Const | AssocItemKind::Fn { .. } => (true, false), - AssocItemKind::Type => (self.tcx.defaultness(def_id).has_value(), true), + let (check_ty, is_assoc_ty) = match item.kind { + ty::AssocKind::Const { .. } | ty::AssocKind::Fn { .. } => (true, false), + ty::AssocKind::Type { .. } => (item.defaultness(self.tcx).has_value(), true), }; check.in_assoc_ty = is_assoc_ty; @@ -1619,30 +1618,20 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'_, 'tcx> { self.check(def_id, item_visibility, effective_vis).generics().bounds(); } DefKind::Trait => { - let item = tcx.hir_item(id); - if let hir::ItemKind::Trait(.., trait_item_refs) = item.kind { - self.check_unnameable(item.owner_id.def_id, effective_vis); + self.check_unnameable(def_id, effective_vis); - self.check(item.owner_id.def_id, item_visibility, effective_vis) - .generics() - .predicates(); + self.check(def_id, item_visibility, effective_vis).generics().predicates(); - for trait_item_ref in trait_item_refs { - self.check_assoc_item( - trait_item_ref.id.owner_id.def_id, - trait_item_ref.kind, + for assoc_item in tcx.associated_items(id.owner_id).in_definition_order() { + self.check_assoc_item(assoc_item, item_visibility, effective_vis); + + if assoc_item.is_type() { + self.check( + assoc_item.def_id.expect_local(), item_visibility, effective_vis, - ); - - if let AssocItemKind::Type = trait_item_ref.kind { - self.check( - trait_item_ref.id.owner_id.def_id, - item_visibility, - effective_vis, - ) - .bounds(); - } + ) + .bounds(); } } } @@ -1669,8 +1658,8 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'_, 'tcx> { DefKind::ForeignMod => { let item = tcx.hir_item(id); if let hir::ItemKind::ForeignMod { items, .. } = item.kind { - for foreign_item in items { - let foreign_item = tcx.hir_foreign_item(foreign_item.id); + for &foreign_item in items { + let foreign_item = tcx.hir_foreign_item(foreign_item); let ev = self.get(foreign_item.owner_id.def_id); let vis = tcx.local_visibility(foreign_item.owner_id.def_id); @@ -1714,69 +1703,52 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'_, 'tcx> { // Subitems of inherent impls have their own publicity. // A trait impl is public when both its type and its trait are public // Subitems of trait impls have inherited publicity. - DefKind::Impl { .. } => { - let item = tcx.hir_item(id); - if let hir::ItemKind::Impl(impl_) = item.kind { - let impl_vis = ty::Visibility::of_impl::<false>( - item.owner_id.def_id, - tcx, - &Default::default(), - ); + DefKind::Impl { of_trait } => { + let impl_vis = ty::Visibility::of_impl::<false>(def_id, tcx, &Default::default()); - // We are using the non-shallow version here, unlike when building the - // effective visisibilities table to avoid large number of false positives. - // For example in - // - // impl From<Priv> for Pub { - // fn from(_: Priv) -> Pub {...} - // } - // - // lints shouldn't be emitted even if `from` effective visibility - // is larger than `Priv` nominal visibility and if `Priv` can leak - // in some scenarios due to type inference. - let impl_ev = EffectiveVisibility::of_impl::<false>( - item.owner_id.def_id, - tcx, - self.effective_visibilities, - ); + // We are using the non-shallow version here, unlike when building the + // effective visisibilities table to avoid large number of false positives. + // For example in + // + // impl From<Priv> for Pub { + // fn from(_: Priv) -> Pub {...} + // } + // + // lints shouldn't be emitted even if `from` effective visibility + // is larger than `Priv` nominal visibility and if `Priv` can leak + // in some scenarios due to type inference. + let impl_ev = + EffectiveVisibility::of_impl::<false>(def_id, tcx, self.effective_visibilities); + + let mut check = self.check(def_id, impl_vis, Some(impl_ev)); + + // Generics and predicates of trait impls are intentionally not checked + // for private components (#90586). + if !of_trait { + check.generics().predicates(); + } - let mut check = self.check(item.owner_id.def_id, impl_vis, Some(impl_ev)); - // Generics and predicates of trait impls are intentionally not checked - // for private components (#90586). - if impl_.of_trait.is_none() { - check.generics().predicates(); - } - // Skip checking private components in associated types, due to lack of full - // normalization they produce very ridiculous false positives. - // FIXME: Remove this when full normalization is implemented. - check.skip_assoc_tys = true; - check.ty().trait_ref(); - - for impl_item_ref in impl_.items { - let impl_item_vis = if impl_.of_trait.is_none() { - min( - tcx.local_visibility(impl_item_ref.id.owner_id.def_id), - impl_vis, - tcx, - ) - } else { - impl_vis - }; + // Skip checking private components in associated types, due to lack of full + // normalization they produce very ridiculous false positives. + // FIXME: Remove this when full normalization is implemented. + check.skip_assoc_tys = true; + check.ty().trait_ref(); - let impl_item_ev = if impl_.of_trait.is_none() { - self.get(impl_item_ref.id.owner_id.def_id) - .map(|ev| ev.min(impl_ev, self.tcx)) - } else { - Some(impl_ev) - }; - - self.check_assoc_item( - impl_item_ref.id.owner_id.def_id, - impl_item_ref.kind, - impl_item_vis, - impl_item_ev, - ); - } + for assoc_item in tcx.associated_items(id.owner_id).in_definition_order() { + let impl_item_vis = if !of_trait { + min(tcx.local_visibility(assoc_item.def_id.expect_local()), impl_vis, tcx) + } else { + impl_vis + }; + + let impl_item_ev = if !of_trait { + self.get(assoc_item.def_id.expect_local()) + .map(|ev| ev.min(impl_ev, self.tcx)) + } else { + Some(impl_ev) + }; + + self.check_assoc_item(assoc_item, impl_item_vis, impl_item_ev); } } _ => {} diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs index bff5e9128cb..db35c988bf7 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs @@ -841,54 +841,32 @@ fn foo(&self) -> Self::T { String::new() } let param_env = tcx.param_env(body_owner_def_id); - match item { - hir::Node::Item(hir::Item { kind: hir::ItemKind::Trait(.., items), .. }) => { - // FIXME: account for `#![feature(specialization)]` - for item in &items[..] { - match item.kind { - hir::AssocItemKind::Type => { - // FIXME: account for returning some type in a trait fn impl that has - // an assoc type as a return type (#72076). - if let hir::Defaultness::Default { has_value: true } = - tcx.defaultness(item.id.owner_id) - { - let assoc_ty = tcx.type_of(item.id.owner_id).instantiate_identity(); - if self.infcx.can_eq(param_env, assoc_ty, found) { - diag.span_label( - item.span, - "associated type defaults can't be assumed inside the \ - trait defining them", - ); - return true; - } - } + if let DefKind::Trait | DefKind::Impl { .. } = tcx.def_kind(parent_id) { + let assoc_items = tcx.associated_items(parent_id); + // FIXME: account for `#![feature(specialization)]` + for assoc_item in assoc_items.in_definition_order() { + if assoc_item.is_type() + // FIXME: account for returning some type in a trait fn impl that has + // an assoc type as a return type (#72076). + && let hir::Defaultness::Default { has_value: true } = assoc_item.defaultness(tcx) + && let assoc_ty = tcx.type_of(assoc_item.def_id).instantiate_identity() + && self.infcx.can_eq(param_env, assoc_ty, found) + { + let msg = match assoc_item.container { + ty::AssocItemContainer::Trait => { + "associated type defaults can't be assumed inside the \ + trait defining them" } - _ => {} - } - } - } - hir::Node::Item(hir::Item { - kind: hir::ItemKind::Impl(hir::Impl { items, .. }), - .. - }) => { - for item in &items[..] { - if let hir::AssocItemKind::Type = item.kind { - let assoc_ty = tcx.type_of(item.id.owner_id).instantiate_identity(); - if let hir::Defaultness::Default { has_value: true } = - tcx.defaultness(item.id.owner_id) - && self.infcx.can_eq(param_env, assoc_ty, found) - { - diag.span_label( - item.span, - "associated type is `default` and may be overridden", - ); - return true; + ty::AssocItemContainer::Impl => { + "associated type is `default` and may be overridden" } - } + }; + diag.span_label(tcx.def_span(assoc_item.def_id), msg); + return true; } } - _ => {} } + false } diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs index a8ba1baf6b9..712e88300ff 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs @@ -362,7 +362,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { && self.tcx.trait_of_item(*item_id) == Some(*trait_id) && let None = self.tainted_by_errors() { - let (verb, noun) = match self.tcx.associated_item(item_id).kind { + let assoc_item = self.tcx.associated_item(item_id); + let (verb, noun) = match assoc_item.kind { ty::AssocKind::Const { .. } => ("refer to the", "constant"), ty::AssocKind::Fn { .. } => ("call", "function"), // This is already covered by E0223, but this following single match @@ -381,17 +382,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { ); err.code(E0790); - if let Some(local_def_id) = data.trait_ref.def_id.as_local() - && let hir::Node::Item(hir::Item { - kind: hir::ItemKind::Trait(_, _, trait_ident, _, _, trait_item_refs), - .. - }) = self.tcx.hir_node_by_def_id(local_def_id) - && let Some(method_ref) = trait_item_refs - .iter() - .find(|item_ref| item_ref.ident == *assoc_item_ident) - { + if item_id.is_local() { + let trait_ident = self.tcx.item_name(*trait_id); err.span_label( - method_ref.span, + self.tcx.def_span(*item_id), format!("`{trait_ident}::{assoc_item_ident}` defined here"), ); } diff --git a/compiler/rustc_trait_selection/src/traits/wf.rs b/compiler/rustc_trait_selection/src/traits/wf.rs index d4e6a23f0eb..fed9f254cdf 100644 --- a/compiler/rustc_trait_selection/src/traits/wf.rs +++ b/compiler/rustc_trait_selection/src/traits/wf.rs @@ -288,9 +288,9 @@ fn extend_cause_with_original_assoc_item_obligation<'tcx>( && let Some(&impl_item_id) = tcx.impl_item_implementor_ids(impl_def_id).get(&projection_ty.def_id) && let Some(impl_item) = - items.iter().find(|item| item.id.owner_id.to_def_id() == impl_item_id) + items.iter().find(|item| item.owner_id.to_def_id() == impl_item_id) { - Some(tcx.hir_impl_item(impl_item.id).expect_type().span) + Some(tcx.hir_impl_item(*impl_item).expect_type().span) } else { None } diff --git a/compiler/rustc_ty_utils/src/assoc.rs b/compiler/rustc_ty_utils/src/assoc.rs index 66c44f72f4e..37cb64511c7 100644 --- a/compiler/rustc_ty_utils/src/assoc.rs +++ b/compiler/rustc_ty_utils/src/assoc.rs @@ -6,6 +6,8 @@ use rustc_hir::{self as hir, ItemKind}; use rustc_middle::query::Providers; use rustc_middle::ty::{self, ImplTraitInTraitData, TyCtxt}; use rustc_middle::{bug, span_bug}; +use rustc_span::Ident; +use rustc_span::symbol::kw; pub(crate) fn provide(providers: &mut Providers) { *providers = Providers { @@ -27,7 +29,7 @@ fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: LocalDefId) -> &[DefId] { // query. let rpitit_items = tcx.associated_types_for_impl_traits_in_trait_or_impl(def_id); tcx.arena.alloc_from_iter(trait_item_refs.iter().flat_map(|trait_item_ref| { - let item_def_id = trait_item_ref.id.owner_id.to_def_id(); + let item_def_id = trait_item_ref.owner_id.to_def_id(); [item_def_id] .into_iter() .chain(rpitit_items.get(&item_def_id).into_iter().flatten().copied()) @@ -39,7 +41,7 @@ fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: LocalDefId) -> &[DefId] { // associated_types_for_impl_traits_in_trait_or_impl query. let rpitit_items = tcx.associated_types_for_impl_traits_in_trait_or_impl(def_id); tcx.arena.alloc_from_iter(impl_.items.iter().flat_map(|impl_item_ref| { - let item_def_id = impl_item_ref.id.owner_id.to_def_id(); + let item_def_id = impl_item_ref.owner_id.to_def_id(); [item_def_id] .into_iter() .chain(rpitit_items.get(&item_def_id).into_iter().flatten().copied()) @@ -66,46 +68,33 @@ fn impl_item_implementor_ids(tcx: TyCtxt<'_>, impl_id: DefId) -> DefIdMap<DefId> } fn associated_item(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AssocItem { - let id = tcx.local_def_id_to_hir_id(def_id); - let parent_def_id = tcx.hir_get_parent_item(id); - let parent_item = tcx.hir_expect_item(parent_def_id.def_id); - match parent_item.kind { - hir::ItemKind::Impl(impl_) => { - if let Some(impl_item_ref) = impl_.items.iter().find(|i| i.id.owner_id.def_id == def_id) - { - let assoc_item = associated_item_from_impl_item_ref(impl_item_ref); - debug_assert_eq!(assoc_item.def_id.expect_local(), def_id); - return assoc_item; - } - } - - hir::ItemKind::Trait(.., trait_item_refs) => { - if let Some(trait_item_ref) = - trait_item_refs.iter().find(|i| i.id.owner_id.def_id == def_id) - { - let assoc_item = associated_item_from_trait_item_ref(trait_item_ref); - debug_assert_eq!(assoc_item.def_id.expect_local(), def_id); - return assoc_item; - } - } - - _ => {} - } + let assoc_item = match tcx.hir_node_by_def_id(def_id) { + hir::Node::TraitItem(ti) => associated_item_from_trait_item(tcx, ti), + hir::Node::ImplItem(ii) => associated_item_from_impl_item(tcx, ii), + node => span_bug!(tcx.def_span(def_id), "impl item or item not found: {:?}", node,), + }; + debug_assert_eq!(assoc_item.def_id.expect_local(), def_id); + assoc_item +} - span_bug!( - parent_item.span, - "unexpected parent of trait or impl item or item not found: {:?}", - parent_item.kind - ) +fn fn_has_self_parameter(tcx: TyCtxt<'_>, owner_id: hir::OwnerId) -> bool { + matches!(tcx.fn_arg_idents(owner_id.def_id), [Some(Ident { name: kw::SelfLower, .. }), ..]) } -fn associated_item_from_trait_item_ref(trait_item_ref: &hir::TraitItemRef) -> ty::AssocItem { - let owner_id = trait_item_ref.id.owner_id; - let name = trait_item_ref.ident.name; - let kind = match trait_item_ref.kind { - hir::AssocItemKind::Const => ty::AssocKind::Const { name }, - hir::AssocItemKind::Fn { has_self } => ty::AssocKind::Fn { name, has_self }, - hir::AssocItemKind::Type => ty::AssocKind::Type { data: ty::AssocTypeData::Normal(name) }, +fn associated_item_from_trait_item( + tcx: TyCtxt<'_>, + trait_item: &hir::TraitItem<'_>, +) -> ty::AssocItem { + let owner_id = trait_item.owner_id; + let name = trait_item.ident.name; + let kind = match trait_item.kind { + hir::TraitItemKind::Const { .. } => ty::AssocKind::Const { name }, + hir::TraitItemKind::Fn { .. } => { + ty::AssocKind::Fn { name, has_self: fn_has_self_parameter(tcx, owner_id) } + } + hir::TraitItemKind::Type { .. } => { + ty::AssocKind::Type { data: ty::AssocTypeData::Normal(name) } + } }; ty::AssocItem { @@ -116,19 +105,23 @@ fn associated_item_from_trait_item_ref(trait_item_ref: &hir::TraitItemRef) -> ty } } -fn associated_item_from_impl_item_ref(impl_item_ref: &hir::ImplItemRef) -> ty::AssocItem { - let def_id = impl_item_ref.id.owner_id; - let name = impl_item_ref.ident.name; - let kind = match impl_item_ref.kind { - hir::AssocItemKind::Const => ty::AssocKind::Const { name }, - hir::AssocItemKind::Fn { has_self } => ty::AssocKind::Fn { name, has_self }, - hir::AssocItemKind::Type => ty::AssocKind::Type { data: ty::AssocTypeData::Normal(name) }, +fn associated_item_from_impl_item(tcx: TyCtxt<'_>, impl_item: &hir::ImplItem<'_>) -> ty::AssocItem { + let owner_id = impl_item.owner_id; + let name = impl_item.ident.name; + let kind = match impl_item.kind { + hir::ImplItemKind::Const { .. } => ty::AssocKind::Const { name }, + hir::ImplItemKind::Fn { .. } => { + ty::AssocKind::Fn { name, has_self: fn_has_self_parameter(tcx, owner_id) } + } + hir::ImplItemKind::Type { .. } => { + ty::AssocKind::Type { data: ty::AssocTypeData::Normal(name) } + } }; ty::AssocItem { kind, - def_id: def_id.to_def_id(), - trait_item_def_id: impl_item_ref.trait_item_def_id, + def_id: owner_id.to_def_id(), + trait_item_def_id: impl_item.trait_item_def_id, container: ty::AssocItemContainer::Impl, } } @@ -137,12 +130,10 @@ struct RPITVisitor<'a, 'tcx> { synthetics: Vec<LocalDefId>, data: DefPathData, disambiguator: &'a mut DisambiguatorState, - depth: u32, } impl<'tcx> Visitor<'tcx> for RPITVisitor<'_, 'tcx> { fn visit_opaque_ty(&mut self, opaque: &'tcx hir::OpaqueTy<'tcx>) -> Self::Result { - self.depth += 1; self.synthetics.push(associated_type_for_impl_trait_in_trait( self.tcx, opaque.def_id, @@ -163,17 +154,16 @@ fn associated_types_for_impl_traits_in_trait_or_impl<'tcx>( ItemKind::Trait(.., trait_item_refs) => trait_item_refs .iter() .filter_map(move |item| { - if !matches!(item.kind, hir::AssocItemKind::Fn { .. }) { + if !matches!(tcx.def_kind(item.owner_id), DefKind::AssocFn) { return None; } - let fn_def_id = item.id.owner_id.def_id; + let fn_def_id = item.owner_id.def_id; let Some(output) = tcx.hir_get_fn_output(fn_def_id) else { return Some((fn_def_id.to_def_id(), vec![])); }; let def_name = tcx.item_name(fn_def_id.to_def_id()); let data = DefPathData::AnonAssocTy(def_name); - let mut visitor = - RPITVisitor { tcx, synthetics: vec![], data, depth: 0, disambiguator }; + let mut visitor = RPITVisitor { tcx, synthetics: vec![], data, disambiguator }; visitor.visit_fn_ret_ty(output); let defs = visitor .synthetics @@ -195,21 +185,17 @@ fn associated_types_for_impl_traits_in_trait_or_impl<'tcx>( .items .iter() .filter_map(|item| { - if !matches!(item.kind, hir::AssocItemKind::Fn { .. }) { + if !matches!(tcx.def_kind(item.owner_id), DefKind::AssocFn) { return None; } - let did = item.id.owner_id.def_id.to_def_id(); + let did = item.owner_id.def_id.to_def_id(); + let item = tcx.hir_impl_item(*item); let Some(trait_item_def_id) = item.trait_item_def_id else { return Some((did, vec![])); }; let iter = in_trait_def[&trait_item_def_id].iter().map(|&id| { - associated_type_for_impl_trait_in_impl( - tcx, - id, - item.id.owner_id.def_id, - disambiguator, - ) - .to_def_id() + associated_type_for_impl_trait_in_impl(tcx, id, item, disambiguator) + .to_def_id() }); Some((did, iter.collect())) }) @@ -288,20 +274,20 @@ fn associated_type_for_impl_trait_in_trait( /// Given an `trait_assoc_def_id` corresponding to an associated item synthesized /// from an `impl Trait` in an associated function from a trait, and an -/// `impl_fn_def_id` that represents an implementation of the associated function +/// `impl_fn` that represents an implementation of the associated function /// that the `impl Trait` comes from, synthesize an associated type for that `impl Trait` /// that inherits properties that we infer from the method and the associated type. fn associated_type_for_impl_trait_in_impl( tcx: TyCtxt<'_>, trait_assoc_def_id: DefId, - impl_fn_def_id: LocalDefId, + impl_fn: &hir::ImplItem<'_>, disambiguator: &mut DisambiguatorState, ) -> LocalDefId { - let impl_local_def_id = tcx.local_parent(impl_fn_def_id); + let impl_local_def_id = tcx.local_parent(impl_fn.owner_id.def_id); - let decl = tcx.hir_node_by_def_id(impl_fn_def_id).fn_decl().expect("expected decl"); - let span = match decl.output { - hir::FnRetTy::DefaultReturn(_) => tcx.def_span(impl_fn_def_id), + let hir::ImplItemKind::Fn(fn_sig, _) = impl_fn.kind else { bug!("expected decl") }; + let span = match fn_sig.decl.output { + hir::FnRetTy::DefaultReturn(_) => tcx.def_span(impl_fn.owner_id), hir::FnRetTy::Return(ty) => ty.span, }; @@ -332,7 +318,7 @@ fn associated_type_for_impl_trait_in_impl( impl_assoc_ty.associated_item(ty::AssocItem { kind: ty::AssocKind::Type { data: ty::AssocTypeData::Rpitit(ImplTraitInTraitData::Impl { - fn_def_id: impl_fn_def_id.to_def_id(), + fn_def_id: impl_fn.owner_id.to_def_id(), }), }, def_id, @@ -341,10 +327,10 @@ fn associated_type_for_impl_trait_in_impl( }); // Copy visility of the containing function. - impl_assoc_ty.visibility(tcx.visibility(impl_fn_def_id)); + impl_assoc_ty.visibility(tcx.visibility(impl_fn.owner_id)); // Copy defaultness of the containing function. - impl_assoc_ty.defaultness(tcx.defaultness(impl_fn_def_id)); + impl_assoc_ty.defaultness(tcx.defaultness(impl_fn.owner_id)); // Copy generics_of the trait's associated item but the impl as the parent. // FIXME: This may be detrimental to diagnostics, as we resolve the early-bound vars |
