Skip to content

Commit deb0af0

Browse files
committed
removing new_from_def_id and alias_ty_kind_from_def_id, using alias-term-kind
1 parent f53b654 commit deb0af0

5 files changed

Lines changed: 63 additions & 43 deletions

File tree

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,20 @@ impl LowerTypeRelativePathMode {
288288
Self::Const => PermitVariants::No,
289289
}
290290
}
291+
292+
fn inherent_alias_term_kind<'tcx>(self, def_id: DefId) -> ty::AliasTermKind<'tcx> {
293+
match self {
294+
Self::Type(_) => ty::AliasTermKind::InherentTy { def_id },
295+
Self::Const => ty::AliasTermKind::InherentConst { def_id },
296+
}
297+
}
298+
299+
fn projection_alias_term_kind<'tcx>(self, def_id: DefId) -> ty::AliasTermKind<'tcx> {
300+
match self {
301+
Self::Type(_) => ty::AliasTermKind::ProjectionTy { def_id },
302+
Self::Const => ty::AliasTermKind::ProjectionConst { def_id },
303+
}
304+
}
291305
}
292306

293307
/// Whether to permit a path to resolve to an enum variant.
@@ -299,7 +313,7 @@ pub enum PermitVariants {
299313

300314
#[derive(Debug, Clone, Copy)]
301315
enum TypeRelativePath<'tcx> {
302-
AssocItem(DefId, GenericArgsRef<'tcx>),
316+
AssocItem(ty::AliasTermKind<'tcx>, GenericArgsRef<'tcx>),
303317
Variant { adt: Ty<'tcx>, variant_did: DefId },
304318
Ctor { ctor_def_id: DefId, args: GenericArgsRef<'tcx> },
305319
}
@@ -1476,12 +1490,21 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
14761490
span,
14771491
LowerTypeRelativePathMode::Type(permit_variants),
14781492
)? {
1479-
TypeRelativePath::AssocItem(def_id, args) => {
1480-
let alias_ty = ty::AliasTy::new_from_args(
1481-
tcx,
1482-
ty::AliasTyKind::new_from_def_id(tcx, def_id),
1483-
args,
1484-
);
1493+
TypeRelativePath::AssocItem(kind, args) => {
1494+
let def_id = kind.def_id();
1495+
let alias_ty_kind = match kind {
1496+
ty::AliasTermKind::ProjectionTy { def_id } => ty::Projection { def_id },
1497+
ty::AliasTermKind::InherentTy { def_id } => ty::Inherent { def_id },
1498+
ty::AliasTermKind::OpaqueTy { .. }
1499+
| ty::AliasTermKind::FreeTy { .. }
1500+
| ty::AliasTermKind::ProjectionConst { .. }
1501+
| ty::AliasTermKind::InherentConst { .. }
1502+
| ty::AliasTermKind::FreeConst { .. }
1503+
| ty::AliasTermKind::UnevaluatedConst { .. } => {
1504+
unreachable!()
1505+
}
1506+
};
1507+
let alias_ty = ty::AliasTy::new_from_args(tcx, alias_ty_kind, args);
14851508
let ty = Ty::new_alias(tcx, alias_ty);
14861509
let ty = self.check_param_uses_if_mcg(ty, span, false);
14871510
Ok((ty, tcx.def_kind(def_id), def_id))
@@ -1516,12 +1539,25 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
15161539
span,
15171540
LowerTypeRelativePathMode::Const,
15181541
)? {
1519-
TypeRelativePath::AssocItem(def_id, args) => {
1520-
self.require_type_const_attribute(def_id, span)?;
1521-
let ct = Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(def_id, args));
1522-
let ct = self.check_param_uses_if_mcg(ct, span, false);
1523-
Ok(ct)
1524-
}
1542+
TypeRelativePath::AssocItem(kind, args) => match kind {
1543+
ty::AliasTermKind::ProjectionConst { def_id }
1544+
| ty::AliasTermKind::InherentConst { def_id } => {
1545+
self.require_type_const_attribute(def_id, span)?;
1546+
let ct = Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(def_id, args));
1547+
let ct = self.check_param_uses_if_mcg(ct, span, false);
1548+
Ok(ct)
1549+
}
1550+
ty::AliasTermKind::ProjectionTy { .. }
1551+
| ty::AliasTermKind::InherentTy { .. }
1552+
| ty::AliasTermKind::OpaqueTy { .. }
1553+
| ty::AliasTermKind::FreeTy { .. }
1554+
| ty::AliasTermKind::UnevaluatedConst { .. }
1555+
| ty::AliasTermKind::FreeConst { .. } => span_bug!(
1556+
span,
1557+
"type-relative const path should only produce \
1558+
ProjectionConst or InherentConst, got {kind:?}"
1559+
),
1560+
},
15251561
TypeRelativePath::Ctor { ctor_def_id, args } => match tcx.def_kind(ctor_def_id) {
15261562
DefKind::Ctor(_, CtorKind::Fn) => {
15271563
Ok(ty::Const::zero_sized(tcx, Ty::new_fn_def(tcx, ctor_def_id, args)))
@@ -1648,15 +1684,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
16481684
}
16491685

16501686
// FIXME(inherent_associated_types, #106719): Support self types other than ADTs.
1651-
if let Some((did, args)) = self.probe_inherent_assoc_item(
1687+
if let Some((def_id, args)) = self.probe_inherent_assoc_item(
16521688
segment,
16531689
adt_def.did(),
16541690
self_ty,
16551691
qpath_hir_id,
16561692
span,
16571693
mode.assoc_tag(),
16581694
)? {
1659-
return Ok(TypeRelativePath::AssocItem(did, args));
1695+
return Ok(TypeRelativePath::AssocItem(
1696+
mode.inherent_alias_term_kind(def_id),
1697+
args,
1698+
));
16601699
}
16611700
}
16621701

@@ -1690,7 +1729,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
16901729
);
16911730
}
16921731

1693-
Ok(TypeRelativePath::AssocItem(item_def_id, args))
1732+
Ok(TypeRelativePath::AssocItem(mode.projection_alias_term_kind(item_def_id), args))
16941733
}
16951734

16961735
/// Resolve a [type-relative](hir::QPath::TypeRelative) (and type-level) path.

compiler/rustc_middle/src/ty/context/impl_interner.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -184,21 +184,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
184184
self.adt_def(adt_def_id)
185185
}
186186

187-
fn alias_ty_kind_from_def_id(self, def_id: DefId) -> ty::AliasTyKind<'tcx> {
188-
match self.def_kind(def_id) {
189-
DefKind::AssocTy
190-
if let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(def_id)) =>
191-
{
192-
ty::Inherent { def_id }
193-
}
194-
DefKind::AssocTy => ty::Projection { def_id },
195-
196-
DefKind::OpaqueTy => ty::Opaque { def_id },
197-
DefKind::TyAlias => ty::Free { def_id },
198-
kind => bug!("unexpected DefKind in AliasTy: {kind:?}"),
199-
}
200-
}
201-
202187
fn alias_term_kind_from_def_id(self, def_id: DefId) -> ty::AliasTermKind<'tcx> {
203188
match self.def_kind(def_id) {
204189
DefKind::AssocTy => {

compiler/rustc_type_ir/src/interner.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,6 @@ pub trait Interner:
209209
type AdtDef: AdtDef<Self>;
210210
fn adt_def(self, adt_def_id: Self::AdtId) -> Self::AdtDef;
211211

212-
fn alias_ty_kind_from_def_id(self, def_id: Self::DefId) -> ty::AliasTyKind<Self>;
213-
214212
// FIXME: remove in favor of explicit construction
215213
fn alias_term_kind_from_def_id(self, def_id: Self::DefId) -> ty::AliasTermKind<Self>;
216214

compiler/rustc_type_ir/src/ty_kind.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,6 @@ pub enum AliasTyKind<I: Interner> {
6565
}
6666

6767
impl<I: Interner> AliasTyKind<I> {
68-
pub fn new_from_def_id(interner: I, def_id: I::DefId) -> Self {
69-
interner.alias_ty_kind_from_def_id(def_id)
70-
}
71-
7268
pub fn descr(self) -> &'static str {
7369
match self {
7470
AliasTyKind::Projection { .. } => "associated type",

src/tools/clippy/clippy_utils/src/ty/mod.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_middle::traits::EvaluationResult;
1919
use rustc_middle::ty::adjustment::{Adjust, Adjustment, DerefAdjustKind};
2020
use rustc_middle::ty::layout::ValidityRequirement;
2121
use rustc_middle::ty::{
22-
self, AdtDef, AliasTy, AssocItem, AssocTag, Binder, BoundRegion, BoundVarIndexKind, FnSig, GenericArg,
22+
self, AdtDef, AliasTy, AssocContainer, AssocItem, AssocTag, Binder, BoundRegion, BoundVarIndexKind, FnSig, GenericArg,
2323
GenericArgKind, GenericArgsRef, IntTy, Region, RegionKind, TraitRef, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable,
2424
TypeVisitableExt, TypeVisitor, UintTy, Upcast, VariantDef, VariantDiscr,
2525
Unnormalized,
@@ -1029,11 +1029,13 @@ pub fn make_projection<'tcx>(
10291029
#[cfg(debug_assertions)]
10301030
assert_generic_args_match(tcx, assoc_item.def_id, args);
10311031

1032-
Some(AliasTy::new_from_args(
1033-
tcx,
1034-
ty::AliasTyKind::new_from_def_id(tcx, assoc_item.def_id),
1035-
args,
1036-
))
1032+
let alias_kind = match assoc_item.container {
1033+
AssocContainer::Trait | AssocContainer::TraitImpl(_) => {
1034+
ty::Projection { def_id: assoc_item.def_id }
1035+
}
1036+
AssocContainer::InherentImpl => ty::Inherent { def_id: assoc_item.def_id },
1037+
};
1038+
Some(AliasTy::new_from_args(tcx, alias_kind, args))
10371039
}
10381040
helper(
10391041
tcx,

0 commit comments

Comments
 (0)