Skip to content

Commit 3ecd03b

Browse files
committed
Move trait_item_def_id from ImplItemRef to ImplItem.
1 parent 50ca0c6 commit 3ecd03b

File tree

6 files changed

+64
-74
lines changed

6 files changed

+64
-74
lines changed

compiler/rustc_ast_lowering/src/index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
392392
fn visit_impl_item_ref(&mut self, ii: &'hir ImplItemRef) {
393393
// Do not visit the duplicate information in ImplItemRef. We want to
394394
// map the actual nodes, not the duplicate ones in the *Ref.
395-
let ImplItemRef { id, ident: _, kind: _, span: _, trait_item_def_id: _ } = *ii;
395+
let ImplItemRef { id, ident: _, kind: _, span: _ } = *ii;
396396

397397
self.visit_nested_impl_item(id);
398398
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
11281128
span: self.lower_span(i.span),
11291129
defaultness,
11301130
has_delayed_lints: !self.delayed_lints.is_empty(),
1131+
trait_item_def_id: self
1132+
.resolver
1133+
.get_partial_res(i.id)
1134+
.map(|r| r.expect_full_res().opt_def_id())
1135+
.unwrap_or(None),
11311136
};
11321137
self.arena.alloc(item)
11331138
}
@@ -1157,11 +1162,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
11571162
panic!("macros should have been expanded by now")
11581163
}
11591164
},
1160-
trait_item_def_id: self
1161-
.resolver
1162-
.get_partial_res(i.id)
1163-
.map(|r| r.expect_full_res().opt_def_id())
1164-
.unwrap_or(None),
11651165
}
11661166
}
11671167

compiler/rustc_hir/src/hir.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3172,6 +3172,8 @@ pub struct ImplItem<'hir> {
31723172
pub span: Span,
31733173
pub vis_span: Span,
31743174
pub has_delayed_lints: bool,
3175+
/// When we are in a trait impl, link to the trait-item's id.
3176+
pub trait_item_def_id: Option<DefId>,
31753177
}
31763178

31773179
impl<'hir> ImplItem<'hir> {
@@ -4429,8 +4431,6 @@ pub struct ImplItemRef {
44294431
pub ident: Ident,
44304432
pub kind: AssocItemKind,
44314433
pub span: Span,
4432-
/// When we are in a trait impl, link to the trait-item's id.
4433-
pub trait_item_def_id: Option<DefId>,
44344434
}
44354435

44364436
#[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)]
@@ -4969,7 +4969,7 @@ mod size_asserts {
49694969
static_assert_size!(GenericBound<'_>, 64);
49704970
static_assert_size!(Generics<'_>, 56);
49714971
static_assert_size!(Impl<'_>, 80);
4972-
static_assert_size!(ImplItem<'_>, 88);
4972+
static_assert_size!(ImplItem<'_>, 96);
49734973
static_assert_size!(ImplItemKind<'_>, 40);
49744974
static_assert_size!(Item<'_>, 88);
49754975
static_assert_size!(ItemKind<'_>, 64);

compiler/rustc_hir/src/intravisit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,7 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(
12711271
span: _,
12721272
vis_span: _,
12731273
has_delayed_lints: _,
1274+
trait_item_def_id: _,
12741275
} = *impl_item;
12751276

12761277
try_visit!(visitor.visit_ident(ident));
@@ -1306,7 +1307,7 @@ pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(
13061307
visitor: &mut V,
13071308
impl_item_ref: &'v ImplItemRef,
13081309
) -> V::Result {
1309-
let ImplItemRef { id, ident, ref kind, span: _, trait_item_def_id: _ } = *impl_item_ref;
1310+
let ImplItemRef { id, ident, ref kind, span: _ } = *impl_item_ref;
13101311
try_visit!(visitor.visit_nested_impl_item(id));
13111312
try_visit!(visitor.visit_ident(ident));
13121313
visitor.visit_associated_item_kind(kind)

compiler/rustc_ty_utils/src/assoc.rs

Lines changed: 51 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use rustc_hir::{self as hir, ItemKind};
66
use rustc_middle::query::Providers;
77
use rustc_middle::ty::{self, ImplTraitInTraitData, TyCtxt};
88
use rustc_middle::{bug, span_bug};
9+
use rustc_span::Ident;
10+
use rustc_span::symbol::kw;
911

1012
pub(crate) fn provide(providers: &mut Providers) {
1113
*providers = Providers {
@@ -66,46 +68,33 @@ fn impl_item_implementor_ids(tcx: TyCtxt<'_>, impl_id: DefId) -> DefIdMap<DefId>
6668
}
6769

6870
fn associated_item(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AssocItem {
69-
let id = tcx.local_def_id_to_hir_id(def_id);
70-
let parent_def_id = tcx.hir_get_parent_item(id);
71-
let parent_item = tcx.hir_expect_item(parent_def_id.def_id);
72-
match parent_item.kind {
73-
hir::ItemKind::Impl(impl_) => {
74-
if let Some(impl_item_ref) = impl_.items.iter().find(|i| i.id.owner_id.def_id == def_id)
75-
{
76-
let assoc_item = associated_item_from_impl_item_ref(impl_item_ref);
77-
debug_assert_eq!(assoc_item.def_id.expect_local(), def_id);
78-
return assoc_item;
79-
}
80-
}
81-
82-
hir::ItemKind::Trait(.., trait_item_refs) => {
83-
if let Some(trait_item_ref) =
84-
trait_item_refs.iter().find(|i| i.id.owner_id.def_id == def_id)
85-
{
86-
let assoc_item = associated_item_from_trait_item_ref(trait_item_ref);
87-
debug_assert_eq!(assoc_item.def_id.expect_local(), def_id);
88-
return assoc_item;
89-
}
90-
}
91-
92-
_ => {}
93-
}
71+
let assoc_item = match tcx.hir_node_by_def_id(def_id) {
72+
hir::Node::TraitItem(ti) => associated_item_from_trait_item(tcx, ti),
73+
hir::Node::ImplItem(ii) => associated_item_from_impl_item(tcx, ii),
74+
node => span_bug!(tcx.def_span(def_id), "impl item or item not found: {:?}", node,),
75+
};
76+
debug_assert_eq!(assoc_item.def_id.expect_local(), def_id);
77+
assoc_item
78+
}
9479

95-
span_bug!(
96-
parent_item.span,
97-
"unexpected parent of trait or impl item or item not found: {:?}",
98-
parent_item.kind
99-
)
80+
fn fn_has_self_parameter(tcx: TyCtxt<'_>, owner_id: hir::OwnerId) -> bool {
81+
matches!(tcx.fn_arg_idents(owner_id.def_id), [Some(Ident { name: kw::SelfLower, .. }), ..])
10082
}
10183

102-
fn associated_item_from_trait_item_ref(trait_item_ref: &hir::TraitItemRef) -> ty::AssocItem {
103-
let owner_id = trait_item_ref.id.owner_id;
104-
let name = trait_item_ref.ident.name;
105-
let kind = match trait_item_ref.kind {
106-
hir::AssocItemKind::Const => ty::AssocKind::Const { name },
107-
hir::AssocItemKind::Fn { has_self } => ty::AssocKind::Fn { name, has_self },
108-
hir::AssocItemKind::Type => ty::AssocKind::Type { data: ty::AssocTypeData::Normal(name) },
84+
fn associated_item_from_trait_item(
85+
tcx: TyCtxt<'_>,
86+
trait_item: &hir::TraitItem<'_>,
87+
) -> ty::AssocItem {
88+
let owner_id = trait_item.owner_id;
89+
let name = trait_item.ident.name;
90+
let kind = match trait_item.kind {
91+
hir::TraitItemKind::Const { .. } => ty::AssocKind::Const { name },
92+
hir::TraitItemKind::Fn { .. } => {
93+
ty::AssocKind::Fn { name, has_self: fn_has_self_parameter(tcx, owner_id) }
94+
}
95+
hir::TraitItemKind::Type { .. } => {
96+
ty::AssocKind::Type { data: ty::AssocTypeData::Normal(name) }
97+
}
10998
};
11099

111100
ty::AssocItem {
@@ -116,19 +105,23 @@ fn associated_item_from_trait_item_ref(trait_item_ref: &hir::TraitItemRef) -> ty
116105
}
117106
}
118107

119-
fn associated_item_from_impl_item_ref(impl_item_ref: &hir::ImplItemRef) -> ty::AssocItem {
120-
let def_id = impl_item_ref.id.owner_id;
121-
let name = impl_item_ref.ident.name;
122-
let kind = match impl_item_ref.kind {
123-
hir::AssocItemKind::Const => ty::AssocKind::Const { name },
124-
hir::AssocItemKind::Fn { has_self } => ty::AssocKind::Fn { name, has_self },
125-
hir::AssocItemKind::Type => ty::AssocKind::Type { data: ty::AssocTypeData::Normal(name) },
108+
fn associated_item_from_impl_item(tcx: TyCtxt<'_>, impl_item: &hir::ImplItem<'_>) -> ty::AssocItem {
109+
let owner_id = impl_item.owner_id;
110+
let name = impl_item.ident.name;
111+
let kind = match impl_item.kind {
112+
hir::ImplItemKind::Const { .. } => ty::AssocKind::Const { name },
113+
hir::ImplItemKind::Fn { .. } => {
114+
ty::AssocKind::Fn { name, has_self: fn_has_self_parameter(tcx, owner_id) }
115+
}
116+
hir::ImplItemKind::Type { .. } => {
117+
ty::AssocKind::Type { data: ty::AssocTypeData::Normal(name) }
118+
}
126119
};
127120

128121
ty::AssocItem {
129122
kind,
130-
def_id: def_id.to_def_id(),
131-
trait_item_def_id: impl_item_ref.trait_item_def_id,
123+
def_id: owner_id.to_def_id(),
124+
trait_item_def_id: impl_item.trait_item_def_id,
132125
container: ty::AssocItemContainer::Impl,
133126
}
134127
}
@@ -196,17 +189,13 @@ fn associated_types_for_impl_traits_in_trait_or_impl<'tcx>(
196189
return None;
197190
}
198191
let did = item.id.owner_id.def_id.to_def_id();
192+
let item = tcx.hir_impl_item(item.id);
199193
let Some(trait_item_def_id) = item.trait_item_def_id else {
200194
return Some((did, vec![]));
201195
};
202196
let iter = in_trait_def[&trait_item_def_id].iter().map(|&id| {
203-
associated_type_for_impl_trait_in_impl(
204-
tcx,
205-
id,
206-
item.id.owner_id.def_id,
207-
disambiguator,
208-
)
209-
.to_def_id()
197+
associated_type_for_impl_trait_in_impl(tcx, id, item, disambiguator)
198+
.to_def_id()
210199
});
211200
Some((did, iter.collect()))
212201
})
@@ -285,20 +274,20 @@ fn associated_type_for_impl_trait_in_trait(
285274

286275
/// Given an `trait_assoc_def_id` corresponding to an associated item synthesized
287276
/// from an `impl Trait` in an associated function from a trait, and an
288-
/// `impl_fn_def_id` that represents an implementation of the associated function
277+
/// `impl_fn` that represents an implementation of the associated function
289278
/// that the `impl Trait` comes from, synthesize an associated type for that `impl Trait`
290279
/// that inherits properties that we infer from the method and the associated type.
291280
fn associated_type_for_impl_trait_in_impl(
292281
tcx: TyCtxt<'_>,
293282
trait_assoc_def_id: DefId,
294-
impl_fn_def_id: LocalDefId,
283+
impl_fn: &hir::ImplItem<'_>,
295284
disambiguator: &mut DisambiguatorState,
296285
) -> LocalDefId {
297-
let impl_local_def_id = tcx.local_parent(impl_fn_def_id);
286+
let impl_local_def_id = tcx.local_parent(impl_fn.owner_id.def_id);
298287

299-
let decl = tcx.hir_node_by_def_id(impl_fn_def_id).fn_decl().expect("expected decl");
300-
let span = match decl.output {
301-
hir::FnRetTy::DefaultReturn(_) => tcx.def_span(impl_fn_def_id),
288+
let hir::ImplItemKind::Fn(fn_sig, _) = impl_fn.kind else { bug!("expected decl") };
289+
let span = match fn_sig.decl.output {
290+
hir::FnRetTy::DefaultReturn(_) => tcx.def_span(impl_fn.owner_id),
302291
hir::FnRetTy::Return(ty) => ty.span,
303292
};
304293

@@ -329,7 +318,7 @@ fn associated_type_for_impl_trait_in_impl(
329318
impl_assoc_ty.associated_item(ty::AssocItem {
330319
kind: ty::AssocKind::Type {
331320
data: ty::AssocTypeData::Rpitit(ImplTraitInTraitData::Impl {
332-
fn_def_id: impl_fn_def_id.to_def_id(),
321+
fn_def_id: impl_fn.owner_id.to_def_id(),
333322
}),
334323
},
335324
def_id,
@@ -338,10 +327,10 @@ fn associated_type_for_impl_trait_in_impl(
338327
});
339328

340329
// Copy visility of the containing function.
341-
impl_assoc_ty.visibility(tcx.visibility(impl_fn_def_id));
330+
impl_assoc_ty.visibility(tcx.visibility(impl_fn.owner_id));
342331

343332
// Copy defaultness of the containing function.
344-
impl_assoc_ty.defaultness(tcx.defaultness(impl_fn_def_id));
333+
impl_assoc_ty.defaultness(tcx.defaultness(impl_fn.owner_id));
345334

346335
// Copy generics_of the trait's associated item but the impl as the parent.
347336
// FIXME: This may be detrimental to diagnostics, as we resolve the early-bound vars

tests/ui/stats/input-stats.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,17 @@ hir-stats WherePredicate 72 (NN.N%) 3 24
107107
hir-stats - BoundPredicate 72 (NN.N%) 3
108108
hir-stats Local 72 (NN.N%) 1 72
109109
hir-stats InlineAsm 72 (NN.N%) 1 72
110-
hir-stats ImplItemRef 72 (NN.N%) 2 36
111110
hir-stats Body 72 (NN.N%) 3 24
112111
hir-stats Param 64 (NN.N%) 2 32
113112
hir-stats GenericArg 64 (NN.N%) 4 16
114113
hir-stats - Type 16 (NN.N%) 1
115114
hir-stats - Lifetime 48 (NN.N%) 3
116115
hir-stats TraitItemRef 56 (NN.N%) 2 28
116+
hir-stats ImplItemRef 56 (NN.N%) 2 28
117117
hir-stats ExprField 40 (NN.N%) 1 40
118118
hir-stats Mod 32 (NN.N%) 1 32
119119
hir-stats Lifetime 28 (NN.N%) 1 28
120120
hir-stats ForeignItemRef 24 (NN.N%) 1 24
121121
hir-stats ----------------------------------------------------------------
122-
hir-stats Total 8_716 173
122+
hir-stats Total 8_700 173
123123
hir-stats ================================================================

0 commit comments

Comments
 (0)