diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 49a4733de3b0a..6e1898ed26d9e 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -52,7 +52,7 @@ use rustc_session::{Limit, Session}; use rustc_span::def_id::{CRATE_DEF_ID, DefPathHash, StableCrateId}; use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym}; use rustc_type_ir::TyKind::*; -use rustc_type_ir::lang_items::TraitSolverLangItem; +use rustc_type_ir::lang_items::{TraitSolverLangItem, TraitSolverTraitLangItem}; pub use rustc_type_ir::lift::Lift; use rustc_type_ir::{ CollectAndApply, Interner, TypeFlags, TypeFoldable, WithCachedTypeInfo, elaborate, search_graph, @@ -93,6 +93,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> { type DefId = DefId; type LocalDefId = LocalDefId; + type TraitId = DefId; type Span = Span; type GenericArgs = ty::GenericArgsRef<'tcx>; @@ -481,11 +482,19 @@ impl<'tcx> Interner for TyCtxt<'tcx> { } fn require_lang_item(self, lang_item: TraitSolverLangItem) -> DefId { - self.require_lang_item(trait_lang_item_to_lang_item(lang_item), DUMMY_SP) + self.require_lang_item(trait_lang_item_to_solver_lang_item(lang_item), DUMMY_SP) + } + + fn require_trait_lang_item(self, lang_item: TraitSolverTraitLangItem) -> DefId { + self.require_lang_item(trait_lang_item_to_solver_trait_lang_item(lang_item), DUMMY_SP) } fn is_lang_item(self, def_id: DefId, lang_item: TraitSolverLangItem) -> bool { - self.is_lang_item(def_id, trait_lang_item_to_lang_item(lang_item)) + self.is_lang_item(def_id, trait_lang_item_to_solver_lang_item(lang_item)) + } + + fn is_trait_lang_item(self, def_id: DefId, lang_item: TraitSolverTraitLangItem) -> bool { + self.is_lang_item(def_id, trait_lang_item_to_solver_trait_lang_item(lang_item)) } fn is_default_trait(self, def_id: DefId) -> bool { @@ -493,7 +502,11 @@ impl<'tcx> Interner for TyCtxt<'tcx> { } fn as_lang_item(self, def_id: DefId) -> Option { - lang_item_to_trait_lang_item(self.lang_items().from_def_id(def_id)?) + solver_lang_item_to_trait_lang_item(self.lang_items().from_def_id(def_id)?) + } + + fn as_trait_lang_item(self, def_id: DefId) -> Option { + solver_trait_lang_item_to_trait_lang_item(self.lang_items().from_def_id(def_id)?) } fn associated_type_def_ids(self, def_id: DefId) -> impl IntoIterator { @@ -724,16 +737,19 @@ impl<'tcx> Interner for TyCtxt<'tcx> { } macro_rules! bidirectional_lang_item_map { - ($($name:ident),+ $(,)?) => { - fn trait_lang_item_to_lang_item(lang_item: TraitSolverLangItem) -> LangItem { + ( + $solver_ty:ident, $to_solver:ident, $from_solver:ident; + $($name:ident),+ $(,)? + ) => { + fn $from_solver(lang_item: $solver_ty) -> LangItem { match lang_item { - $(TraitSolverLangItem::$name => LangItem::$name,)+ + $($solver_ty::$name => LangItem::$name,)+ } } - fn lang_item_to_trait_lang_item(lang_item: LangItem) -> Option { + fn $to_solver(lang_item: LangItem) -> Option<$solver_ty> { Some(match lang_item { - $(LangItem::$name => TraitSolverLangItem::$name,)+ + $(LangItem::$name => $solver_ty::$name,)+ _ => return None, }) } @@ -741,40 +757,50 @@ macro_rules! bidirectional_lang_item_map { } bidirectional_lang_item_map! { + TraitSolverLangItem, solver_lang_item_to_trait_lang_item, trait_lang_item_to_solver_lang_item; + +// tidy-alphabetical-start + AsyncFnKindUpvars, + AsyncFnOnceOutput, + CallOnceFuture, + CallRefFuture, + CoroutineReturn, + CoroutineYield, + DynMetadata, + FutureOutput, + Metadata, + Option, + Poll, +// tidy-alphabetical-end +} + +bidirectional_lang_item_map! { + TraitSolverTraitLangItem, solver_trait_lang_item_to_trait_lang_item, trait_lang_item_to_solver_trait_lang_item; + // tidy-alphabetical-start AsyncFn, AsyncFnKindHelper, - AsyncFnKindUpvars, AsyncFnMut, AsyncFnOnce, AsyncFnOnceOutput, AsyncIterator, BikeshedGuaranteedNoDrop, - CallOnceFuture, - CallRefFuture, Clone, Copy, Coroutine, - CoroutineReturn, - CoroutineYield, Destruct, DiscriminantKind, Drop, - DynMetadata, Fn, FnMut, FnOnce, FnPtrTrait, FusedIterator, Future, - FutureOutput, Iterator, MetaSized, - Metadata, - Option, PointeeSized, PointeeTrait, - Poll, Sized, TransmuteTrait, Tuple, diff --git a/compiler/rustc_next_trait_solver/src/coherence.rs b/compiler/rustc_next_trait_solver/src/coherence.rs index f8215a228f5b6..79219b34e2907 100644 --- a/compiler/rustc_next_trait_solver/src/coherence.rs +++ b/compiler/rustc_next_trait_solver/src/coherence.rs @@ -295,7 +295,7 @@ where ControlFlow::Break(OrphanCheckEarlyExit::UncoveredTyParam(ty)) } - fn def_id_is_local(&mut self, def_id: I::DefId) -> bool { + fn def_id_is_local(&mut self, def_id: impl DefId) -> bool { match self.in_crate { InCrate::Local { .. } => def_id.is_local(), InCrate::Remote => false, diff --git a/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs b/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs index a4a8317912a81..a95abfde1b0a1 100644 --- a/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs +++ b/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs @@ -7,7 +7,7 @@ use std::ops::ControlFlow; use derive_where::derive_where; use rustc_type_ir::inherent::*; -use rustc_type_ir::lang_items::TraitSolverLangItem; +use rustc_type_ir::lang_items::TraitSolverTraitLangItem; use rustc_type_ir::search_graph::CandidateHeadUsages; use rustc_type_ir::solve::SizedTraitKind; use rustc_type_ir::{ @@ -54,7 +54,7 @@ where fn with_replaced_self_ty(self, cx: I, self_ty: I::Ty) -> Self; - fn trait_def_id(self, cx: I) -> I::DefId; + fn trait_def_id(self, cx: I) -> I::TraitId; /// Consider a clause, which consists of a "assumption" and some "requirements", /// to satisfy a goal. If the requirements hold, then attempt to satisfy our @@ -516,80 +516,82 @@ where } else if cx.trait_is_alias(trait_def_id) { G::consider_trait_alias_candidate(self, goal) } else { - match cx.as_lang_item(trait_def_id) { - Some(TraitSolverLangItem::Sized) => { + match cx.as_trait_lang_item(trait_def_id) { + Some(TraitSolverTraitLangItem::Sized) => { G::consider_builtin_sizedness_candidates(self, goal, SizedTraitKind::Sized) } - Some(TraitSolverLangItem::MetaSized) => { + Some(TraitSolverTraitLangItem::MetaSized) => { G::consider_builtin_sizedness_candidates(self, goal, SizedTraitKind::MetaSized) } - Some(TraitSolverLangItem::PointeeSized) => { + Some(TraitSolverTraitLangItem::PointeeSized) => { unreachable!("`PointeeSized` is removed during lowering"); } - Some(TraitSolverLangItem::Copy | TraitSolverLangItem::Clone) => { + Some(TraitSolverTraitLangItem::Copy | TraitSolverTraitLangItem::Clone) => { G::consider_builtin_copy_clone_candidate(self, goal) } - Some(TraitSolverLangItem::Fn) => { + Some(TraitSolverTraitLangItem::Fn) => { G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::Fn) } - Some(TraitSolverLangItem::FnMut) => { + Some(TraitSolverTraitLangItem::FnMut) => { G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::FnMut) } - Some(TraitSolverLangItem::FnOnce) => { + Some(TraitSolverTraitLangItem::FnOnce) => { G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::FnOnce) } - Some(TraitSolverLangItem::AsyncFn) => { + Some(TraitSolverTraitLangItem::AsyncFn) => { G::consider_builtin_async_fn_trait_candidates(self, goal, ty::ClosureKind::Fn) } - Some(TraitSolverLangItem::AsyncFnMut) => { + Some(TraitSolverTraitLangItem::AsyncFnMut) => { G::consider_builtin_async_fn_trait_candidates( self, goal, ty::ClosureKind::FnMut, ) } - Some(TraitSolverLangItem::AsyncFnOnce) => { + Some(TraitSolverTraitLangItem::AsyncFnOnce) => { G::consider_builtin_async_fn_trait_candidates( self, goal, ty::ClosureKind::FnOnce, ) } - Some(TraitSolverLangItem::FnPtrTrait) => { + Some(TraitSolverTraitLangItem::FnPtrTrait) => { G::consider_builtin_fn_ptr_trait_candidate(self, goal) } - Some(TraitSolverLangItem::AsyncFnKindHelper) => { + Some(TraitSolverTraitLangItem::AsyncFnKindHelper) => { G::consider_builtin_async_fn_kind_helper_candidate(self, goal) } - Some(TraitSolverLangItem::Tuple) => G::consider_builtin_tuple_candidate(self, goal), - Some(TraitSolverLangItem::PointeeTrait) => { + Some(TraitSolverTraitLangItem::Tuple) => { + G::consider_builtin_tuple_candidate(self, goal) + } + Some(TraitSolverTraitLangItem::PointeeTrait) => { G::consider_builtin_pointee_candidate(self, goal) } - Some(TraitSolverLangItem::Future) => { + Some(TraitSolverTraitLangItem::Future) => { G::consider_builtin_future_candidate(self, goal) } - Some(TraitSolverLangItem::Iterator) => { + Some(TraitSolverTraitLangItem::Iterator) => { G::consider_builtin_iterator_candidate(self, goal) } - Some(TraitSolverLangItem::FusedIterator) => { + Some(TraitSolverTraitLangItem::FusedIterator) => { G::consider_builtin_fused_iterator_candidate(self, goal) } - Some(TraitSolverLangItem::AsyncIterator) => { + Some(TraitSolverTraitLangItem::AsyncIterator) => { G::consider_builtin_async_iterator_candidate(self, goal) } - Some(TraitSolverLangItem::Coroutine) => { + Some(TraitSolverTraitLangItem::Coroutine) => { G::consider_builtin_coroutine_candidate(self, goal) } - Some(TraitSolverLangItem::DiscriminantKind) => { + Some(TraitSolverTraitLangItem::DiscriminantKind) => { G::consider_builtin_discriminant_kind_candidate(self, goal) } - Some(TraitSolverLangItem::Destruct) => { + Some(TraitSolverTraitLangItem::Destruct) => { G::consider_builtin_destruct_candidate(self, goal) } - Some(TraitSolverLangItem::TransmuteTrait) => { + Some(TraitSolverTraitLangItem::TransmuteTrait) => { G::consider_builtin_transmute_candidate(self, goal) } - Some(TraitSolverLangItem::BikeshedGuaranteedNoDrop) => { + Some(TraitSolverTraitLangItem::BikeshedGuaranteedNoDrop) => { G::consider_builtin_bikeshed_guaranteed_no_drop_candidate(self, goal) } _ => Err(NoSolution), @@ -600,7 +602,7 @@ where // There may be multiple unsize candidates for a trait with several supertraits: // `trait Foo: Bar + Bar` and `dyn Foo: Unsize>` - if cx.is_lang_item(trait_def_id, TraitSolverLangItem::Unsize) { + if cx.is_trait_lang_item(trait_def_id, TraitSolverTraitLangItem::Unsize) { candidates.extend(G::consider_structural_builtin_unsize_candidates(self, goal)); } } diff --git a/compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs b/compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs index faa86734d08c2..66037af5463cd 100644 --- a/compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs +++ b/compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs @@ -4,7 +4,7 @@ use derive_where::derive_where; use rustc_type_ir::data_structures::HashMap; use rustc_type_ir::inherent::*; -use rustc_type_ir::lang_items::TraitSolverLangItem; +use rustc_type_ir::lang_items::{TraitSolverLangItem, TraitSolverTraitLangItem}; use rustc_type_ir::solve::SizedTraitKind; use rustc_type_ir::solve::inspect::ProbeKind; use rustc_type_ir::{ @@ -466,7 +466,7 @@ pub(in crate::solve) fn extract_tupled_inputs_and_output_from_async_callable( bound_sig: ty::Binder>, ) -> Result<(ty::Binder>, Vec), NoSolution> { let sig = bound_sig.skip_binder(); - let future_trait_def_id = cx.require_lang_item(TraitSolverLangItem::Future); + let future_trait_def_id = cx.require_trait_lang_item(TraitSolverTraitLangItem::Future); // `FnDef` and `FnPtr` only implement `AsyncFn*` when their // return type implements `Future`. let nested = vec![ @@ -744,7 +744,7 @@ pub(in crate::solve) fn const_conditions_for_destruct( cx: I, self_ty: I::Ty, ) -> Result>, NoSolution> { - let destruct_def_id = cx.require_lang_item(TraitSolverLangItem::Destruct); + let destruct_def_id = cx.require_trait_lang_item(TraitSolverTraitLangItem::Destruct); match self_ty.kind() { // `ManuallyDrop` is trivially `[const] Destruct` as we do not run any drop glue on it. @@ -763,7 +763,7 @@ pub(in crate::solve) fn const_conditions_for_destruct( Some(AdtDestructorKind::NotConst) => return Err(NoSolution), // `Drop` impl exists, and it's const. Require `Ty: [const] Drop` to hold. Some(AdtDestructorKind::Const) => { - let drop_def_id = cx.require_lang_item(TraitSolverLangItem::Drop); + let drop_def_id = cx.require_trait_lang_item(TraitSolverTraitLangItem::Drop); let drop_trait_ref = ty::TraitRef::new(cx, drop_def_id, [self_ty]); const_conditions.push(drop_trait_ref); } @@ -881,7 +881,7 @@ where // FIXME(associated_const_equality): Also add associated consts to // the requirements here. - for associated_type_def_id in cx.associated_type_def_ids(trait_ref.def_id) { + for associated_type_def_id in cx.associated_type_def_ids(trait_ref.def_id.into()) { // associated types that require `Self: Sized` do not show up in the built-in // implementation of `Trait for dyn Trait`, and can be dropped here. if cx.generics_require_sized_self(associated_type_def_id) { diff --git a/compiler/rustc_next_trait_solver/src/solve/effect_goals.rs b/compiler/rustc_next_trait_solver/src/solve/effect_goals.rs index 9a22bf58c0352..05ffd1c2c9aff 100644 --- a/compiler/rustc_next_trait_solver/src/solve/effect_goals.rs +++ b/compiler/rustc_next_trait_solver/src/solve/effect_goals.rs @@ -3,7 +3,7 @@ use rustc_type_ir::fast_reject::DeepRejectCtxt; use rustc_type_ir::inherent::*; -use rustc_type_ir::lang_items::TraitSolverLangItem; +use rustc_type_ir::lang_items::TraitSolverTraitLangItem; use rustc_type_ir::solve::SizedTraitKind; use rustc_type_ir::solve::inspect::ProbeKind; use rustc_type_ir::{self as ty, Interner, elaborate}; @@ -33,7 +33,7 @@ where self.with_replaced_self_ty(cx, self_ty) } - fn trait_def_id(self, _: I) -> I::DefId { + fn trait_def_id(self, _: I) -> I::TraitId { self.def_id() } @@ -233,7 +233,11 @@ where // A built-in `Fn` impl only holds if the output is sized. // (FIXME: technically we only need to check this if the type is a fn ptr...) let output_is_sized_pred = inputs_and_output.map_bound(|(_, output)| { - ty::TraitRef::new(cx, cx.require_lang_item(TraitSolverLangItem::Sized), [output]) + ty::TraitRef::new( + cx, + cx.require_trait_lang_item(TraitSolverTraitLangItem::Sized), + [output], + ) }); let requirements = cx .const_conditions(def_id) diff --git a/compiler/rustc_next_trait_solver/src/solve/mod.rs b/compiler/rustc_next_trait_solver/src/solve/mod.rs index c2745c878dc12..710b59f662a77 100644 --- a/compiler/rustc_next_trait_solver/src/solve/mod.rs +++ b/compiler/rustc_next_trait_solver/src/solve/mod.rs @@ -130,7 +130,7 @@ where } } - fn compute_dyn_compatible_goal(&mut self, trait_def_id: I::DefId) -> QueryResult { + fn compute_dyn_compatible_goal(&mut self, trait_def_id: I::TraitId) -> QueryResult { if self.cx().trait_is_dyn_compatible(trait_def_id) { self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) } else { diff --git a/compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs b/compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs index 93434dce79fd5..67f5d2ab9fdc4 100644 --- a/compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs +++ b/compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs @@ -5,7 +5,7 @@ mod opaque_types; use rustc_type_ir::fast_reject::DeepRejectCtxt; use rustc_type_ir::inherent::*; -use rustc_type_ir::lang_items::TraitSolverLangItem; +use rustc_type_ir::lang_items::{TraitSolverLangItem, TraitSolverTraitLangItem}; use rustc_type_ir::solve::SizedTraitKind; use rustc_type_ir::{self as ty, Interner, NormalizesTo, PredicateKind, Upcast as _}; use tracing::instrument; @@ -103,7 +103,7 @@ where self.with_replaced_self_ty(cx, self_ty) } - fn trait_def_id(self, cx: I) -> I::DefId { + fn trait_def_id(self, cx: I) -> I::TraitId { self.trait_def_id(cx) } @@ -456,7 +456,11 @@ where // A built-in `Fn` impl only holds if the output is sized. // (FIXME: technically we only need to check this if the type is a fn ptr...) let output_is_sized_pred = tupled_inputs_and_output.map_bound(|(_, output)| { - ty::TraitRef::new(cx, cx.require_lang_item(TraitSolverLangItem::Sized), [output]) + ty::TraitRef::new( + cx, + cx.require_trait_lang_item(TraitSolverTraitLangItem::Sized), + [output], + ) }); let pred = tupled_inputs_and_output @@ -503,7 +507,11 @@ where // (FIXME: technically we only need to check this if the type is a fn ptr...) let output_is_sized_pred = tupled_inputs_and_output_and_coroutine.map_bound( |AsyncCallableRelevantTypes { output_coroutine_ty: output_ty, .. }| { - ty::TraitRef::new(cx, cx.require_lang_item(TraitSolverLangItem::Sized), [output_ty]) + ty::TraitRef::new( + cx, + cx.require_trait_lang_item(TraitSolverTraitLangItem::Sized), + [output_ty], + ) }, ); @@ -678,7 +686,7 @@ where ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| { let sized_predicate = ty::TraitRef::new( cx, - cx.require_lang_item(TraitSolverLangItem::Sized), + cx.require_trait_lang_item(TraitSolverTraitLangItem::Sized), [I::GenericArg::from(goal.predicate.self_ty())], ); ecx.add_goal(GoalSource::Misc, goal.with(cx, sized_predicate)); @@ -983,13 +991,13 @@ where target_container_def_id: I::DefId, ) -> Result { let cx = self.cx(); - Ok(if target_container_def_id == impl_trait_ref.def_id { + Ok(if target_container_def_id == impl_trait_ref.def_id.into() { // Default value from the trait definition. No need to rebase. goal.predicate.alias.args } else if target_container_def_id == impl_def_id { // Same impl, no need to fully translate, just a rebase from // the trait is sufficient. - goal.predicate.alias.args.rebase_onto(cx, impl_trait_ref.def_id, impl_args) + goal.predicate.alias.args.rebase_onto(cx, impl_trait_ref.def_id.into(), impl_args) } else { let target_args = self.fresh_args_for_item(target_container_def_id); let target_trait_ref = @@ -1004,7 +1012,7 @@ where .iter_instantiated(cx, target_args) .map(|pred| goal.with(cx, pred)), ); - goal.predicate.alias.args.rebase_onto(cx, impl_trait_ref.def_id, target_args) + goal.predicate.alias.args.rebase_onto(cx, impl_trait_ref.def_id.into(), target_args) }) } } diff --git a/compiler/rustc_next_trait_solver/src/solve/trait_goals.rs b/compiler/rustc_next_trait_solver/src/solve/trait_goals.rs index 04ede365a2149..5c5fd285d98b8 100644 --- a/compiler/rustc_next_trait_solver/src/solve/trait_goals.rs +++ b/compiler/rustc_next_trait_solver/src/solve/trait_goals.rs @@ -3,7 +3,7 @@ use rustc_type_ir::data_structures::IndexSet; use rustc_type_ir::fast_reject::DeepRejectCtxt; use rustc_type_ir::inherent::*; -use rustc_type_ir::lang_items::TraitSolverLangItem; +use rustc_type_ir::lang_items::TraitSolverTraitLangItem; use rustc_type_ir::solve::{CanonicalResponse, SizedTraitKind}; use rustc_type_ir::{ self as ty, Interner, Movability, TraitPredicate, TraitRef, TypeVisitableExt as _, TypingMode, @@ -39,7 +39,7 @@ where self.with_replaced_self_ty(cx, self_ty) } - fn trait_def_id(self, _: I) -> I::DefId { + fn trait_def_id(self, _: I) -> I::TraitId { self.def_id() } @@ -131,16 +131,16 @@ where ) -> Result<(), NoSolution> { fn trait_def_id_matches( cx: I, - clause_def_id: I::DefId, - goal_def_id: I::DefId, + clause_def_id: I::TraitId, + goal_def_id: I::TraitId, ) -> bool { clause_def_id == goal_def_id // PERF(sized-hierarchy): Sizedness supertraits aren't elaborated to improve perf, so // check for a `MetaSized` supertrait being matched against a `Sized` assumption. // // `PointeeSized` bounds are syntactic sugar for a lack of bounds so don't need this. - || (cx.is_lang_item(clause_def_id, TraitSolverLangItem::Sized) - && cx.is_lang_item(goal_def_id, TraitSolverLangItem::MetaSized)) + || (cx.is_trait_lang_item(clause_def_id, TraitSolverTraitLangItem::Sized) + && cx.is_trait_lang_item(goal_def_id, TraitSolverTraitLangItem::MetaSized)) } if let Some(trait_clause) = assumption.as_trait_clause() @@ -168,8 +168,8 @@ where // PERF(sized-hierarchy): Sizedness supertraits aren't elaborated to improve perf, so // check for a `Sized` subtrait when looking for `MetaSized`. `PointeeSized` bounds // are syntactic sugar for a lack of bounds so don't need this. - if ecx.cx().is_lang_item(goal.predicate.def_id(), TraitSolverLangItem::MetaSized) - && ecx.cx().is_lang_item(trait_clause.def_id(), TraitSolverLangItem::Sized) + if ecx.cx().is_trait_lang_item(goal.predicate.def_id(), TraitSolverTraitLangItem::MetaSized) + && ecx.cx().is_trait_lang_item(trait_clause.def_id(), TraitSolverTraitLangItem::Sized) { let meta_sized_clause = trait_predicate_with_def_id(ecx.cx(), trait_clause, goal.predicate.def_id()); @@ -254,7 +254,7 @@ where ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| { let nested_obligations = cx - .predicates_of(goal.predicate.def_id()) + .predicates_of(goal.predicate.def_id().into()) .iter_instantiated(cx, goal.predicate.trait_ref.args) .map(|p| goal.with(cx, p)); // While you could think of trait aliases to have a single builtin impl @@ -363,7 +363,11 @@ where // A built-in `Fn` impl only holds if the output is sized. // (FIXME: technically we only need to check this if the type is a fn ptr...) let output_is_sized_pred = tupled_inputs_and_output.map_bound(|(_, output)| { - ty::TraitRef::new(cx, cx.require_lang_item(TraitSolverLangItem::Sized), [output]) + ty::TraitRef::new( + cx, + cx.require_trait_lang_item(TraitSolverTraitLangItem::Sized), + [output], + ) }); let pred = tupled_inputs_and_output @@ -405,7 +409,7 @@ where |AsyncCallableRelevantTypes { output_coroutine_ty, .. }| { ty::TraitRef::new( cx, - cx.require_lang_item(TraitSolverLangItem::Sized), + cx.require_trait_lang_item(TraitSolverTraitLangItem::Sized), [output_coroutine_ty], ) }, @@ -748,7 +752,7 @@ where cx, ty::TraitRef::new( cx, - cx.require_lang_item(TraitSolverLangItem::Copy), + cx.require_trait_lang_item(TraitSolverTraitLangItem::Copy), [ty], ), ), @@ -848,7 +852,7 @@ where fn trait_predicate_with_def_id( cx: I, clause: ty::Binder>, - did: I::DefId, + did: I::TraitId, ) -> I::Clause { clause .map_bound(|c| TraitPredicate { @@ -947,7 +951,11 @@ where GoalSource::ImplWhereBound, goal.with( cx, - ty::TraitRef::new(cx, cx.require_lang_item(TraitSolverLangItem::Sized), [a_ty]), + ty::TraitRef::new( + cx, + cx.require_trait_lang_item(TraitSolverTraitLangItem::Sized), + [a_ty], + ), ), ); @@ -972,7 +980,7 @@ where // We may upcast to auto traits that are either explicitly listed in // the object type's bounds, or implied by the principal trait ref's // supertraits. - let a_auto_traits: IndexSet = a_data + let a_auto_traits: IndexSet = a_data .auto_traits() .into_iter() .chain(a_data.principal_def_id().into_iter().flat_map(|principal_def_id| { @@ -1134,7 +1142,7 @@ where cx, ty::TraitRef::new( cx, - cx.require_lang_item(TraitSolverLangItem::Unsize), + cx.require_trait_lang_item(TraitSolverTraitLangItem::Unsize), [a_tail_ty, b_tail_ty], ), ), @@ -1199,7 +1207,10 @@ where // takes precedence over the structural auto trait candidate being // assembled. ty::Coroutine(def_id, _) - if self.cx().is_lang_item(goal.predicate.def_id(), TraitSolverLangItem::Unpin) => + if self.cx().is_trait_lang_item( + goal.predicate.def_id(), + TraitSolverTraitLangItem::Unpin, + ) => { match self.cx().coroutine_movability(def_id) { Movability::Static => Some(Err(NoSolution)), diff --git a/compiler/rustc_type_ir/src/elaborate.rs b/compiler/rustc_type_ir/src/elaborate.rs index dc15cc3eb5539..662ac2db9b691 100644 --- a/compiler/rustc_type_ir/src/elaborate.rs +++ b/compiler/rustc_type_ir/src/elaborate.rs @@ -4,7 +4,7 @@ use smallvec::smallvec; use crate::data_structures::HashSet; use crate::inherent::*; -use crate::lang_items::TraitSolverLangItem; +use crate::lang_items::TraitSolverTraitLangItem; use crate::outlives::{Component, push_outlives_components}; use crate::{self as ty, Interner, Upcast as _}; @@ -140,7 +140,7 @@ impl> Elaborator { // is present. if self.elaborate_sized == ElaborateSized::No && let Some(did) = clause.as_trait_clause().map(|c| c.def_id()) - && self.cx.is_lang_item(did, TraitSolverLangItem::Sized) + && self.cx.is_trait_lang_item(did, TraitSolverTraitLangItem::Sized) { return; } @@ -166,7 +166,7 @@ impl> Elaborator { // Get predicates implied by the trait, or only super predicates if we only care about self predicates. match self.mode { Filter::All => self.extend_deduped( - cx.explicit_implied_predicates_of(data.def_id()) + cx.explicit_implied_predicates_of(data.def_id().into()) .iter_identity() .enumerate() .map(map_to_child_clause), @@ -181,13 +181,15 @@ impl> Elaborator { } // `T: [const] Trait` implies `T: [const] Supertrait`. ty::ClauseKind::HostEffect(data) => self.extend_deduped( - cx.explicit_implied_const_bounds(data.def_id()).iter_identity().map(|trait_ref| { - elaboratable.child( - trait_ref - .to_host_effect_clause(cx, data.constness) - .instantiate_supertrait(cx, bound_clause.rebind(data.trait_ref)), - ) - }), + cx.explicit_implied_const_bounds(data.def_id().into()).iter_identity().map( + |trait_ref| { + elaboratable.child( + trait_ref + .to_host_effect_clause(cx, data.constness) + .instantiate_supertrait(cx, bound_clause.rebind(data.trait_ref)), + ) + }, + ), ), ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(ty_max, r_min)) => { // We know that `T: 'a` for some type `T`. We can @@ -312,8 +314,8 @@ impl> Iterator for Elaborator { /// and to make size estimates for vtable layout computation. pub fn supertrait_def_ids( cx: I, - trait_def_id: I::DefId, -) -> impl Iterator { + trait_def_id: I::TraitId, +) -> impl Iterator { let mut set = HashSet::default(); let mut stack = vec![trait_def_id]; diff --git a/compiler/rustc_type_ir/src/error.rs b/compiler/rustc_type_ir/src/error.rs index e8840bcfaca34..eb5b6b4a1b450 100644 --- a/compiler/rustc_type_ir/src/error.rs +++ b/compiler/rustc_type_ir/src/error.rs @@ -38,7 +38,7 @@ pub enum TypeError { Sorts(ExpectedFound), ArgumentSorts(ExpectedFound, usize), - Traits(ExpectedFound), + Traits(ExpectedFound), VariadicMismatch(ExpectedFound), /// Instantiating a type variable with the given type would have diff --git a/compiler/rustc_type_ir/src/fast_reject.rs b/compiler/rustc_type_ir/src/fast_reject.rs index d88c88fc6f3f5..5a05630e1bd11 100644 --- a/compiler/rustc_type_ir/src/fast_reject.rs +++ b/compiler/rustc_type_ir/src/fast_reject.rs @@ -130,7 +130,7 @@ pub fn simplify_type( ty::RawPtr(_, mutbl) => Some(SimplifiedType::Ptr(mutbl)), ty::Dynamic(trait_info, ..) => match trait_info.principal_def_id() { Some(principal_def_id) if !cx.trait_is_auto(principal_def_id) => { - Some(SimplifiedType::Trait(principal_def_id)) + Some(SimplifiedType::Trait(principal_def_id.into())) } _ => Some(SimplifiedType::MarkerTraitObject), }, diff --git a/compiler/rustc_type_ir/src/inherent.rs b/compiler/rustc_type_ir/src/inherent.rs index 1a6c99ce7dec0..d897b6837c81f 100644 --- a/compiler/rustc_type_ir/src/inherent.rs +++ b/compiler/rustc_type_ir/src/inherent.rs @@ -643,11 +643,11 @@ pub trait DefId: Copy + Debug + Hash + Eq + TypeFoldable { pub trait BoundExistentialPredicates: Copy + Debug + Hash + Eq + Relate + SliceLike>> { - fn principal_def_id(self) -> Option; + fn principal_def_id(self) -> Option; fn principal(self) -> Option>>; - fn auto_traits(self) -> impl IntoIterator; + fn auto_traits(self) -> impl IntoIterator; fn projection_bounds( self, diff --git a/compiler/rustc_type_ir/src/interner.rs b/compiler/rustc_type_ir/src/interner.rs index e3231244577f6..b02d8a3d98cd1 100644 --- a/compiler/rustc_type_ir/src/interner.rs +++ b/compiler/rustc_type_ir/src/interner.rs @@ -8,7 +8,7 @@ use rustc_index::bit_set::DenseBitSet; use crate::fold::TypeFoldable; use crate::inherent::*; use crate::ir_print::IrPrint; -use crate::lang_items::TraitSolverLangItem; +use crate::lang_items::{TraitSolverLangItem, TraitSolverTraitLangItem}; use crate::relate::Relate; use crate::solve::{CanonicalInput, ExternalConstraintsData, PredefinedOpaquesData, QueryResult}; use crate::visit::{Flags, TypeVisitable}; @@ -38,6 +38,7 @@ pub trait Interner: type DefId: DefId; type LocalDefId: Copy + Debug + Hash + Eq + Into + TypeFoldable; + type TraitId: DefId + Into + TryFrom; type Span: Span; type GenericArgs: GenericArgs; @@ -271,7 +272,7 @@ pub trait Interner: fn explicit_super_predicates_of( self, - def_id: Self::DefId, + def_id: Self::TraitId, ) -> ty::EarlyBinder>; fn explicit_implied_predicates_of( @@ -304,17 +305,24 @@ pub trait Interner: fn require_lang_item(self, lang_item: TraitSolverLangItem) -> Self::DefId; + fn require_trait_lang_item(self, lang_item: TraitSolverTraitLangItem) -> Self::TraitId; + fn is_lang_item(self, def_id: Self::DefId, lang_item: TraitSolverLangItem) -> bool; - fn is_default_trait(self, def_id: Self::DefId) -> bool; + fn is_trait_lang_item(self, def_id: Self::TraitId, lang_item: TraitSolverTraitLangItem) + -> bool; + + fn is_default_trait(self, def_id: Self::TraitId) -> bool; fn as_lang_item(self, def_id: Self::DefId) -> Option; + fn as_trait_lang_item(self, def_id: Self::TraitId) -> Option; + fn associated_type_def_ids(self, def_id: Self::DefId) -> impl IntoIterator; fn for_each_relevant_impl( self, - trait_def_id: Self::DefId, + trait_def_id: Self::TraitId, self_ty: Self::Ty, f: impl FnMut(Self::DefId), ); @@ -329,20 +337,20 @@ pub trait Interner: fn impl_polarity(self, impl_def_id: Self::DefId) -> ty::ImplPolarity; - fn trait_is_auto(self, trait_def_id: Self::DefId) -> bool; + fn trait_is_auto(self, trait_def_id: Self::TraitId) -> bool; - fn trait_is_coinductive(self, trait_def_id: Self::DefId) -> bool; + fn trait_is_coinductive(self, trait_def_id: Self::TraitId) -> bool; - fn trait_is_alias(self, trait_def_id: Self::DefId) -> bool; + fn trait_is_alias(self, trait_def_id: Self::TraitId) -> bool; - fn trait_is_dyn_compatible(self, trait_def_id: Self::DefId) -> bool; + fn trait_is_dyn_compatible(self, trait_def_id: Self::TraitId) -> bool; - fn trait_is_fundamental(self, def_id: Self::DefId) -> bool; + fn trait_is_fundamental(self, def_id: Self::TraitId) -> bool; - fn trait_may_be_implemented_via_object(self, trait_def_id: Self::DefId) -> bool; + fn trait_may_be_implemented_via_object(self, trait_def_id: Self::TraitId) -> bool; /// Returns `true` if this is an `unsafe trait`. - fn trait_is_unsafe(self, trait_def_id: Self::DefId) -> bool; + fn trait_is_unsafe(self, trait_def_id: Self::TraitId) -> bool; fn is_impl_trait_in_trait(self, def_id: Self::DefId) -> bool; diff --git a/compiler/rustc_type_ir/src/lang_items.rs b/compiler/rustc_type_ir/src/lang_items.rs index f9994448e282f..25d75706ea135 100644 --- a/compiler/rustc_type_ir/src/lang_items.rs +++ b/compiler/rustc_type_ir/src/lang_items.rs @@ -1,40 +1,46 @@ /// Lang items used by the new trait solver. This can be mapped to whatever internal /// representation of `LangItem`s used in the underlying compiler implementation. pub enum TraitSolverLangItem { + // tidy-alphabetical-start + AsyncFnKindUpvars, + AsyncFnOnceOutput, + CallOnceFuture, + CallRefFuture, + CoroutineReturn, + CoroutineYield, + DynMetadata, + FutureOutput, + Metadata, + Option, + Poll, + // tidy-alphabetical-end +} + +pub enum TraitSolverTraitLangItem { // tidy-alphabetical-start AsyncFn, AsyncFnKindHelper, - AsyncFnKindUpvars, AsyncFnMut, AsyncFnOnce, AsyncFnOnceOutput, AsyncIterator, BikeshedGuaranteedNoDrop, - CallOnceFuture, - CallRefFuture, Clone, Copy, Coroutine, - CoroutineReturn, - CoroutineYield, Destruct, DiscriminantKind, Drop, - DynMetadata, Fn, FnMut, FnOnce, FnPtrTrait, FusedIterator, Future, - FutureOutput, Iterator, MetaSized, - Metadata, - Option, PointeeSized, PointeeTrait, - Poll, Sized, TransmuteTrait, Tuple, diff --git a/compiler/rustc_type_ir/src/predicate.rs b/compiler/rustc_type_ir/src/predicate.rs index b53eb099c4b95..865a3e3935422 100644 --- a/compiler/rustc_type_ir/src/predicate.rs +++ b/compiler/rustc_type_ir/src/predicate.rs @@ -58,7 +58,7 @@ where derive(Decodable_NoContext, Encodable_NoContext, HashStable_NoContext) )] pub struct TraitRef { - pub def_id: I::DefId, + pub def_id: I::TraitId, pub args: I::GenericArgs, /// This field exists to prevent the creation of `TraitRef` without /// calling [`TraitRef::new_from_args`]. @@ -68,32 +68,32 @@ pub struct TraitRef { impl Eq for TraitRef {} impl TraitRef { - pub fn new_from_args(interner: I, trait_def_id: I::DefId, args: I::GenericArgs) -> Self { - interner.debug_assert_args_compatible(trait_def_id, args); + pub fn new_from_args(interner: I, trait_def_id: I::TraitId, args: I::GenericArgs) -> Self { + interner.debug_assert_args_compatible(trait_def_id.into(), args); Self { def_id: trait_def_id, args, _use_trait_ref_new_instead: () } } pub fn new( interner: I, - trait_def_id: I::DefId, + trait_def_id: I::TraitId, args: impl IntoIterator>, ) -> Self { let args = interner.mk_args_from_iter(args.into_iter().map(Into::into)); Self::new_from_args(interner, trait_def_id, args) } - pub fn from_assoc(interner: I, trait_id: I::DefId, args: I::GenericArgs) -> TraitRef { - let generics = interner.generics_of(trait_id); + pub fn from_assoc(interner: I, trait_id: I::TraitId, args: I::GenericArgs) -> TraitRef { + let generics = interner.generics_of(trait_id.into()); TraitRef::new(interner, trait_id, args.iter().take(generics.count())) } /// Returns a `TraitRef` of the form `P0: Foo` where `Pi` /// are the parameters defined on trait. - pub fn identity(interner: I, def_id: I::DefId) -> TraitRef { + pub fn identity(interner: I, def_id: I::TraitId) -> TraitRef { TraitRef::new_from_args( interner, def_id, - I::GenericArgs::identity_for_item(interner, def_id), + I::GenericArgs::identity_for_item(interner, def_id.into()), ) } @@ -116,7 +116,7 @@ impl ty::Binder> { self.map_bound_ref(|tr| tr.self_ty()) } - pub fn def_id(&self) -> I::DefId { + pub fn def_id(&self) -> I::TraitId { self.skip_binder().def_id } @@ -155,7 +155,7 @@ impl TraitPredicate { } } - pub fn def_id(self) -> I::DefId { + pub fn def_id(self) -> I::TraitId { self.trait_ref.def_id } @@ -165,7 +165,7 @@ impl TraitPredicate { } impl ty::Binder> { - pub fn def_id(self) -> I::DefId { + pub fn def_id(self) -> I::TraitId { // Ok to skip binder since trait `DefId` does not care about regions. self.skip_binder().def_id() } @@ -285,7 +285,7 @@ pub enum ExistentialPredicate { /// E.g., `Iterator::Item = T`. Projection(ExistentialProjection), /// E.g., `Send`. - AutoTrait(I::DefId), + AutoTrait(I::TraitId), } impl Eq for ExistentialPredicate {} @@ -301,13 +301,14 @@ impl ty::Binder> { self.rebind(p.with_self_ty(cx, self_ty)).upcast(cx) } ExistentialPredicate::AutoTrait(did) => { - let generics = cx.generics_of(did); + let generics = cx.generics_of(did.into()); let trait_ref = if generics.count() == 1 { ty::TraitRef::new(cx, did, [self_ty]) } else { // If this is an ill-formed auto trait, then synthesize // new error args for the missing generics. - let err_args = GenericArgs::extend_with_error(cx, did, &[self_ty.into()]); + let err_args = + GenericArgs::extend_with_error(cx, did.into(), &[self_ty.into()]); ty::TraitRef::new_from_args(cx, did, err_args) }; self.rebind(trait_ref).upcast(cx) @@ -330,7 +331,7 @@ impl ty::Binder> { derive(Decodable_NoContext, Encodable_NoContext, HashStable_NoContext) )] pub struct ExistentialTraitRef { - pub def_id: I::DefId, + pub def_id: I::TraitId, pub args: I::GenericArgs, /// This field exists to prevent the creation of `ExistentialTraitRef` without /// calling [`ExistentialTraitRef::new_from_args`]. @@ -340,14 +341,14 @@ pub struct ExistentialTraitRef { impl Eq for ExistentialTraitRef {} impl ExistentialTraitRef { - pub fn new_from_args(interner: I, trait_def_id: I::DefId, args: I::GenericArgs) -> Self { - interner.debug_assert_existential_args_compatible(trait_def_id, args); + pub fn new_from_args(interner: I, trait_def_id: I::TraitId, args: I::GenericArgs) -> Self { + interner.debug_assert_existential_args_compatible(trait_def_id.into(), args); Self { def_id: trait_def_id, args, _use_existential_trait_ref_new_instead: () } } pub fn new( interner: I, - trait_def_id: I::DefId, + trait_def_id: I::TraitId, args: impl IntoIterator>, ) -> Self { let args = interner.mk_args_from_iter(args.into_iter().map(Into::into)); @@ -378,7 +379,7 @@ impl ExistentialTraitRef { } impl ty::Binder> { - pub fn def_id(&self) -> I::DefId { + pub fn def_id(&self) -> I::TraitId { self.skip_binder().def_id } @@ -439,7 +440,11 @@ impl ExistentialProjection { let def_id = interner.parent(self.def_id); let args_count = interner.generics_of(def_id).count() - 1; let args = interner.mk_args(&self.args.as_slice()[..args_count]); - ExistentialTraitRef { def_id, args, _use_existential_trait_ref_new_instead: () } + ExistentialTraitRef { + def_id: def_id.try_into().unwrap(), + args, + _use_existential_trait_ref_new_instead: (), + } } pub fn with_self_ty(&self, interner: I, self_ty: I::Ty) -> ProjectionPredicate { @@ -675,7 +680,7 @@ impl AliasTerm { ) } - pub fn trait_def_id(self, interner: I) -> I::DefId { + pub fn trait_def_id(self, interner: I) -> I::TraitId { assert!( matches!( self.kind(interner), @@ -683,7 +688,7 @@ impl AliasTerm { ), "expected a projection" ); - interner.parent(self.def_id) + interner.parent(self.def_id).try_into().unwrap() } /// Extracts the underlying trait reference and own args from this projection. @@ -787,7 +792,7 @@ impl ProjectionPredicate { } } - pub fn trait_def_id(self, interner: I) -> I::DefId { + pub fn trait_def_id(self, interner: I) -> I::TraitId { self.projection_term.trait_def_id(interner) } @@ -799,7 +804,7 @@ impl ProjectionPredicate { impl ty::Binder> { /// Returns the `DefId` of the trait of the associated item being projected. #[inline] - pub fn trait_def_id(&self, cx: I) -> I::DefId { + pub fn trait_def_id(&self, cx: I) -> I::TraitId { self.skip_binder().projection_term.trait_def_id(cx) } @@ -847,7 +852,7 @@ impl NormalizesTo { Self { alias: self.alias.with_replaced_self_ty(interner, self_ty), ..self } } - pub fn trait_def_id(self, interner: I) -> I::DefId { + pub fn trait_def_id(self, interner: I) -> I::TraitId { self.alias.trait_def_id(interner) } @@ -884,13 +889,13 @@ impl HostEffectPredicate { Self { trait_ref: self.trait_ref.with_replaced_self_ty(interner, self_ty), ..self } } - pub fn def_id(self) -> I::DefId { + pub fn def_id(self) -> I::TraitId { self.trait_ref.def_id } } impl ty::Binder> { - pub fn def_id(self) -> I::DefId { + pub fn def_id(self) -> I::TraitId { // Ok to skip binder since trait `DefId` does not care about regions. self.skip_binder().def_id() } diff --git a/compiler/rustc_type_ir/src/predicate_kind.rs b/compiler/rustc_type_ir/src/predicate_kind.rs index ff92a0070cc5b..785d41929cfce 100644 --- a/compiler/rustc_type_ir/src/predicate_kind.rs +++ b/compiler/rustc_type_ir/src/predicate_kind.rs @@ -68,7 +68,7 @@ pub enum PredicateKind { Clause(ClauseKind), /// Trait must be dyn-compatible. - DynCompatible(I::DefId), + DynCompatible(I::TraitId), /// `T1 <: T2` /// diff --git a/compiler/rustc_type_ir/src/solve/mod.rs b/compiler/rustc_type_ir/src/solve/mod.rs index 1497236039f3f..1dc50700c7ad8 100644 --- a/compiler/rustc_type_ir/src/solve/mod.rs +++ b/compiler/rustc_type_ir/src/solve/mod.rs @@ -7,7 +7,7 @@ use derive_where::derive_where; use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContext}; use rustc_type_ir_macros::{Lift_Generic, TypeFoldable_Generic, TypeVisitable_Generic}; -use crate::lang_items::TraitSolverLangItem; +use crate::lang_items::TraitSolverTraitLangItem; use crate::search_graph::PathKind; use crate::{self as ty, Canonical, CanonicalVarValues, Interner, Upcast}; @@ -386,10 +386,10 @@ pub enum SizedTraitKind { impl SizedTraitKind { /// Returns `DefId` of corresponding language item. - pub fn require_lang_item(self, cx: I) -> I::DefId { - cx.require_lang_item(match self { - SizedTraitKind::Sized => TraitSolverLangItem::Sized, - SizedTraitKind::MetaSized => TraitSolverLangItem::MetaSized, + pub fn require_lang_item(self, cx: I) -> I::TraitId { + cx.require_trait_lang_item(match self { + SizedTraitKind::Sized => TraitSolverTraitLangItem::Sized, + SizedTraitKind::MetaSized => TraitSolverTraitLangItem::MetaSized, }) } } diff --git a/tests/ui-fulldeps/internal-lints/import-of-type-ir-traits.rs b/tests/ui-fulldeps/internal-lints/import-of-type-ir-traits.rs index 3fdd65d6c87f1..965fd832722c1 100644 --- a/tests/ui-fulldeps/internal-lints/import-of-type-ir-traits.rs +++ b/tests/ui-fulldeps/internal-lints/import-of-type-ir-traits.rs @@ -8,7 +8,7 @@ extern crate rustc_type_ir; use rustc_type_ir::Interner; -fn foo(cx: I, did: I::DefId) { +fn foo(cx: I, did: I::TraitId) { let _ = cx.trait_is_unsafe(did); //~^ ERROR do not use `rustc_type_ir::Interner` or `rustc_type_ir::InferCtxtLike` unless you're inside of the trait solver }