Skip to content

Commit 2d047d1

Browse files
committed
Auto merge of #143357 - cjgillot:no-assoc-item-kind, r=<try>
Retire hir::*ItemRef. r? `@ghost` for perf
2 parents d612081 + 4e5d3e2 commit 2d047d1

File tree

80 files changed

+651
-880
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+651
-880
lines changed

‎compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ use rustc_errors::ErrorGuaranteed;
4747
use rustc_hir::def_id::DefId;
4848
use rustc_middle::span_bug;
4949
use rustc_middle::ty::{Asyncness, ResolverAstLowering};
50+
use rustc_span::symbol::kw;
5051
use rustc_span::{Ident, Span, Symbol};
5152
use {rustc_ast as ast, rustc_hir as hir};
5253

@@ -61,21 +62,6 @@ pub(crate) struct DelegationResults<'hir> {
6162
}
6263

6364
impl<'hir> LoweringContext<'_, 'hir> {
64-
/// Defines whether the delegatee is an associated function whose first parameter is `self`.
65-
pub(crate) fn delegatee_is_method(
66-
&self,
67-
item_id: NodeId,
68-
path_id: NodeId,
69-
span: Span,
70-
is_in_trait_impl: bool,
71-
) -> bool {
72-
let sig_id = self.get_delegation_sig_id(item_id, path_id, span, is_in_trait_impl);
73-
let Ok(sig_id) = sig_id else {
74-
return false;
75-
};
76-
self.is_method(sig_id, span)
77-
}
78-
7965
fn is_method(&self, def_id: DefId, span: Span) -> bool {
8066
match self.tcx.def_kind(def_id) {
8167
DefKind::Fn => false,
@@ -101,10 +87,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
10187
let sig_id = self.get_delegation_sig_id(item_id, delegation.id, span, is_in_trait_impl);
10288
match sig_id {
10389
Ok(sig_id) => {
90+
let is_method = self.is_method(sig_id, span);
10491
let (param_count, c_variadic) = self.param_count(sig_id);
10592
let decl = self.lower_delegation_decl(sig_id, param_count, c_variadic, span);
10693
let sig = self.lower_delegation_sig(sig_id, decl, span);
107-
let body_id = self.lower_delegation_body(delegation, param_count, span);
94+
let body_id = self.lower_delegation_body(delegation, is_method, param_count, span);
10895
let ident = self.lower_ident(delegation.ident);
10996
let generics = self.lower_delegation_generics(span);
11097
DelegationResults { body_id, sig, ident, generics }
@@ -234,10 +221,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
234221
hir::FnSig { decl, header, span }
235222
}
236223

237-
fn generate_param(&mut self, idx: usize, span: Span) -> (hir::Param<'hir>, NodeId) {
224+
fn generate_param(
225+
&mut self,
226+
is_method: bool,
227+
idx: usize,
228+
span: Span,
229+
) -> (hir::Param<'hir>, NodeId) {
238230
let pat_node_id = self.next_node_id();
239231
let pat_id = self.lower_node_id(pat_node_id);
240-
let ident = Ident::with_dummy_span(Symbol::intern(&format!("arg{idx}")));
232+
// FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
233+
let name = if is_method && idx == 0 {
234+
kw::SelfLower
235+
} else {
236+
Symbol::intern(&format!("arg{idx}"))
237+
};
238+
let ident = Ident::with_dummy_span(name);
241239
let pat = self.arena.alloc(hir::Pat {
242240
hir_id: pat_id,
243241
kind: hir::PatKind::Binding(hir::BindingMode::NONE, pat_id, ident, None),
@@ -248,9 +246,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
248246
(hir::Param { hir_id: self.next_id(), pat, ty_span: span, span }, pat_node_id)
249247
}
250248

251-
fn generate_arg(&mut self, idx: usize, param_id: HirId, span: Span) -> hir::Expr<'hir> {
249+
fn generate_arg(
250+
&mut self,
251+
is_method: bool,
252+
idx: usize,
253+
param_id: HirId,
254+
span: Span,
255+
) -> hir::Expr<'hir> {
256+
// FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
257+
let name = if is_method && idx == 0 {
258+
kw::SelfLower
259+
} else {
260+
Symbol::intern(&format!("arg{idx}"))
261+
};
252262
let segments = self.arena.alloc_from_iter(iter::once(hir::PathSegment {
253-
ident: Ident::with_dummy_span(Symbol::intern(&format!("arg{idx}"))),
263+
ident: Ident::with_dummy_span(name),
254264
hir_id: self.next_id(),
255265
res: Res::Local(param_id),
256266
args: None,
@@ -264,6 +274,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
264274
fn lower_delegation_body(
265275
&mut self,
266276
delegation: &Delegation,
277+
is_method: bool,
267278
param_count: usize,
268279
span: Span,
269280
) -> BodyId {
@@ -274,7 +285,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
274285
let mut args: Vec<hir::Expr<'_>> = Vec::with_capacity(param_count);
275286

276287
for idx in 0..param_count {
277-
let (param, pat_node_id) = this.generate_param(idx, span);
288+
let (param, pat_node_id) = this.generate_param(is_method, idx, span);
278289
parameters.push(param);
279290

280291
let arg = if let Some(block) = block
@@ -290,7 +301,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
290301
this.ident_and_label_to_local_id.insert(pat_node_id, param.pat.hir_id.local_id);
291302
this.lower_target_expr(&block)
292303
} else {
293-
this.generate_arg(idx, param.pat.hir_id, span)
304+
this.generate_arg(is_method, idx, param.pat.hir_id, span)
294305
};
295306
args.push(arg);
296307
}

‎compiler/rustc_ast_lowering/src/index.rs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -381,28 +381,16 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
381381
})
382382
}
383383

384-
fn visit_trait_item_ref(&mut self, ii: &'hir TraitItemRef) {
385-
// Do not visit the duplicate information in TraitItemRef. We want to
386-
// map the actual nodes, not the duplicate ones in the *Ref.
387-
let TraitItemRef { id, ident: _, kind: _, span: _ } = *ii;
388-
389-
self.visit_nested_trait_item(id);
384+
fn visit_trait_item_ref(&mut self, id: &'hir TraitItemId) {
385+
self.visit_nested_trait_item(*id);
390386
}
391387

392-
fn visit_impl_item_ref(&mut self, ii: &'hir ImplItemRef) {
393-
// Do not visit the duplicate information in ImplItemRef. We want to
394-
// map the actual nodes, not the duplicate ones in the *Ref.
395-
let ImplItemRef { id, ident: _, kind: _, span: _, trait_item_def_id: _ } = *ii;
396-
397-
self.visit_nested_impl_item(id);
388+
fn visit_impl_item_ref(&mut self, id: &'hir ImplItemId) {
389+
self.visit_nested_impl_item(*id);
398390
}
399391

400-
fn visit_foreign_item_ref(&mut self, fi: &'hir ForeignItemRef) {
401-
// Do not visit the duplicate information in ForeignItemRef. We want to
402-
// map the actual nodes, not the duplicate ones in the *Ref.
403-
let ForeignItemRef { id, ident: _, span: _ } = *fi;
404-
405-
self.visit_nested_foreign_item(id);
392+
fn visit_foreign_item_ref(&mut self, id: &'hir ForeignItemId) {
393+
self.visit_nested_foreign_item(*id);
406394
}
407395

408396
fn visit_where_predicate(&mut self, predicate: &'hir WherePredicate<'hir>) {

‎compiler/rustc_ast_lowering/src/item.rs

Lines changed: 13 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
392392
(trait_ref, lowered_ty)
393393
});
394394

395-
let new_impl_items = self.arena.alloc_from_iter(
396-
impl_items
397-
.iter()
398-
.map(|item| self.lower_impl_item_ref(item, trait_ref.is_some())),
399-
);
395+
let new_impl_items = self
396+
.arena
397+
.alloc_from_iter(impl_items.iter().map(|item| self.lower_impl_item_ref(item)));
400398

401399
// `defaultness.has_value()` is never called for an `impl`, always `true` in order
402400
// to not cause an assertion failure inside the `lower_defaultness` function.
@@ -705,14 +703,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
705703
self.arena.alloc(item)
706704
}
707705

708-
fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef {
709-
hir::ForeignItemRef {
710-
id: hir::ForeignItemId { owner_id: self.owner_id(i.id) },
711-
// `unwrap` is safe because `ForeignItemKind::MacCall` is the only foreign item kind
712-
// without an identifier and it cannot reach here.
713-
ident: self.lower_ident(i.kind.ident().unwrap()),
714-
span: self.lower_span(i.span),
715-
}
706+
fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemId {
707+
hir::ForeignItemId { owner_id: self.owner_id(i.id) }
716708
}
717709

718710
fn lower_variant(&mut self, item_kind: &ItemKind, v: &Variant) -> hir::Variant<'hir> {
@@ -971,32 +963,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
971963
self.arena.alloc(item)
972964
}
973965

974-
fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef {
975-
let (ident, kind) = match &i.kind {
976-
AssocItemKind::Const(box ConstItem { ident, .. }) => {
977-
(*ident, hir::AssocItemKind::Const)
978-
}
979-
AssocItemKind::Type(box TyAlias { ident, .. }) => (*ident, hir::AssocItemKind::Type),
980-
AssocItemKind::Fn(box Fn { ident, sig, .. }) => {
981-
(*ident, hir::AssocItemKind::Fn { has_self: sig.decl.has_self() })
982-
}
983-
AssocItemKind::Delegation(box delegation) => (
984-
delegation.ident,
985-
hir::AssocItemKind::Fn {
986-
has_self: self.delegatee_is_method(i.id, delegation.id, i.span, false),
987-
},
988-
),
989-
AssocItemKind::MacCall(..) | AssocItemKind::DelegationMac(..) => {
990-
panic!("macros should have been expanded by now")
991-
}
992-
};
993-
let id = hir::TraitItemId { owner_id: self.owner_id(i.id) };
994-
hir::TraitItemRef {
995-
id,
996-
ident: self.lower_ident(ident),
997-
span: self.lower_span(i.span),
998-
kind,
999-
}
966+
fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemId {
967+
hir::TraitItemId { owner_id: self.owner_id(i.id) }
1000968
}
1001969

1002970
/// Construct `ExprKind::Err` for the given `span`.
@@ -1127,41 +1095,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
11271095
span: self.lower_span(i.span),
11281096
defaultness,
11291097
has_delayed_lints: !self.delayed_lints.is_empty(),
1130-
};
1131-
self.arena.alloc(item)
1132-
}
1133-
1134-
fn lower_impl_item_ref(&mut self, i: &AssocItem, is_in_trait_impl: bool) -> hir::ImplItemRef {
1135-
hir::ImplItemRef {
1136-
id: hir::ImplItemId { owner_id: self.owner_id(i.id) },
1137-
// `unwrap` is safe because `AssocItemKind::{MacCall,DelegationMac}` are the only
1138-
// assoc item kinds without an identifier and they cannot reach here.
1139-
ident: self.lower_ident(i.kind.ident().unwrap()),
1140-
span: self.lower_span(i.span),
1141-
kind: match &i.kind {
1142-
AssocItemKind::Const(..) => hir::AssocItemKind::Const,
1143-
AssocItemKind::Type(..) => hir::AssocItemKind::Type,
1144-
AssocItemKind::Fn(box Fn { sig, .. }) => {
1145-
hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }
1146-
}
1147-
AssocItemKind::Delegation(box delegation) => hir::AssocItemKind::Fn {
1148-
has_self: self.delegatee_is_method(
1149-
i.id,
1150-
delegation.id,
1151-
i.span,
1152-
is_in_trait_impl,
1153-
),
1154-
},
1155-
AssocItemKind::MacCall(..) | AssocItemKind::DelegationMac(..) => {
1156-
panic!("macros should have been expanded by now")
1157-
}
1158-
},
11591098
trait_item_def_id: self
11601099
.resolver
11611100
.get_partial_res(i.id)
11621101
.map(|r| r.expect_full_res().opt_def_id())
11631102
.unwrap_or(None),
1164-
}
1103+
};
1104+
self.arena.alloc(item)
1105+
}
1106+
1107+
fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemId {
1108+
hir::ImplItemId { owner_id: self.owner_id(i.id) }
11651109
}
11661110

11671111
fn lower_defaultness(

‎compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -681,46 +681,30 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
681681
return (false, false, None);
682682
}
683683
let my_def = self.body.source.def_id();
684-
let my_hir = self.infcx.tcx.local_def_id_to_hir_id(my_def.as_local().unwrap());
685684
let Some(td) =
686685
self.infcx.tcx.impl_of_method(my_def).and_then(|x| self.infcx.tcx.trait_id_of_impl(x))
687686
else {
688687
return (false, false, None);
689688
};
689+
690+
let implemented_trait_item = self.infcx.tcx.associated_item(my_def).trait_item_def_id;
691+
690692
(
691693
true,
692694
td.is_local(),
693-
td.as_local().and_then(|tld| match self.infcx.tcx.hir_node_by_def_id(tld) {
694-
Node::Item(hir::Item {
695-
kind: hir::ItemKind::Trait(_, _, _, _, _, items), ..
696-
}) => {
697-
let mut f_in_trait_opt = None;
698-
for hir::TraitItemRef { id: fi, kind: k, .. } in *items {
699-
let hi = fi.hir_id();
700-
if !matches!(k, hir::AssocItemKind::Fn { .. }) {
701-
continue;
702-
}
703-
if self.infcx.tcx.hir_name(hi) != self.infcx.tcx.hir_name(my_hir) {
704-
continue;
705-
}
706-
f_in_trait_opt = Some(hi);
707-
break;
708-
}
709-
f_in_trait_opt.and_then(|f_in_trait| {
710-
if let Node::TraitItem(ti) = self.infcx.tcx.hir_node(f_in_trait)
711-
&& let hir::TraitItemKind::Fn(sig, _) = ti.kind
712-
&& let Some(ty) = sig.decl.inputs.get(local.index() - 1)
713-
&& let hir::TyKind::Ref(_, mut_ty) = ty.kind
714-
&& let hir::Mutability::Not = mut_ty.mutbl
715-
&& sig.decl.implicit_self.has_implicit_self()
716-
{
717-
Some(ty.span)
718-
} else {
719-
None
720-
}
721-
})
695+
implemented_trait_item.and_then(|f_in_trait| {
696+
let f_in_trait = f_in_trait.as_local()?;
697+
if let Node::TraitItem(ti) = self.infcx.tcx.hir_node_by_def_id(f_in_trait)
698+
&& let hir::TraitItemKind::Fn(sig, _) = ti.kind
699+
&& let Some(ty) = sig.decl.inputs.get(local.index() - 1)
700+
&& let hir::TyKind::Ref(_, mut_ty) = ty.kind
701+
&& let hir::Mutability::Not = mut_ty.mutbl
702+
&& sig.decl.implicit_self.has_implicit_self()
703+
{
704+
Some(ty.span)
705+
} else {
706+
None
722707
}
723-
_ => None,
724708
}),
725709
)
726710
}

0 commit comments

Comments
 (0)