Skip to content

Commit ecc5941

Browse files
Auto merge of #145186 - camsteffen:assoc-impl-kind, r=<try>
Make `AssocItem` aware of its impl kind
2 parents 41ede7b + 77a42ec commit ecc5941

File tree

50 files changed

+373
-326
lines changed

Some content is hidden

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

50 files changed

+373
-326
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ 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, find_attr};
8+
use rustc_hir::{self as hir, HirId, ImplItemImplKind, LifetimeSource, PredicateOrigin, find_attr};
99
use rustc_index::{IndexSlice, IndexVec};
1010
use rustc_middle::span_bug;
1111
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
@@ -1100,16 +1100,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
11001100
owner_id: hir_id.expect_owner(),
11011101
ident: self.lower_ident(ident),
11021102
generics,
1103+
impl_kind: if is_in_trait_impl {
1104+
ImplItemImplKind::Trait {
1105+
defaultness,
1106+
trait_item_def_id: self
1107+
.resolver
1108+
.get_partial_res(i.id)
1109+
.map(|r| r.expect_full_res().opt_def_id())
1110+
.unwrap_or(None),
1111+
}
1112+
} else {
1113+
ImplItemImplKind::Inherent { vis_span: self.lower_span(i.vis.span) }
1114+
},
11031115
kind,
1104-
vis_span: self.lower_span(i.vis.span),
11051116
span: self.lower_span(i.span),
1106-
defaultness,
11071117
has_delayed_lints: !self.delayed_lints.is_empty(),
1108-
trait_item_def_id: self
1109-
.resolver
1110-
.get_partial_res(i.id)
1111-
.map(|r| r.expect_full_res().opt_def_id())
1112-
.unwrap_or(None),
11131118
};
11141119
self.arena.alloc(item)
11151120
}

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ use rustc_middle::traits::query::NoSolution;
2525
use rustc_middle::ty::adjustment::PointerCoercion;
2626
use rustc_middle::ty::cast::CastTy;
2727
use rustc_middle::ty::{
28-
self, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, CoroutineArgsExt,
29-
GenericArgsRef, OpaqueHiddenType, OpaqueTypeKey, RegionVid, Ty, TyCtxt, TypeVisitableExt,
30-
UserArgs, UserTypeAnnotationIndex, fold_regions,
28+
self, AssocItemContainer, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations,
29+
CoroutineArgsExt, GenericArgsRef, OpaqueHiddenType, OpaqueTypeKey, RegionVid, Ty, TyCtxt,
30+
TypeVisitableExt, UserArgs, UserTypeAnnotationIndex, fold_regions,
3131
};
3232
use rustc_middle::{bug, span_bug};
3333
use rustc_mir_dataflow::move_paths::MoveData;
@@ -1774,8 +1774,8 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
17741774
);
17751775

17761776
assert!(!matches!(
1777-
tcx.impl_of_assoc(def_id).map(|imp| tcx.def_kind(imp)),
1778-
Some(DefKind::Impl { of_trait: true })
1777+
tcx.opt_associated_item(def_id).map(|assoc| assoc.container),
1778+
Some(AssocItemContainer::TraitImpl),
17791779
));
17801780
self.prove_predicates(
17811781
args.types().map(|ty| ty::ClauseKind::WellFormed(ty.into())),

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_hir::def_id::{DefId, DefIdMap};
1717
use rustc_index::IndexVec;
1818
use rustc_middle::mir;
1919
use rustc_middle::ty::layout::{HasTypingEnv, LayoutOf};
20-
use rustc_middle::ty::{self, GenericArgsRef, Instance, Ty, TypeVisitableExt};
20+
use rustc_middle::ty::{self, AssocItemContainer, GenericArgsRef, Instance, Ty, TypeVisitableExt};
2121
use rustc_session::Session;
2222
use rustc_session::config::{self, DebugInfo};
2323
use rustc_span::{
@@ -533,31 +533,29 @@ impl<'ll, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
533533
// First, let's see if this is a method within an inherent impl. Because
534534
// if yes, we want to make the result subroutine DIE a child of the
535535
// subroutine's self-type.
536-
if let Some(impl_def_id) = cx.tcx.impl_of_assoc(instance.def_id()) {
537-
// If the method does *not* belong to a trait, proceed
538-
if cx.tcx.trait_id_of_impl(impl_def_id).is_none() {
539-
let impl_self_ty = cx.tcx.instantiate_and_normalize_erasing_regions(
540-
instance.args,
541-
cx.typing_env(),
542-
cx.tcx.type_of(impl_def_id),
543-
);
544-
545-
// Only "class" methods are generally understood by LLVM,
546-
// so avoid methods on other types (e.g., `<*mut T>::null`).
547-
if let ty::Adt(def, ..) = impl_self_ty.kind()
548-
&& !def.is_box()
549-
{
550-
// Again, only create type information if full debuginfo is enabled
551-
if cx.sess().opts.debuginfo == DebugInfo::Full && !impl_self_ty.has_param()
552-
{
553-
return (type_di_node(cx, impl_self_ty), true);
554-
} else {
555-
return (namespace::item_namespace(cx, def.did()), false);
556-
}
536+
if let Some(assoc) = cx.tcx.opt_associated_item(instance.def_id())
537+
// For trait method impls we still use the "parallel namespace"
538+
// strategy
539+
&& assoc.container == AssocItemContainer::InherentImpl
540+
{
541+
let impl_def_id = cx.tcx.parent(instance.def_id());
542+
let impl_self_ty = cx.tcx.instantiate_and_normalize_erasing_regions(
543+
instance.args,
544+
cx.typing_env(),
545+
cx.tcx.type_of(impl_def_id),
546+
);
547+
548+
// Only "class" methods are generally understood by LLVM,
549+
// so avoid methods on other types (e.g., `<*mut T>::null`).
550+
if let ty::Adt(def, ..) = impl_self_ty.kind()
551+
&& !def.is_box()
552+
{
553+
// Again, only create type information if full debuginfo is enabled
554+
if cx.sess().opts.debuginfo == DebugInfo::Full && !impl_self_ty.has_param() {
555+
return (type_di_node(cx, impl_self_ty), true);
556+
} else {
557+
return (namespace::item_namespace(cx, def.did()), false);
557558
}
558-
} else {
559-
// For trait method impls we still use the "parallel namespace"
560-
// strategy
561559
}
562560
}
563561

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
600600
fn opt_trait_item(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
601601
let impl_item = tcx.opt_associated_item(def_id)?;
602602
match impl_item.container {
603-
ty::AssocItemContainer::Impl => impl_item.trait_item_def_id,
603+
ty::AssocItemContainer::TraitImpl => impl_item.trait_item_def_id,
604604
_ => None,
605605
}
606606
}

compiler/rustc_hir/src/hir.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3190,12 +3190,22 @@ pub struct ImplItem<'hir> {
31903190
pub owner_id: OwnerId,
31913191
pub generics: &'hir Generics<'hir>,
31923192
pub kind: ImplItemKind<'hir>,
3193-
pub defaultness: Defaultness,
3193+
pub impl_kind: ImplItemImplKind,
31943194
pub span: Span,
3195-
pub vis_span: Span,
31963195
pub has_delayed_lints: bool,
3197-
/// When we are in a trait impl, link to the trait-item's id.
3198-
pub trait_item_def_id: Option<DefId>,
3196+
}
3197+
3198+
#[derive(Debug, Clone, Copy, HashStable_Generic)]
3199+
pub enum ImplItemImplKind {
3200+
Inherent {
3201+
vis_span: Span,
3202+
},
3203+
Trait {
3204+
defaultness: Defaultness,
3205+
/// Item in the trait that this item implements.
3206+
/// May be `None` if there is an error.
3207+
trait_item_def_id: Option<DefId>,
3208+
},
31993209
}
32003210

32013211
impl<'hir> ImplItem<'hir> {
@@ -3209,6 +3219,13 @@ impl<'hir> ImplItem<'hir> {
32093219
ImplItemId { owner_id: self.owner_id }
32103220
}
32113221

3222+
pub fn vis_span(&self) -> Option<Span> {
3223+
match self.impl_kind {
3224+
ImplItemImplKind::Trait { .. } => None,
3225+
ImplItemImplKind::Inherent { vis_span, .. } => Some(vis_span),
3226+
}
3227+
}
3228+
32123229
expect_methods_self_kind! {
32133230
expect_const, (&'hir Ty<'hir>, BodyId), ImplItemKind::Const(ty, body), (ty, *body);
32143231
expect_fn, (&FnSig<'hir>, BodyId), ImplItemKind::Fn(ty, body), (ty, *body);
@@ -4953,7 +4970,7 @@ mod size_asserts {
49534970
static_assert_size!(GenericBound<'_>, 64);
49544971
static_assert_size!(Generics<'_>, 56);
49554972
static_assert_size!(Impl<'_>, 80);
4956-
static_assert_size!(ImplItem<'_>, 96);
4973+
static_assert_size!(ImplItem<'_>, 88);
49574974
static_assert_size!(ImplItemKind<'_>, 40);
49584975
static_assert_size!(Item<'_>, 88);
49594976
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/check/check.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
10091009
res = res.and(check_associated_item(tcx, def_id));
10101010
let assoc_item = tcx.associated_item(def_id);
10111011
match assoc_item.container {
1012-
ty::AssocItemContainer::Impl => {}
1012+
ty::AssocItemContainer::InherentImpl | ty::AssocItemContainer::TraitImpl => {}
10131013
ty::AssocItemContainer::Trait => {
10141014
res = res.and(check_trait_item(tcx, def_id));
10151015
}
@@ -1026,7 +1026,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
10261026
res = res.and(check_associated_item(tcx, def_id));
10271027
let assoc_item = tcx.associated_item(def_id);
10281028
match assoc_item.container {
1029-
ty::AssocItemContainer::Impl => {}
1029+
ty::AssocItemContainer::InherentImpl | ty::AssocItemContainer::TraitImpl => {}
10301030
ty::AssocItemContainer::Trait => {
10311031
res = res.and(check_trait_item(tcx, def_id));
10321032
}
@@ -1043,7 +1043,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
10431043

10441044
let assoc_item = tcx.associated_item(def_id);
10451045
let has_type = match assoc_item.container {
1046-
ty::AssocItemContainer::Impl => true,
1046+
ty::AssocItemContainer::InherentImpl | ty::AssocItemContainer::TraitImpl => true,
10471047
ty::AssocItemContainer::Trait => {
10481048
tcx.ensure_ok().explicit_item_bounds(def_id);
10491049
tcx.ensure_ok().explicit_item_self_bounds(def_id);

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -445,10 +445,10 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
445445
tcx: TyCtxt<'tcx>,
446446
impl_m_def_id: LocalDefId,
447447
) -> Result<&'tcx DefIdMap<ty::EarlyBinder<'tcx, Ty<'tcx>>>, ErrorGuaranteed> {
448-
let impl_m = tcx.opt_associated_item(impl_m_def_id.to_def_id()).unwrap();
449-
let trait_m = tcx.opt_associated_item(impl_m.trait_item_def_id.unwrap()).unwrap();
448+
let impl_m = tcx.associated_item(impl_m_def_id.to_def_id());
449+
let trait_m = tcx.associated_item(impl_m.trait_item_def_id.unwrap());
450450
let impl_trait_ref =
451-
tcx.impl_trait_ref(impl_m.impl_container(tcx).unwrap()).unwrap().instantiate_identity();
451+
tcx.impl_trait_ref(tcx.parent(impl_m_def_id.to_def_id())).unwrap().instantiate_identity();
452452
// First, check a few of the same things as `compare_impl_method`,
453453
// just so we don't ICE during instantiation later.
454454
check_method_is_structurally_compatible(tcx, impl_m, trait_m, impl_trait_ref, true)?;
@@ -1449,7 +1449,9 @@ fn compare_self_type<'tcx>(
14491449

14501450
let self_string = |method: ty::AssocItem| {
14511451
let untransformed_self_ty = match method.container {
1452-
ty::AssocItemContainer::Impl => impl_trait_ref.self_ty(),
1452+
ty::AssocItemContainer::InherentImpl | ty::AssocItemContainer::TraitImpl => {
1453+
impl_trait_ref.self_ty()
1454+
}
14531455
ty::AssocItemContainer::Trait => tcx.types.self_param,
14541456
};
14551457
let self_arg_ty = tcx.fn_sig(method.def_id).instantiate_identity().input(0);
@@ -2458,8 +2460,11 @@ fn param_env_with_gat_bounds<'tcx>(
24582460

24592461
for impl_ty in impl_tys_to_install {
24602462
let trait_ty = match impl_ty.container {
2463+
ty::AssocItemContainer::InherentImpl => bug!(),
24612464
ty::AssocItemContainer::Trait => impl_ty,
2462-
ty::AssocItemContainer::Impl => tcx.associated_item(impl_ty.trait_item_def_id.unwrap()),
2465+
ty::AssocItemContainer::TraitImpl => {
2466+
tcx.associated_item(impl_ty.trait_item_def_id.unwrap())
2467+
}
24632468
};
24642469

24652470
let mut bound_vars: smallvec::SmallVec<[ty::BoundVariableKind; 8]> =

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ pub(crate) fn check_associated_item(
949949

950950
let self_ty = match item.container {
951951
ty::AssocItemContainer::Trait => tcx.types.self_param,
952-
ty::AssocItemContainer::Impl => {
952+
ty::AssocItemContainer::InherentImpl | ty::AssocItemContainer::TraitImpl => {
953953
tcx.type_of(item.container_id(tcx)).instantiate_identity()
954954
}
955955
};

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

0 commit comments

Comments
 (0)