Skip to content

Commit 5327d39

Browse files
committed
removing new_from_def_id and alias_ty_kind_from_def_id, introduce alias-term-kind
1 parent 14196db commit 5327d39

5 files changed

Lines changed: 64 additions & 45 deletions

File tree

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,20 @@ impl LowerTypeRelativePathMode {
287287
Self::Const => PermitVariants::No,
288288
}
289289
}
290+
291+
fn inherent_alias_term_kind(self) -> ty::AliasTermKind {
292+
match self {
293+
Self::Type(_) => ty::AliasTermKind::InherentTy,
294+
Self::Const => ty::AliasTermKind::InherentConst,
295+
}
296+
}
297+
298+
fn projection_alias_term_kind(self) -> ty::AliasTermKind {
299+
match self {
300+
Self::Type(_) => ty::AliasTermKind::ProjectionTy,
301+
Self::Const => ty::AliasTermKind::ProjectionConst,
302+
}
303+
}
290304
}
291305

292306
/// Whether to permit a path to resolve to an enum variant.
@@ -298,7 +312,7 @@ pub enum PermitVariants {
298312

299313
#[derive(Debug, Clone, Copy)]
300314
enum TypeRelativePath<'tcx> {
301-
AssocItem(DefId, GenericArgsRef<'tcx>),
315+
AssocItem(ty::AliasTermKind, DefId, GenericArgsRef<'tcx>),
302316
Variant { adt: Ty<'tcx>, variant_did: DefId },
303317
Ctor { ctor_def_id: DefId, args: GenericArgsRef<'tcx> },
304318
}
@@ -1400,12 +1414,20 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
14001414
span,
14011415
LowerTypeRelativePathMode::Type(permit_variants),
14021416
)? {
1403-
TypeRelativePath::AssocItem(def_id, args) => {
1404-
let alias_ty = ty::AliasTy::new_from_args(
1405-
tcx,
1406-
ty::AliasTyKind::new_from_def_id(tcx, def_id),
1407-
args,
1408-
);
1417+
TypeRelativePath::AssocItem(kind, def_id, args) => {
1418+
let alias_ty_kind = match kind {
1419+
ty::AliasTermKind::ProjectionTy => ty::Projection { def_id },
1420+
ty::AliasTermKind::InherentTy => ty::Inherent { def_id },
1421+
ty::AliasTermKind::OpaqueTy
1422+
| ty::AliasTermKind::FreeTy
1423+
| ty::AliasTermKind::ProjectionConst
1424+
| ty::AliasTermKind::InherentConst
1425+
| ty::AliasTermKind::FreeConst
1426+
| ty::AliasTermKind::UnevaluatedConst => {
1427+
unreachable!()
1428+
}
1429+
};
1430+
let alias_ty = ty::AliasTy::new_from_args(tcx, alias_ty_kind, args);
14091431
let ty = Ty::new_alias(tcx, alias_ty);
14101432
let ty = self.check_param_uses_if_mcg(ty, span, false);
14111433
Ok((ty, tcx.def_kind(def_id), def_id))
@@ -1440,12 +1462,24 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
14401462
span,
14411463
LowerTypeRelativePathMode::Const,
14421464
)? {
1443-
TypeRelativePath::AssocItem(def_id, args) => {
1444-
self.require_type_const_attribute(def_id, span)?;
1445-
let ct = Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(def_id, args));
1446-
let ct = self.check_param_uses_if_mcg(ct, span, false);
1447-
Ok(ct)
1448-
}
1465+
TypeRelativePath::AssocItem(kind, def_id, args) => match kind {
1466+
ty::AliasTermKind::ProjectionConst | ty::AliasTermKind::InherentConst => {
1467+
self.require_type_const_attribute(def_id, span)?;
1468+
let ct = Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(def_id, args));
1469+
let ct = self.check_param_uses_if_mcg(ct, span, false);
1470+
Ok(ct)
1471+
}
1472+
ty::AliasTermKind::ProjectionTy
1473+
| ty::AliasTermKind::InherentTy
1474+
| ty::AliasTermKind::OpaqueTy
1475+
| ty::AliasTermKind::FreeTy
1476+
| ty::AliasTermKind::UnevaluatedConst
1477+
| ty::AliasTermKind::FreeConst => span_bug!(
1478+
span,
1479+
"type-relative const path should only produce \
1480+
ProjectionConst or InherentConst, got {kind:?}"
1481+
),
1482+
},
14491483
TypeRelativePath::Ctor { ctor_def_id, args } => match tcx.def_kind(ctor_def_id) {
14501484
DefKind::Ctor(_, CtorKind::Fn) => {
14511485
Ok(ty::Const::zero_sized(tcx, Ty::new_fn_def(tcx, ctor_def_id, args)))
@@ -1572,15 +1606,19 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
15721606
}
15731607

15741608
// FIXME(inherent_associated_types, #106719): Support self types other than ADTs.
1575-
if let Some((did, args)) = self.probe_inherent_assoc_item(
1609+
if let Some((def_id, args)) = self.probe_inherent_assoc_item(
15761610
segment,
15771611
adt_def.did(),
15781612
self_ty,
15791613
qpath_hir_id,
15801614
span,
15811615
mode.assoc_tag(),
15821616
)? {
1583-
return Ok(TypeRelativePath::AssocItem(did, args));
1617+
return Ok(TypeRelativePath::AssocItem(
1618+
mode.inherent_alias_term_kind(),
1619+
def_id,
1620+
args,
1621+
));
15841622
}
15851623
}
15861624

@@ -1614,7 +1652,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
16141652
);
16151653
}
16161654

1617-
Ok(TypeRelativePath::AssocItem(item_def_id, args))
1655+
Ok(TypeRelativePath::AssocItem(mode.projection_alias_term_kind(), item_def_id, args))
16181656
}
16191657

16201658
/// 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
@@ -187,21 +187,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
187187
self.adt_def(adt_def_id)
188188
}
189189

190-
fn alias_ty_kind_from_def_id(self, def_id: DefId) -> ty::AliasTyKind<'tcx> {
191-
match self.def_kind(def_id) {
192-
DefKind::AssocTy
193-
if let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(def_id)) =>
194-
{
195-
ty::Inherent { def_id }
196-
}
197-
DefKind::AssocTy => ty::Projection { def_id },
198-
199-
DefKind::OpaqueTy => ty::Opaque { def_id },
200-
DefKind::TyAlias => ty::Free { def_id },
201-
kind => bug!("unexpected DefKind in AliasTy: {kind:?}"),
202-
}
203-
}
204-
205190
fn alias_term_kind(self, alias: ty::AliasTerm<'tcx>) -> ty::AliasTermKind {
206191
match self.def_kind(alias.def_id) {
207192
DefKind::AssocTy => {

compiler/rustc_type_ir/src/interner.rs

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

215-
fn alias_ty_kind_from_def_id(self, def_id: Self::DefId) -> ty::AliasTyKind<Self>;
216-
217215
fn alias_term_kind(self, alias: ty::AliasTerm<Self>) -> ty::AliasTermKind;
218216

219217
fn trait_ref_and_own_args_for_alias(

compiler/rustc_type_ir/src/ty_kind.rs

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

6565
impl<I: Interner> AliasTyKind<I> {
66-
pub fn new_from_def_id(interner: I, def_id: I::DefId) -> Self {
67-
interner.alias_ty_kind_from_def_id(def_id)
68-
}
69-
7066
pub fn descr(self) -> &'static str {
7167
match self {
7268
AliasTyKind::Projection { .. } => "associated type",

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ 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,
23-
GenericArgKind, GenericArgsRef, IntTy, Region, RegionKind, TraitRef, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable,
24-
TypeVisitableExt, TypeVisitor, UintTy, Upcast, VariantDef, VariantDiscr,
22+
self, AdtDef, AliasTy, AssocContainer, AssocItem, AssocTag, Binder, BoundRegion, BoundVarIndexKind, FnSig,
23+
GenericArg, GenericArgKind, GenericArgsRef, IntTy, Region, RegionKind, TraitRef, Ty, TyCtxt,
24+
TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, UintTy, Upcast, VariantDef, VariantDiscr,
2525
};
2626
use rustc_span::symbol::Ident;
2727
use rustc_span::{DUMMY_SP, Span, Symbol};
@@ -1023,11 +1023,13 @@ pub fn make_projection<'tcx>(
10231023
#[cfg(debug_assertions)]
10241024
assert_generic_args_match(tcx, assoc_item.def_id, args);
10251025

1026-
Some(AliasTy::new_from_args(
1027-
tcx,
1028-
ty::AliasTyKind::new_from_def_id(tcx, assoc_item.def_id),
1029-
args,
1030-
))
1026+
let alias_kind = match assoc_item.container {
1027+
AssocContainer::Trait | AssocContainer::TraitImpl(_) => {
1028+
ty::Projection { def_id: assoc_item.def_id }
1029+
}
1030+
AssocContainer::InherentImpl => ty::Inherent { def_id: assoc_item.def_id },
1031+
};
1032+
Some(AliasTy::new_from_args(tcx, alias_kind, args))
10311033
}
10321034
helper(
10331035
tcx,

0 commit comments

Comments
 (0)