Skip to content

Commit 64ea4b9

Browse files
committed
Split AssocContainer::{InherentImpl,TraitImpl}
1 parent bd8b08d commit 64ea4b9

File tree

31 files changed

+144
-190
lines changed

31 files changed

+144
-190
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 4 additions & 4 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::AssocContainer::Impl => {}
1012+
ty::AssocContainer::InherentImpl | ty::AssocContainer::TraitImpl(_) => {}
10131013
ty::AssocContainer::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::AssocContainer::Impl => {}
1029+
ty::AssocContainer::InherentImpl | ty::AssocContainer::TraitImpl(_) => {}
10301030
ty::AssocContainer::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::AssocContainer::Impl => true,
1046+
ty::AssocContainer::InherentImpl | ty::AssocContainer::TraitImpl(_) => true,
10471047
ty::AssocContainer::Trait => {
10481048
tcx.ensure_ok().explicit_item_bounds(def_id);
10491049
tcx.ensure_ok().explicit_item_self_bounds(def_id);
@@ -1177,7 +1177,7 @@ fn check_impl_items_against_trait<'tcx>(
11771177

11781178
for &impl_item in impl_item_refs {
11791179
let ty_impl_item = tcx.associated_item(impl_item);
1180-
let ty_trait_item = if let Some(trait_item_id) = ty_impl_item.trait_item_def_id {
1180+
let ty_trait_item = if let Some(trait_item_id) = ty_impl_item.trait_item_def_id() {
11811181
tcx.associated_item(trait_item_id)
11821182
} else {
11831183
// Checked in `associated_item`.

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub(super) fn compare_impl_item(
3737
impl_item_def_id: LocalDefId,
3838
) -> Result<(), ErrorGuaranteed> {
3939
let impl_item = tcx.associated_item(impl_item_def_id);
40-
let trait_item = tcx.associated_item(impl_item.trait_item_def_id.unwrap());
40+
let trait_item = tcx.associated_item(impl_item.expect_trait_impl()?);
4141
let impl_trait_ref =
4242
tcx.impl_trait_ref(impl_item.container_id(tcx)).unwrap().instantiate_identity();
4343
debug!(?impl_trait_ref);
@@ -446,7 +446,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
446446
impl_m_def_id: LocalDefId,
447447
) -> Result<&'tcx DefIdMap<ty::EarlyBinder<'tcx, Ty<'tcx>>>, ErrorGuaranteed> {
448448
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());
449+
let trait_m = tcx.associated_item(impl_m.expect_trait_impl()?);
450450
let impl_trait_ref =
451451
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`,
@@ -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::AssocContainer::Impl => impl_trait_ref.self_ty(),
1452+
ty::AssocContainer::InherentImpl | ty::AssocContainer::TraitImpl(_) => {
1453+
impl_trait_ref.self_ty()
1454+
}
14531455
ty::AssocContainer::Trait => tcx.types.self_param,
14541456
};
14551457
let self_arg_ty = tcx.fn_sig(method.def_id).instantiate_identity().input(0);
@@ -2458,8 +2460,12 @@ 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::AssocContainer::InherentImpl => bug!(),
24612464
ty::AssocContainer::Trait => impl_ty,
2462-
ty::AssocContainer::Impl => tcx.associated_item(impl_ty.trait_item_def_id.unwrap()),
2465+
ty::AssocContainer::TraitImpl(Err(_)) => continue,
2466+
ty::AssocContainer::TraitImpl(Ok(trait_item_def_id)) => {
2467+
tcx.associated_item(trait_item_def_id)
2468+
}
24632469
};
24642470

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

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -945,11 +945,13 @@ pub(crate) fn check_associated_item(
945945
// Avoid bogus "type annotations needed `Foo: Bar`" errors on `impl Bar for Foo` in case
946946
// other `Foo` impls are incoherent.
947947
tcx.ensure_ok()
948-
.coherent_trait(tcx.parent(item.trait_item_def_id.unwrap_or(item_id.into())))?;
948+
.coherent_trait(tcx.parent(item.trait_item_def_id().unwrap_or(item_id.into())))?;
949949

950950
let self_ty = match item.container {
951951
ty::AssocContainer::Trait => tcx.types.self_param,
952-
ty::AssocContainer::Impl => tcx.type_of(item.container_id(tcx)).instantiate_identity(),
952+
ty::AssocContainer::InherentImpl | ty::AssocContainer::TraitImpl(_) => {
953+
tcx.type_of(item.container_id(tcx)).instantiate_identity()
954+
}
953955
};
954956

955957
let span = tcx.def_span(item_id);

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::AssocContainer::Impl => {
1036+
ty::AssocContainer::InherentImpl | ty::AssocContainer::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/fn_ctxt/checks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2347,7 +2347,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23472347
// We want it to always point to the trait item.
23482348
// If we're pointing at an inherent function, we don't need to do anything,
23492349
// so we fetch the parent and verify if it's a trait item.
2350-
&& let maybe_trait_item_def_id = assoc_item.trait_item_def_id.unwrap_or(def_id)
2350+
&& let maybe_trait_item_def_id = assoc_item.trait_item_def_id().unwrap_or(def_id)
23512351
&& let maybe_trait_def_id = self.tcx.parent(maybe_trait_item_def_id)
23522352
// Just an easy way to check "trait_def_id == Fn/FnMut/FnOnce"
23532353
&& let Some(call_kind) = self.tcx.fn_trait_kind_from_def_id(maybe_trait_def_id)

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +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::AssocContainer::Impl = item.container
294-
&& let Some(trait_item_def_id) = item.trait_item_def_id
293+
&& let ty::AssocContainer::TraitImpl(Ok(trait_item_def_id)) = item.container
295294
{
296295
let impl_def_id = item.container_id(tcx);
297296
let impl_trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap().instantiate_identity();

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1672,7 +1672,7 @@ impl<'tcx> Pick<'tcx> {
16721672
/// Do not use for type checking.
16731673
pub(crate) fn differs_from(&self, other: &Self) -> bool {
16741674
let Self {
1675-
item: AssocItem { def_id, kind: _, container: _, trait_item_def_id: _ },
1675+
item: AssocItem { def_id, kind: _, container: _ },
16761676
kind: _,
16771677
import_ids: _,
16781678
autoderefs: _,

compiler/rustc_lint/src/builtin.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use rustc_middle::bug;
3434
use rustc_middle::lint::LevelAndSource;
3535
use rustc_middle::ty::layout::LayoutOf;
3636
use rustc_middle::ty::print::with_no_trimmed_paths;
37-
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt, Upcast, VariantDef};
37+
use rustc_middle::ty::{self, AssocContainer, Ty, TyCtxt, TypeVisitableExt, Upcast, VariantDef};
3838
use rustc_session::lint::FutureIncompatibilityReason;
3939
// hardwired lints from rustc_lint_defs
4040
pub use rustc_session::lint::builtin::*;
@@ -61,7 +61,6 @@ use crate::lints::{
6161
BuiltinUnreachablePub, BuiltinUnsafe, BuiltinUnstableFeatures, BuiltinUnusedDocComment,
6262
BuiltinUnusedDocCommentSub, BuiltinWhileTrue, InvalidAsmLabel,
6363
};
64-
use crate::nonstandard_style::{MethodLateContext, method_context};
6564
use crate::{
6665
EarlyContext, EarlyLintPass, LateContext, LateLintPass, Level, LintContext,
6766
fluent_generated as fluent,
@@ -469,14 +468,14 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
469468
}
470469

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

474-
match context {
473+
match container {
475474
// If the method is an impl for a trait, don't doc.
476-
MethodLateContext::TraitImpl => return,
477-
MethodLateContext::TraitAutoImpl => {}
475+
AssocContainer::TraitImpl(_) => return,
476+
AssocContainer::Trait => {}
478477
// If the method is an impl for an item with docs_hidden, don't doc.
479-
MethodLateContext::PlainImpl => {
478+
AssocContainer::InherentImpl => {
480479
let parent = cx.tcx.hir_get_parent_item(impl_item.hir_id());
481480
let impl_ty = cx.tcx.type_of(parent).instantiate_identity();
482481
let outerdef = match impl_ty.kind() {

compiler/rustc_lint/src/nonstandard_style.rs

Lines changed: 7 additions & 29 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::AssocContainer;
1111
use rustc_session::config::CrateType;
1212
use rustc_session::{declare_lint, declare_lint_pass};
1313
use rustc_span::def_id::LocalDefId;
@@ -20,29 +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::AssocContainer::Trait => MethodLateContext::TraitAutoImpl,
34-
ty::AssocContainer::Impl => match cx.tcx.impl_trait_ref(item.container_id(cx.tcx)) {
35-
Some(_) => MethodLateContext::TraitImpl,
36-
None => MethodLateContext::PlainImpl,
37-
},
38-
}
39-
}
40-
41-
fn assoc_item_in_trait_impl(cx: &LateContext<'_>, ii: &hir::ImplItem<'_>) -> bool {
42-
let item = cx.tcx.associated_item(ii.owner_id);
43-
item.trait_item_def_id.is_some()
44-
}
45-
4623
declare_lint! {
4724
/// The `non_camel_case_types` lint detects types, variants, traits and
4825
/// type parameters that don't have camel case names.
@@ -397,19 +374,19 @@ impl<'tcx> LateLintPass<'tcx> for NonSnakeCase {
397374
id: LocalDefId,
398375
) {
399376
match &fk {
400-
FnKind::Method(ident, sig, ..) => match method_context(cx, id) {
401-
MethodLateContext::PlainImpl => {
377+
FnKind::Method(ident, sig, ..) => match cx.tcx.associated_item(id).container {
378+
AssocContainer::InherentImpl => {
402379
if sig.header.abi != ExternAbi::Rust
403380
&& find_attr!(cx.tcx.get_all_attrs(id), AttributeKind::NoMangle(..))
404381
{
405382
return;
406383
}
407384
self.check_snake_case(cx, "method", ident);
408385
}
409-
MethodLateContext::TraitAutoImpl => {
386+
AssocContainer::Trait => {
410387
self.check_snake_case(cx, "trait method", ident);
411388
}
412-
_ => (),
389+
AssocContainer::TraitImpl(_) => {}
413390
},
414391
FnKind::ItemFn(ident, _, header) => {
415392
// Skip foreign-ABI #[no_mangle] functions (Issue #31924)
@@ -610,7 +587,8 @@ impl<'tcx> LateLintPass<'tcx> for NonUpperCaseGlobals {
610587

611588
fn check_impl_item(&mut self, cx: &LateContext<'_>, ii: &hir::ImplItem<'_>) {
612589
if let hir::ImplItemKind::Const(..) = ii.kind
613-
&& !assoc_item_in_trait_impl(cx, ii)
590+
&& let assoc = cx.tcx.associated_item(ii.owner_id)
591+
&& !matches!(assoc.container, AssocContainer::TraitImpl(_))
614592
{
615593
NonUpperCaseGlobals::check_upper_case(cx, "associated constant", None, &ii.ident);
616594
}

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,10 +1194,6 @@ impl<'a> CrateMetadataRef<'a> {
11941194
self.root.tables.default_fields.get(self, id).map(|d| d.decode(self))
11951195
}
11961196

1197-
fn get_trait_item_def_id(self, id: DefIndex) -> Option<DefId> {
1198-
self.root.tables.trait_item_def_id.get(self, id).map(|d| d.decode_from_cdata(self))
1199-
}
1200-
12011197
fn get_expn_that_defined(self, id: DefIndex, sess: &Session) -> ExpnId {
12021198
self.root
12031199
.tables
@@ -1359,14 +1355,9 @@ impl<'a> CrateMetadataRef<'a> {
13591355
}
13601356
_ => bug!("cannot get associated-item of `{:?}`", self.def_key(id)),
13611357
};
1362-
let container = self.root.tables.assoc_container.get(self, id).unwrap();
1358+
let container = self.root.tables.assoc_container.get(self, id).unwrap().decode(self);
13631359

1364-
ty::AssocItem {
1365-
kind,
1366-
def_id: self.local_def_id(id),
1367-
trait_item_def_id: self.get_trait_item_def_id(id),
1368-
container,
1369-
}
1360+
ty::AssocItem { kind, def_id: self.local_def_id(id), container }
13701361
}
13711362

13721363
fn get_ctor(self, node_id: DefIndex) -> Option<(CtorKind, DefId)> {

0 commit comments

Comments
 (0)