Skip to content

Commit b7269fd

Browse files
committed
Split AssocItemContainer::{InherentImpl,TraitImpl}
1 parent 18b3463 commit b7269fd

File tree

30 files changed

+203
-199
lines changed

30 files changed

+203
-199
lines changed

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_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_typeck/src/fn_ctxt/_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10331033
self.set_tainted_by_errors(e);
10341034
}
10351035
}
1036-
ty::AssocItemContainer::Impl => {
1036+
ty::AssocItemContainer::InherentImpl | ty::AssocItemContainer::TraitImpl => {
10371037
if segments.len() == 1 {
10381038
// `<T>::assoc` will end up here, and so
10391039
// can `T::assoc`. If this came from an

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ fn infer_type_if_missing<'tcx>(fcx: &FnCtxt<'_, 'tcx>, node: Node<'tcx>) -> Opti
290290
{
291291
if let Some(item) = tcx.opt_associated_item(def_id.into())
292292
&& let ty::AssocKind::Const { .. } = item.kind
293-
&& let ty::AssocItemContainer::Impl = item.container
293+
&& let ty::AssocItemContainer::TraitImpl = item.container
294294
&& let Some(trait_item_def_id) = item.trait_item_def_id
295295
{
296296
let impl_def_id = item.container_id(tcx);

compiler/rustc_lint/src/builtin.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ use rustc_middle::bug;
3333
use rustc_middle::lint::LevelAndSource;
3434
use rustc_middle::ty::layout::LayoutOf;
3535
use rustc_middle::ty::print::with_no_trimmed_paths;
36-
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt, Upcast, VariantDef};
36+
use rustc_middle::ty::{
37+
self, AssocItemContainer, Ty, TyCtxt, TypeVisitableExt, Upcast, VariantDef,
38+
};
3739
use rustc_session::lint::FutureIncompatibilityReason;
3840
// hardwired lints from rustc_lint_defs
3941
pub use rustc_session::lint::builtin::*;
@@ -60,7 +62,6 @@ use crate::lints::{
6062
BuiltinUnreachablePub, BuiltinUnsafe, BuiltinUnstableFeatures, BuiltinUnusedDocComment,
6163
BuiltinUnusedDocCommentSub, BuiltinWhileTrue, InvalidAsmLabel,
6264
};
63-
use crate::nonstandard_style::{MethodLateContext, method_context};
6465
use crate::{
6566
EarlyContext, EarlyLintPass, LateContext, LateLintPass, Level, LintContext,
6667
fluent_generated as fluent,
@@ -458,14 +459,14 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
458459
}
459460

460461
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {
461-
let context = method_context(cx, impl_item.owner_id.def_id);
462+
let container = cx.tcx.associated_item(impl_item.owner_id.def_id).container;
462463

463-
match context {
464+
match container {
464465
// If the method is an impl for a trait, don't doc.
465-
MethodLateContext::TraitImpl => return,
466-
MethodLateContext::TraitAutoImpl => {}
466+
AssocItemContainer::TraitImpl => return,
467+
AssocItemContainer::Trait => {}
467468
// If the method is an impl for an item with docs_hidden, don't doc.
468-
MethodLateContext::PlainImpl => {
469+
AssocItemContainer::InherentImpl => {
469470
let parent = cx.tcx.hir_get_parent_item(impl_item.hir_id());
470471
let impl_ty = cx.tcx.type_of(parent).instantiate_identity();
471472
let outerdef = match impl_ty.kind() {

compiler/rustc_lint/src/nonstandard_style.rs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hir::def_id::DefId;
77
use rustc_hir::intravisit::{FnKind, Visitor};
88
use rustc_hir::{AttrArgs, AttrItem, Attribute, GenericParamKind, PatExprKind, PatKind, find_attr};
99
use rustc_middle::hir::nested_filter::All;
10-
use rustc_middle::ty;
10+
use rustc_middle::ty::AssocItemContainer;
1111
use rustc_session::config::CrateType;
1212
use rustc_session::{declare_lint, declare_lint_pass};
1313
use rustc_span::def_id::LocalDefId;
@@ -20,24 +20,6 @@ use crate::lints::{
2020
};
2121
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
2222

23-
#[derive(PartialEq)]
24-
pub(crate) enum MethodLateContext {
25-
TraitAutoImpl,
26-
TraitImpl,
27-
PlainImpl,
28-
}
29-
30-
pub(crate) fn method_context(cx: &LateContext<'_>, id: LocalDefId) -> MethodLateContext {
31-
let item = cx.tcx.associated_item(id);
32-
match item.container {
33-
ty::AssocItemContainer::Trait => MethodLateContext::TraitAutoImpl,
34-
ty::AssocItemContainer::Impl => match cx.tcx.impl_trait_ref(item.container_id(cx.tcx)) {
35-
Some(_) => MethodLateContext::TraitImpl,
36-
None => MethodLateContext::PlainImpl,
37-
},
38-
}
39-
}
40-
4123
fn assoc_item_in_trait_impl(cx: &LateContext<'_>, ii: &hir::ImplItem<'_>) -> bool {
4224
let item = cx.tcx.associated_item(ii.owner_id);
4325
item.trait_item_def_id.is_some()
@@ -397,19 +379,19 @@ impl<'tcx> LateLintPass<'tcx> for NonSnakeCase {
397379
id: LocalDefId,
398380
) {
399381
match &fk {
400-
FnKind::Method(ident, sig, ..) => match method_context(cx, id) {
401-
MethodLateContext::PlainImpl => {
382+
FnKind::Method(ident, sig, ..) => match cx.tcx.associated_item(id).container {
383+
AssocItemContainer::InherentImpl => {
402384
if sig.header.abi != ExternAbi::Rust
403385
&& find_attr!(cx.tcx.get_all_attrs(id), AttributeKind::NoMangle(..))
404386
{
405387
return;
406388
}
407389
self.check_snake_case(cx, "method", ident);
408390
}
409-
MethodLateContext::TraitAutoImpl => {
391+
AssocItemContainer::Trait => {
410392
self.check_snake_case(cx, "trait method", ident);
411393
}
412-
_ => (),
394+
AssocItemContainer::TraitImpl => {}
413395
},
414396
FnKind::ItemFn(ident, _, header) => {
415397
// Skip foreign-ABI #[no_mangle] functions (Issue #31924)

0 commit comments

Comments
 (0)