Skip to content

Commit 81000a1

Browse files
Use special DefIds for aliases
Like we do for other things for better experience in rust-analyzer. It's possible now that the `AliasTyKind` and `AliasTermKind` contains the DefId. It does require a few `try_into().unwrap()`s since in the solver's `consider_X_candidate()` only get an untyped `DefId`. It's possible to reduce that considerably if we'd pass them the typed def id as a parameter, but I don't know what will be the impact on perf.
1 parent a021a77 commit 81000a1

14 files changed

Lines changed: 212 additions & 147 deletions

File tree

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_hir::def::{CtorKind, CtorOf, DefKind};
88
use rustc_hir::def_id::{DefId, LocalDefId};
99
use rustc_hir::lang_items::LangItem;
1010
use rustc_span::{DUMMY_SP, Span, Symbol};
11-
use rustc_type_ir::lang_items::{SolverAdtLangItem, SolverLangItem, SolverTraitLangItem};
11+
use rustc_type_ir::lang_items::{SolverAdtLangItem, SolverProjectionLangItem, SolverTraitLangItem};
1212
use rustc_type_ir::{CollectAndApply, Interner, TypeFoldable, Unnormalized, search_graph};
1313

1414
use crate::dep_graph::{DepKind, DepNodeIndex};
@@ -39,6 +39,15 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
3939
type AdtId = DefId;
4040
type ImplId = DefId;
4141
type UnevaluatedConstId = DefId;
42+
type DefinitionAssocTyId = DefId;
43+
type DefinitionAssocConstId = DefId;
44+
type DefinitionAssocTermId = DefId;
45+
type OpaqueTyId = DefId;
46+
type FreeTyAliasId = DefId;
47+
type FreeConstAliasId = DefId;
48+
type ImplAssocTyId = DefId;
49+
type ImplAssocConstId = DefId;
50+
type ImplAssocTermId = DefId;
4251
type Span = Span;
4352

4453
type GenericArgs = ty::GenericArgsRef<'tcx>;
@@ -285,7 +294,11 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
285294
self.mk_type_list_from_iter(args)
286295
}
287296

288-
fn parent(self, def_id: DefId) -> DefId {
297+
fn projection_parent(self, def_id: Self::DefinitionAssocTermId) -> Self::TraitId {
298+
self.parent(def_id)
299+
}
300+
301+
fn impl_assoc_term_parent(self, def_id: Self::ImplAssocTyId) -> Self::ImplId {
289302
self.parent(def_id)
290303
}
291304

@@ -443,7 +456,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
443456
!self.codegen_fn_attrs(def_id).target_features.is_empty()
444457
}
445458

446-
fn require_lang_item(self, lang_item: SolverLangItem) -> DefId {
459+
fn require_projection_lang_item(self, lang_item: SolverProjectionLangItem) -> DefId {
447460
self.require_lang_item(solver_lang_item_to_lang_item(lang_item), DUMMY_SP)
448461
}
449462

@@ -455,7 +468,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
455468
self.require_lang_item(solver_adt_lang_item_to_lang_item(lang_item), DUMMY_SP)
456469
}
457470

458-
fn is_lang_item(self, def_id: DefId, lang_item: SolverLangItem) -> bool {
471+
fn is_projection_lang_item(self, def_id: DefId, lang_item: SolverProjectionLangItem) -> bool {
459472
self.is_lang_item(def_id, solver_lang_item_to_lang_item(lang_item))
460473
}
461474

@@ -475,7 +488,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
475488
self.is_sizedness_trait(def_id)
476489
}
477490

478-
fn as_lang_item(self, def_id: DefId) -> Option<SolverLangItem> {
491+
fn as_projection_lang_item(self, def_id: DefId) -> Option<SolverProjectionLangItem> {
479492
lang_item_to_solver_lang_item(self.lang_items().from_def_id(def_id)?)
480493
}
481494

@@ -754,7 +767,7 @@ macro_rules! bidirectional_lang_item_map {
754767
}
755768

756769
bidirectional_lang_item_map! {
757-
SolverLangItem, fn lang_item_to_solver_lang_item, fn solver_lang_item_to_lang_item;
770+
SolverProjectionLangItem, fn lang_item_to_solver_lang_item, fn solver_lang_item_to_lang_item;
758771

759772
// tidy-alphabetical-start
760773
AsyncFnKindUpvars,
@@ -788,7 +801,6 @@ bidirectional_lang_item_map! {
788801
AsyncFnKindHelper,
789802
AsyncFnMut,
790803
AsyncFnOnce,
791-
AsyncFnOnceOutput,
792804
AsyncIterator,
793805
BikeshedGuaranteedNoDrop,
794806
Clone,

compiler/rustc_next_trait_solver/src/delegate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ pub trait SolverDelegate: Deref<Target = Self::Infcx> + Sized {
7575
fn fetch_eligible_assoc_item(
7676
&self,
7777
goal_trait_ref: ty::TraitRef<Self::Interner>,
78-
trait_assoc_def_id: <Self::Interner as Interner>::DefId,
78+
trait_assoc_def_id: <Self::Interner as Interner>::DefinitionAssocTermId,
7979
impl_def_id: <Self::Interner as Interner>::ImplId,
8080
) -> Result<
81-
Option<<Self::Interner as Interner>::DefId>,
81+
Option<<Self::Interner as Interner>::ImplAssocTermId>,
8282
<Self::Interner as Interner>::ErrorGuaranteed,
8383
>;
8484

compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use derive_where::derive_where;
55
use rustc_type_ir::data_structures::HashMap;
66
use rustc_type_ir::inherent::*;
7-
use rustc_type_ir::lang_items::{SolverLangItem, SolverTraitLangItem};
7+
use rustc_type_ir::lang_items::{SolverProjectionLangItem, SolverTraitLangItem};
88
use rustc_type_ir::solve::SizedTraitKind;
99
use rustc_type_ir::solve::inspect::ProbeKind;
1010
use rustc_type_ir::{
@@ -106,7 +106,9 @@ where
106106
// We can resolve the `impl Trait` to its concrete type,
107107
// which enforces a DAG between the functions requiring
108108
// the auto trait bounds in question.
109-
Ok(ty::Binder::dummy(vec![cx.type_of(def_id).instantiate(cx, args).skip_norm_wip()]))
109+
Ok(ty::Binder::dummy(vec![
110+
cx.type_of(def_id.into()).instantiate(cx, args).skip_norm_wip(),
111+
]))
110112
}
111113
}
112114
}
@@ -541,7 +543,8 @@ pub(in crate::solve) fn extract_tupled_inputs_and_output_from_async_callable<I:
541543
);
542544
}
543545

544-
let future_output_def_id = cx.require_lang_item(SolverLangItem::FutureOutput);
546+
let future_output_def_id =
547+
cx.require_projection_lang_item(SolverProjectionLangItem::FutureOutput);
545548
let future_output_ty = Ty::new_projection(cx, future_output_def_id, [sig.output()]);
546549
Ok((
547550
bound_sig.rebind(AsyncCallableRelevantTypes {
@@ -596,7 +599,8 @@ fn fn_item_to_async_callable<I: Interner>(
596599
let nested = vec![
597600
bound_sig.rebind(ty::TraitRef::new(cx, future_trait_def_id, [sig.output()])).upcast(cx),
598601
];
599-
let future_output_def_id = cx.require_lang_item(SolverLangItem::FutureOutput);
602+
let future_output_def_id =
603+
cx.require_projection_lang_item(SolverProjectionLangItem::FutureOutput);
600604
let future_output_ty = Ty::new_projection(cx, future_output_def_id, [sig.output()]);
601605
Ok((
602606
bound_sig.rebind(AsyncCallableRelevantTypes {
@@ -642,7 +646,8 @@ fn coroutine_closure_to_ambiguous_coroutine<I: Interner>(
642646
args: ty::CoroutineClosureArgs<I>,
643647
sig: ty::CoroutineClosureSignature<I>,
644648
) -> I::Ty {
645-
let upvars_projection_def_id = cx.require_lang_item(SolverLangItem::AsyncFnKindUpvars);
649+
let upvars_projection_def_id =
650+
cx.require_projection_lang_item(SolverProjectionLangItem::AsyncFnKindUpvars);
646651
let tupled_upvars_ty = Ty::new_projection(
647652
cx,
648653
upvars_projection_def_id,
@@ -920,7 +925,10 @@ where
920925
// show up in the bounds, but just ones that come from substituting
921926
// `Self` with the dyn type.
922927
let proj = proj.with_self_ty(cx, trait_ref.self_ty());
923-
replace_projection_with.entry(proj.def_id()).or_default().push(bound.rebind(proj));
928+
replace_projection_with
929+
.entry(proj.def_id().into())
930+
.or_default()
931+
.push(bound.rebind(proj));
924932
}
925933
}
926934

compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,9 +1128,9 @@ where
11281128
pub(super) fn fetch_eligible_assoc_item(
11291129
&self,
11301130
goal_trait_ref: ty::TraitRef<I>,
1131-
trait_assoc_def_id: I::DefId,
1131+
trait_assoc_def_id: I::DefinitionAssocTermId,
11321132
impl_def_id: I::ImplId,
1133-
) -> Result<Option<I::DefId>, I::ErrorGuaranteed> {
1133+
) -> Result<Option<I::ImplAssocTermId>, I::ErrorGuaranteed> {
11341134
self.delegate.fetch_eligible_assoc_item(goal_trait_ref, trait_assoc_def_id, impl_def_id)
11351135
}
11361136

compiler/rustc_next_trait_solver/src/solve/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ where
357357
}
358358
}
359359

360-
fn opaque_type_is_rigid(&self, def_id: I::DefId) -> bool {
360+
fn opaque_type_is_rigid(&self, def_id: I::OpaqueTyId) -> bool {
361361
match self.typing_mode() {
362362
// Opaques are never rigid outside of analysis mode.
363363
TypingMode::Coherence | TypingMode::PostAnalysis => false,

compiler/rustc_next_trait_solver/src/solve/normalizes_to/inherent.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ where
2222
let cx = self.cx();
2323
let inherent = goal.predicate.alias;
2424

25-
let impl_def_id = cx.parent(inherent.def_id());
26-
let impl_args = self.fresh_args_for_item(impl_def_id);
25+
let impl_def_id = cx.impl_assoc_term_parent(inherent.def_id().try_into().unwrap());
26+
let impl_args = self.fresh_args_for_item(impl_def_id.into());
2727

2828
// Equate impl header and add impl where clauses
2929
self.eq(
3030
goal.param_env,
3131
inherent.self_ty(),
32-
cx.type_of(impl_def_id).instantiate(cx, impl_args).skip_norm_wip(),
32+
cx.type_of(impl_def_id.into()).instantiate(cx, impl_args).skip_norm_wip(),
3333
)?;
3434

3535
// Equate IAT with the RHS of the project goal

compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs

Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod opaque_types;
55

66
use rustc_type_ir::fast_reject::DeepRejectCtxt;
77
use rustc_type_ir::inherent::*;
8-
use rustc_type_ir::lang_items::{SolverAdtLangItem, SolverLangItem, SolverTraitLangItem};
8+
use rustc_type_ir::lang_items::{SolverAdtLangItem, SolverProjectionLangItem, SolverTraitLangItem};
99
use rustc_type_ir::{
1010
self as ty, FieldInfo, Interner, NormalizesTo, PredicateKind, Unnormalized, Upcast as _,
1111
};
@@ -277,7 +277,7 @@ where
277277

278278
let target_item_def_id = match ecx.fetch_eligible_assoc_item(
279279
goal_trait_ref,
280-
goal.predicate.def_id(),
280+
goal.predicate.def_id().try_into().unwrap(),
281281
impl_def_id,
282282
) {
283283
Ok(Some(target_item_def_id)) => target_item_def_id,
@@ -354,7 +354,7 @@ where
354354
}
355355
}
356356

357-
let target_container_def_id = cx.parent(target_item_def_id);
357+
let target_container_def_id = cx.impl_assoc_term_parent(target_item_def_id);
358358

359359
// Getting the right args here is complex, e.g. given:
360360
// - a goal `<Vec<u32> as Trait<i32>>::Assoc<u64>`
@@ -371,10 +371,10 @@ where
371371
impl_def_id,
372372
impl_args,
373373
impl_trait_ref,
374-
target_container_def_id,
374+
target_container_def_id.into(),
375375
)?;
376376

377-
if !cx.check_args_compatible(target_item_def_id, target_args) {
377+
if !cx.check_args_compatible(target_item_def_id.into(), target_args) {
378378
return error_response(
379379
ecx,
380380
cx.delay_bug("associated item has mismatched arguments"),
@@ -384,10 +384,10 @@ where
384384
// Finally we construct the actual value of the associated type.
385385
let term = match goal.predicate.alias.kind(cx) {
386386
ty::AliasTermKind::ProjectionTy { .. } => {
387-
cx.type_of(target_item_def_id).map_bound(|ty| ty.into())
387+
cx.type_of(target_item_def_id.into()).map_bound(|ty| ty.into())
388388
}
389389
ty::AliasTermKind::ProjectionConst { .. } => {
390-
cx.const_of_item(target_item_def_id).map_bound(|ct| ct.into())
390+
cx.const_of_item(target_item_def_id.into()).map_bound(|ct| ct.into())
391391
}
392392
kind => panic!("expected projection, found {kind:?}"),
393393
};
@@ -493,6 +493,7 @@ where
493493
goal_kind: ty::ClosureKind,
494494
) -> Result<Candidate<I>, NoSolution> {
495495
let cx = ecx.cx();
496+
let def_id = goal.predicate.def_id().try_into().unwrap();
496497

497498
let env_region = match goal_kind {
498499
ty::ClosureKind::Fn | ty::ClosureKind::FnMut => goal.predicate.alias.args.region_at(2),
@@ -520,41 +521,42 @@ where
520521
[output_coroutine_ty],
521522
);
522523

523-
let (projection_term, term) =
524-
if cx.is_lang_item(goal.predicate.def_id(), SolverLangItem::CallOnceFuture) {
525-
(
526-
ty::AliasTerm::new(
527-
cx,
528-
cx.alias_term_kind_from_def_id(goal.predicate.def_id()),
529-
[goal.predicate.self_ty(), tupled_inputs_ty],
530-
),
531-
output_coroutine_ty.into(),
532-
)
533-
} else if cx.is_lang_item(goal.predicate.def_id(), SolverLangItem::CallRefFuture) {
534-
(
535-
ty::AliasTerm::new(
536-
cx,
537-
cx.alias_term_kind_from_def_id(goal.predicate.def_id()),
538-
[
539-
I::GenericArg::from(goal.predicate.self_ty()),
540-
tupled_inputs_ty.into(),
541-
env_region.into(),
542-
],
543-
),
544-
output_coroutine_ty.into(),
545-
)
546-
} else if cx.is_lang_item(goal.predicate.def_id(), SolverLangItem::AsyncFnOnceOutput) {
547-
(
548-
ty::AliasTerm::new(
549-
cx,
550-
cx.alias_term_kind_from_def_id(goal.predicate.def_id()),
551-
[goal.predicate.self_ty(), tupled_inputs_ty],
552-
),
553-
coroutine_return_ty.into(),
554-
)
555-
} else {
556-
panic!("no such associated type in `AsyncFn*`: {:?}", goal.predicate.def_id())
557-
};
524+
let (projection_term, term) = if cx
525+
.is_projection_lang_item(def_id, SolverProjectionLangItem::CallOnceFuture)
526+
{
527+
(
528+
ty::AliasTerm::new(
529+
cx,
530+
cx.alias_term_kind_from_def_id(goal.predicate.def_id()),
531+
[goal.predicate.self_ty(), tupled_inputs_ty],
532+
),
533+
output_coroutine_ty.into(),
534+
)
535+
} else if cx.is_projection_lang_item(def_id, SolverProjectionLangItem::CallRefFuture) {
536+
(
537+
ty::AliasTerm::new(
538+
cx,
539+
cx.alias_term_kind_from_def_id(goal.predicate.def_id()),
540+
[
541+
I::GenericArg::from(goal.predicate.self_ty()),
542+
tupled_inputs_ty.into(),
543+
env_region.into(),
544+
],
545+
),
546+
output_coroutine_ty.into(),
547+
)
548+
} else if cx.is_projection_lang_item(def_id, SolverProjectionLangItem::AsyncFnOnceOutput) {
549+
(
550+
ty::AliasTerm::new(
551+
cx,
552+
cx.alias_term_kind_from_def_id(goal.predicate.def_id()),
553+
[goal.predicate.self_ty(), tupled_inputs_ty],
554+
),
555+
coroutine_return_ty.into(),
556+
)
557+
} else {
558+
panic!("no such associated type in `AsyncFn*`: {:?}", goal.predicate.def_id())
559+
};
558560
let pred = ty::ProjectionPredicate { projection_term, term }.upcast(cx);
559561

560562
Self::probe_and_consider_implied_clause(
@@ -628,8 +630,8 @@ where
628630
goal: Goal<I, Self>,
629631
) -> Result<Candidate<I>, NoSolution> {
630632
let cx = ecx.cx();
631-
let metadata_def_id = cx.require_lang_item(SolverLangItem::Metadata);
632-
assert_eq!(metadata_def_id, goal.predicate.def_id());
633+
let metadata_def_id = cx.require_projection_lang_item(SolverProjectionLangItem::Metadata);
634+
assert_eq!(Into::<I::DefId>::into(metadata_def_id), goal.predicate.def_id());
633635
let metadata_ty = match goal.predicate.self_ty().kind() {
634636
ty::Bool
635637
| ty::Char
@@ -655,8 +657,9 @@ where
655657
ty::Str | ty::Slice(_) => Ty::new_usize(cx),
656658

657659
ty::Dynamic(_, _) => {
658-
let dyn_metadata = cx.require_lang_item(SolverLangItem::DynMetadata);
659-
cx.type_of(dyn_metadata)
660+
let dyn_metadata =
661+
cx.require_projection_lang_item(SolverProjectionLangItem::DynMetadata);
662+
cx.type_of(dyn_metadata.into())
660663
.instantiate(cx, &[I::GenericArg::from(goal.predicate.self_ty())])
661664
.skip_norm_wip()
662665
}
@@ -854,10 +857,12 @@ where
854857
}
855858

856859
let coroutine = args.as_coroutine();
860+
let def_id = goal.predicate.def_id().try_into().unwrap();
857861

858-
let term = if cx.is_lang_item(goal.predicate.def_id(), SolverLangItem::CoroutineReturn) {
862+
let term = if cx.is_projection_lang_item(def_id, SolverProjectionLangItem::CoroutineReturn)
863+
{
859864
coroutine.return_ty().into()
860-
} else if cx.is_lang_item(goal.predicate.def_id(), SolverLangItem::CoroutineYield) {
865+
} else if cx.is_projection_lang_item(def_id, SolverProjectionLangItem::CoroutineYield) {
861866
coroutine.yield_ty().into()
862867
} else {
863868
panic!("unexpected associated item `{:?}` for `{self_ty:?}`", goal.predicate.def_id())
@@ -981,9 +986,10 @@ where
981986
else {
982987
return Err(NoSolution);
983988
};
984-
let ty = match ecx.cx().as_lang_item(goal.predicate.def_id()) {
985-
Some(SolverLangItem::FieldBase) => base,
986-
Some(SolverLangItem::FieldType) => ty,
989+
let ty = match ecx.cx().as_projection_lang_item(goal.predicate.def_id().try_into().unwrap())
990+
{
991+
Some(SolverProjectionLangItem::FieldBase) => base,
992+
Some(SolverProjectionLangItem::FieldType) => ty,
987993
_ => panic!("unexpected associated type {:?} in `Field`", goal.predicate),
988994
};
989995
ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {

compiler/rustc_next_trait_solver/src/solve/trait_goals.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ where
238238
goal.predicate.self_ty().kind()
239239
{
240240
debug_assert!(ecx.opaque_type_is_rigid(def_id));
241-
for item_bound in cx.item_self_bounds(def_id).skip_binder() {
241+
for item_bound in cx.item_self_bounds(def_id.into()).skip_binder() {
242242
if item_bound
243243
.as_trait_clause()
244244
.is_some_and(|b| b.def_id() == goal.predicate.def_id())

0 commit comments

Comments
 (0)