Skip to content

Commit 1e46fa9

Browse files
authored
Rollup merge of #146111 - ChayimFriedman2:more-ns-specific-defid, r=lcnr
Migrate more things in the new solver to specific `DefId`s Continuation of #145377. I migrated the rest of the types, except aliases. Aliases are problematic because opaques and associated types share the same type in the new solver. `@jackh726,` `@lcnr,` `@ShoyuVanilla` I'd like to hear ideas here. Anyway, even if we do nothing with them we already got a substantial improvement. r? types
2 parents beeb8e3 + 6f0ebdb commit 1e46fa9

File tree

17 files changed

+154
-80
lines changed

17 files changed

+154
-80
lines changed

compiler/rustc_middle/src/ty/context.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use rustc_session::{Limit, Session};
5252
use rustc_span::def_id::{CRATE_DEF_ID, DefPathHash, StableCrateId};
5353
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
5454
use rustc_type_ir::TyKind::*;
55-
use rustc_type_ir::lang_items::{SolverLangItem, SolverTraitLangItem};
55+
use rustc_type_ir::lang_items::{SolverAdtLangItem, SolverLangItem, SolverTraitLangItem};
5656
pub use rustc_type_ir::lift::Lift;
5757
use rustc_type_ir::{
5858
CollectAndApply, Interner, TypeFlags, TypeFoldable, WithCachedTypeInfo, elaborate, search_graph,
@@ -94,6 +94,13 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
9494
type DefId = DefId;
9595
type LocalDefId = LocalDefId;
9696
type TraitId = DefId;
97+
type ForeignId = DefId;
98+
type FunctionId = DefId;
99+
type ClosureId = DefId;
100+
type CoroutineClosureId = DefId;
101+
type CoroutineId = DefId;
102+
type AdtId = DefId;
103+
type ImplId = DefId;
97104
type Span = Span;
98105

99106
type GenericArgs = ty::GenericArgsRef<'tcx>;
@@ -492,6 +499,10 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
492499
self.require_lang_item(solver_trait_lang_item_to_lang_item(lang_item), DUMMY_SP)
493500
}
494501

502+
fn require_adt_lang_item(self, lang_item: SolverAdtLangItem) -> DefId {
503+
self.require_lang_item(solver_adt_lang_item_to_lang_item(lang_item), DUMMY_SP)
504+
}
505+
495506
fn is_lang_item(self, def_id: DefId, lang_item: SolverLangItem) -> bool {
496507
self.is_lang_item(def_id, solver_lang_item_to_lang_item(lang_item))
497508
}
@@ -500,6 +511,10 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
500511
self.is_lang_item(def_id, solver_trait_lang_item_to_lang_item(lang_item))
501512
}
502513

514+
fn is_adt_lang_item(self, def_id: DefId, lang_item: SolverAdtLangItem) -> bool {
515+
self.is_lang_item(def_id, solver_adt_lang_item_to_lang_item(lang_item))
516+
}
517+
503518
fn is_default_trait(self, def_id: DefId) -> bool {
504519
self.is_default_trait(def_id)
505520
}
@@ -512,6 +527,10 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
512527
lang_item_to_solver_trait_lang_item(self.lang_items().from_def_id(def_id)?)
513528
}
514529

530+
fn as_adt_lang_item(self, def_id: DefId) -> Option<SolverAdtLangItem> {
531+
lang_item_to_solver_adt_lang_item(self.lang_items().from_def_id(def_id)?)
532+
}
533+
515534
fn associated_type_def_ids(self, def_id: DefId) -> impl IntoIterator<Item = DefId> {
516535
self.associated_items(def_id)
517536
.in_definition_order()
@@ -783,6 +802,13 @@ bidirectional_lang_item_map! {
783802
DynMetadata,
784803
FutureOutput,
785804
Metadata,
805+
// tidy-alphabetical-end
806+
}
807+
808+
bidirectional_lang_item_map! {
809+
SolverAdtLangItem, lang_item_to_solver_adt_lang_item, solver_adt_lang_item_to_lang_item;
810+
811+
// tidy-alphabetical-start
786812
Option,
787813
Poll,
788814
// tidy-alphabetical-end

compiler/rustc_next_trait_solver/src/coherence.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,21 @@ where
435435
}
436436
}
437437
ty::Error(_) => ControlFlow::Break(OrphanCheckEarlyExit::LocalTy(ty)),
438-
ty::Closure(did, ..) | ty::CoroutineClosure(did, ..) | ty::Coroutine(did, ..) => {
438+
ty::Closure(did, ..) => {
439+
if self.def_id_is_local(did) {
440+
ControlFlow::Break(OrphanCheckEarlyExit::LocalTy(ty))
441+
} else {
442+
self.found_non_local_ty(ty)
443+
}
444+
}
445+
ty::CoroutineClosure(did, ..) => {
446+
if self.def_id_is_local(did) {
447+
ControlFlow::Break(OrphanCheckEarlyExit::LocalTy(ty))
448+
} else {
449+
self.found_non_local_ty(ty)
450+
}
451+
}
452+
ty::Coroutine(did, ..) => {
439453
if self.def_id_is_local(did) {
440454
ControlFlow::Break(OrphanCheckEarlyExit::LocalTy(ty))
441455
} else {

compiler/rustc_next_trait_solver/src/delegate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub trait SolverDelegate: Deref<Target = Self::Infcx> + Sized {
7676
&self,
7777
goal_trait_ref: ty::TraitRef<Self::Interner>,
7878
trait_assoc_def_id: <Self::Interner as Interner>::DefId,
79-
impl_def_id: <Self::Interner as Interner>::DefId,
79+
impl_def_id: <Self::Interner as Interner>::ImplId,
8080
) -> Result<
8181
Option<<Self::Interner as Interner>::DefId>,
8282
<Self::Interner as Interner>::ErrorGuaranteed,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ where
186186
fn consider_impl_candidate(
187187
ecx: &mut EvalCtxt<'_, D>,
188188
goal: Goal<I, Self>,
189-
impl_def_id: I::DefId,
189+
impl_def_id: I::ImplId,
190190
) -> Result<Candidate<I>, NoSolution>;
191191

192192
/// If the predicate contained an error, we want to avoid emitting unnecessary trait

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ fn coroutine_closure_to_certain_coroutine<I: Interner>(
605605
cx: I,
606606
goal_kind: ty::ClosureKind,
607607
goal_region: I::Region,
608-
def_id: I::DefId,
608+
def_id: I::CoroutineClosureId,
609609
args: ty::CoroutineClosureArgs<I>,
610610
sig: ty::CoroutineClosureSignature<I>,
611611
) -> I::Ty {
@@ -629,7 +629,7 @@ fn coroutine_closure_to_ambiguous_coroutine<I: Interner>(
629629
cx: I,
630630
goal_kind: ty::ClosureKind,
631631
goal_region: I::Region,
632-
def_id: I::DefId,
632+
def_id: I::CoroutineClosureId,
633633
args: ty::CoroutineClosureArgs<I>,
634634
sig: ty::CoroutineClosureSignature<I>,
635635
) -> I::Ty {
@@ -664,7 +664,7 @@ fn coroutine_closure_to_ambiguous_coroutine<I: Interner>(
664664
pub(in crate::solve) fn extract_fn_def_from_const_callable<I: Interner>(
665665
cx: I,
666666
self_ty: I::Ty,
667-
) -> Result<(ty::Binder<I, (I::FnInputTys, I::Ty)>, I::DefId, I::GenericArgs), NoSolution> {
667+
) -> Result<(ty::Binder<I, (I::FnInputTys, I::Ty)>, I::FunctionId, I::GenericArgs), NoSolution> {
668668
match self_ty.kind() {
669669
ty::FnDef(def_id, args) => {
670670
let sig = cx.fn_sig(def_id);

compiler/rustc_next_trait_solver/src/solve/effect_goals.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ where
123123
fn consider_impl_candidate(
124124
ecx: &mut EvalCtxt<'_, D>,
125125
goal: Goal<I, Self>,
126-
impl_def_id: I::DefId,
126+
impl_def_id: I::ImplId,
127127
) -> Result<Candidate<I>, NoSolution> {
128128
let cx = ecx.cx();
129129

@@ -152,20 +152,20 @@ where
152152
}
153153

154154
ecx.probe_trait_candidate(CandidateSource::Impl(impl_def_id)).enter(|ecx| {
155-
let impl_args = ecx.fresh_args_for_item(impl_def_id);
155+
let impl_args = ecx.fresh_args_for_item(impl_def_id.into());
156156
ecx.record_impl_args(impl_args);
157157
let impl_trait_ref = impl_trait_ref.instantiate(cx, impl_args);
158158

159159
ecx.eq(goal.param_env, goal.predicate.trait_ref, impl_trait_ref)?;
160160
let where_clause_bounds = cx
161-
.predicates_of(impl_def_id)
161+
.predicates_of(impl_def_id.into())
162162
.iter_instantiated(cx, impl_args)
163163
.map(|pred| goal.with(cx, pred));
164164
ecx.add_goals(GoalSource::ImplWhereBound, where_clause_bounds);
165165

166166
// For this impl to be `const`, we need to check its `[const]` bounds too.
167167
let const_conditions = cx
168-
.const_conditions(impl_def_id)
168+
.const_conditions(impl_def_id.into())
169169
.iter_instantiated(cx, impl_args)
170170
.map(|bound_trait_ref| {
171171
goal.with(
@@ -240,7 +240,7 @@ where
240240
ty::TraitRef::new(cx, cx.require_trait_lang_item(SolverTraitLangItem::Sized), [output])
241241
});
242242
let requirements = cx
243-
.const_conditions(def_id)
243+
.const_conditions(def_id.into())
244244
.iter_instantiated(cx, args)
245245
.map(|trait_ref| {
246246
(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ where
10921092
&self,
10931093
goal_trait_ref: ty::TraitRef<I>,
10941094
trait_assoc_def_id: I::DefId,
1095-
impl_def_id: I::DefId,
1095+
impl_def_id: I::ImplId,
10961096
) -> Result<Option<I::DefId>, I::ErrorGuaranteed> {
10971097
self.delegate.fetch_eligible_assoc_item(goal_trait_ref, trait_assoc_def_id, impl_def_id)
10981098
}

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

Lines changed: 11 additions & 10 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::{SolverLangItem, SolverTraitLangItem};
8+
use rustc_type_ir::lang_items::{SolverAdtLangItem, SolverLangItem, SolverTraitLangItem};
99
use rustc_type_ir::solve::SizedTraitKind;
1010
use rustc_type_ir::{self as ty, Interner, NormalizesTo, PredicateKind, Upcast as _};
1111
use tracing::instrument;
@@ -193,7 +193,7 @@ where
193193
fn consider_impl_candidate(
194194
ecx: &mut EvalCtxt<'_, D>,
195195
goal: Goal<I, NormalizesTo<I>>,
196-
impl_def_id: I::DefId,
196+
impl_def_id: I::ImplId,
197197
) -> Result<Candidate<I>, NoSolution> {
198198
let cx = ecx.cx();
199199

@@ -217,13 +217,13 @@ where
217217
};
218218

219219
ecx.probe_trait_candidate(CandidateSource::Impl(impl_def_id)).enter(|ecx| {
220-
let impl_args = ecx.fresh_args_for_item(impl_def_id);
220+
let impl_args = ecx.fresh_args_for_item(impl_def_id.into());
221221
let impl_trait_ref = impl_trait_ref.instantiate(cx, impl_args);
222222

223223
ecx.eq(goal.param_env, goal_trait_ref, impl_trait_ref)?;
224224

225225
let where_clause_bounds = cx
226-
.predicates_of(impl_def_id)
226+
.predicates_of(impl_def_id.into())
227227
.iter_instantiated(cx, impl_args)
228228
.map(|pred| goal.with(cx, pred));
229229
ecx.add_goals(GoalSource::ImplWhereBound, where_clause_bounds);
@@ -824,10 +824,10 @@ where
824824
// coroutine yield ty `Poll<Option<I>>`.
825825
let wrapped_expected_ty = Ty::new_adt(
826826
cx,
827-
cx.adt_def(cx.require_lang_item(SolverLangItem::Poll)),
827+
cx.adt_def(cx.require_adt_lang_item(SolverAdtLangItem::Poll)),
828828
cx.mk_args(&[Ty::new_adt(
829829
cx,
830-
cx.adt_def(cx.require_lang_item(SolverLangItem::Option)),
830+
cx.adt_def(cx.require_adt_lang_item(SolverAdtLangItem::Option)),
831831
cx.mk_args(&[expected_ty.into()]),
832832
)
833833
.into()]),
@@ -979,7 +979,7 @@ where
979979
fn translate_args(
980980
&mut self,
981981
goal: Goal<I, ty::NormalizesTo<I>>,
982-
impl_def_id: I::DefId,
982+
impl_def_id: I::ImplId,
983983
impl_args: I::GenericArgs,
984984
impl_trait_ref: rustc_type_ir::TraitRef<I>,
985985
target_container_def_id: I::DefId,
@@ -988,14 +988,15 @@ where
988988
Ok(if target_container_def_id == impl_trait_ref.def_id.into() {
989989
// Default value from the trait definition. No need to rebase.
990990
goal.predicate.alias.args
991-
} else if target_container_def_id == impl_def_id {
991+
} else if target_container_def_id == impl_def_id.into() {
992992
// Same impl, no need to fully translate, just a rebase from
993993
// the trait is sufficient.
994994
goal.predicate.alias.args.rebase_onto(cx, impl_trait_ref.def_id.into(), impl_args)
995995
} else {
996996
let target_args = self.fresh_args_for_item(target_container_def_id);
997-
let target_trait_ref =
998-
cx.impl_trait_ref(target_container_def_id).instantiate(cx, target_args);
997+
let target_trait_ref = cx
998+
.impl_trait_ref(target_container_def_id.try_into().unwrap())
999+
.instantiate(cx, target_args);
9991000
// Relate source impl to target impl by equating trait refs.
10001001
self.eq(goal.param_env, impl_trait_ref, target_trait_ref)?;
10011002
// Also add predicates since they may be needed to constrain the

compiler/rustc_next_trait_solver/src/solve/trait_goals.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ where
5454
fn consider_impl_candidate(
5555
ecx: &mut EvalCtxt<'_, D>,
5656
goal: Goal<I, TraitPredicate<I>>,
57-
impl_def_id: I::DefId,
57+
impl_def_id: I::ImplId,
5858
) -> Result<Candidate<I>, NoSolution> {
5959
let cx = ecx.cx();
6060

@@ -91,13 +91,13 @@ where
9191
};
9292

9393
ecx.probe_trait_candidate(CandidateSource::Impl(impl_def_id)).enter(|ecx| {
94-
let impl_args = ecx.fresh_args_for_item(impl_def_id);
94+
let impl_args = ecx.fresh_args_for_item(impl_def_id.into());
9595
ecx.record_impl_args(impl_args);
9696
let impl_trait_ref = impl_trait_ref.instantiate(cx, impl_args);
9797

9898
ecx.eq(goal.param_env, goal.predicate.trait_ref, impl_trait_ref)?;
9999
let where_clause_bounds = cx
100-
.predicates_of(impl_def_id)
100+
.predicates_of(impl_def_id.into())
101101
.iter_instantiated(cx, impl_args)
102102
.map(|pred| goal.with(cx, pred));
103103
ecx.add_goals(GoalSource::ImplWhereBound, where_clause_bounds);

compiler/rustc_type_ir/src/fast_reject.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub fn simplify_type<I: Interner>(
122122
ty::Int(int_type) => Some(SimplifiedType::Int(int_type)),
123123
ty::Uint(uint_type) => Some(SimplifiedType::Uint(uint_type)),
124124
ty::Float(float_type) => Some(SimplifiedType::Float(float_type)),
125-
ty::Adt(def, _) => Some(SimplifiedType::Adt(def.def_id())),
125+
ty::Adt(def, _) => Some(SimplifiedType::Adt(def.def_id().into())),
126126
ty::Str => Some(SimplifiedType::Str),
127127
ty::Array(..) => Some(SimplifiedType::Array),
128128
ty::Slice(..) => Some(SimplifiedType::Slice),
@@ -135,11 +135,11 @@ pub fn simplify_type<I: Interner>(
135135
_ => Some(SimplifiedType::MarkerTraitObject),
136136
},
137137
ty::Ref(_, _, mutbl) => Some(SimplifiedType::Ref(mutbl)),
138-
ty::FnDef(def_id, _) | ty::Closure(def_id, _) | ty::CoroutineClosure(def_id, _) => {
139-
Some(SimplifiedType::Closure(def_id))
140-
}
141-
ty::Coroutine(def_id, _) => Some(SimplifiedType::Coroutine(def_id)),
142-
ty::CoroutineWitness(def_id, _) => Some(SimplifiedType::CoroutineWitness(def_id)),
138+
ty::FnDef(def_id, _) => Some(SimplifiedType::Closure(def_id.into())),
139+
ty::Closure(def_id, _) => Some(SimplifiedType::Closure(def_id.into())),
140+
ty::CoroutineClosure(def_id, _) => Some(SimplifiedType::Closure(def_id.into())),
141+
ty::Coroutine(def_id, _) => Some(SimplifiedType::Coroutine(def_id.into())),
142+
ty::CoroutineWitness(def_id, _) => Some(SimplifiedType::CoroutineWitness(def_id.into())),
143143
ty::Never => Some(SimplifiedType::Never),
144144
ty::Tuple(tys) => Some(SimplifiedType::Tuple(tys.len())),
145145
ty::FnPtr(sig_tys, _hdr) => {
@@ -161,7 +161,7 @@ pub fn simplify_type<I: Interner>(
161161
}
162162
TreatParams::AsRigid | TreatParams::InstantiateWithInfer => None,
163163
},
164-
ty::Foreign(def_id) => Some(SimplifiedType::Foreign(def_id)),
164+
ty::Foreign(def_id) => Some(SimplifiedType::Foreign(def_id.into())),
165165
ty::Error(_) => Some(SimplifiedType::Error),
166166
ty::Bound(..) | ty::Infer(_) => None,
167167
}

0 commit comments

Comments
 (0)