diff --git a/crates/hir-def/src/lang_item.rs b/crates/hir-def/src/lang_item.rs index 600f206770..df0705bf90 100644 --- a/crates/hir-def/src/lang_item.rs +++ b/crates/hir-def/src/lang_item.rs @@ -84,6 +84,15 @@ impl LangItemTarget { _ => None, } } + + pub fn as_adt(self) -> Option { + match self { + LangItemTarget::Union(it) => Some(it.into()), + LangItemTarget::EnumId(it) => Some(it.into()), + LangItemTarget::Struct(it) => Some(it.into()), + _ => None, + } + } } /// Salsa query. This will look for lang items in a specific crate. @@ -289,6 +298,10 @@ impl LangItem { lang_item(db, start_crate, self).and_then(|t| t.as_trait()) } + pub fn resolve_adt(self, db: &dyn DefDatabase, start_crate: Crate) -> Option { + lang_item(db, start_crate, self).and_then(|t| t.as_adt()) + } + pub fn resolve_enum(self, db: &dyn DefDatabase, start_crate: Crate) -> Option { lang_item(db, start_crate, self).and_then(|t| t.as_enum()) } diff --git a/crates/hir-ty/src/display.rs b/crates/hir-ty/src/display.rs index fcb79e9ffb..ea8bdf8bcb 100644 --- a/crates/hir-ty/src/display.rs +++ b/crates/hir-ty/src/display.rs @@ -59,7 +59,7 @@ use crate::{ lt_from_placeholder_idx, mir::pad16, next_solver::{ - BoundExistentialPredicate, Ctor, DbInterner, GenericArgs, SolverDefId, + BoundExistentialPredicate, DbInterner, GenericArgs, SolverDefId, mapping::{ ChalkToNextSolver, convert_args_for_result, convert_const_for_result, convert_region_for_result, convert_ty_for_result, @@ -911,14 +911,13 @@ fn render_const_scalar_inner( f.write_str("&")?; render_const_scalar_ns(f, bytes, memory_map, t) } - TyKind::Adt(adt, _) if b.len() == 2 * size_of::() => match adt.def_id() { - SolverDefId::AdtId(hir_def::AdtId::StructId(s)) => { + TyKind::Adt(adt, _) if b.len() == 2 * size_of::() => match adt.def_id().0 { + hir_def::AdtId::StructId(s) => { let data = f.db.struct_signature(s); write!(f, "&{}", data.name.display(f.db, f.edition()))?; Ok(()) } - SolverDefId::AdtId(_) => f.write_str(""), - _ => unreachable!(), + _ => f.write_str(""), }, _ => { let addr = usize::from_le_bytes(match b.try_into() { @@ -966,10 +965,7 @@ fn render_const_scalar_inner( f.write_str(")") } TyKind::Adt(def, args) => { - let def = match def.def_id() { - SolverDefId::AdtId(def) => def, - _ => unreachable!(), - }; + let def = def.def_id().0; let Ok(layout) = f.db.layout_of_adt(def, args, trait_env.clone()) else { return f.write_str(""); }; @@ -1300,12 +1296,7 @@ impl<'db> HirDisplay for crate::next_solver::Ty<'db> { sig.hir_fmt(f)?; } TyKind::FnDef(def, args) => { - let def = match def { - SolverDefId::FunctionId(id) => CallableDefId::FunctionId(id), - SolverDefId::Ctor(Ctor::Enum(e)) => CallableDefId::EnumVariantId(e), - SolverDefId::Ctor(Ctor::Struct(s)) => CallableDefId::StructId(s), - _ => unreachable!(), - }; + let def = def.0; let sig = db .callable_item_signature(def) .substitute(Interner, &convert_args_for_result(interner, args.as_slice())); @@ -1406,10 +1397,7 @@ impl<'db> HirDisplay for crate::next_solver::Ty<'db> { } } TyKind::Adt(def, parameters) => { - let def_id = match def.def_id() { - SolverDefId::AdtId(id) => id, - _ => unreachable!(), - }; + let def_id = def.def_id().0; f.start_location_link(def_id.into()); match f.display_kind { DisplayKind::Diagnostics | DisplayKind::Test => { @@ -1448,7 +1436,7 @@ impl<'db> HirDisplay for crate::next_solver::Ty<'db> { hir_fmt_generics( f, convert_args_for_result(interner, parameters.as_slice()).as_slice(Interner), - def.def_id().try_into().ok(), + Some(def.def_id().0.into()), None, )?; } @@ -1466,13 +1454,9 @@ impl<'db> HirDisplay for crate::next_solver::Ty<'db> { projection_ty.hir_fmt(f)?; } - TyKind::Foreign(type_alias) => { - let alias = match type_alias { - SolverDefId::TypeAliasId(id) => id, - _ => unreachable!(), - }; - let type_alias = db.type_alias_signature(alias); - f.start_location_link(alias.into()); + TyKind::Foreign(alias) => { + let type_alias = db.type_alias_signature(alias.0); + f.start_location_link(alias.0.into()); write!(f, "{}", type_alias.name.display(f.db, f.edition()))?; f.end_location_link(); } @@ -1549,10 +1533,7 @@ impl<'db> HirDisplay for crate::next_solver::Ty<'db> { } } TyKind::Closure(id, substs) => { - let id = match id { - SolverDefId::InternedClosureId(id) => id, - _ => unreachable!(), - }; + let id = id.0; let substs = convert_args_for_result(interner, substs.as_slice()); if f.display_kind.is_source_code() { if !f.display_kind.allows_opaque() { diff --git a/crates/hir-ty/src/layout.rs b/crates/hir-ty/src/layout.rs index f8abb3b7f6..2020a8b34b 100644 --- a/crates/hir-ty/src/layout.rs +++ b/crates/hir-ty/src/layout.rs @@ -25,7 +25,7 @@ use crate::{ consteval_nextsolver::try_const_usize, db::HirDatabase, next_solver::{ - DbInterner, GenericArgs, ParamEnv, SolverDefId, Ty, TyKind, TypingMode, + DbInterner, GenericArgs, ParamEnv, Ty, TyKind, TypingMode, infer::{DbInternerInferExt, traits::ObligationCause}, mapping::{ChalkToNextSolver, convert_args_for_result}, project::solve_normalize::deeply_normalize, @@ -323,14 +323,10 @@ pub fn layout_of_ty_query<'db>( ptr.valid_range_mut().start = 1; Layout::scalar(dl, ptr) } - TyKind::Closure(c, args) => { - let id = match c { - SolverDefId::InternedClosureId(id) => id, - _ => unreachable!(), - }; - let def = db.lookup_intern_closure(id); + TyKind::Closure(id, args) => { + let def = db.lookup_intern_closure(id.0); let infer = db.infer(def.0); - let (captures, _) = infer.closure_info(&id.into()); + let (captures, _) = infer.closure_info(&id.0.into()); let fields = captures .iter() .map(|it| { diff --git a/crates/hir-ty/src/method_resolution.rs b/crates/hir-ty/src/method_resolution.rs index fa80567b1e..3f966694d5 100644 --- a/crates/hir-ty/src/method_resolution.rs +++ b/crates/hir-ty/src/method_resolution.rs @@ -161,10 +161,7 @@ impl TyFingerprint { rustc_ast_ir::Mutability::Mut => TyFingerprint::RawPtr(Mutability::Mut), rustc_ast_ir::Mutability::Not => TyFingerprint::RawPtr(Mutability::Not), }, - TyKind::Foreign(def) => { - let SolverDefId::TypeAliasId(def) = def else { unreachable!() }; - TyFingerprint::ForeignType(crate::to_foreign_def_id(def)) - } + TyKind::Foreign(def) => TyFingerprint::ForeignType(crate::to_foreign_def_id(def.0)), TyKind::Dynamic(bounds, _, _) => { let trait_ref = bounds .as_slice() diff --git a/crates/hir-ty/src/mir/eval.rs b/crates/hir-ty/src/mir/eval.rs index c60ace85be..ca7467d385 100644 --- a/crates/hir-ty/src/mir/eval.rs +++ b/crates/hir-ty/src/mir/eval.rs @@ -42,7 +42,7 @@ use crate::{ layout::{Layout, LayoutError, RustcEnumVariantIdx}, method_resolution::{is_dyn_method, lookup_impl_const}, next_solver::{ - Ctor, DbInterner, SolverDefId, + DbInterner, mapping::{ChalkToNextSolver, convert_args_for_result, convert_ty_for_result}, }, static_lifetime, @@ -2366,8 +2366,8 @@ impl<'db> Evaluator<'db> { let new_id = self.vtable_map.id(ty); self.write_memory(addr, &new_id.to_le_bytes())?; } - TyKind::Adt(id, args) => match id.def_id() { - SolverDefId::AdtId(AdtId::StructId(s)) => { + TyKind::Adt(id, args) => match id.def_id().0 { + AdtId::StructId(s) => { for (i, (_, ty)) in self.db.field_types_ns(s.into()).iter().enumerate() { let offset = layout.fields.offset(i).bytes_usize(); let ty = ty.instantiate(interner, args); @@ -2380,8 +2380,8 @@ impl<'db> Evaluator<'db> { )?; } } - SolverDefId::AdtId(AdtId::UnionId(_)) => (), - SolverDefId::AdtId(AdtId::EnumId(e)) => { + AdtId::UnionId(_) => (), + AdtId::EnumId(e) => { if let Some((ev, layout)) = detect_variant_from_bytes( &layout, self.db, @@ -2402,7 +2402,6 @@ impl<'db> Evaluator<'db> { } } } - _ => unreachable!(), }, TyKind::Tuple(tys) => { for (id, ty) in tys.iter().enumerate() { @@ -2472,38 +2471,24 @@ impl<'db> Evaluator<'db> { let interner = DbInterner::new_with(self.db, None, None); use rustc_type_ir::TyKind; match next_ty.kind() { - TyKind::FnDef(def, generic_args) => { - let def = match def { - SolverDefId::FunctionId(id) => CallableDefId::FunctionId(id), - SolverDefId::Ctor(Ctor::Struct(s)) => CallableDefId::StructId(s), - SolverDefId::Ctor(Ctor::Enum(e)) => CallableDefId::EnumVariantId(e), - _ => unreachable!(), - }; - self.exec_fn_def( - def, - &convert_args_for_result(interner, generic_args.as_slice()), - destination, - args, - locals, - target_bb, - span, - ) - } - TyKind::Closure(id, generic_args) => { - let id = match id { - SolverDefId::InternedClosureId(id) => id, - _ => unreachable!(), - }; - self.exec_closure( - id.into(), - bytes.slice(0..0), - &convert_args_for_result(interner, generic_args.as_slice()), - destination, - args, - locals, - span, - ) - } + TyKind::FnDef(def, generic_args) => self.exec_fn_def( + def.0, + &convert_args_for_result(interner, generic_args.as_slice()), + destination, + args, + locals, + target_bb, + span, + ), + TyKind::Closure(id, generic_args) => self.exec_closure( + id.0.into(), + bytes.slice(0..0), + &convert_args_for_result(interner, generic_args.as_slice()), + destination, + args, + locals, + span, + ), _ => Err(MirEvalError::InternalError("function pointer to non function".into())), } } diff --git a/crates/hir-ty/src/next_solver/def_id.rs b/crates/hir-ty/src/next_solver/def_id.rs index 8bbc6e3370..a9c572d3f3 100644 --- a/crates/hir-ty/src/next_solver/def_id.rs +++ b/crates/hir-ty/src/next_solver/def_id.rs @@ -1,8 +1,8 @@ //! Definition of `SolverDefId` use hir_def::{ - AdtId, ConstId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, StaticId, StructId, - TraitId, TypeAliasId, UnionId, + AdtId, CallableDefId, ConstId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, + StaticId, StructId, TraitId, TypeAliasId, UnionId, }; use rustc_type_ir::inherent; use stdx::impl_from; @@ -42,7 +42,8 @@ impl_from!( TypeAliasId, InternedClosureId, InternedCoroutineId, - InternedOpaqueTyId + InternedOpaqueTyId, + Ctor for SolverDefId ); @@ -145,3 +146,59 @@ macro_rules! declare_id_wrapper { } declare_id_wrapper!(TraitIdWrapper, TraitId); +declare_id_wrapper!(TypeAliasIdWrapper, TypeAliasId); +declare_id_wrapper!(ClosureIdWrapper, InternedClosureId); +declare_id_wrapper!(CoroutineIdWrapper, InternedCoroutineId); +declare_id_wrapper!(AdtIdWrapper, AdtId); +declare_id_wrapper!(ImplIdWrapper, ImplId); + +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +pub struct CallableIdWrapper(pub CallableDefId); + +impl std::fmt::Debug for CallableIdWrapper { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Debug::fmt(&self.0, f) + } +} +impl From for CallableDefId { + #[inline] + fn from(value: CallableIdWrapper) -> CallableDefId { + value.0 + } +} +impl From for CallableIdWrapper { + #[inline] + fn from(value: CallableDefId) -> CallableIdWrapper { + Self(value) + } +} +impl From for SolverDefId { + #[inline] + fn from(value: CallableIdWrapper) -> SolverDefId { + match value.0 { + CallableDefId::FunctionId(it) => it.into(), + CallableDefId::StructId(it) => Ctor::Struct(it).into(), + CallableDefId::EnumVariantId(it) => Ctor::Enum(it).into(), + } + } +} +impl TryFrom for CallableIdWrapper { + type Error = (); + #[inline] + fn try_from(value: SolverDefId) -> Result { + match value { + SolverDefId::FunctionId(it) => Ok(Self(it.into())), + SolverDefId::Ctor(Ctor::Struct(it)) => Ok(Self(it.into())), + SolverDefId::Ctor(Ctor::Enum(it)) => Ok(Self(it.into())), + _ => Err(()), + } + } +} +impl<'db> inherent::DefId> for CallableIdWrapper { + fn as_local(self) -> Option { + Some(self.into()) + } + fn is_local(self) -> bool { + true + } +} diff --git a/crates/hir-ty/src/next_solver/infer/canonical/canonicalizer.rs b/crates/hir-ty/src/next_solver/infer/canonical/canonicalizer.rs index ffb9c076fa..beaac11a2d 100644 --- a/crates/hir-ty/src/next_solver/infer/canonical/canonicalizer.rs +++ b/crates/hir-ty/src/next_solver/infer/canonical/canonicalizer.rs @@ -400,7 +400,7 @@ impl<'cx, 'db> TypeFolder> for Canonicalizer<'cx, 'db> { TyKind::Infer(IntVar(vid)) => { let nt = self.infcx.opportunistic_resolve_int_var(vid); if nt != t { - return self.fold_ty(nt); + self.fold_ty(nt) } else { self.canonicalize_ty_var(CanonicalVarKind::Int, t) } @@ -408,7 +408,7 @@ impl<'cx, 'db> TypeFolder> for Canonicalizer<'cx, 'db> { TyKind::Infer(FloatVar(vid)) => { let nt = self.infcx.opportunistic_resolve_float_var(vid); if nt != t { - return self.fold_ty(nt); + self.fold_ty(nt) } else { self.canonicalize_ty_var(CanonicalVarKind::Float, t) } diff --git a/crates/hir-ty/src/next_solver/infer/canonical/mod.rs b/crates/hir-ty/src/next_solver/infer/canonical/mod.rs index 8db4320acc..d0669f5c3b 100644 --- a/crates/hir-ty/src/next_solver/infer/canonical/mod.rs +++ b/crates/hir-ty/src/next_solver/infer/canonical/mod.rs @@ -82,9 +82,7 @@ impl<'db> InferCtxt<'db> { let var_values = CanonicalVarValues::instantiate( self.interner, canonical.variables, - |var_values, info| { - self.instantiate_canonical_var(info, &var_values, |ui| universes[ui]) - }, + |var_values, info| self.instantiate_canonical_var(info, var_values, |ui| universes[ui]), ); let result = canonical.instantiate(self.interner, &var_values); (result, var_values) diff --git a/crates/hir-ty/src/next_solver/interner.rs b/crates/hir-ty/src/next_solver/interner.rs index 5709beaefc..0f512fdaf8 100644 --- a/crates/hir-ty/src/next_solver/interner.rs +++ b/crates/hir-ty/src/next_solver/interner.rs @@ -19,7 +19,7 @@ use rustc_type_ir::error::TypeError; use rustc_type_ir::inherent::{ AdtDef as _, GenericArgs as _, GenericsOf, IntoKind, SliceLike as _, Span as _, }; -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::{ AliasTerm, AliasTermKind, AliasTy, AliasTyKind, EarlyBinder, FlagComputation, Flags, @@ -47,8 +47,9 @@ use crate::method_resolution::{ALL_FLOAT_FPS, ALL_INT_FPS, TyFingerprint}; use crate::next_solver::infer::InferCtxt; use crate::next_solver::util::{ContainsTypeErrors, explicit_item_bounds, for_trait_impls}; use crate::next_solver::{ - BoundConst, CanonicalVarKind, FxIndexMap, InternedWrapperNoDebug, RegionAssumptions, - SolverContext, SolverDefIds, TraitIdWrapper, + AdtIdWrapper, BoundConst, CallableIdWrapper, CanonicalVarKind, ClosureIdWrapper, + CoroutineIdWrapper, FxIndexMap, ImplIdWrapper, InternedWrapperNoDebug, RegionAssumptions, + SolverContext, SolverDefIds, TraitIdWrapper, TypeAliasIdWrapper, }; use crate::{ConstScalar, FnAbi, Interner, db::HirDatabase}; @@ -609,8 +610,8 @@ impl AdtDef { } impl<'db> inherent::AdtDef> for AdtDef { - fn def_id(self) -> as rustc_type_ir::Interner>::DefId { - SolverDefId::AdtId(self.inner().id) + fn def_id(self) -> AdtIdWrapper { + self.inner().id.into() } fn is_struct(self) -> bool { @@ -889,6 +890,13 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> { type LocalDefId = SolverDefId; type LocalDefIds = SolverDefIds; type TraitId = TraitIdWrapper; + type ForeignId = TypeAliasIdWrapper; + type FunctionId = CallableIdWrapper; + type ClosureId = ClosureIdWrapper; + type CoroutineClosureId = CoroutineIdWrapper; + type CoroutineId = CoroutineIdWrapper; + type AdtId = AdtIdWrapper; + type ImplId = ImplIdWrapper; type Span = Span; type GenericArgs = GenericArgs<'db>; @@ -1103,12 +1111,8 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> { self.db().ty_ns(def_id) } - fn adt_def(self, adt_def_id: Self::DefId) -> Self::AdtDef { - let def_id = match adt_def_id { - SolverDefId::AdtId(adt_id) => adt_id, - _ => panic!("Invalid DefId passed to adt_def"), - }; - AdtDef::new(def_id, self) + fn adt_def(self, def_id: Self::AdtId) -> Self::AdtDef { + AdtDef::new(def_id.0, self) } fn alias_ty_kind(self, alias: rustc_type_ir::AliasTy) -> AliasTyKind { @@ -1219,24 +1223,16 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> { fn fn_sig( self, - def_id: Self::DefId, + def_id: Self::FunctionId, ) -> EarlyBinder>> { - let id = match def_id { - SolverDefId::FunctionId(id) => CallableDefId::FunctionId(id), - SolverDefId::Ctor(ctor) => match ctor { - super::Ctor::Struct(struct_id) => CallableDefId::StructId(struct_id), - super::Ctor::Enum(enum_variant_id) => CallableDefId::EnumVariantId(enum_variant_id), - }, - def => unreachable!("{:?}", def), - }; - self.db().callable_item_signature_ns(id) + self.db().callable_item_signature_ns(def_id.0) } - fn coroutine_movability(self, def_id: Self::DefId) -> rustc_ast_ir::Movability { + fn coroutine_movability(self, def_id: Self::CoroutineId) -> rustc_ast_ir::Movability { unimplemented!() } - fn coroutine_for_closure(self, def_id: Self::DefId) -> Self::DefId { + fn coroutine_for_closure(self, def_id: Self::CoroutineId) -> Self::CoroutineId { unimplemented!() } @@ -1361,13 +1357,9 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> { fn impl_super_outlives( self, - impl_def_id: Self::DefId, + impl_id: Self::ImplId, ) -> EarlyBinder> { - let impl_id = match impl_def_id { - SolverDefId::ImplId(id) => id, - _ => unreachable!(), - }; - let trait_ref = self.db().impl_trait_ns(impl_id).expect("expected an impl of trait"); + let trait_ref = self.db().impl_trait_ns(impl_id.0).expect("expected an impl of trait"); trait_ref.map_bound(|trait_ref| { let clause: Clause<'_> = trait_ref.upcast(self); Clauses::new_from_iter( @@ -1392,7 +1384,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> { EarlyBinder::bind([unimplemented!()]) } - fn has_target_features(self, def_id: Self::DefId) -> bool { + fn has_target_features(self, def_id: Self::FunctionId) -> bool { false } @@ -1407,8 +1399,6 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> { SolverLangItem::DynMetadata => LangItem::DynMetadata, SolverLangItem::FutureOutput => LangItem::FutureOutput, SolverLangItem::Metadata => LangItem::Metadata, - SolverLangItem::Option => LangItem::Option, - SolverLangItem::Poll => LangItem::Poll, }; let target = hir_def::lang_item::lang_item( self.db(), @@ -1468,24 +1458,33 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> { .into() } - #[allow(clippy::match_like_matches_macro)] - fn is_lang_item(self, def_id: Self::DefId, lang_item: SolverLangItem) -> bool { - use SolverLangItem::*; + fn require_adt_lang_item(self, lang_item: SolverAdtLangItem) -> AdtIdWrapper { + let lang_item = match lang_item { + SolverAdtLangItem::Option => LangItem::Option, + SolverAdtLangItem::Poll => LangItem::Poll, + }; + lang_item + .resolve_adt(self.db(), self.krate.expect("Must have self.krate")) + .unwrap_or_else(|| panic!("Lang item {lang_item:?} required but not found.")) + .into() + } - // FIXME: derive PartialEq on SolverLangItem + fn is_lang_item(self, def_id: Self::DefId, lang_item: SolverLangItem) -> bool { self.as_lang_item(def_id) .map_or(false, |l| std::mem::discriminant(&l) == std::mem::discriminant(&lang_item)) } - #[allow(clippy::match_like_matches_macro)] fn is_trait_lang_item(self, def_id: Self::TraitId, lang_item: SolverTraitLangItem) -> bool { - use SolverTraitLangItem::*; - - // FIXME: derive PartialEq on SolverTraitLangItem self.as_trait_lang_item(def_id) .map_or(false, |l| std::mem::discriminant(&l) == std::mem::discriminant(&lang_item)) } + fn is_adt_lang_item(self, def_id: Self::AdtId, lang_item: SolverAdtLangItem) -> bool { + // FIXME: derive PartialEq on SolverTraitLangItem + self.as_adt_lang_item(def_id) + .map_or(false, |l| std::mem::discriminant(&l) == std::mem::discriminant(&lang_item)) + } + fn as_lang_item(self, def_id: Self::DefId) -> Option { let def_id: AttrDefId = match def_id { SolverDefId::TraitId(id) => id.into(), @@ -1505,9 +1504,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> { DynMetadata, CoroutineReturn, CoroutineYield, - Poll, FutureOutput, - Option, AsyncFnOnceOutput, CallRefFuture, CallOnceFuture, @@ -1556,6 +1553,19 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> { ) } + fn as_adt_lang_item(self, def_id: Self::AdtId) -> Option { + let def_id: AttrDefId = def_id.0.into(); + let lang_item = self.db().lang_attr(def_id)?; + as_lang_item!( + SolverAdtLangItem, lang_item; + + ignore = {} + + Option, + Poll, + ) + } + fn associated_type_def_ids(self, def_id: Self::DefId) -> impl IntoIterator { let trait_ = match def_id { SolverDefId::TraitId(id) => id, @@ -1568,7 +1578,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> { self, trait_: Self::TraitId, self_ty: Self::Ty, - mut f: impl FnMut(Self::DefId), + mut f: impl FnMut(Self::ImplId), ) { let trait_ = trait_.0; let self_ty_fp = TyFingerprint::for_trait_impl_ns(&self_ty); @@ -1595,7 +1605,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> { continue; } - f(SolverDefId::ImplId(i)); + f(i.into()); } ControlFlow::Continue(()) }, @@ -1618,7 +1628,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> { continue; } - f(SolverDefId::ImplId(i)); + f(i.into()); } } ControlFlow::Continue(()) @@ -1632,7 +1642,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> { true } - fn impl_is_default(self, impl_def_id: Self::DefId) -> bool { + fn impl_is_default(self, impl_def_id: Self::ImplId) -> bool { // FIXME false } @@ -1640,26 +1650,16 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> { #[tracing::instrument(skip(self), ret)] fn impl_trait_ref( self, - impl_def_id: Self::DefId, + impl_id: Self::ImplId, ) -> EarlyBinder> { - let impl_id = match impl_def_id { - SolverDefId::ImplId(id) => id, - _ => panic!("Unexpected SolverDefId in impl_trait_ref"), - }; - let db = self.db(); - - db.impl_trait_ns(impl_id) + db.impl_trait_ns(impl_id.0) // ImplIds for impls where the trait ref can't be resolved should never reach trait solving .expect("invalid impl passed to trait solver") } - fn impl_polarity(self, impl_def_id: Self::DefId) -> rustc_type_ir::ImplPolarity { - let impl_id = match impl_def_id { - SolverDefId::ImplId(id) => id, - _ => unreachable!(), - }; - let impl_data = self.db().impl_signature(impl_id); + fn impl_polarity(self, impl_id: Self::ImplId) -> rustc_type_ir::ImplPolarity { + let impl_data = self.db().impl_signature(impl_id.0); if impl_data.flags.contains(ImplFlags::NEGATIVE) { ImplPolarity::Negative } else { @@ -1701,33 +1701,29 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> { panic!("Bug encountered in next-trait-solver.") } - fn is_general_coroutine(self, coroutine_def_id: Self::DefId) -> bool { + fn is_general_coroutine(self, coroutine_def_id: Self::CoroutineId) -> bool { // FIXME(next-solver) true } - fn coroutine_is_async(self, coroutine_def_id: Self::DefId) -> bool { + fn coroutine_is_async(self, coroutine_def_id: Self::CoroutineId) -> bool { // FIXME(next-solver) true } - fn coroutine_is_gen(self, coroutine_def_id: Self::DefId) -> bool { + fn coroutine_is_gen(self, coroutine_def_id: Self::CoroutineId) -> bool { // FIXME(next-solver) false } - fn coroutine_is_async_gen(self, coroutine_def_id: Self::DefId) -> bool { + fn coroutine_is_async_gen(self, coroutine_def_id: Self::CoroutineId) -> bool { // FIXME(next-solver) false } - fn unsizing_params_for_adt(self, adt_def_id: Self::DefId) -> Self::UnsizingParams { - let id = match adt_def_id { - SolverDefId::AdtId(id) => id, - _ => unreachable!(), - }; - let def = AdtDef::new(id, self); - let num_params = self.generics_of(adt_def_id).count(); + fn unsizing_params_for_adt(self, id: Self::AdtId) -> Self::UnsizingParams { + let def = AdtDef::new(id.0, self); + let num_params = self.generics_of(id.into()).count(); let maybe_unsizing_param_idx = |arg: GenericArg<'db>| match arg.kind() { GenericArgKind::Type(ty) => match ty.kind() { @@ -1835,15 +1831,15 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> { EarlyBinder::bind([]) } - fn fn_is_const(self, def_id: Self::DefId) -> bool { - let id = match def_id { - SolverDefId::FunctionId(id) => id, - _ => unreachable!(), + fn fn_is_const(self, id: Self::FunctionId) -> bool { + let id = match id.0 { + CallableDefId::FunctionId(id) => id, + _ => return false, }; self.db().function_signature(id).flags.contains(FnFlags::CONST) } - fn impl_is_const(self, def_id: Self::DefId) -> bool { + fn impl_is_const(self, def_id: Self::ImplId) -> bool { false } @@ -1877,7 +1873,7 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> { fn coroutine_hidden_types( self, - def_id: Self::DefId, + def_id: Self::CoroutineId, ) -> EarlyBinder>> { // FIXME(next-solver) @@ -1896,11 +1892,11 @@ impl<'db> rustc_type_ir::Interner for DbInterner<'db> { self.db().trait_signature(trait_.0).flags.contains(TraitFlags::UNSAFE) } - fn impl_self_is_guaranteed_unsized(self, def_id: Self::DefId) -> bool { + fn impl_self_is_guaranteed_unsized(self, def_id: Self::ImplId) -> bool { false } - fn impl_specializes(self, impl_def_id: Self::DefId, victim_def_id: Self::DefId) -> bool { + fn impl_specializes(self, impl_def_id: Self::ImplId, victim_def_id: Self::ImplId) -> bool { false } @@ -2020,6 +2016,12 @@ macro_rules! TrivialTypeTraversalImpls { TrivialTypeTraversalImpls! { SolverDefId, TraitIdWrapper, + TypeAliasIdWrapper, + CallableIdWrapper, + ClosureIdWrapper, + CoroutineIdWrapper, + AdtIdWrapper, + ImplIdWrapper, Pattern<'db>, Safety, FnAbi, diff --git a/crates/hir-ty/src/next_solver/mapping.rs b/crates/hir-ty/src/next_solver/mapping.rs index 11bc6e6abe..ce56282a00 100644 --- a/crates/hir-ty/src/next_solver/mapping.rs +++ b/crates/hir-ty/src/next_solver/mapping.rs @@ -243,12 +243,10 @@ impl<'db> ChalkToNextSolver<'db, Ty<'db>> for chalk_ir::Ty { } chalk_ir::TyKind::FnDef(fn_def_id, substitution) => { let def_id = CallableDefId::from_chalk(interner.db(), *fn_def_id); - let id: SolverDefId = match def_id { - CallableDefId::FunctionId(id) => id.into(), - CallableDefId::StructId(id) => SolverDefId::Ctor(Ctor::Struct(id)), - CallableDefId::EnumVariantId(id) => SolverDefId::Ctor(Ctor::Enum(id)), - }; - rustc_type_ir::TyKind::FnDef(id, substitution.to_nextsolver(interner)) + rustc_type_ir::TyKind::FnDef( + def_id.into(), + substitution.to_nextsolver(interner), + ) } chalk_ir::TyKind::Str => rustc_type_ir::TyKind::Str, chalk_ir::TyKind::Never => rustc_type_ir::TyKind::Never, @@ -271,7 +269,7 @@ impl<'db> ChalkToNextSolver<'db, Ty<'db>> for chalk_ir::Ty { ) } chalk_ir::TyKind::Foreign(foreign_def_id) => rustc_type_ir::TyKind::Foreign( - SolverDefId::TypeAliasId(crate::from_foreign_def_id(*foreign_def_id)), + crate::from_foreign_def_id(*foreign_def_id).into(), ), chalk_ir::TyKind::Error => rustc_type_ir::TyKind::Error(ErrorGuaranteed), chalk_ir::TyKind::Dyn(dyn_ty) => { @@ -1511,13 +1509,7 @@ pub(crate) fn convert_ty_for_result<'db>(interner: DbInterner<'db>, ty: Ty<'db>) TyKind::Slice(ty) } - rustc_type_ir::TyKind::Foreign(foreign) => { - let def_id = match foreign { - SolverDefId::TypeAliasId(id) => id, - _ => unreachable!(), - }; - TyKind::Foreign(to_foreign_def_id(def_id)) - } + rustc_type_ir::TyKind::Foreign(foreign) => TyKind::Foreign(to_foreign_def_id(foreign.0)), rustc_type_ir::TyKind::Pat(_, _) => unimplemented!(), rustc_type_ir::TyKind::RawPtr(ty, mutability) => { let mutability = match mutability { @@ -1528,40 +1520,22 @@ pub(crate) fn convert_ty_for_result<'db>(interner: DbInterner<'db>, ty: Ty<'db>) TyKind::Raw(mutability, ty) } rustc_type_ir::TyKind::FnDef(def_id, args) => { - let id = match def_id { - SolverDefId::FunctionId(id) => CallableDefId::FunctionId(id), - SolverDefId::Ctor(Ctor::Struct(id)) => CallableDefId::StructId(id), - SolverDefId::Ctor(Ctor::Enum(id)) => CallableDefId::EnumVariantId(id), - _ => unreachable!(), - }; let subst = convert_args_for_result(interner, args.as_slice()); - TyKind::FnDef(id.to_chalk(interner.db()), subst) + TyKind::FnDef(def_id.0.to_chalk(interner.db()), subst) } rustc_type_ir::TyKind::Closure(def_id, args) => { - let id = match def_id { - SolverDefId::InternedClosureId(id) => id, - _ => unreachable!(), - }; let subst = convert_args_for_result(interner, args.as_slice()); - TyKind::Closure(id.into(), subst) + TyKind::Closure(def_id.0.into(), subst) } rustc_type_ir::TyKind::CoroutineClosure(_, _) => unimplemented!(), rustc_type_ir::TyKind::Coroutine(def_id, args) => { - let id = match def_id { - SolverDefId::InternedCoroutineId(id) => id, - _ => unreachable!(), - }; let subst = convert_args_for_result(interner, args.as_slice()); - TyKind::Coroutine(id.into(), subst) + TyKind::Coroutine(def_id.0.into(), subst) } rustc_type_ir::TyKind::CoroutineWitness(def_id, args) => { - let id = match def_id { - SolverDefId::InternedCoroutineId(id) => id, - _ => unreachable!(), - }; let subst = convert_args_for_result(interner, args.as_slice()); - TyKind::CoroutineWitness(id.into(), subst) + TyKind::CoroutineWitness(def_id.0.into(), subst) } rustc_type_ir::TyKind::UnsafeBinder(_) => unimplemented!(), diff --git a/crates/hir-ty/src/next_solver/solver.rs b/crates/hir-ty/src/next_solver/solver.rs index dc5073305c..c7591c0c77 100644 --- a/crates/hir-ty/src/next_solver/solver.rs +++ b/crates/hir-ty/src/next_solver/solver.rs @@ -9,8 +9,8 @@ use rustc_type_ir::{ solve::{Certainty, NoSolution}, }; -use crate::next_solver::CanonicalVarKind; use crate::next_solver::mapping::NextSolverToChalk; +use crate::next_solver::{CanonicalVarKind, ImplIdWrapper}; use crate::{ TraitRefExt, db::HirDatabase, @@ -148,12 +148,8 @@ impl<'db> SolverDelegate for SolverContext<'db> { &self, goal_trait_ref: rustc_type_ir::TraitRef, trait_assoc_def_id: ::DefId, - impl_def_id: ::DefId, + impl_id: ImplIdWrapper, ) -> Result::DefId>, ErrorGuaranteed> { - let impl_id = match impl_def_id { - SolverDefId::ImplId(id) => id, - _ => panic!("Unexpected SolverDefId"), - }; let trait_assoc_id = match trait_assoc_def_id { SolverDefId::TypeAliasId(id) => id, _ => panic!("Unexpected SolverDefId"), @@ -162,7 +158,7 @@ impl<'db> SolverDelegate for SolverContext<'db> { .0 .interner .db() - .impl_trait(impl_id) + .impl_trait(impl_id.0) // ImplIds for impls where the trait ref can't be resolved should never reach solver .expect("invalid impl passed to next-solver") .into_value_and_skipped_binders() @@ -170,7 +166,7 @@ impl<'db> SolverDelegate for SolverContext<'db> { let trait_ = trait_ref.hir_trait_id(); let trait_data = trait_.trait_items(self.0.interner.db()); let id = - impl_id.impl_items(self.0.interner.db()).items.iter().find_map(|item| -> Option<_> { + impl_id.0.impl_items(self.0.interner.db()).items.iter().find_map(|item| -> Option<_> { match item { (_, AssocItemId::TypeAliasId(type_alias)) => { let name = &self.0.interner.db().type_alias_signature(*type_alias).name; diff --git a/crates/hir-ty/src/next_solver/ty.rs b/crates/hir-ty/src/next_solver/ty.rs index 4794e2d604..e1c29160ad 100644 --- a/crates/hir-ty/src/next_solver/ty.rs +++ b/crates/hir-ty/src/next_solver/ty.rs @@ -20,7 +20,9 @@ use rustc_type_ir::{ use salsa::plumbing::{AsId, FromId}; use smallvec::SmallVec; -use crate::next_solver::GenericArg; +use crate::next_solver::{ + CallableIdWrapper, ClosureIdWrapper, CoroutineIdWrapper, GenericArg, TypeAliasIdWrapper, +}; use crate::{ db::HirDatabase, interner::InternedWrapperNoDebug, @@ -599,10 +601,7 @@ impl<'db> rustc_type_ir::inherent::Ty> for Ty<'db> { Ty::new(interner, TyKind::Adt(adt_def, args)) } - fn new_foreign( - interner: DbInterner<'db>, - def_id: as rustc_type_ir::Interner>::DefId, - ) -> Self { + fn new_foreign(interner: DbInterner<'db>, def_id: TypeAliasIdWrapper) -> Self { Ty::new(interner, TyKind::Foreign(def_id)) } @@ -617,7 +616,7 @@ impl<'db> rustc_type_ir::inherent::Ty> for Ty<'db> { fn new_coroutine( interner: DbInterner<'db>, - def_id: as rustc_type_ir::Interner>::DefId, + def_id: CoroutineIdWrapper, args: as rustc_type_ir::Interner>::GenericArgs, ) -> Self { Ty::new(interner, TyKind::Coroutine(def_id, args)) @@ -625,7 +624,7 @@ impl<'db> rustc_type_ir::inherent::Ty> for Ty<'db> { fn new_coroutine_closure( interner: DbInterner<'db>, - def_id: as rustc_type_ir::Interner>::DefId, + def_id: CoroutineIdWrapper, args: as rustc_type_ir::Interner>::GenericArgs, ) -> Self { Ty::new(interner, TyKind::CoroutineClosure(def_id, args)) @@ -633,7 +632,7 @@ impl<'db> rustc_type_ir::inherent::Ty> for Ty<'db> { fn new_closure( interner: DbInterner<'db>, - def_id: as rustc_type_ir::Interner>::DefId, + def_id: ClosureIdWrapper, args: as rustc_type_ir::Interner>::GenericArgs, ) -> Self { Ty::new(interner, TyKind::Closure(def_id, args)) @@ -641,7 +640,7 @@ impl<'db> rustc_type_ir::inherent::Ty> for Ty<'db> { fn new_coroutine_witness( interner: DbInterner<'db>, - def_id: as rustc_type_ir::Interner>::DefId, + def_id: CoroutineIdWrapper, args: as rustc_type_ir::Interner>::GenericArgs, ) -> Self { Ty::new(interner, TyKind::CoroutineWitness(def_id, args)) @@ -649,7 +648,7 @@ impl<'db> rustc_type_ir::inherent::Ty> for Ty<'db> { fn new_coroutine_witness_for_coroutine( interner: DbInterner<'db>, - def_id: as rustc_type_ir::Interner>::DefId, + def_id: CoroutineIdWrapper, coroutine_args: as rustc_type_ir::Interner>::GenericArgs, ) -> Self { // HACK: Coroutine witness types are lifetime erased, so they @@ -714,7 +713,7 @@ impl<'db> rustc_type_ir::inherent::Ty> for Ty<'db> { fn new_fn_def( interner: DbInterner<'db>, - def_id: as rustc_type_ir::Interner>::DefId, + def_id: CallableIdWrapper, args: as rustc_type_ir::Interner>::GenericArgs, ) -> Self { Ty::new(interner, TyKind::FnDef(def_id, args))