Skip to content

Commit bd8b08d

Browse files
committed
Introduce hir::ImplItemImplKind
1 parent 0e3db70 commit bd8b08d

File tree

15 files changed

+141
-92
lines changed

15 files changed

+141
-92
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use rustc_errors::{E0570, ErrorGuaranteed, struct_span_code_err};
55
use rustc_hir::attrs::AttributeKind;
66
use rustc_hir::def::{DefKind, PerNS, Res};
77
use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
8-
use rustc_hir::{self as hir, HirId, LifetimeSource, PredicateOrigin, Target, find_attr};
8+
use rustc_hir::{
9+
self as hir, HirId, ImplItemImplKind, LifetimeSource, PredicateOrigin, Target, find_attr,
10+
};
911
use rustc_index::{IndexSlice, IndexVec};
1012
use rustc_middle::span_bug;
1113
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
@@ -1117,20 +1119,31 @@ impl<'hir> LoweringContext<'_, 'hir> {
11171119
}
11181120
};
11191121

1122+
let span = self.lower_span(i.span);
11201123
let item = hir::ImplItem {
11211124
owner_id: hir_id.expect_owner(),
11221125
ident: self.lower_ident(ident),
11231126
generics,
1127+
impl_kind: if is_in_trait_impl {
1128+
ImplItemImplKind::Trait {
1129+
defaultness,
1130+
trait_item_def_id: self
1131+
.resolver
1132+
.get_partial_res(i.id)
1133+
.and_then(|r| r.expect_full_res().opt_def_id())
1134+
.ok_or_else(|| {
1135+
self.dcx().span_delayed_bug(
1136+
span,
1137+
"could not resolve trait item being implemented",
1138+
)
1139+
}),
1140+
}
1141+
} else {
1142+
ImplItemImplKind::Inherent { vis_span: self.lower_span(i.vis.span) }
1143+
},
11241144
kind,
1125-
vis_span: self.lower_span(i.vis.span),
1126-
span: self.lower_span(i.span),
1127-
defaultness,
1145+
span,
11281146
has_delayed_lints: !self.delayed_lints.is_empty(),
1129-
trait_item_def_id: self
1130-
.resolver
1131-
.get_partial_res(i.id)
1132-
.map(|r| r.expect_full_res().opt_def_id())
1133-
.unwrap_or(None),
11341147
};
11351148
self.arena.alloc(item)
11361149
}

compiler/rustc_hir/src/hir.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3193,12 +3193,21 @@ pub struct ImplItem<'hir> {
31933193
pub owner_id: OwnerId,
31943194
pub generics: &'hir Generics<'hir>,
31953195
pub kind: ImplItemKind<'hir>,
3196-
pub defaultness: Defaultness,
3196+
pub impl_kind: ImplItemImplKind,
31973197
pub span: Span,
3198-
pub vis_span: Span,
31993198
pub has_delayed_lints: bool,
3200-
/// When we are in a trait impl, link to the trait-item's id.
3201-
pub trait_item_def_id: Option<DefId>,
3199+
}
3200+
3201+
#[derive(Debug, Clone, Copy, HashStable_Generic)]
3202+
pub enum ImplItemImplKind {
3203+
Inherent {
3204+
vis_span: Span,
3205+
},
3206+
Trait {
3207+
defaultness: Defaultness,
3208+
/// Item in the trait that this item implements
3209+
trait_item_def_id: Result<DefId, ErrorGuaranteed>,
3210+
},
32023211
}
32033212

32043213
impl<'hir> ImplItem<'hir> {
@@ -3212,6 +3221,13 @@ impl<'hir> ImplItem<'hir> {
32123221
ImplItemId { owner_id: self.owner_id }
32133222
}
32143223

3224+
pub fn vis_span(&self) -> Option<Span> {
3225+
match self.impl_kind {
3226+
ImplItemImplKind::Trait { .. } => None,
3227+
ImplItemImplKind::Inherent { vis_span, .. } => Some(vis_span),
3228+
}
3229+
}
3230+
32153231
expect_methods_self_kind! {
32163232
expect_const, (&'hir Ty<'hir>, BodyId), ImplItemKind::Const(ty, body), (ty, *body);
32173233
expect_fn, (&FnSig<'hir>, BodyId), ImplItemKind::Fn(ty, body), (ty, *body);
@@ -4958,7 +4974,7 @@ mod size_asserts {
49584974
static_assert_size!(GenericBound<'_>, 64);
49594975
static_assert_size!(Generics<'_>, 56);
49604976
static_assert_size!(Impl<'_>, 40);
4961-
static_assert_size!(ImplItem<'_>, 96);
4977+
static_assert_size!(ImplItem<'_>, 88);
49624978
static_assert_size!(ImplItemKind<'_>, 40);
49634979
static_assert_size!(Item<'_>, 88);
49644980
static_assert_size!(ItemKind<'_>, 64);

compiler/rustc_hir/src/intravisit.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,18 +1257,21 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(
12571257
owner_id: _,
12581258
ident,
12591259
ref generics,
1260+
ref impl_kind,
12601261
ref kind,
1261-
ref defaultness,
12621262
span: _,
1263-
vis_span: _,
12641263
has_delayed_lints: _,
1265-
trait_item_def_id: _,
12661264
} = *impl_item;
12671265

12681266
try_visit!(visitor.visit_ident(ident));
12691267
try_visit!(visitor.visit_generics(generics));
1270-
try_visit!(visitor.visit_defaultness(defaultness));
12711268
try_visit!(visitor.visit_id(impl_item.hir_id()));
1269+
match impl_kind {
1270+
ImplItemImplKind::Inherent { vis_span: _ } => {}
1271+
ImplItemImplKind::Trait { defaultness, trait_item_def_id: _ } => {
1272+
try_visit!(visitor.visit_defaultness(defaultness));
1273+
}
1274+
}
12721275
match *kind {
12731276
ImplItemKind::Const(ref ty, body) => {
12741277
try_visit!(visitor.visit_ty_unambig(ty));

compiler/rustc_hir_analysis/src/collect/type_of.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
198198
}
199199
}
200200
ImplItemKind::Type(ty) => {
201-
if tcx.impl_trait_ref(tcx.hir_get_parent_item(hir_id)).is_none() {
201+
if let ImplItemImplKind::Inherent { .. } = item.impl_kind {
202202
check_feature_inherent_assoc_ty(tcx, item.span);
203203
}
204204

compiler/rustc_lint/src/builtin.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc_hir::attrs::AttributeKind;
2929
use rustc_hir::def::{DefKind, Res};
3030
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
3131
use rustc_hir::intravisit::FnKind as HirFnKind;
32-
use rustc_hir::{Body, FnDecl, PatKind, PredicateOrigin, find_attr};
32+
use rustc_hir::{Body, FnDecl, ImplItemImplKind, PatKind, PredicateOrigin, find_attr};
3333
use rustc_middle::bug;
3434
use rustc_middle::lint::LevelAndSource;
3535
use rustc_middle::ty::layout::LayoutOf;
@@ -1321,9 +1321,8 @@ impl<'tcx> LateLintPass<'tcx> for UnreachablePub {
13211321
}
13221322

13231323
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {
1324-
// Only lint inherent impl items.
1325-
if cx.tcx.associated_item(impl_item.owner_id).trait_item_def_id.is_none() {
1326-
self.perform_lint(cx, "item", impl_item.owner_id.def_id, impl_item.vis_span, false);
1324+
if let ImplItemImplKind::Inherent { vis_span } = impl_item.impl_kind {
1325+
self.perform_lint(cx, "item", impl_item.owner_id.def_id, vis_span, false);
13271326
}
13281327
}
13291328
}

compiler/rustc_middle/src/ty/context.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,7 +2203,16 @@ impl<'tcx> TyCtxt<'tcx> {
22032203

22042204
let is_impl_item = match self.hir_node_by_def_id(suitable_region_binding_scope) {
22052205
Node::Item(..) | Node::TraitItem(..) => false,
2206-
Node::ImplItem(..) => self.is_bound_region_in_impl_item(suitable_region_binding_scope),
2206+
Node::ImplItem(impl_item) => match impl_item.impl_kind {
2207+
// For now, we do not try to target impls of traits. This is
2208+
// because this message is going to suggest that the user
2209+
// change the fn signature, but they may not be free to do so,
2210+
// since the signature must match the trait.
2211+
//
2212+
// FIXME(#42706) -- in some cases, we could do better here.
2213+
hir::ImplItemImplKind::Trait { .. } => true,
2214+
_ => false,
2215+
},
22072216
_ => false,
22082217
};
22092218

@@ -2257,21 +2266,6 @@ impl<'tcx> TyCtxt<'tcx> {
22572266
None
22582267
}
22592268

2260-
/// Checks if the bound region is in Impl Item.
2261-
pub fn is_bound_region_in_impl_item(self, suitable_region_binding_scope: LocalDefId) -> bool {
2262-
let container_id = self.parent(suitable_region_binding_scope.to_def_id());
2263-
if self.impl_trait_ref(container_id).is_some() {
2264-
// For now, we do not try to target impls of traits. This is
2265-
// because this message is going to suggest that the user
2266-
// change the fn signature, but they may not be free to do so,
2267-
// since the signature must match the trait.
2268-
//
2269-
// FIXME(#42706) -- in some cases, we could do better here.
2270-
return true;
2271-
}
2272-
false
2273-
}
2274-
22752269
/// Determines whether identifiers in the assembly have strict naming rules.
22762270
/// Currently, only NVPTX* targets need it.
22772271
pub fn has_strict_asm_symbol_naming(self) -> bool {

compiler/rustc_passes/src/dead.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -370,31 +370,27 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
370370
/// Automatically generated items marked with `rustc_trivial_field_reads`
371371
/// will be ignored for the purposes of dead code analysis (see PR #85200
372372
/// for discussion).
373-
fn should_ignore_item(&mut self, def_id: DefId) -> bool {
374-
if let Some(impl_of) = self.tcx.trait_impl_of_assoc(def_id) {
375-
if !self.tcx.is_automatically_derived(impl_of) {
376-
return false;
377-
}
378-
379-
if let Some(trait_of) = self.tcx.trait_id_of_impl(impl_of)
380-
&& self.tcx.has_attr(trait_of, sym::rustc_trivial_field_reads)
373+
fn should_ignore_impl_item(&mut self, impl_item: &hir::ImplItem<'_>) -> bool {
374+
if let hir::ImplItemImplKind::Trait { .. } = impl_item.impl_kind
375+
&& let impl_of = self.tcx.parent(impl_item.owner_id.to_def_id())
376+
&& self.tcx.is_automatically_derived(impl_of)
377+
&& let trait_ref = self.tcx.impl_trait_ref(impl_of).unwrap().instantiate_identity()
378+
&& self.tcx.has_attr(trait_ref.def_id, sym::rustc_trivial_field_reads)
379+
{
380+
if let ty::Adt(adt_def, _) = trait_ref.self_ty().kind()
381+
&& let Some(adt_def_id) = adt_def.did().as_local()
381382
{
382-
let trait_ref = self.tcx.impl_trait_ref(impl_of).unwrap().instantiate_identity();
383-
if let ty::Adt(adt_def, _) = trait_ref.self_ty().kind()
384-
&& let Some(adt_def_id) = adt_def.did().as_local()
385-
{
386-
self.ignored_derived_traits.entry(adt_def_id).or_default().insert(trait_of);
387-
}
388-
return true;
383+
self.ignored_derived_traits.entry(adt_def_id).or_default().insert(trait_ref.def_id);
389384
}
385+
return true;
390386
}
391387

392388
false
393389
}
394390

395391
fn visit_node(&mut self, node: Node<'tcx>) {
396-
if let Node::ImplItem(hir::ImplItem { owner_id, .. }) = node
397-
&& self.should_ignore_item(owner_id.to_def_id())
392+
if let Node::ImplItem(impl_item) = node
393+
&& self.should_ignore_impl_item(impl_item)
398394
{
399395
return;
400396
}
@@ -436,7 +432,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
436432
}
437433
Node::ImplItem(impl_item) => {
438434
let item = self.tcx.local_parent(impl_item.owner_id.def_id);
439-
if self.tcx.impl_trait_ref(item).is_none() {
435+
if let hir::ImplItemImplKind::Inherent { .. } = impl_item.impl_kind {
440436
//// If it's a type whose items are live, then it's live, too.
441437
//// This is done to handle the case where, for example, the static
442438
//// method of a private type is used, but the type itself is never

compiler/rustc_passes/src/stability.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,7 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
486486

487487
fn visit_impl_item(&mut self, ii: &'tcx hir::ImplItem<'tcx>) {
488488
self.check_compatible_stability(ii.owner_id.def_id);
489-
let impl_def_id = self.tcx.hir_get_parent_item(ii.hir_id());
490-
if self.tcx.impl_trait_ref(impl_def_id).is_none() {
489+
if let hir::ImplItemImplKind::Inherent { .. } = ii.impl_kind {
491490
self.check_missing_stability(ii.owner_id.def_id);
492491
self.check_missing_const_stability(ii.owner_id.def_id);
493492
}

compiler/rustc_ty_utils/src/assoc.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_hir::def::DefKind;
22
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId};
33
use rustc_hir::definitions::{DefPathData, DisambiguatorState};
44
use rustc_hir::intravisit::{self, Visitor};
5-
use rustc_hir::{self as hir, ItemKind};
5+
use rustc_hir::{self as hir, ImplItemImplKind, ItemKind};
66
use rustc_middle::query::Providers;
77
use rustc_middle::ty::{self, ImplTraitInTraitData, TyCtxt};
88
use rustc_middle::{bug, span_bug};
@@ -121,8 +121,11 @@ fn associated_item_from_impl_item(tcx: TyCtxt<'_>, impl_item: &hir::ImplItem<'_>
121121
ty::AssocItem {
122122
kind,
123123
def_id: owner_id.to_def_id(),
124-
trait_item_def_id: impl_item.trait_item_def_id,
125124
container: ty::AssocContainer::Impl,
125+
trait_item_def_id: match impl_item.impl_kind {
126+
ImplItemImplKind::Inherent { .. } => None,
127+
ImplItemImplKind::Trait { trait_item_def_id, .. } => trait_item_def_id.ok(),
128+
},
126129
}
127130
}
128131
struct RPITVisitor<'a, 'tcx> {
@@ -190,7 +193,10 @@ fn associated_types_for_impl_traits_in_trait_or_impl<'tcx>(
190193
}
191194
let did = item.owner_id.def_id.to_def_id();
192195
let item = tcx.hir_impl_item(*item);
193-
let Some(trait_item_def_id) = item.trait_item_def_id else {
196+
let ImplItemImplKind::Trait {
197+
trait_item_def_id: Ok(trait_item_def_id), ..
198+
} = item.impl_kind
199+
else {
194200
return Some((did, vec![]));
195201
};
196202
let iter = in_trait_def[&trait_item_def_id].iter().map(|&id| {

compiler/rustc_ty_utils/src/ty.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,15 @@ fn defaultness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Defaultness {
8888
}),
8989
..
9090
})
91-
| hir::Node::ImplItem(hir::ImplItem { defaultness, .. })
91+
| hir::Node::ImplItem(hir::ImplItem {
92+
impl_kind: hir::ImplItemImplKind::Trait { defaultness, .. },
93+
..
94+
})
9295
| hir::Node::TraitItem(hir::TraitItem { defaultness, .. }) => *defaultness,
96+
hir::Node::ImplItem(hir::ImplItem {
97+
impl_kind: hir::ImplItemImplKind::Inherent { .. },
98+
..
99+
}) => hir::Defaultness::Final,
93100
node => {
94101
bug!("`defaultness` called on {:?}", node);
95102
}

0 commit comments

Comments
 (0)