Skip to content
/ rust Public
forked from rust-lang/rust

Commit f24120d

Browse files
authored
Rollup merge of rust-lang#155323 - josetorrs:remove-new-from-def-id, r=fmease
refactor `TypeRelativePath::AssocItem` to use `AliasTerm` r? @WaffleLapkin related issue: rust-lang#154941 tackling the task: - [ ] remove calls to `new_from_def_id` and `alias_ty_kind_from_def_id` and replace them with calls to e.g. `new_projection` directly also doing: - `probe_inherent_assoc_item` returns `AliasTerm<'tcx>`
2 parents feef043 + 24c895a commit f24120d

5 files changed

Lines changed: 30 additions & 50 deletions

File tree

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ pub enum PermitVariants {
297297

298298
#[derive(Debug, Clone, Copy)]
299299
enum TypeRelativePath<'tcx> {
300-
AssocItem(DefId, GenericArgsRef<'tcx>),
300+
AssocItem(ty::AliasTerm<'tcx>),
301301
Variant { adt: Ty<'tcx>, variant_did: DefId },
302302
Ctor { ctor_def_id: DefId, args: GenericArgsRef<'tcx> },
303303
}
@@ -1356,15 +1356,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
13561356
span,
13571357
LowerTypeRelativePathMode::Type(permit_variants),
13581358
)? {
1359-
TypeRelativePath::AssocItem(def_id, args) => {
1360-
let alias_ty = ty::AliasTy::new_from_args(
1361-
tcx,
1362-
ty::AliasTyKind::new_from_def_id(tcx, def_id),
1363-
args,
1364-
);
1365-
let ty = Ty::new_alias(tcx, alias_ty);
1359+
TypeRelativePath::AssocItem(alias_term) => {
1360+
let ty = alias_term.expect_ty().to_ty(tcx);
13661361
let ty = self.check_param_uses_if_mcg(ty, span, false);
1367-
Ok((ty, tcx.def_kind(def_id), def_id))
1362+
Ok((ty, tcx.def_kind(alias_term.def_id()), alias_term.def_id()))
13681363
}
13691364
TypeRelativePath::Variant { adt, variant_did } => {
13701365
let adt = self.check_param_uses_if_mcg(adt, span, false);
@@ -1396,16 +1391,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
13961391
span,
13971392
LowerTypeRelativePathMode::Const,
13981393
)? {
1399-
TypeRelativePath::AssocItem(def_id, args) => {
1400-
self.require_type_const_attribute(def_id, span)?;
1401-
let ct = Const::new_unevaluated(
1402-
tcx,
1403-
ty::UnevaluatedConst::new(
1404-
tcx,
1405-
ty::UnevaluatedConstKind::new_from_def_id(tcx, def_id),
1406-
args,
1407-
),
1408-
);
1394+
TypeRelativePath::AssocItem(alias_term) => {
1395+
self.require_type_const_attribute(alias_term.def_id(), span)?;
1396+
let ct = Const::new_unevaluated(tcx, alias_term.expect_ct());
14091397
let ct = self.check_param_uses_if_mcg(ct, span, false);
14101398
Ok(ct)
14111399
}
@@ -1487,15 +1475,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
14871475
}
14881476

14891477
// FIXME(inherent_associated_types, #106719): Support self types other than ADTs.
1490-
if let Some((did, args)) = self.probe_inherent_assoc_item(
1478+
if let Some(alias_term) = self.probe_inherent_assoc_item(
14911479
segment,
14921480
adt_def.did(),
14931481
self_ty,
14941482
qpath_hir_id,
14951483
span,
14961484
mode.assoc_tag(),
14971485
)? {
1498-
return Ok(TypeRelativePath::AssocItem(did, args));
1486+
return Ok(TypeRelativePath::AssocItem(alias_term));
14991487
}
15001488
}
15011489

@@ -1529,7 +1517,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
15291517
);
15301518
}
15311519

1532-
Ok(TypeRelativePath::AssocItem(item_def_id, args))
1520+
Ok(TypeRelativePath::AssocItem(ty::AliasTerm::new_from_def_id(tcx, item_def_id, args)))
15331521
}
15341522

15351523
/// Resolve a [type-relative](hir::QPath::TypeRelative) (and type-level) path.
@@ -1609,7 +1597,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
16091597
block: HirId,
16101598
span: Span,
16111599
assoc_tag: ty::AssocTag,
1612-
) -> Result<Option<(DefId, GenericArgsRef<'tcx>)>, ErrorGuaranteed> {
1600+
) -> Result<Option<ty::AliasTerm<'tcx>>, ErrorGuaranteed> {
16131601
let tcx = self.tcx();
16141602

16151603
if !tcx.features().inherent_associated_types() {
@@ -1692,7 +1680,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
16921680
.chain(args.into_iter().skip(parent_args.len())),
16931681
);
16941682

1695-
Ok(Some((assoc_item, args)))
1683+
let kind = match assoc_tag {
1684+
ty::AssocTag::Type => ty::AliasTermKind::InherentTy { def_id: assoc_item },
1685+
ty::AssocTag::Const => {
1686+
// FIXME(mgca): drop once `InherentConst` accepts IAC-shaped args (issue #156181)
1687+
// without this, `new_from_args` errors (#155341).
1688+
self.require_type_const_attribute(assoc_item, span)?;
1689+
ty::AliasTermKind::InherentConst { def_id: assoc_item }
1690+
}
1691+
ty::AssocTag::Fn => unreachable!(),
1692+
};
1693+
1694+
Ok(Some(ty::AliasTerm::new_from_args(tcx, kind, args)))
16961695
}
16971696

16981697
/// Given name and kind search for the assoc item in the provided scope and check if it's accessible[^1].

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

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

211-
fn alias_ty_kind_from_def_id(self, def_id: DefId) -> ty::AliasTyKind<'tcx> {
212-
match self.def_kind(def_id) {
213-
DefKind::AssocTy
214-
if let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(def_id)) =>
215-
{
216-
ty::Inherent { def_id }
217-
}
218-
DefKind::AssocTy => ty::Projection { def_id },
219-
220-
DefKind::OpaqueTy => ty::Opaque { def_id },
221-
DefKind::TyAlias => ty::Free { def_id },
222-
kind => bug!("unexpected DefKind in AliasTy: {kind:?}"),
223-
}
224-
}
225-
226211
fn unevaluated_const_kind_from_def_id(
227212
self,
228213
def_id: Self::DefId,

compiler/rustc_type_ir/src/interner.rs

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

247-
fn alias_ty_kind_from_def_id(self, def_id: Self::DefId) -> ty::AliasTyKind<Self>;
248-
249247
fn unevaluated_const_kind_from_def_id(
250248
self,
251249
def_id: Self::DefId,

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: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,11 +1050,13 @@ pub fn make_projection<'tcx>(
10501050
#[cfg(debug_assertions)]
10511051
assert_generic_args_match(tcx, assoc_item.def_id, args);
10521052

1053-
Some(AliasTy::new_from_args(
1054-
tcx,
1055-
ty::AliasTyKind::new_from_def_id(tcx, assoc_item.def_id),
1056-
args,
1057-
))
1053+
let kind = if let DefKind::Impl { of_trait: false } = tcx.def_kind(tcx.parent(assoc_item.def_id)) {
1054+
ty::AliasTyKind::Inherent { def_id: assoc_item.def_id }
1055+
} else {
1056+
ty::AliasTyKind::Projection { def_id: assoc_item.def_id }
1057+
};
1058+
1059+
Some(AliasTy::new_from_args(tcx, kind, args))
10581060
}
10591061
helper(
10601062
tcx,

0 commit comments

Comments
 (0)