Skip to content

Commit 38bd808

Browse files
Switch next solver to use a specific associated type for trait def id
The compiler just puts `DefId` in there, but rust-analyzer uses different types for each kind of item.
1 parent f6d2341 commit 38bd808

File tree

18 files changed

+234
-178
lines changed

18 files changed

+234
-178
lines changed

compiler/rustc_middle/src/ty/context.rs

Lines changed: 48 additions & 22 deletions
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::TraitSolverLangItem;
55+
use rustc_type_ir::lang_items::{SolverLangItem, SolverTraitLangItem};
5656
pub use rustc_type_ir::lift::Lift;
5757
use rustc_type_ir::{
5858
CollectAndApply, Interner, TypeFlags, TypeFoldable, WithCachedTypeInfo, elaborate, search_graph,
@@ -93,6 +93,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
9393

9494
type DefId = DefId;
9595
type LocalDefId = LocalDefId;
96+
type TraitId = DefId;
9697
type Span = Span;
9798

9899
type GenericArgs = ty::GenericArgsRef<'tcx>;
@@ -483,20 +484,32 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
483484
!self.codegen_fn_attrs(def_id).target_features.is_empty()
484485
}
485486

486-
fn require_lang_item(self, lang_item: TraitSolverLangItem) -> DefId {
487-
self.require_lang_item(trait_lang_item_to_lang_item(lang_item), DUMMY_SP)
487+
fn require_lang_item(self, lang_item: SolverLangItem) -> DefId {
488+
self.require_lang_item(solver_lang_item_to_lang_item(lang_item), DUMMY_SP)
488489
}
489490

490-
fn is_lang_item(self, def_id: DefId, lang_item: TraitSolverLangItem) -> bool {
491-
self.is_lang_item(def_id, trait_lang_item_to_lang_item(lang_item))
491+
fn require_trait_lang_item(self, lang_item: SolverTraitLangItem) -> DefId {
492+
self.require_lang_item(solver_trait_lang_item_to_lang_item(lang_item), DUMMY_SP)
493+
}
494+
495+
fn is_lang_item(self, def_id: DefId, lang_item: SolverLangItem) -> bool {
496+
self.is_lang_item(def_id, solver_lang_item_to_lang_item(lang_item))
497+
}
498+
499+
fn is_trait_lang_item(self, def_id: DefId, lang_item: SolverTraitLangItem) -> bool {
500+
self.is_lang_item(def_id, solver_trait_lang_item_to_lang_item(lang_item))
492501
}
493502

494503
fn is_default_trait(self, def_id: DefId) -> bool {
495504
self.is_default_trait(def_id)
496505
}
497506

498-
fn as_lang_item(self, def_id: DefId) -> Option<TraitSolverLangItem> {
499-
lang_item_to_trait_lang_item(self.lang_items().from_def_id(def_id)?)
507+
fn as_lang_item(self, def_id: DefId) -> Option<SolverLangItem> {
508+
lang_item_to_solver_lang_item(self.lang_items().from_def_id(def_id)?)
509+
}
510+
511+
fn as_trait_lang_item(self, def_id: DefId) -> Option<SolverTraitLangItem> {
512+
lang_item_to_solver_trait_lang_item(self.lang_items().from_def_id(def_id)?)
500513
}
501514

502515
fn associated_type_def_ids(self, def_id: DefId) -> impl IntoIterator<Item = DefId> {
@@ -727,57 +740,70 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
727740
}
728741

729742
macro_rules! bidirectional_lang_item_map {
730-
($($name:ident),+ $(,)?) => {
731-
fn trait_lang_item_to_lang_item(lang_item: TraitSolverLangItem) -> LangItem {
743+
(
744+
$solver_ty:ident, $to_solver:ident, $from_solver:ident;
745+
$($name:ident),+ $(,)?
746+
) => {
747+
fn $from_solver(lang_item: $solver_ty) -> LangItem {
732748
match lang_item {
733-
$(TraitSolverLangItem::$name => LangItem::$name,)+
749+
$($solver_ty::$name => LangItem::$name,)+
734750
}
735751
}
736752

737-
fn lang_item_to_trait_lang_item(lang_item: LangItem) -> Option<TraitSolverLangItem> {
753+
fn $to_solver(lang_item: LangItem) -> Option<$solver_ty> {
738754
Some(match lang_item {
739-
$(LangItem::$name => TraitSolverLangItem::$name,)+
755+
$(LangItem::$name => $solver_ty::$name,)+
740756
_ => return None,
741757
})
742758
}
743759
}
744760
}
745761

746762
bidirectional_lang_item_map! {
763+
SolverLangItem, lang_item_to_solver_lang_item, solver_lang_item_to_lang_item;
764+
765+
// tidy-alphabetical-start
766+
AsyncFnKindUpvars,
767+
AsyncFnOnceOutput,
768+
CallOnceFuture,
769+
CallRefFuture,
770+
CoroutineReturn,
771+
CoroutineYield,
772+
DynMetadata,
773+
FutureOutput,
774+
Metadata,
775+
Option,
776+
Poll,
777+
// tidy-alphabetical-end
778+
}
779+
780+
bidirectional_lang_item_map! {
781+
SolverTraitLangItem, lang_item_to_solver_trait_lang_item, solver_trait_lang_item_to_lang_item;
782+
747783
// tidy-alphabetical-start
748784
AsyncFn,
749785
AsyncFnKindHelper,
750-
AsyncFnKindUpvars,
751786
AsyncFnMut,
752787
AsyncFnOnce,
753788
AsyncFnOnceOutput,
754789
AsyncIterator,
755790
BikeshedGuaranteedNoDrop,
756-
CallOnceFuture,
757-
CallRefFuture,
758791
Clone,
759792
Copy,
760793
Coroutine,
761-
CoroutineReturn,
762-
CoroutineYield,
763794
Destruct,
764795
DiscriminantKind,
765796
Drop,
766-
DynMetadata,
767797
Fn,
768798
FnMut,
769799
FnOnce,
770800
FnPtrTrait,
771801
FusedIterator,
772802
Future,
773-
FutureOutput,
774803
Iterator,
775804
MetaSized,
776-
Metadata,
777-
Option,
778805
PointeeSized,
779806
PointeeTrait,
780-
Poll,
781807
Sized,
782808
TransmuteTrait,
783809
Tuple,

compiler/rustc_next_trait_solver/src/coherence.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ where
295295
ControlFlow::Break(OrphanCheckEarlyExit::UncoveredTyParam(ty))
296296
}
297297

298-
fn def_id_is_local(&mut self, def_id: I::DefId) -> bool {
298+
fn def_id_is_local(&mut self, def_id: impl DefId<I>) -> bool {
299299
match self.in_crate {
300300
InCrate::Local { .. } => def_id.is_local(),
301301
InCrate::Remote => false,

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

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::ops::ControlFlow;
77

88
use derive_where::derive_where;
99
use rustc_type_ir::inherent::*;
10-
use rustc_type_ir::lang_items::TraitSolverLangItem;
10+
use rustc_type_ir::lang_items::SolverTraitLangItem;
1111
use rustc_type_ir::search_graph::CandidateHeadUsages;
1212
use rustc_type_ir::solve::SizedTraitKind;
1313
use rustc_type_ir::{
@@ -54,7 +54,7 @@ where
5454

5555
fn with_replaced_self_ty(self, cx: I, self_ty: I::Ty) -> Self;
5656

57-
fn trait_def_id(self, cx: I) -> I::DefId;
57+
fn trait_def_id(self, cx: I) -> I::TraitId;
5858

5959
/// Consider a clause, which consists of a "assumption" and some "requirements",
6060
/// to satisfy a goal. If the requirements hold, then attempt to satisfy our
@@ -516,80 +516,80 @@ where
516516
} else if cx.trait_is_alias(trait_def_id) {
517517
G::consider_trait_alias_candidate(self, goal)
518518
} else {
519-
match cx.as_lang_item(trait_def_id) {
520-
Some(TraitSolverLangItem::Sized) => {
519+
match cx.as_trait_lang_item(trait_def_id) {
520+
Some(SolverTraitLangItem::Sized) => {
521521
G::consider_builtin_sizedness_candidates(self, goal, SizedTraitKind::Sized)
522522
}
523-
Some(TraitSolverLangItem::MetaSized) => {
523+
Some(SolverTraitLangItem::MetaSized) => {
524524
G::consider_builtin_sizedness_candidates(self, goal, SizedTraitKind::MetaSized)
525525
}
526-
Some(TraitSolverLangItem::PointeeSized) => {
526+
Some(SolverTraitLangItem::PointeeSized) => {
527527
unreachable!("`PointeeSized` is removed during lowering");
528528
}
529-
Some(TraitSolverLangItem::Copy | TraitSolverLangItem::Clone) => {
529+
Some(SolverTraitLangItem::Copy | SolverTraitLangItem::Clone) => {
530530
G::consider_builtin_copy_clone_candidate(self, goal)
531531
}
532-
Some(TraitSolverLangItem::Fn) => {
532+
Some(SolverTraitLangItem::Fn) => {
533533
G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::Fn)
534534
}
535-
Some(TraitSolverLangItem::FnMut) => {
535+
Some(SolverTraitLangItem::FnMut) => {
536536
G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::FnMut)
537537
}
538-
Some(TraitSolverLangItem::FnOnce) => {
538+
Some(SolverTraitLangItem::FnOnce) => {
539539
G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::FnOnce)
540540
}
541-
Some(TraitSolverLangItem::AsyncFn) => {
541+
Some(SolverTraitLangItem::AsyncFn) => {
542542
G::consider_builtin_async_fn_trait_candidates(self, goal, ty::ClosureKind::Fn)
543543
}
544-
Some(TraitSolverLangItem::AsyncFnMut) => {
544+
Some(SolverTraitLangItem::AsyncFnMut) => {
545545
G::consider_builtin_async_fn_trait_candidates(
546546
self,
547547
goal,
548548
ty::ClosureKind::FnMut,
549549
)
550550
}
551-
Some(TraitSolverLangItem::AsyncFnOnce) => {
551+
Some(SolverTraitLangItem::AsyncFnOnce) => {
552552
G::consider_builtin_async_fn_trait_candidates(
553553
self,
554554
goal,
555555
ty::ClosureKind::FnOnce,
556556
)
557557
}
558-
Some(TraitSolverLangItem::FnPtrTrait) => {
558+
Some(SolverTraitLangItem::FnPtrTrait) => {
559559
G::consider_builtin_fn_ptr_trait_candidate(self, goal)
560560
}
561-
Some(TraitSolverLangItem::AsyncFnKindHelper) => {
561+
Some(SolverTraitLangItem::AsyncFnKindHelper) => {
562562
G::consider_builtin_async_fn_kind_helper_candidate(self, goal)
563563
}
564-
Some(TraitSolverLangItem::Tuple) => G::consider_builtin_tuple_candidate(self, goal),
565-
Some(TraitSolverLangItem::PointeeTrait) => {
564+
Some(SolverTraitLangItem::Tuple) => G::consider_builtin_tuple_candidate(self, goal),
565+
Some(SolverTraitLangItem::PointeeTrait) => {
566566
G::consider_builtin_pointee_candidate(self, goal)
567567
}
568-
Some(TraitSolverLangItem::Future) => {
568+
Some(SolverTraitLangItem::Future) => {
569569
G::consider_builtin_future_candidate(self, goal)
570570
}
571-
Some(TraitSolverLangItem::Iterator) => {
571+
Some(SolverTraitLangItem::Iterator) => {
572572
G::consider_builtin_iterator_candidate(self, goal)
573573
}
574-
Some(TraitSolverLangItem::FusedIterator) => {
574+
Some(SolverTraitLangItem::FusedIterator) => {
575575
G::consider_builtin_fused_iterator_candidate(self, goal)
576576
}
577-
Some(TraitSolverLangItem::AsyncIterator) => {
577+
Some(SolverTraitLangItem::AsyncIterator) => {
578578
G::consider_builtin_async_iterator_candidate(self, goal)
579579
}
580-
Some(TraitSolverLangItem::Coroutine) => {
580+
Some(SolverTraitLangItem::Coroutine) => {
581581
G::consider_builtin_coroutine_candidate(self, goal)
582582
}
583-
Some(TraitSolverLangItem::DiscriminantKind) => {
583+
Some(SolverTraitLangItem::DiscriminantKind) => {
584584
G::consider_builtin_discriminant_kind_candidate(self, goal)
585585
}
586-
Some(TraitSolverLangItem::Destruct) => {
586+
Some(SolverTraitLangItem::Destruct) => {
587587
G::consider_builtin_destruct_candidate(self, goal)
588588
}
589-
Some(TraitSolverLangItem::TransmuteTrait) => {
589+
Some(SolverTraitLangItem::TransmuteTrait) => {
590590
G::consider_builtin_transmute_candidate(self, goal)
591591
}
592-
Some(TraitSolverLangItem::BikeshedGuaranteedNoDrop) => {
592+
Some(SolverTraitLangItem::BikeshedGuaranteedNoDrop) => {
593593
G::consider_builtin_bikeshed_guaranteed_no_drop_candidate(self, goal)
594594
}
595595
_ => Err(NoSolution),
@@ -600,7 +600,7 @@ where
600600

601601
// There may be multiple unsize candidates for a trait with several supertraits:
602602
// `trait Foo: Bar<A> + Bar<B>` and `dyn Foo: Unsize<dyn Bar<_>>`
603-
if cx.is_lang_item(trait_def_id, TraitSolverLangItem::Unsize) {
603+
if cx.is_trait_lang_item(trait_def_id, SolverTraitLangItem::Unsize) {
604604
candidates.extend(G::consider_structural_builtin_unsize_candidates(self, goal));
605605
}
606606
}

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

Lines changed: 11 additions & 11 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::TraitSolverLangItem;
7+
use rustc_type_ir::lang_items::{SolverLangItem, SolverTraitLangItem};
88
use rustc_type_ir::solve::SizedTraitKind;
99
use rustc_type_ir::solve::inspect::ProbeKind;
1010
use rustc_type_ir::{
@@ -454,7 +454,7 @@ pub(in crate::solve) fn extract_tupled_inputs_and_output_from_async_callable<I:
454454
nested.push(
455455
ty::TraitRef::new(
456456
cx,
457-
cx.require_lang_item(TraitSolverLangItem::AsyncFnKindHelper),
457+
cx.require_trait_lang_item(SolverTraitLangItem::AsyncFnKindHelper),
458458
[kind_ty, Ty::from_closure_kind(cx, goal_kind)],
459459
)
460460
.upcast(cx),
@@ -496,7 +496,7 @@ pub(in crate::solve) fn extract_tupled_inputs_and_output_from_async_callable<I:
496496
let args = args.as_closure();
497497
let bound_sig = args.sig();
498498
let sig = bound_sig.skip_binder();
499-
let future_trait_def_id = cx.require_lang_item(TraitSolverLangItem::Future);
499+
let future_trait_def_id = cx.require_trait_lang_item(SolverTraitLangItem::Future);
500500
// `Closure`s only implement `AsyncFn*` when their return type
501501
// implements `Future`.
502502
let mut nested = vec![
@@ -514,7 +514,7 @@ pub(in crate::solve) fn extract_tupled_inputs_and_output_from_async_callable<I:
514514
}
515515
} else {
516516
let async_fn_kind_trait_def_id =
517-
cx.require_lang_item(TraitSolverLangItem::AsyncFnKindHelper);
517+
cx.require_trait_lang_item(SolverTraitLangItem::AsyncFnKindHelper);
518518
// When we don't know the closure kind (and therefore also the closure's upvars,
519519
// which are computed at the same time), we must delay the computation of the
520520
// generator's upvars. We do this using the `AsyncFnKindHelper`, which as a trait
@@ -532,7 +532,7 @@ pub(in crate::solve) fn extract_tupled_inputs_and_output_from_async_callable<I:
532532
);
533533
}
534534

535-
let future_output_def_id = cx.require_lang_item(TraitSolverLangItem::FutureOutput);
535+
let future_output_def_id = cx.require_lang_item(SolverLangItem::FutureOutput);
536536
let future_output_ty = Ty::new_projection(cx, future_output_def_id, [sig.output()]);
537537
Ok((
538538
bound_sig.rebind(AsyncCallableRelevantTypes {
@@ -581,13 +581,13 @@ fn fn_item_to_async_callable<I: Interner>(
581581
bound_sig: ty::Binder<I, ty::FnSig<I>>,
582582
) -> Result<(ty::Binder<I, AsyncCallableRelevantTypes<I>>, Vec<I::Predicate>), NoSolution> {
583583
let sig = bound_sig.skip_binder();
584-
let future_trait_def_id = cx.require_lang_item(TraitSolverLangItem::Future);
584+
let future_trait_def_id = cx.require_trait_lang_item(SolverTraitLangItem::Future);
585585
// `FnDef` and `FnPtr` only implement `AsyncFn*` when their
586586
// return type implements `Future`.
587587
let nested = vec![
588588
bound_sig.rebind(ty::TraitRef::new(cx, future_trait_def_id, [sig.output()])).upcast(cx),
589589
];
590-
let future_output_def_id = cx.require_lang_item(TraitSolverLangItem::FutureOutput);
590+
let future_output_def_id = cx.require_lang_item(SolverLangItem::FutureOutput);
591591
let future_output_ty = Ty::new_projection(cx, future_output_def_id, [sig.output()]);
592592
Ok((
593593
bound_sig.rebind(AsyncCallableRelevantTypes {
@@ -633,7 +633,7 @@ fn coroutine_closure_to_ambiguous_coroutine<I: Interner>(
633633
args: ty::CoroutineClosureArgs<I>,
634634
sig: ty::CoroutineClosureSignature<I>,
635635
) -> I::Ty {
636-
let upvars_projection_def_id = cx.require_lang_item(TraitSolverLangItem::AsyncFnKindUpvars);
636+
let upvars_projection_def_id = cx.require_lang_item(SolverLangItem::AsyncFnKindUpvars);
637637
let tupled_upvars_ty = Ty::new_projection(
638638
cx,
639639
upvars_projection_def_id,
@@ -732,7 +732,7 @@ pub(in crate::solve) fn const_conditions_for_destruct<I: Interner>(
732732
cx: I,
733733
self_ty: I::Ty,
734734
) -> Result<Vec<ty::TraitRef<I>>, NoSolution> {
735-
let destruct_def_id = cx.require_lang_item(TraitSolverLangItem::Destruct);
735+
let destruct_def_id = cx.require_trait_lang_item(SolverTraitLangItem::Destruct);
736736

737737
match self_ty.kind() {
738738
// `ManuallyDrop` is trivially `[const] Destruct` as we do not run any drop glue on it.
@@ -751,7 +751,7 @@ pub(in crate::solve) fn const_conditions_for_destruct<I: Interner>(
751751
Some(AdtDestructorKind::NotConst) => return Err(NoSolution),
752752
// `Drop` impl exists, and it's const. Require `Ty: [const] Drop` to hold.
753753
Some(AdtDestructorKind::Const) => {
754-
let drop_def_id = cx.require_lang_item(TraitSolverLangItem::Drop);
754+
let drop_def_id = cx.require_trait_lang_item(SolverTraitLangItem::Drop);
755755
let drop_trait_ref = ty::TraitRef::new(cx, drop_def_id, [self_ty]);
756756
const_conditions.push(drop_trait_ref);
757757
}
@@ -869,7 +869,7 @@ where
869869

870870
// FIXME(associated_const_equality): Also add associated consts to
871871
// the requirements here.
872-
for associated_type_def_id in cx.associated_type_def_ids(trait_ref.def_id) {
872+
for associated_type_def_id in cx.associated_type_def_ids(trait_ref.def_id.into()) {
873873
// associated types that require `Self: Sized` do not show up in the built-in
874874
// implementation of `Trait for dyn Trait`, and can be dropped here.
875875
if cx.generics_require_sized_self(associated_type_def_id) {

0 commit comments

Comments
 (0)