Skip to content
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
28 changes: 27 additions & 1 deletion 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::{SolverLangItem, SolverTraitLangItem};
use rustc_type_ir::lang_items::{SolverAdtLangItem, SolverLangItem, SolverTraitLangItem};
pub use rustc_type_ir::lift::Lift;
use rustc_type_ir::{
CollectAndApply, Interner, TypeFlags, TypeFoldable, WithCachedTypeInfo, elaborate, search_graph,
Expand Down Expand Up @@ -94,6 +94,13 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
type DefId = DefId;
type LocalDefId = LocalDefId;
type TraitId = DefId;
type ForeignId = DefId;
type FunctionId = DefId;
type ClosureId = DefId;
type CoroutineClosureId = DefId;
type CoroutineId = DefId;
type AdtId = DefId;
type ImplId = DefId;
type Span = Span;

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

fn require_adt_lang_item(self, lang_item: SolverAdtLangItem) -> DefId {
self.require_lang_item(solver_adt_lang_item_to_lang_item(lang_item), DUMMY_SP)
}

fn is_lang_item(self, def_id: DefId, lang_item: SolverLangItem) -> bool {
self.is_lang_item(def_id, solver_lang_item_to_lang_item(lang_item))
}
Expand All @@ -500,6 +511,10 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
self.is_lang_item(def_id, solver_trait_lang_item_to_lang_item(lang_item))
}

fn is_adt_lang_item(self, def_id: DefId, lang_item: SolverAdtLangItem) -> bool {
self.is_lang_item(def_id, solver_adt_lang_item_to_lang_item(lang_item))
}

fn is_default_trait(self, def_id: DefId) -> bool {
self.is_default_trait(def_id)
}
Expand All @@ -512,6 +527,10 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
lang_item_to_solver_trait_lang_item(self.lang_items().from_def_id(def_id)?)
}

fn as_adt_lang_item(self, def_id: DefId) -> Option<SolverAdtLangItem> {
lang_item_to_solver_adt_lang_item(self.lang_items().from_def_id(def_id)?)
}

fn associated_type_def_ids(self, def_id: DefId) -> impl IntoIterator<Item = DefId> {
self.associated_items(def_id)
.in_definition_order()
Expand Down Expand Up @@ -772,6 +791,13 @@ bidirectional_lang_item_map! {
DynMetadata,
FutureOutput,
Metadata,
// tidy-alphabetical-end
}

bidirectional_lang_item_map! {
SolverAdtLangItem, lang_item_to_solver_adt_lang_item, solver_adt_lang_item_to_lang_item;

// tidy-alphabetical-start
Option,
Poll,
// tidy-alphabetical-end
Expand Down
16 changes: 15 additions & 1 deletion compiler/rustc_next_trait_solver/src/coherence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,21 @@ where
}
}
ty::Error(_) => ControlFlow::Break(OrphanCheckEarlyExit::LocalTy(ty)),
ty::Closure(did, ..) | ty::CoroutineClosure(did, ..) | ty::Coroutine(did, ..) => {
ty::Closure(did, ..) => {
if self.def_id_is_local(did) {
ControlFlow::Break(OrphanCheckEarlyExit::LocalTy(ty))
} else {
self.found_non_local_ty(ty)
}
}
ty::CoroutineClosure(did, ..) => {
if self.def_id_is_local(did) {
ControlFlow::Break(OrphanCheckEarlyExit::LocalTy(ty))
} else {
self.found_non_local_ty(ty)
}
}
ty::Coroutine(did, ..) => {
Copy link
Contributor Author

@ChayimFriedman2 ChayimFriedman2 Sep 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only place in the PR I think there is a (slight) regression in code quality. The reason is that now the types of the variables are different. It's possible to avoid if we unify ClosureId, CoroutineId and CoroutineClosureId. This will be slightly worse for r-a and slightly better here. Given that the code duplicated is very small, and for r-a the effects of unifying them will be bigger, I decided to duplicate the code here, but I agree to reverse my decision if the reviewer disagrees.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you do

(ty::Closure(did, ..) if let did = did.into()) |
...

I don't remember if this got implemented - being able to write guards for each pattern. (Even if it is, maybe you need a turbofish here, idk).

You could also remove the duplication by moving this into a closure and then calling check(did.into()) for each arm

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't seem to work: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=33963a324a2ffe51340b0ca533c710ef.

I can extract to a closure if wanted, I just didn't because the duplicated code is really small.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, so guard_patterns seems implemented? But doesn't support if let. Not sure if that's planned or not - sure seems possibly useful.

In this case, duplicating is probably fine, but again would defer to lcnr.

if self.def_id_is_local(did) {
ControlFlow::Break(OrphanCheckEarlyExit::LocalTy(ty))
} else {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_next_trait_solver/src/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub trait SolverDelegate: Deref<Target = Self::Infcx> + Sized {
&self,
goal_trait_ref: ty::TraitRef<Self::Interner>,
trait_assoc_def_id: <Self::Interner as Interner>::DefId,
impl_def_id: <Self::Interner as Interner>::DefId,
impl_def_id: <Self::Interner as Interner>::ImplId,
) -> Result<
Option<<Self::Interner as Interner>::DefId>,
<Self::Interner as Interner>::ErrorGuaranteed,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ where
fn consider_impl_candidate(
ecx: &mut EvalCtxt<'_, D>,
goal: Goal<I, Self>,
impl_def_id: I::DefId,
impl_def_id: I::ImplId,
) -> Result<Candidate<I>, NoSolution>;

/// If the predicate contained an error, we want to avoid emitting unnecessary trait
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ fn coroutine_closure_to_certain_coroutine<I: Interner>(
cx: I,
goal_kind: ty::ClosureKind,
goal_region: I::Region,
def_id: I::DefId,
def_id: I::CoroutineClosureId,
args: ty::CoroutineClosureArgs<I>,
sig: ty::CoroutineClosureSignature<I>,
) -> I::Ty {
Expand All @@ -629,7 +629,7 @@ fn coroutine_closure_to_ambiguous_coroutine<I: Interner>(
cx: I,
goal_kind: ty::ClosureKind,
goal_region: I::Region,
def_id: I::DefId,
def_id: I::CoroutineClosureId,
args: ty::CoroutineClosureArgs<I>,
sig: ty::CoroutineClosureSignature<I>,
) -> I::Ty {
Expand Down Expand Up @@ -664,7 +664,7 @@ fn coroutine_closure_to_ambiguous_coroutine<I: Interner>(
pub(in crate::solve) fn extract_fn_def_from_const_callable<I: Interner>(
cx: I,
self_ty: I::Ty,
) -> Result<(ty::Binder<I, (I::FnInputTys, I::Ty)>, I::DefId, I::GenericArgs), NoSolution> {
) -> Result<(ty::Binder<I, (I::FnInputTys, I::Ty)>, I::FunctionId, I::GenericArgs), NoSolution> {
match self_ty.kind() {
ty::FnDef(def_id, args) => {
let sig = cx.fn_sig(def_id);
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_next_trait_solver/src/solve/effect_goals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ where
fn consider_impl_candidate(
ecx: &mut EvalCtxt<'_, D>,
goal: Goal<I, Self>,
impl_def_id: I::DefId,
impl_def_id: I::ImplId,
) -> Result<Candidate<I>, NoSolution> {
let cx = ecx.cx();

Expand Down Expand Up @@ -152,20 +152,20 @@ where
}

ecx.probe_trait_candidate(CandidateSource::Impl(impl_def_id)).enter(|ecx| {
let impl_args = ecx.fresh_args_for_item(impl_def_id);
let impl_args = ecx.fresh_args_for_item(impl_def_id.into());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accept Into<DefId>?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a specific reason to want that? This will make the method generic, meaning monomorphization costs, and the into() is a very small cost to pay.

ecx.record_impl_args(impl_args);
let impl_trait_ref = impl_trait_ref.instantiate(cx, impl_args);

ecx.eq(goal.param_env, goal.predicate.trait_ref, impl_trait_ref)?;
let where_clause_bounds = cx
.predicates_of(impl_def_id)
.predicates_of(impl_def_id.into())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

.iter_instantiated(cx, impl_args)
.map(|pred| goal.with(cx, pred));
ecx.add_goals(GoalSource::ImplWhereBound, where_clause_bounds);

// For this impl to be `const`, we need to check its `[const]` bounds too.
let const_conditions = cx
.const_conditions(impl_def_id)
.const_conditions(impl_def_id.into())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and same

.iter_instantiated(cx, impl_args)
.map(|bound_trait_ref| {
goal.with(
Expand Down Expand Up @@ -240,7 +240,7 @@ where
ty::TraitRef::new(cx, cx.require_trait_lang_item(SolverTraitLangItem::Sized), [output])
});
let requirements = cx
.const_conditions(def_id)
.const_conditions(def_id.into())
.iter_instantiated(cx, args)
.map(|trait_ref| {
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ where
&self,
goal_trait_ref: ty::TraitRef<I>,
trait_assoc_def_id: I::DefId,
impl_def_id: I::DefId,
impl_def_id: I::ImplId,
) -> Result<Option<I::DefId>, I::ErrorGuaranteed> {
self.delegate.fetch_eligible_assoc_item(goal_trait_ref, trait_assoc_def_id, impl_def_id)
}
Expand Down
21 changes: 11 additions & 10 deletions compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{SolverLangItem, SolverTraitLangItem};
use rustc_type_ir::lang_items::{SolverAdtLangItem, SolverLangItem, SolverTraitLangItem};
use rustc_type_ir::solve::SizedTraitKind;
use rustc_type_ir::{self as ty, Interner, NormalizesTo, PredicateKind, Upcast as _};
use tracing::instrument;
Expand Down Expand Up @@ -193,7 +193,7 @@ where
fn consider_impl_candidate(
ecx: &mut EvalCtxt<'_, D>,
goal: Goal<I, NormalizesTo<I>>,
impl_def_id: I::DefId,
impl_def_id: I::ImplId,
) -> Result<Candidate<I>, NoSolution> {
let cx = ecx.cx();

Expand All @@ -217,13 +217,13 @@ where
};

ecx.probe_trait_candidate(CandidateSource::Impl(impl_def_id)).enter(|ecx| {
let impl_args = ecx.fresh_args_for_item(impl_def_id);
let impl_args = ecx.fresh_args_for_item(impl_def_id.into());
let impl_trait_ref = impl_trait_ref.instantiate(cx, impl_args);

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

let where_clause_bounds = cx
.predicates_of(impl_def_id)
.predicates_of(impl_def_id.into())
.iter_instantiated(cx, impl_args)
.map(|pred| goal.with(cx, pred));
ecx.add_goals(GoalSource::ImplWhereBound, where_clause_bounds);
Expand Down Expand Up @@ -824,10 +824,10 @@ where
// coroutine yield ty `Poll<Option<I>>`.
let wrapped_expected_ty = Ty::new_adt(
cx,
cx.adt_def(cx.require_lang_item(SolverLangItem::Poll)),
cx.adt_def(cx.require_adt_lang_item(SolverAdtLangItem::Poll)),
cx.mk_args(&[Ty::new_adt(
cx,
cx.adt_def(cx.require_lang_item(SolverLangItem::Option)),
cx.adt_def(cx.require_adt_lang_item(SolverAdtLangItem::Option)),
cx.mk_args(&[expected_ty.into()]),
)
.into()]),
Expand Down Expand Up @@ -979,7 +979,7 @@ where
fn translate_args(
&mut self,
goal: Goal<I, ty::NormalizesTo<I>>,
impl_def_id: I::DefId,
impl_def_id: I::ImplId,
impl_args: I::GenericArgs,
impl_trait_ref: rustc_type_ir::TraitRef<I>,
target_container_def_id: I::DefId,
Expand All @@ -988,14 +988,15 @@ where
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 {
} else if target_container_def_id == impl_def_id.into() {
// 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.into(), impl_args)
} else {
let target_args = self.fresh_args_for_item(target_container_def_id);
let target_trait_ref =
cx.impl_trait_ref(target_container_def_id).instantiate(cx, target_args);
let target_trait_ref = cx
.impl_trait_ref(target_container_def_id.try_into().unwrap())
.instantiate(cx, target_args);
// Relate source impl to target impl by equating trait refs.
self.eq(goal.param_env, impl_trait_ref, target_trait_ref)?;
// Also add predicates since they may be needed to constrain the
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_next_trait_solver/src/solve/trait_goals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ where
fn consider_impl_candidate(
ecx: &mut EvalCtxt<'_, D>,
goal: Goal<I, TraitPredicate<I>>,
impl_def_id: I::DefId,
impl_def_id: I::ImplId,
) -> Result<Candidate<I>, NoSolution> {
let cx = ecx.cx();

Expand Down Expand Up @@ -91,13 +91,13 @@ where
};

ecx.probe_trait_candidate(CandidateSource::Impl(impl_def_id)).enter(|ecx| {
let impl_args = ecx.fresh_args_for_item(impl_def_id);
let impl_args = ecx.fresh_args_for_item(impl_def_id.into());
ecx.record_impl_args(impl_args);
let impl_trait_ref = impl_trait_ref.instantiate(cx, impl_args);

ecx.eq(goal.param_env, goal.predicate.trait_ref, impl_trait_ref)?;
let where_clause_bounds = cx
.predicates_of(impl_def_id)
.predicates_of(impl_def_id.into())
.iter_instantiated(cx, impl_args)
.map(|pred| goal.with(cx, pred));
ecx.add_goals(GoalSource::ImplWhereBound, where_clause_bounds);
Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_type_ir/src/fast_reject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub fn simplify_type<I: Interner>(
ty::Int(int_type) => Some(SimplifiedType::Int(int_type)),
ty::Uint(uint_type) => Some(SimplifiedType::Uint(uint_type)),
ty::Float(float_type) => Some(SimplifiedType::Float(float_type)),
ty::Adt(def, _) => Some(SimplifiedType::Adt(def.def_id())),
ty::Adt(def, _) => Some(SimplifiedType::Adt(def.def_id().into())),
ty::Str => Some(SimplifiedType::Str),
ty::Array(..) => Some(SimplifiedType::Array),
ty::Slice(..) => Some(SimplifiedType::Slice),
Expand All @@ -135,11 +135,11 @@ pub fn simplify_type<I: Interner>(
_ => Some(SimplifiedType::MarkerTraitObject),
},
ty::Ref(_, _, mutbl) => Some(SimplifiedType::Ref(mutbl)),
ty::FnDef(def_id, _) | ty::Closure(def_id, _) | ty::CoroutineClosure(def_id, _) => {
Some(SimplifiedType::Closure(def_id))
}
ty::Coroutine(def_id, _) => Some(SimplifiedType::Coroutine(def_id)),
ty::CoroutineWitness(def_id, _) => Some(SimplifiedType::CoroutineWitness(def_id)),
ty::FnDef(def_id, _) => Some(SimplifiedType::Closure(def_id.into())),
ty::Closure(def_id, _) => Some(SimplifiedType::Closure(def_id.into())),
ty::CoroutineClosure(def_id, _) => Some(SimplifiedType::Closure(def_id.into())),
ty::Coroutine(def_id, _) => Some(SimplifiedType::Coroutine(def_id.into())),
ty::CoroutineWitness(def_id, _) => Some(SimplifiedType::CoroutineWitness(def_id.into())),
ty::Never => Some(SimplifiedType::Never),
ty::Tuple(tys) => Some(SimplifiedType::Tuple(tys.len())),
ty::FnPtr(sig_tys, _hdr) => {
Expand All @@ -161,7 +161,7 @@ pub fn simplify_type<I: Interner>(
}
TreatParams::AsRigid | TreatParams::InstantiateWithInfer => None,
},
ty::Foreign(def_id) => Some(SimplifiedType::Foreign(def_id)),
ty::Foreign(def_id) => Some(SimplifiedType::Foreign(def_id.into())),
ty::Error(_) => Some(SimplifiedType::Error),
ty::Bound(..) | ty::Infer(_) => None,
}
Expand Down
30 changes: 22 additions & 8 deletions compiler/rustc_type_ir/src/inherent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub trait Ty<I: Interner<Ty = Self>>:

fn new_adt(interner: I, adt_def: I::AdtDef, args: I::GenericArgs) -> Self;

fn new_foreign(interner: I, def_id: I::DefId) -> Self;
fn new_foreign(interner: I, def_id: I::ForeignId) -> Self;

fn new_dynamic(
interner: I,
Expand All @@ -83,17 +83,21 @@ pub trait Ty<I: Interner<Ty = Self>>:
kind: ty::DynKind,
) -> Self;

fn new_coroutine(interner: I, def_id: I::DefId, args: I::GenericArgs) -> Self;
fn new_coroutine(interner: I, def_id: I::CoroutineId, args: I::GenericArgs) -> Self;

fn new_coroutine_closure(interner: I, def_id: I::DefId, args: I::GenericArgs) -> Self;
fn new_coroutine_closure(
interner: I,
def_id: I::CoroutineClosureId,
args: I::GenericArgs,
) -> Self;

fn new_closure(interner: I, def_id: I::DefId, args: I::GenericArgs) -> Self;
fn new_closure(interner: I, def_id: I::ClosureId, args: I::GenericArgs) -> Self;

fn new_coroutine_witness(interner: I, def_id: I::DefId, args: I::GenericArgs) -> Self;
fn new_coroutine_witness(interner: I, def_id: I::CoroutineId, args: I::GenericArgs) -> Self;

fn new_coroutine_witness_for_coroutine(
interner: I,
def_id: I::DefId,
def_id: I::CoroutineId,
coroutine_args: I::GenericArgs,
) -> Self;

Expand All @@ -112,7 +116,7 @@ pub trait Ty<I: Interner<Ty = Self>>:
It: Iterator<Item = T>,
T: CollectAndApply<Self, Self>;

fn new_fn_def(interner: I, def_id: I::DefId, args: I::GenericArgs) -> Self;
fn new_fn_def(interner: I, def_id: I::FunctionId, args: I::GenericArgs) -> Self;

fn new_fn_ptr(interner: I, sig: ty::Binder<I, ty::FnSig<I>>) -> Self;

Expand Down Expand Up @@ -599,7 +603,7 @@ pub trait ParamLike: Copy + Debug + Hash + Eq {
}

pub trait AdtDef<I: Interner>: Copy + Debug + Hash + Eq {
fn def_id(self) -> I::DefId;
fn def_id(self) -> I::AdtId;

fn is_struct(self) -> bool;

Expand Down Expand Up @@ -646,6 +650,16 @@ pub trait DefId<I: Interner>: Copy + Debug + Hash + Eq + TypeFoldable<I> {
fn as_local(self) -> Option<I::LocalDefId>;
}

pub trait SpecificDefId<I: Interner>:
DefId<I> + Into<I::DefId> + TryFrom<I::DefId, Error: std::fmt::Debug>
{
}

impl<I: Interner, T: DefId<I> + Into<I::DefId> + TryFrom<I::DefId, Error: std::fmt::Debug>>
SpecificDefId<I> for T
{
}

pub trait BoundExistentialPredicates<I: Interner>:
Copy + Debug + Hash + Eq + Relate<I> + SliceLike<Item = ty::Binder<I, ty::ExistentialPredicate<I>>>
{
Expand Down
Loading
Loading