diff --git a/Cargo.lock b/Cargo.lock index 344e6d101f..b70b89ea54 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1863,9 +1863,9 @@ dependencies = [ [[package]] name = "ra-ap-rustc_abi" -version = "0.126.0" +version = "0.128.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c6789d94fb3e6e30d62f55e99a321ba63484a8bb3b4ead338687c9ddc282d28" +checksum = "8da95e732b424802b1f043ab4007c78a0fc515ab249587abbea4634bf5fdce9a" dependencies = [ "bitflags 2.9.1", "ra-ap-rustc_hashes", @@ -1875,24 +1875,24 @@ dependencies = [ [[package]] name = "ra-ap-rustc_ast_ir" -version = "0.126.0" +version = "0.128.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aaab80bda0f05e9842e3afb7779b0bad0a4b54e0f7ba6deb5705dcf86482811d" +checksum = "3838d9d7a3a5cdc511cfb6ad78740ce532f75a2366d3fc3b9853ea1b5c872779" [[package]] name = "ra-ap-rustc_hashes" -version = "0.126.0" +version = "0.128.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64bd405e538102b5f699241794b2eefee39d5414c0e4bc72435e91430c51f905" +checksum = "bdc8995d268d3bb4ece910f575ea5a063d6003e193ec155d15703b65882d53fb" dependencies = [ "rustc-stable-hash", ] [[package]] name = "ra-ap-rustc_index" -version = "0.126.0" +version = "0.128.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "521621e271aa03b8433dad5981838278d6cfd7d2d8c9f4eb6d427f1d671f90fc" +checksum = "ed0ccdf6e5627c6c3e54e571e52ce0bc8b94d5f0b94b7460269ca68a4706be69" dependencies = [ "ra-ap-rustc_index_macros", "smallvec", @@ -1900,9 +1900,9 @@ dependencies = [ [[package]] name = "ra-ap-rustc_index_macros" -version = "0.126.0" +version = "0.128.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "245e30f2e1fef258913cc548b36f575549c8af31cbc4649929d21deda96ceeb7" +checksum = "bd28f42362b5c9fb9b8766c3189df02a402b13363600c6885e11027889f03ee6" dependencies = [ "proc-macro2", "quote", @@ -1911,9 +1911,9 @@ dependencies = [ [[package]] name = "ra-ap-rustc_lexer" -version = "0.126.0" +version = "0.128.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a82681f924500e888c860e60ed99e9bf702a219a69374f59116c4261525a2157" +checksum = "f1c31a82f091b910a27ee53a86a9af28a2df10c3484e2f1bbfe70633aa84dee9" dependencies = [ "memchr", "unicode-properties", @@ -1922,9 +1922,9 @@ dependencies = [ [[package]] name = "ra-ap-rustc_next_trait_solver" -version = "0.126.0" +version = "0.128.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c9ce51f2431fbdc7fabd2d957522b6e27f41f68ec2af74b52a6f4116352ce1a" +checksum = "f8cac6c2b5a8924209d4ca682cbc507252c58a664911e0ef463c112882ba6f72" dependencies = [ "derive-where", "ra-ap-rustc_index", @@ -1935,9 +1935,9 @@ dependencies = [ [[package]] name = "ra-ap-rustc_parse_format" -version = "0.126.0" +version = "0.128.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adc85ef3fdb6c084bde84857d8948dc66b752129dc8417a8614ce490e99a143f" +checksum = "a085a1cf902dcca8abbc537faaef154bbccbbb51850f779ce5484ae3782b5d8f" dependencies = [ "ra-ap-rustc_lexer", "rustc-literal-escaper 0.0.5", @@ -1945,9 +1945,9 @@ dependencies = [ [[package]] name = "ra-ap-rustc_pattern_analysis" -version = "0.126.0" +version = "0.128.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3cd81eccf33d9528905d4e5abaa254b3129a6405d6c5f123fed9b73a3d217f35" +checksum = "8ba32e3985367bc34856b41c7604133649d4a367eb5d7bdf50623025731459d8" dependencies = [ "ra-ap-rustc_index", "rustc-hash 2.1.1", @@ -1958,10 +1958,11 @@ dependencies = [ [[package]] name = "ra-ap-rustc_type_ir" -version = "0.126.0" +version = "0.128.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "11cb0da02853698d9c89e1d1c01657b9969752befd56365e8899d4310e52b373" +checksum = "9c9911d72f75d85d21fe88374d7bcec94f2200feffb7234108a24cc3da7c3591" dependencies = [ + "arrayvec", "bitflags 2.9.1", "derive-where", "ena", @@ -1977,9 +1978,9 @@ dependencies = [ [[package]] name = "ra-ap-rustc_type_ir_macros" -version = "0.126.0" +version = "0.128.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffc93adeb52c483ede13bee6680466458218243ab479c04fb71bb53925a6e0ff" +checksum = "22f539b87991683ce17cc52e62600fdf2b4a8af43952db30387edc1a576d3b43" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index f325027ee5..c5ffad544a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -89,14 +89,14 @@ vfs-notify = { path = "./crates/vfs-notify", version = "0.0.0" } vfs = { path = "./crates/vfs", version = "0.0.0" } edition = { path = "./crates/edition", version = "0.0.0" } -ra-ap-rustc_lexer = { version = "0.126", default-features = false } -ra-ap-rustc_parse_format = { version = "0.126", default-features = false } -ra-ap-rustc_index = { version = "0.126", default-features = false } -ra-ap-rustc_abi = { version = "0.126", default-features = false } -ra-ap-rustc_pattern_analysis = { version = "0.126", default-features = false } -ra-ap-rustc_ast_ir = { version = "0.126", default-features = false } -ra-ap-rustc_type_ir = { version = "0.126", default-features = false } -ra-ap-rustc_next_trait_solver = { version = "0.126", default-features = false } +ra-ap-rustc_lexer = { version = "0.128", default-features = false } +ra-ap-rustc_parse_format = { version = "0.128", default-features = false } +ra-ap-rustc_index = { version = "0.128", default-features = false } +ra-ap-rustc_abi = { version = "0.128", default-features = false } +ra-ap-rustc_pattern_analysis = { version = "0.128", default-features = false } +ra-ap-rustc_ast_ir = { version = "0.128", default-features = false } +ra-ap-rustc_type_ir = { version = "0.128", default-features = false } +ra-ap-rustc_next_trait_solver = { version = "0.128", default-features = false } # local crates that aren't published to crates.io. These should not have versions. 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 a234312173..ac85bf7950 100644 --- a/crates/hir-ty/src/method_resolution.rs +++ b/crates/hir-ty/src/method_resolution.rs @@ -163,10 +163,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 5d11525cd1..beaac11a2d 100644 --- a/crates/hir-ty/src/next_solver/infer/canonical/canonicalizer.rs +++ b/crates/hir-ty/src/next_solver/infer/canonical/canonicalizer.rs @@ -10,9 +10,8 @@ use rustc_index::Idx; use rustc_type_ir::InferTy::{self, FloatVar, IntVar, TyVar}; use rustc_type_ir::inherent::{Const as _, IntoKind as _, Region as _, SliceLike, Ty as _}; use rustc_type_ir::{ - BoundVar, CanonicalQueryInput, CanonicalTyVarKind, DebruijnIndex, Flags, InferConst, - RegionKind, TypeFlags, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt, - UniverseIndex, + BoundVar, CanonicalQueryInput, DebruijnIndex, Flags, InferConst, RegionKind, TyVid, TypeFlags, + TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt, UniverseIndex, }; use smallvec::SmallVec; use tracing::debug; @@ -316,6 +315,13 @@ struct Canonicalizer<'cx, 'db> { // Note that indices is only used once `var_values` is big enough to be // heap-allocated. indices: FxHashMap, BoundVar>, + /// Maps each `sub_unification_table_root_var` to the index of the first + /// variable which used it. + /// + /// This means in case two type variables have the same sub relations root, + /// we set the `sub_root` of the second variable to the position of the first. + /// Otherwise the `sub_root` of each type variable is just its own position. + sub_root_lookup_table: FxHashMap, canonicalize_mode: &'cx dyn CanonicalizeMode, needs_canonical_flags: TypeFlags, @@ -384,10 +390,9 @@ impl<'cx, 'db> TypeFolder> for Canonicalizer<'cx, 'db> { // FIXME: perf problem described in #55921. ui = UniverseIndex::ROOT; } - self.canonicalize_ty_var( - CanonicalVarKind::Ty(CanonicalTyVarKind::General(ui)), - t, - ) + + let sub_root = self.get_or_insert_sub_root(vid); + self.canonicalize_ty_var(CanonicalVarKind::Ty { ui, sub_root }, t) } } } @@ -397,7 +402,7 @@ impl<'cx, 'db> TypeFolder> for Canonicalizer<'cx, 'db> { if nt != t { self.fold_ty(nt) } else { - self.canonicalize_ty_var(CanonicalVarKind::Ty(CanonicalTyVarKind::Int), t) + self.canonicalize_ty_var(CanonicalVarKind::Int, t) } } TyKind::Infer(FloatVar(vid)) => { @@ -405,7 +410,7 @@ impl<'cx, 'db> TypeFolder> for Canonicalizer<'cx, 'db> { if nt != t { self.fold_ty(nt) } else { - self.canonicalize_ty_var(CanonicalVarKind::Ty(CanonicalTyVarKind::Float), t) + self.canonicalize_ty_var(CanonicalVarKind::Float, t) } } @@ -579,6 +584,7 @@ impl<'cx, 'db> Canonicalizer<'cx, 'db> { variables: SmallVec::from_slice(base.variables.as_slice()), query_state, indices: FxHashMap::default(), + sub_root_lookup_table: Default::default(), binder_index: DebruijnIndex::ZERO, }; if canonicalizer.query_state.var_values.spilled() { @@ -673,6 +679,13 @@ impl<'cx, 'db> Canonicalizer<'cx, 'db> { } } + fn get_or_insert_sub_root(&mut self, vid: TyVid) -> BoundVar { + let root_vid = self.infcx.sub_unification_table_root_var(vid); + let idx = + *self.sub_root_lookup_table.entry(root_vid).or_insert_with(|| self.variables.len()); + BoundVar::from(idx) + } + /// Replaces the universe indexes used in `var_values` with their index in /// `query_state.universe_map`. This minimizes the maximum universe used in /// the canonicalized value. @@ -692,9 +705,9 @@ impl<'cx, 'db> Canonicalizer<'cx, 'db> { self.variables .iter() .map(|v| match *v { - CanonicalVarKind::Ty(CanonicalTyVarKind::Int | CanonicalTyVarKind::Float) => *v, - CanonicalVarKind::Ty(CanonicalTyVarKind::General(u)) => { - CanonicalVarKind::Ty(CanonicalTyVarKind::General(reverse_universe_map[&u])) + CanonicalVarKind::Int | CanonicalVarKind::Float => *v, + CanonicalVarKind::Ty { ui, sub_root } => { + CanonicalVarKind::Ty { ui: reverse_universe_map[&ui], sub_root } } CanonicalVarKind::Region(u) => CanonicalVarKind::Region(reverse_universe_map[&u]), CanonicalVarKind::Const(u) => CanonicalVarKind::Const(reverse_universe_map[&u]), 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 ec41111ed8..d0669f5c3b 100644 --- a/crates/hir-ty/src/next_solver/infer/canonical/mod.rs +++ b/crates/hir-ty/src/next_solver/infer/canonical/mod.rs @@ -32,9 +32,10 @@ use crate::next_solver::{ }; use instantiate::CanonicalExt; use rustc_index::IndexVec; +use rustc_type_ir::inherent::IntoKind; use rustc_type_ir::{ - AliasRelationDirection, AliasTyKind, CanonicalTyVarKind, CanonicalVarKind, InferTy, - TypeFoldable, UniverseIndex, Upcast, Variance, + AliasRelationDirection, AliasTyKind, CanonicalVarKind, InferTy, TypeFoldable, UniverseIndex, + Upcast, Variance, inherent::{SliceLike, Ty as _}, relate::{ Relate, TypeRelation, VarianceDiagInfo, @@ -78,27 +79,13 @@ impl<'db> InferCtxt<'db> { .chain((1..=canonical.max_universe.as_u32()).map(|_| self.create_next_universe())) .collect(); - let canonical_inference_vars = - self.instantiate_canonical_vars(canonical.variables, |ui| universes[ui]); - let result = canonical.instantiate(self.interner, &canonical_inference_vars); - (result, canonical_inference_vars) - } - - /// Given the "infos" about the canonical variables from some - /// canonical, creates fresh variables with the same - /// characteristics (see `instantiate_canonical_var` for - /// details). You can then use `instantiate` to instantiate the - /// canonical variable with these inference variables. - fn instantiate_canonical_vars( - &self, - variables: CanonicalVars<'db>, - universe_map: impl Fn(UniverseIndex) -> UniverseIndex, - ) -> CanonicalVarValues<'db> { - CanonicalVarValues { - var_values: self.interner.mk_args_from_iter( - variables.iter().map(|info| self.instantiate_canonical_var(info, &universe_map)), - ), - } + let var_values = CanonicalVarValues::instantiate( + self.interner, + canonical.variables, + |var_values, info| self.instantiate_canonical_var(info, var_values, |ui| universes[ui]), + ); + let result = canonical.instantiate(self.interner, &var_values); + (result, var_values) } /// Given the "info" about a canonical variable, creates a fresh @@ -112,22 +99,28 @@ impl<'db> InferCtxt<'db> { pub fn instantiate_canonical_var( &self, cv_info: CanonicalVarKind>, + previous_var_values: &[GenericArg<'db>], universe_map: impl Fn(UniverseIndex) -> UniverseIndex, ) -> GenericArg<'db> { match cv_info { - CanonicalVarKind::Ty(ty_kind) => { - let ty = match ty_kind { - CanonicalTyVarKind::General(ui) => { - self.next_ty_var_in_universe(universe_map(ui)) + CanonicalVarKind::Ty { ui, sub_root } => { + let vid = self.next_ty_var_id_in_universe(universe_map(ui)); + // If this inference variable is related to an earlier variable + // via subtyping, we need to add that info to the inference context. + if let Some(prev) = previous_var_values.get(sub_root.as_usize()) { + if let TyKind::Infer(InferTy::TyVar(sub_root)) = prev.expect_ty().kind() { + self.sub_unify_ty_vids_raw(vid, sub_root); + } else { + unreachable!() } - - CanonicalTyVarKind::Int => self.next_int_var(), - - CanonicalTyVarKind::Float => self.next_float_var(), - }; - ty.into() + } + Ty::new_var(self.interner, vid).into() } + CanonicalVarKind::Int => self.next_int_var().into(), + + CanonicalVarKind::Float => self.next_float_var().into(), + CanonicalVarKind::PlaceholderTy(PlaceholderTy { universe, bound }) => { let universe_mapped = universe_map(universe); let placeholder_mapped = PlaceholderTy { universe: universe_mapped, bound }; diff --git a/crates/hir-ty/src/next_solver/infer/context.rs b/crates/hir-ty/src/next_solver/infer/context.rs index 93fd6eeab3..45ce7e6f6c 100644 --- a/crates/hir-ty/src/next_solver/infer/context.rs +++ b/crates/hir-ty/src/next_solver/infer/context.rs @@ -313,4 +313,12 @@ impl<'db> rustc_type_ir::InferCtxtLike for InferCtxt<'db> { fn reset_opaque_types(&self) { let _ = self.take_opaque_types(); } + + fn sub_unification_table_root_var(&self, var: rustc_type_ir::TyVid) -> rustc_type_ir::TyVid { + self.sub_unification_table_root_var(var) + } + + fn sub_unify_ty_vids_raw(&self, a: rustc_type_ir::TyVid, b: rustc_type_ir::TyVid) { + self.sub_unify_ty_vids_raw(a, b); + } } diff --git a/crates/hir-ty/src/next_solver/infer/mod.rs b/crates/hir-ty/src/next_solver/infer/mod.rs index 2630f2a8cc..ce6c941287 100644 --- a/crates/hir-ty/src/next_solver/infer/mod.rs +++ b/crates/hir-ty/src/next_solver/infer/mod.rs @@ -1019,6 +1019,14 @@ impl<'db> InferCtxt<'db> { } } } + + fn sub_unification_table_root_var(&self, var: rustc_type_ir::TyVid) -> rustc_type_ir::TyVid { + self.inner.borrow_mut().type_variables().sub_unification_table_root_var(var) + } + + fn sub_unify_ty_vids_raw(&self, a: rustc_type_ir::TyVid, b: rustc_type_ir::TyVid) { + self.inner.borrow_mut().type_variables().sub_unify(a, b); + } } /// Helper for [InferCtxt::ty_or_const_infer_var_changed] (see comment on that), currently diff --git a/crates/hir-ty/src/next_solver/infer/snapshot/undo_log.rs b/crates/hir-ty/src/next_solver/infer/snapshot/undo_log.rs index 0fa6421f51..28ae56f4ee 100644 --- a/crates/hir-ty/src/next_solver/infer/snapshot/undo_log.rs +++ b/crates/hir-ty/src/next_solver/infer/snapshot/undo_log.rs @@ -25,7 +25,7 @@ pub struct Snapshot { pub(crate) enum UndoLog<'db> { DuplicateOpaqueType, OpaqueTypes(OpaqueTypeKey<'db>, Option>), - TypeVariables(sv::UndoLog>>), + TypeVariables(type_variable::UndoLog<'db>), ConstUnificationTable(sv::UndoLog>>), IntUnificationTable(sv::UndoLog>), FloatUnificationTable(sv::UndoLog>), @@ -51,6 +51,8 @@ impl_from! { RegionConstraintCollector(region_constraints::UndoLog<'db>), TypeVariables(sv::UndoLog>>), + TypeVariables(sv::UndoLog>), + TypeVariables(type_variable::UndoLog<'db>), IntUnificationTable(sv::UndoLog>), FloatUnificationTable(sv::UndoLog>), diff --git a/crates/hir-ty/src/next_solver/infer/type_variable.rs b/crates/hir-ty/src/next_solver/infer/type_variable.rs index 5217308af4..b640039af6 100644 --- a/crates/hir-ty/src/next_solver/infer/type_variable.rs +++ b/crates/hir-ty/src/next_solver/infer/type_variable.rs @@ -17,12 +17,48 @@ use crate::next_solver::SolverDefId; use crate::next_solver::Ty; use crate::next_solver::infer::InferCtxtUndoLogs; +/// Represents a single undo-able action that affects a type inference variable. +#[derive(Clone)] +pub(crate) enum UndoLog<'tcx> { + EqRelation(sv::UndoLog>>), + SubRelation(sv::UndoLog>), +} + +/// Convert from a specific kind of undo to the more general UndoLog +impl<'db> From>>> for UndoLog<'db> { + fn from(l: sv::UndoLog>>) -> Self { + UndoLog::EqRelation(l) + } +} + +/// Convert from a specific kind of undo to the more general UndoLog +impl<'db> From>> for UndoLog<'db> { + fn from(l: sv::UndoLog>) -> Self { + UndoLog::SubRelation(l) + } +} + impl<'db> Rollback>>> for TypeVariableStorage<'db> { fn reverse(&mut self, undo: sv::UndoLog>>) { self.eq_relations.reverse(undo) } } +impl<'tcx> Rollback>> for TypeVariableStorage<'tcx> { + fn reverse(&mut self, undo: sv::UndoLog>) { + self.sub_unification_table.reverse(undo) + } +} + +impl<'tcx> Rollback> for TypeVariableStorage<'tcx> { + fn reverse(&mut self, undo: UndoLog<'tcx>) { + match undo { + UndoLog::EqRelation(undo) => self.eq_relations.reverse(undo), + UndoLog::SubRelation(undo) => self.sub_unification_table.reverse(undo), + } + } +} + #[derive(Clone, Default)] pub(crate) struct TypeVariableStorage<'db> { /// The origins of each type variable. @@ -31,6 +67,25 @@ pub(crate) struct TypeVariableStorage<'db> { /// constraint `?X == ?Y`. This table also stores, for each key, /// the known value. eq_relations: ut::UnificationTableStorage>, + /// Only used by `-Znext-solver` and for diagnostics. Tracks whether + /// type variables are related via subtyping at all, ignoring which of + /// the two is the subtype. + /// + /// When reporting ambiguity errors, we sometimes want to + /// treat all inference vars which are subtypes of each + /// others as if they are equal. For this case we compute + /// the transitive closure of our subtype obligations here. + /// + /// E.g. when encountering ambiguity errors, we want to suggest + /// specifying some method argument or to add a type annotation + /// to a local variable. Because subtyping cannot change the + /// shape of a type, it's fine if the cause of the ambiguity error + /// is only related to the suggested variable via subtyping. + /// + /// Even for something like `let x = returns_arg(); x.method();` the + /// type of `x` is only a supertype of the argument of `returns_arg`. We + /// still want to suggest specifying the type of the argument. + sub_unification_table: ut::UnificationTableStorage, } pub(crate) struct TypeVariableTable<'a, 'db> { @@ -112,6 +167,17 @@ impl<'db> TypeVariableTable<'_, 'db> { debug_assert!(self.probe(a).is_unknown()); debug_assert!(self.probe(b).is_unknown()); self.eq_relations().union(a, b); + self.sub_unification_table().union(a, b); + } + + /// Records that `a` and `b` are related via subtyping. We don't track + /// which of the two is the subtype. + /// + /// Precondition: neither `a` nor `b` are known. + pub(crate) fn sub_unify(&mut self, a: TyVid, b: TyVid) { + debug_assert!(self.probe(a).is_unknown()); + debug_assert!(self.probe(b).is_unknown()); + self.sub_unification_table().union(a, b); } /// Instantiates `vid` with the type `ty`. @@ -141,6 +207,10 @@ impl<'db> TypeVariableTable<'_, 'db> { /// for improving error messages. pub(crate) fn new_var(&mut self, universe: UniverseIndex, origin: TypeVariableOrigin) -> TyVid { let eq_key = self.eq_relations().new_key(TypeVariableValue::Unknown { universe }); + + let sub_key = self.sub_unification_table().new_key(()); + debug_assert_eq!(eq_key.vid, sub_key.vid); + let index = self.storage.values.push(TypeVariableData { origin }); debug_assert_eq!(eq_key.vid, index); @@ -163,6 +233,18 @@ impl<'db> TypeVariableTable<'_, 'db> { self.eq_relations().find(vid).vid } + /// Returns the "root" variable of `vid` in the `sub_unification_table` + /// equivalence table. All type variables that have been are related via + /// equality or subtyping will yield the same root variable (per the + /// union-find algorithm), so `sub_unification_table_root_var(a) + /// == sub_unification_table_root_var(b)` implies that: + /// ```text + /// exists X. (a <: X || X <: a) && (b <: X || X <: b) + /// ``` + pub(crate) fn sub_unification_table_root_var(&mut self, vid: TyVid) -> TyVid { + self.sub_unification_table().find(vid).vid + } + /// Retrieves the type to which `vid` has been instantiated, if /// any. pub(crate) fn probe(&mut self, vid: TyVid) -> TypeVariableValue<'db> { @@ -180,6 +262,11 @@ impl<'db> TypeVariableTable<'_, 'db> { self.storage.eq_relations.with_log(self.undo_log) } + #[inline] + fn sub_unification_table(&mut self) -> super::UnificationTable<'_, 'db, TyVidSubKey> { + self.storage.sub_unification_table.with_log(self.undo_log) + } + /// Returns indices of all variables that are not yet /// instantiated. pub(crate) fn unresolved_variables(&mut self) -> Vec { @@ -228,6 +315,36 @@ impl<'db> ut::UnifyKey for TyVidEqKey<'db> { fn tag() -> &'static str { "TyVidEqKey" } + fn order_roots(a: Self, _: &Self::Value, b: Self, _: &Self::Value) -> Option<(Self, Self)> { + if a.vid.as_u32() < b.vid.as_u32() { Some((a, b)) } else { Some((b, a)) } + } +} + +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub(crate) struct TyVidSubKey { + vid: TyVid, +} + +impl From for TyVidSubKey { + #[inline] // make this function eligible for inlining - it is quite hot. + fn from(vid: TyVid) -> Self { + TyVidSubKey { vid } + } +} + +impl ut::UnifyKey for TyVidSubKey { + type Value = (); + #[inline] + fn index(&self) -> u32 { + self.vid.as_u32() + } + #[inline] + fn from_index(i: u32) -> TyVidSubKey { + TyVidSubKey { vid: TyVid::from_u32(i) } + } + fn tag() -> &'static str { + "TyVidSubKey" + } } impl<'db> ut::UnifyValue for TypeVariableValue<'db> { diff --git a/crates/hir-ty/src/next_solver/infer/unify_key.rs b/crates/hir-ty/src/next_solver/infer/unify_key.rs index b9afb45ba8..dc913b262a 100644 --- a/crates/hir-ty/src/next_solver/infer/unify_key.rs +++ b/crates/hir-ty/src/next_solver/infer/unify_key.rs @@ -139,6 +139,9 @@ impl<'db> UnifyKey for ConstVidKey<'db> { fn tag() -> &'static str { "ConstVidKey" } + fn order_roots(a: Self, _: &Self::Value, b: Self, _: &Self::Value) -> Option<(Self, Self)> { + if a.vid.as_u32() < b.vid.as_u32() { Some((a, b)) } else { Some((b, a)) } + } } impl<'db> UnifyValue for ConstVariableValue<'db> { 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 57a56b3f0c..eb2cc69167 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) => { @@ -734,15 +732,13 @@ impl<'db, T: HasInterner + ChalkToNextSolver<'db, U>, U> interner, self.binders.iter(Interner).map(|k| match &k.kind { chalk_ir::VariableKind::Ty(ty_variable_kind) => match ty_variable_kind { - TyVariableKind::General => rustc_type_ir::CanonicalVarKind::Ty( - rustc_type_ir::CanonicalTyVarKind::General(UniverseIndex::ROOT), - ), - TyVariableKind::Integer => { - rustc_type_ir::CanonicalVarKind::Ty(rustc_type_ir::CanonicalTyVarKind::Int) - } - TyVariableKind::Float => rustc_type_ir::CanonicalVarKind::Ty( - rustc_type_ir::CanonicalTyVarKind::Float, - ), + // FIXME(next-solver): the info is incorrect, but we have no way to store the information in Chalk. + TyVariableKind::General => rustc_type_ir::CanonicalVarKind::Ty { + ui: UniverseIndex::ROOT, + sub_root: BoundVar::from_u32(0), + }, + TyVariableKind::Integer => rustc_type_ir::CanonicalVarKind::Int, + TyVariableKind::Float => rustc_type_ir::CanonicalVarKind::Float, }, chalk_ir::VariableKind::Lifetime => { rustc_type_ir::CanonicalVarKind::Region(UniverseIndex::ROOT) @@ -767,24 +763,20 @@ impl<'db, T: NextSolverToChalk<'db, U>, U: HasInterner> let binders = chalk_ir::CanonicalVarKinds::from_iter( Interner, self.variables.iter().map(|v| match v { - rustc_type_ir::CanonicalVarKind::Ty( - rustc_type_ir::CanonicalTyVarKind::General(ui), - ) => chalk_ir::CanonicalVarKind::new( - chalk_ir::VariableKind::Ty(TyVariableKind::General), - chalk_ir::UniverseIndex { counter: ui.as_usize() }, + rustc_type_ir::CanonicalVarKind::Ty { ui, sub_root: _ } => { + chalk_ir::CanonicalVarKind::new( + chalk_ir::VariableKind::Ty(TyVariableKind::General), + chalk_ir::UniverseIndex { counter: ui.as_usize() }, + ) + } + rustc_type_ir::CanonicalVarKind::Int => chalk_ir::CanonicalVarKind::new( + chalk_ir::VariableKind::Ty(TyVariableKind::Integer), + chalk_ir::UniverseIndex::root(), + ), + rustc_type_ir::CanonicalVarKind::Float => chalk_ir::CanonicalVarKind::new( + chalk_ir::VariableKind::Ty(TyVariableKind::Float), + chalk_ir::UniverseIndex::root(), ), - rustc_type_ir::CanonicalVarKind::Ty(rustc_type_ir::CanonicalTyVarKind::Int) => { - chalk_ir::CanonicalVarKind::new( - chalk_ir::VariableKind::Ty(TyVariableKind::Integer), - chalk_ir::UniverseIndex::root(), - ) - } - rustc_type_ir::CanonicalVarKind::Ty(rustc_type_ir::CanonicalTyVarKind::Float) => { - chalk_ir::CanonicalVarKind::new( - chalk_ir::VariableKind::Ty(TyVariableKind::Float), - chalk_ir::UniverseIndex::root(), - ) - } rustc_type_ir::CanonicalVarKind::Region(ui) => chalk_ir::CanonicalVarKind::new( chalk_ir::VariableKind::Lifetime, chalk_ir::UniverseIndex { counter: ui.as_usize() }, @@ -1520,13 +1512,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 { @@ -1537,40 +1523,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 385149d784..c7591c0c77 100644 --- a/crates/hir-ty/src/next_solver/solver.rs +++ b/crates/hir-ty/src/next_solver/solver.rs @@ -10,6 +10,7 @@ use rustc_type_ir::{ }; use crate::next_solver::mapping::NextSolverToChalk; +use crate::next_solver::{CanonicalVarKind, ImplIdWrapper}; use crate::{ TraitRefExt, db::HirDatabase, @@ -117,13 +118,14 @@ impl<'db> SolverDelegate for SolverContext<'db> { canonical.instantiate(self.cx(), &values) } - fn instantiate_canonical_var_with_infer( + fn instantiate_canonical_var( &self, - cv_info: rustc_type_ir::CanonicalVarKind, - _span: ::Span, + kind: CanonicalVarKind<'db>, + span: ::Span, + var_values: &[GenericArg<'db>], universe_map: impl Fn(rustc_type_ir::UniverseIndex) -> rustc_type_ir::UniverseIndex, - ) -> ::GenericArg { - self.0.instantiate_canonical_var(cv_info, universe_map) + ) -> GenericArg<'db> { + self.0.instantiate_canonical_var(kind, var_values, universe_map) } fn add_item_bounds_for_hidden_type( @@ -146,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"), @@ -160,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() @@ -168,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)) diff --git a/crates/hir-ty/src/next_solver/util.rs b/crates/hir-ty/src/next_solver/util.rs index 50b96a160e..97d3ea72c9 100644 --- a/crates/hir-ty/src/next_solver/util.rs +++ b/crates/hir-ty/src/next_solver/util.rs @@ -532,17 +532,14 @@ pub(crate) fn mini_canonicalize<'db, T: TypeFoldable>>( max_universe: UniverseIndex::from_u32(1), variables: CanonicalVars::new_from_iter( context.cx(), - vars.iter().map(|(k, v)| match (*k).kind() { + vars.iter().enumerate().map(|(idx, (k, v))| match (*k).kind() { GenericArgKind::Type(ty) => match ty.kind() { - TyKind::Int(..) | TyKind::Uint(..) => { - rustc_type_ir::CanonicalVarKind::Ty(rustc_type_ir::CanonicalTyVarKind::Int) - } - TyKind::Float(..) => rustc_type_ir::CanonicalVarKind::Ty( - rustc_type_ir::CanonicalTyVarKind::Float, - ), - _ => rustc_type_ir::CanonicalVarKind::Ty( - rustc_type_ir::CanonicalTyVarKind::General(UniverseIndex::ZERO), - ), + TyKind::Int(..) | TyKind::Uint(..) => rustc_type_ir::CanonicalVarKind::Int, + TyKind::Float(..) => rustc_type_ir::CanonicalVarKind::Float, + _ => rustc_type_ir::CanonicalVarKind::Ty { + ui: UniverseIndex::ZERO, + sub_root: BoundVar::from_usize(idx), + }, }, GenericArgKind::Lifetime(_) => { rustc_type_ir::CanonicalVarKind::Region(UniverseIndex::ZERO)