diff options
| author | bors <bors@rust-lang.org> | 2025-07-13 22:39:10 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2025-07-13 22:39:10 +0000 |
| commit | 9c3064e131f4939cc95a29bb11413c49bbda1491 (patch) | |
| tree | 8320ff7f32aa67eec55081c56987eb517095cb49 /compiler/rustc_ast_lowering/src | |
| parent | e9182f195b8505c87c4bd055b9f6e114ccda0981 (diff) | |
| parent | 27127e342d28ebeb0734916ada6979b6938fb532 (diff) | |
| download | rust-9c3064e131f4939cc95a29bb11413c49bbda1491.tar.gz rust-9c3064e131f4939cc95a29bb11413c49bbda1491.zip | |
Auto merge of #143357 - cjgillot:no-assoc-item-kind, r=compiler-errors
Retire hir::*ItemRef. This information was kept for various places that iterate on HIR to know about trait-items and impl-items. This PR replaces them by uses of the `associated_items` query that contain pretty much the same information. This shortens many spans to just `def_span`, which can be easier to read.
Diffstat (limited to 'compiler/rustc_ast_lowering/src')
| -rw-r--r-- | compiler/rustc_ast_lowering/src/delegation.rs | 55 | ||||
| -rw-r--r-- | compiler/rustc_ast_lowering/src/index.rs | 24 | ||||
| -rw-r--r-- | compiler/rustc_ast_lowering/src/item.rs | 82 |
3 files changed, 52 insertions, 109 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( |
