Skip to content

Switch next solver to use a specific associated type for trait def id #145377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 45 additions & 19 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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>;
Expand Down Expand Up @@ -481,19 +482,31 @@ 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 {
self.is_default_trait(def_id)
}

fn as_lang_item(self, def_id: DefId) -> Option<TraitSolverLangItem> {
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<TraitSolverTraitLangItem> {
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<Item = DefId> {
Expand Down Expand Up @@ -724,57 +737,70 @@ 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<TraitSolverLangItem> {
fn $to_solver(lang_item: LangItem) -> Option<$solver_ty> {
Some(match lang_item {
$(LangItem::$name => TraitSolverLangItem::$name,)+
$(LangItem::$name => $solver_ty::$name,)+
_ => return None,
})
}
}
}

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,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_next_trait_solver/src/coherence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<I>) -> bool {
match self.in_crate {
InCrate::Local { .. } => def_id.is_local(),
InCrate::Remote => false,
Expand Down
56 changes: 29 additions & 27 deletions compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::solve::SizedTraitKind;
use rustc_type_ir::{
self as ty, Interner, TypeFlags, TypeFoldable, TypeSuperVisitable, TypeVisitable,
Expand Down Expand Up @@ -52,7 +52,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
Expand Down Expand Up @@ -490,80 +490,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),
Expand All @@ -574,7 +576,7 @@ where

// There may be multiple unsize candidates for a trait with several supertraits:
// `trait Foo: Bar<A> + Bar<B>` and `dyn Foo: Unsize<dyn Bar<_>>`
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));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -466,7 +466,7 @@ pub(in crate::solve) fn extract_tupled_inputs_and_output_from_async_callable<I:
nested.push(
ty::TraitRef::new(
cx,
cx.require_lang_item(TraitSolverLangItem::AsyncFnKindHelper),
cx.require_trait_lang_item(TraitSolverTraitLangItem::AsyncFnKindHelper),
[kind_ty, Ty::from_closure_kind(cx, goal_kind)],
)
.upcast(cx),
Expand Down Expand Up @@ -508,7 +508,7 @@ pub(in crate::solve) fn extract_tupled_inputs_and_output_from_async_callable<I:
let args = args.as_closure();
let bound_sig = args.sig();
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);
// `Closure`s only implement `AsyncFn*` when their return type
// implements `Future`.
let mut nested = vec![
Expand All @@ -526,7 +526,7 @@ pub(in crate::solve) fn extract_tupled_inputs_and_output_from_async_callable<I:
}
} else {
let async_fn_kind_trait_def_id =
cx.require_lang_item(TraitSolverLangItem::AsyncFnKindHelper);
cx.require_trait_lang_item(TraitSolverTraitLangItem::AsyncFnKindHelper);
// When we don't know the closure kind (and therefore also the closure's upvars,
// which are computed at the same time), we must delay the computation of the
// generator's upvars. We do this using the `AsyncFnKindHelper`, which as a trait
Expand Down Expand Up @@ -593,7 +593,7 @@ fn fn_item_to_async_callable<I: Interner>(
bound_sig: ty::Binder<I, ty::FnSig<I>>,
) -> Result<(ty::Binder<I, AsyncCallableRelevantTypes<I>>, Vec<I::Predicate>), 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![
Expand Down Expand Up @@ -744,7 +744,7 @@ pub(in crate::solve) fn const_conditions_for_destruct<I: Interner>(
cx: I,
self_ty: I::Ty,
) -> Result<Vec<ty::TraitRef<I>>, 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.
Expand All @@ -763,7 +763,7 @@ pub(in crate::solve) fn const_conditions_for_destruct<I: Interner>(
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);
}
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 7 additions & 3 deletions compiler/rustc_next_trait_solver/src/solve/effect_goals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_next_trait_solver/src/solve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ where
}
}

fn compute_dyn_compatible_goal(&mut self, trait_def_id: I::DefId) -> QueryResult<I> {
fn compute_dyn_compatible_goal(&mut self, trait_def_id: I::TraitId) -> QueryResult<I> {
if self.cx().trait_is_dyn_compatible(trait_def_id) {
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
} else {
Expand Down
Loading
Loading