Skip to content

Commit 768965b

Browse files
committed
Trying to fix rust-analyzer's placeholder crimes
1 parent a4fdea7 commit 768965b

File tree

3 files changed

+111
-15
lines changed

3 files changed

+111
-15
lines changed

crates/hir-ty/src/display.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use chalk_ir::{BoundVar, Safety, TyKind};
1212
use either::Either;
1313
use hir_def::{
1414
GeneralConstId, GenericDefId, HasModule, ImportPathConfig, LocalFieldId, Lookup, ModuleDefId,
15-
ModuleId, TraitId,
15+
ModuleId, TraitId, TypeOrConstParamId,
1616
db::DefDatabase,
1717
expr_store::{ExpressionStore, path::Path},
1818
find_path::{self, PrefixKind},
@@ -1612,9 +1612,28 @@ impl<'db> HirDisplay for crate::next_solver::Ty<'db> {
16121612
}
16131613
}
16141614
TyKind::Placeholder(idx) => {
1615-
let placeholder_index = chalk_ir::PlaceholderIndex {
1616-
idx: idx.bound.var.as_usize(),
1617-
ui: chalk_ir::UniverseIndex { counter: idx.universe.as_usize() },
1615+
let placeholder_index = match idx.bound.kind {
1616+
crate::next_solver::BoundTyKind::Anon => chalk_ir::PlaceholderIndex {
1617+
idx: idx.bound.var.as_usize(),
1618+
ui: chalk_ir::UniverseIndex { counter: idx.universe.as_usize() },
1619+
},
1620+
crate::next_solver::BoundTyKind::Param(solver_def_id) => {
1621+
let Some(def) =
1622+
crate::next_solver::mapping::solver_def_id_to_generic_def_id_opt(
1623+
interner.db,
1624+
solver_def_id,
1625+
)
1626+
else {
1627+
unreachable!()
1628+
};
1629+
crate::mapping::to_placeholder_idx(
1630+
interner.db,
1631+
TypeOrConstParamId {
1632+
parent: def,
1633+
local_id: crate::Idx::from_raw(idx.bound.var.as_u32().into()),
1634+
},
1635+
)
1636+
}
16181637
};
16191638
let id = from_placeholder_idx(db, placeholder_index);
16201639
let generics = generics(db, id.parent);

crates/hir-ty/src/infer/closure.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,10 @@ impl InferenceContext<'_> {
568568
let supplied_sig = self.supplied_sig_of_closure(body, ret_type, arg_types, closure_kind);
569569

570570
let snapshot = self.table.snapshot();
571-
if !self.table.unify::<_, crate::next_solver::GenericArgs<'_>>(&expected_sig.substitution.0, &supplied_sig.expected_sig.substitution.0) {
571+
if !self.table.unify::<_, crate::next_solver::GenericArgs<'_>>(
572+
&expected_sig.substitution.0,
573+
&supplied_sig.expected_sig.substitution.0,
574+
) {
572575
self.table.rollback_to(snapshot);
573576
}
574577

@@ -878,7 +881,9 @@ impl CapturedItemWithoutTy {
878881
idx: chalk_ir::PlaceholderIndex,
879882
outer_binder: DebruijnIndex,
880883
) -> std::result::Result<Ty, Self::Error> {
884+
dbg!(&idx);
881885
let x = from_placeholder_idx(self.db, idx);
886+
dbg!(&x);
882887
let Some(idx) = self.generics.type_or_const_param_idx(x) else {
883888
return Err(());
884889
};

crates/hir-ty/src/next_solver/mapping.rs

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use chalk_ir::{
66
WellFormed, cast::Cast, fold::Shift, interner::HasInterner,
77
};
88
use hir_def::{
9-
CallableDefId, ConstParamId, FunctionId, GeneralConstId, LifetimeParamId, TypeAliasId,
10-
TypeOrConstParamId, TypeParamId, signatures::TraitFlags,
9+
CallableDefId, ConstParamId, FunctionId, GeneralConstId, GenericDefId, LifetimeParamId,
10+
TypeAliasId, TypeOrConstParamId, TypeParamId, signatures::TraitFlags,
1111
};
1212
use intern::sym;
1313
use rustc_type_ir::{
@@ -276,10 +276,23 @@ impl<'db> ChalkToNextSolver<'db, Ty<'db>> for chalk_ir::Ty<Interner> {
276276
),
277277
chalk_ir::TyKind::Error => rustc_type_ir::TyKind::Error(ErrorGuaranteed),
278278
chalk_ir::TyKind::Placeholder(placeholder_index) => {
279-
rustc_type_ir::TyKind::Placeholder(PlaceholderTy::new_anon(
280-
placeholder_index.ui.to_nextsolver(interner),
281-
rustc_type_ir::BoundVar::from_usize(placeholder_index.idx),
282-
))
279+
if placeholder_index.idx < 128 {
280+
dbg!(placeholder_index.idx);
281+
rustc_type_ir::TyKind::Placeholder(PlaceholderTy::new_anon(
282+
placeholder_index.ui.to_nextsolver(interner),
283+
rustc_type_ir::BoundVar::from_usize(placeholder_index.idx),
284+
))
285+
} else {
286+
let id =
287+
crate::mapping::from_placeholder_idx(interner.db, *placeholder_index);
288+
rustc_type_ir::TyKind::Placeholder(PlaceholderTy::new(
289+
placeholder_index.ui.to_nextsolver(interner),
290+
BoundTy {
291+
var: BoundVar::from_u32(id.local_id.into_raw().into_u32()),
292+
kind: BoundTyKind::Param(id.parent.into()),
293+
},
294+
))
295+
}
283296
}
284297
chalk_ir::TyKind::Dyn(dyn_ty) => {
285298
// exists<type> { for<...> ^1.0: ... }
@@ -559,8 +572,27 @@ impl<'db> ChalkToNextSolver<'db, Ty<'db>> for chalk_ir::Ty<Interner> {
559572

560573
rustc_type_ir::TyKind::Placeholder(placeholder) => {
561574
let ui = chalk_ir::UniverseIndex { counter: placeholder.universe.as_usize() };
562-
let placeholder_index =
563-
chalk_ir::PlaceholderIndex { idx: placeholder.bound.var.as_usize(), ui };
575+
let placeholder_index = match placeholder.bound.kind {
576+
BoundTyKind::Anon => {
577+
chalk_ir::PlaceholderIndex { idx: placeholder.bound.var.as_usize(), ui }
578+
}
579+
BoundTyKind::Param(solver_def_id) => {
580+
let Some(def) =
581+
solver_def_id_to_generic_def_id_opt(interner.db, solver_def_id)
582+
else {
583+
unreachable!()
584+
};
585+
crate::mapping::to_placeholder_idx(
586+
interner.db,
587+
TypeOrConstParamId {
588+
parent: def,
589+
local_id: crate::Idx::from_raw(
590+
placeholder.bound.var.as_u32().into(),
591+
),
592+
},
593+
)
594+
}
595+
};
564596
TyKind::Placeholder(placeholder_index)
565597
}
566598

@@ -1986,8 +2018,24 @@ pub(crate) fn convert_ty_for_result<'db>(interner: DbInterner<'db>, ty: Ty<'db>)
19862018

19872019
rustc_type_ir::TyKind::Placeholder(placeholder) => {
19882020
let ui = chalk_ir::UniverseIndex { counter: placeholder.universe.as_usize() };
1989-
let placeholder_index =
1990-
chalk_ir::PlaceholderIndex { idx: placeholder.bound.var.as_usize(), ui };
2021+
let placeholder_index = match placeholder.bound.kind {
2022+
BoundTyKind::Anon => {
2023+
chalk_ir::PlaceholderIndex { idx: placeholder.bound.var.as_usize(), ui }
2024+
}
2025+
BoundTyKind::Param(solver_def_id) => {
2026+
let Some(def) = solver_def_id_to_generic_def_id_opt(interner.db, solver_def_id)
2027+
else {
2028+
unreachable!()
2029+
};
2030+
crate::mapping::to_placeholder_idx(
2031+
interner.db,
2032+
TypeOrConstParamId {
2033+
parent: def,
2034+
local_id: crate::Idx::from_raw(placeholder.bound.var.as_u32().into()),
2035+
},
2036+
)
2037+
}
2038+
};
19912039
TyKind::Placeholder(placeholder_index)
19922040
}
19932041

@@ -2298,3 +2346,27 @@ impl InferenceVarExt for InferenceVar {
22982346
InferenceVar::from(vid.as_u32())
22992347
}
23002348
}
2349+
2350+
pub fn solver_def_id_to_generic_def_id_opt(
2351+
db: &dyn HirDatabase,
2352+
id: SolverDefId,
2353+
) -> Option<GenericDefId> {
2354+
match id {
2355+
SolverDefId::AdtId(it) => Some(it.into()),
2356+
SolverDefId::ConstId(it) => Some(it.into()),
2357+
SolverDefId::FunctionId(it) => Some(it.into()),
2358+
SolverDefId::ImplId(it) => Some(it.into()),
2359+
SolverDefId::StaticId(it) => Some(it.into()),
2360+
SolverDefId::TraitId(it) => Some(it.into()),
2361+
SolverDefId::TypeAliasId(it) => Some(it.into()),
2362+
SolverDefId::Ctor(crate::next_solver::Ctor::Struct(it)) => Some(it.into()),
2363+
SolverDefId::Ctor(crate::next_solver::Ctor::Enum(it)) => Some(it.loc(db).parent.into()),
2364+
SolverDefId::InternedOpaqueTyId(it) => match it.loc(db) {
2365+
crate::ImplTraitId::ReturnTypeImplTrait(it, _) => Some(it.into()),
2366+
crate::ImplTraitId::TypeAliasImplTrait(it, _) => Some(it.into()),
2367+
crate::ImplTraitId::AsyncBlockTypeImplTrait(..) => None,
2368+
},
2369+
SolverDefId::InternedClosureId(_) => None,
2370+
SolverDefId::InternedCoroutineId(_) => None,
2371+
}
2372+
}

0 commit comments

Comments
 (0)