Skip to content

Commit e979349

Browse files
committed
Switch TraitRef in hir::TraitRef to next solver
1 parent 7f0ec8b commit e979349

File tree

4 files changed

+288
-41
lines changed

4 files changed

+288
-41
lines changed

crates/hir-ty/src/display.rs

Lines changed: 218 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use base_db::Crate;
1111
use chalk_ir::{BoundVar, Safety, TyKind};
1212
use either::Either;
1313
use hir_def::{
14-
GenericDefId, HasModule, ImportPathConfig, LocalFieldId, Lookup, ModuleDefId, ModuleId,
15-
TraitId,
14+
GeneralConstId, GenericDefId, HasModule, ImportPathConfig, LocalFieldId, Lookup, ModuleDefId,
15+
ModuleId, TraitId,
1616
db::DefDatabase,
1717
expr_store::{ExpressionStore, path::Path},
1818
find_path::{self, PrefixKind},
@@ -38,7 +38,7 @@ use rustc_apfloat::{
3838
};
3939
use rustc_hash::FxHashSet;
4040
use rustc_type_ir::{
41-
AliasTyKind,
41+
AliasTyKind, RegionKind,
4242
inherent::{AdtDef, IntoKind, SliceLike},
4343
};
4444
use smallvec::SmallVec;
@@ -61,8 +61,9 @@ use crate::{
6161
next_solver::{
6262
BoundExistentialPredicate, Ctor, DbInterner, GenericArgs, SolverDefId,
6363
mapping::{
64-
ChalkToNextSolver, convert_args_for_result, convert_const_for_result,
65-
convert_region_for_result, convert_ty_for_result,
64+
ChalkToNextSolver, bound_var_to_lifetime_idx, bound_var_to_type_or_const_param_idx,
65+
convert_args_for_result, convert_const_for_result, convert_region_for_result,
66+
convert_ty_for_result,
6667
},
6768
},
6869
primitive, to_assoc_type_id,
@@ -715,28 +716,56 @@ impl HirDisplay for GenericArg {
715716
}
716717
}
717718

719+
impl<'db> HirDisplay for crate::next_solver::GenericArg<'db> {
720+
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
721+
match self.kind() {
722+
rustc_type_ir::GenericArgKind::Type(ty) => ty.hir_fmt(f),
723+
rustc_type_ir::GenericArgKind::Lifetime(lt) => lt.hir_fmt(f),
724+
rustc_type_ir::GenericArgKind::Const(c) => c.hir_fmt(f),
725+
}
726+
}
727+
}
728+
718729
impl HirDisplay for Const {
719730
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
720-
let data = self.interned();
721-
match &data.value {
722-
ConstValue::BoundVar(idx) => idx.hir_fmt(f),
723-
ConstValue::InferenceVar(..) => write!(f, "#c#"),
724-
ConstValue::Placeholder(idx) => {
725-
let id = from_placeholder_idx(f.db, *idx);
731+
let c = self.to_nextsolver(DbInterner::new_with(f.db, None, None));
732+
c.hir_fmt(f)
733+
}
734+
}
735+
736+
impl<'db> HirDisplay for crate::next_solver::Const<'db> {
737+
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
738+
match self.kind() {
739+
rustc_type_ir::ConstKind::Bound(db, bound_const) => {
740+
write!(f, "?{}.{}", db.as_u32(), bound_const.as_u32())
741+
}
742+
rustc_type_ir::ConstKind::Infer(..) => write!(f, "#c#"),
743+
rustc_type_ir::ConstKind::Placeholder(idx) => {
744+
let id = bound_var_to_type_or_const_param_idx(f.db, idx.bound);
726745
let generics = generics(f.db, id.parent);
727746
let param_data = &generics[id.local_id];
728747
write!(f, "{}", param_data.name().unwrap().display(f.db, f.edition()))?;
729748
Ok(())
730749
}
731-
ConstValue::Concrete(c) => match &c.interned {
732-
ConstScalar::Bytes(b, m) => render_const_scalar(f, b, m, &data.ty),
733-
ConstScalar::UnevaluatedConst(c, parameters) => {
734-
write!(f, "{}", c.name(f.db))?;
735-
hir_fmt_generics(f, parameters.as_slice(Interner), c.generic_def(f.db), None)?;
736-
Ok(())
737-
}
738-
ConstScalar::Unknown => f.write_char('_'),
739-
},
750+
rustc_type_ir::ConstKind::Value(const_bytes) => render_const_scalar_ns(
751+
f,
752+
&const_bytes.value.inner().0,
753+
&const_bytes.value.inner().1,
754+
const_bytes.ty,
755+
),
756+
rustc_type_ir::ConstKind::Unevaluated(unev) => {
757+
let c = match unev.def {
758+
SolverDefId::ConstId(id) => GeneralConstId::ConstId(id),
759+
SolverDefId::StaticId(id) => GeneralConstId::StaticId(id),
760+
_ => unreachable!(),
761+
};
762+
write!(f, "{}", c.name(f.db))?;
763+
hir_fmt_generics_ns(f, unev.args.as_slice(), c.generic_def(f.db), None)?;
764+
Ok(())
765+
}
766+
rustc_type_ir::ConstKind::Error(..) => f.write_char('_'),
767+
rustc_type_ir::ConstKind::Expr(..) => write!(f, "<const-expr>"),
768+
rustc_type_ir::ConstKind::Param(_) => write!(f, "<param>"),
740769
}
741770
}
742771
}
@@ -1748,6 +1777,27 @@ fn hir_fmt_generics(
17481777
Ok(())
17491778
}
17501779

1780+
fn hir_fmt_generics_ns<'db>(
1781+
f: &mut HirFormatter<'_>,
1782+
parameters: &[crate::next_solver::GenericArg<'db>],
1783+
generic_def: Option<hir_def::GenericDefId>,
1784+
self_: Option<crate::next_solver::Ty<'db>>,
1785+
) -> Result<(), HirDisplayError> {
1786+
if parameters.is_empty() {
1787+
return Ok(());
1788+
}
1789+
1790+
let parameters_to_write = generic_args_sans_defaults_ns(f, generic_def, parameters);
1791+
1792+
if !parameters_to_write.is_empty() {
1793+
write!(f, "<")?;
1794+
hir_fmt_generic_arguments_ns(f, parameters_to_write, self_)?;
1795+
write!(f, ">")?;
1796+
}
1797+
1798+
Ok(())
1799+
}
1800+
17511801
fn generic_args_sans_defaults<'ga>(
17521802
f: &mut HirFormatter<'_>,
17531803
generic_def: Option<hir_def::GenericDefId>,
@@ -1803,6 +1853,87 @@ fn generic_args_sans_defaults<'ga>(
18031853
}
18041854
}
18051855

1856+
fn hir_fmt_generic_args<'db>(
1857+
f: &mut HirFormatter<'_>,
1858+
parameters: &[crate::next_solver::GenericArg<'db>],
1859+
generic_def: Option<hir_def::GenericDefId>,
1860+
self_: Option<crate::next_solver::Ty<'db>>,
1861+
) -> Result<(), HirDisplayError> {
1862+
if parameters.is_empty() {
1863+
return Ok(());
1864+
}
1865+
1866+
let parameters_to_write = generic_args_sans_defaults_ns(f, generic_def, parameters);
1867+
1868+
if !parameters_to_write.is_empty() {
1869+
write!(f, "<")?;
1870+
hir_fmt_generic_arguments_ns(f, parameters_to_write, self_)?;
1871+
write!(f, ">")?;
1872+
}
1873+
1874+
Ok(())
1875+
}
1876+
1877+
fn generic_args_sans_defaults_ns<'ga, 'db>(
1878+
f: &mut HirFormatter<'_>,
1879+
generic_def: Option<hir_def::GenericDefId>,
1880+
parameters: &'ga [crate::next_solver::GenericArg<'db>],
1881+
) -> &'ga [crate::next_solver::GenericArg<'db>] {
1882+
let interner = DbInterner::new_with(f.db, Some(f.krate()), None);
1883+
if f.display_kind.is_source_code() || f.omit_verbose_types() {
1884+
match generic_def
1885+
.map(|generic_def_id| f.db.generic_defaults(generic_def_id))
1886+
.filter(|it| !it.is_empty())
1887+
{
1888+
None => parameters,
1889+
Some(default_parameters) => {
1890+
let should_show = |arg: &crate::next_solver::GenericArg<'db>, i: usize| {
1891+
let is_err = |arg: &crate::next_solver::GenericArg<'db>| match arg.kind() {
1892+
rustc_type_ir::GenericArgKind::Lifetime(it) => {
1893+
matches!(it.kind(), RegionKind::ReError(..))
1894+
}
1895+
rustc_type_ir::GenericArgKind::Type(it) => {
1896+
matches!(it.kind(), rustc_type_ir::TyKind::Error(..))
1897+
}
1898+
rustc_type_ir::GenericArgKind::Const(it) => {
1899+
matches!(it.kind(), rustc_type_ir::ConstKind::Error(..),)
1900+
}
1901+
};
1902+
// if the arg is error like, render it to inform the user
1903+
if is_err(arg) {
1904+
return true;
1905+
}
1906+
// otherwise, if the arg is equal to the param default, hide it (unless the
1907+
// default is an error which can happen for the trait Self type)
1908+
match default_parameters.get(i) {
1909+
None => true,
1910+
Some(default_parameter) => {
1911+
// !is_err(default_parameter.skip_binders())
1912+
// &&
1913+
arg != &default_parameter
1914+
.clone()
1915+
.substitute(
1916+
Interner,
1917+
&convert_args_for_result(interner, &parameters[..i]),
1918+
)
1919+
.to_nextsolver(interner)
1920+
}
1921+
}
1922+
};
1923+
let mut default_from = 0;
1924+
for (i, parameter) in parameters.iter().enumerate() {
1925+
if should_show(parameter, i) {
1926+
default_from = i + 1;
1927+
}
1928+
}
1929+
&parameters[0..default_from]
1930+
}
1931+
}
1932+
} else {
1933+
parameters
1934+
}
1935+
}
1936+
18061937
fn hir_fmt_generic_arguments(
18071938
f: &mut HirFormatter<'_>,
18081939
parameters: &[GenericArg],
@@ -1827,6 +1958,30 @@ fn hir_fmt_generic_arguments(
18271958
Ok(())
18281959
}
18291960

1961+
fn hir_fmt_generic_arguments_ns<'db>(
1962+
f: &mut HirFormatter<'_>,
1963+
parameters: &[crate::next_solver::GenericArg<'db>],
1964+
self_: Option<crate::next_solver::Ty<'db>>,
1965+
) -> Result<(), HirDisplayError> {
1966+
let mut first = true;
1967+
let lifetime_offset = parameters.iter().position(|arg| arg.region().is_some());
1968+
1969+
let (ty_or_const, lifetimes) = match lifetime_offset {
1970+
Some(offset) => parameters.split_at(offset),
1971+
None => (parameters, &[][..]),
1972+
};
1973+
for generic_arg in lifetimes.iter().chain(ty_or_const) {
1974+
if !mem::take(&mut first) {
1975+
write!(f, ", ")?;
1976+
}
1977+
match self_ {
1978+
self_ @ Some(_) if generic_arg.ty() == self_ => write!(f, "Self")?,
1979+
_ => generic_arg.hir_fmt(f)?,
1980+
}
1981+
}
1982+
Ok(())
1983+
}
1984+
18301985
impl HirDisplay for CallableSig {
18311986
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
18321987
let CallableSig { params_and_return: _, is_varargs, safety, abi: _ } = *self;
@@ -2067,6 +2222,20 @@ impl HirDisplay for TraitRef {
20672222
}
20682223
}
20692224

2225+
impl<'db> HirDisplay for crate::next_solver::TraitRef<'db> {
2226+
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
2227+
let trait_ = match self.def_id {
2228+
SolverDefId::TraitId(id) => id,
2229+
_ => unreachable!(),
2230+
};
2231+
f.start_location_link(trait_.into());
2232+
write!(f, "{}", f.db.trait_signature(trait_).name.display(f.db, f.edition()))?;
2233+
f.end_location_link();
2234+
let substs = self.args.as_slice();
2235+
hir_fmt_generic_args(f, &substs[1..], None, substs[0].ty())
2236+
}
2237+
}
2238+
20702239
impl HirDisplay for WhereClause {
20712240
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
20722241
if f.should_truncate() {
@@ -2147,6 +2316,35 @@ impl HirDisplay for LifetimeData {
21472316
}
21482317
}
21492318

2319+
impl<'db> HirDisplay for crate::next_solver::Region<'db> {
2320+
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
2321+
match self.kind() {
2322+
rustc_type_ir::RegionKind::RePlaceholder(idx) => {
2323+
let id = bound_var_to_lifetime_idx(f.db, idx.bound.var);
2324+
let generics = generics(f.db, id.parent);
2325+
let param_data = &generics[id.local_id];
2326+
write!(f, "{}", param_data.name.display(f.db, f.edition()))?;
2327+
Ok(())
2328+
}
2329+
rustc_type_ir::RegionKind::ReBound(db, idx) => {
2330+
write!(f, "?{}.{}", db.as_u32(), idx.var.as_u32())
2331+
}
2332+
rustc_type_ir::RegionKind::ReVar(_) => write!(f, "_"),
2333+
rustc_type_ir::RegionKind::ReStatic => write!(f, "'static"),
2334+
rustc_type_ir::RegionKind::ReError(..) => {
2335+
if cfg!(test) {
2336+
write!(f, "'?")
2337+
} else {
2338+
write!(f, "'_")
2339+
}
2340+
}
2341+
rustc_type_ir::RegionKind::ReErased => write!(f, "'<erased>"),
2342+
rustc_type_ir::RegionKind::ReEarlyParam(_) => write!(f, "<param>"),
2343+
rustc_type_ir::RegionKind::ReLateParam(_) => write!(f, "<late-param>"),
2344+
}
2345+
}
2346+
}
2347+
21502348
impl HirDisplay for DomainGoal {
21512349
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
21522350
match self {

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,29 @@ impl<'db> std::fmt::Debug for GenericArg<'db> {
3535
}
3636
}
3737

38+
impl<'db> GenericArg<'db> {
39+
pub fn ty(self) -> Option<Ty<'db>> {
40+
match self.kind() {
41+
GenericArgKind::Type(ty) => Some(ty),
42+
_ => None,
43+
}
44+
}
45+
46+
pub fn expect_ty(self) -> Ty<'db> {
47+
match self.kind() {
48+
GenericArgKind::Type(ty) => ty,
49+
_ => panic!("Expected ty, got {:?}", self),
50+
}
51+
}
52+
53+
pub fn region(self) -> Option<Region<'db>> {
54+
match self.kind() {
55+
GenericArgKind::Lifetime(r) => Some(r),
56+
_ => None,
57+
}
58+
}
59+
}
60+
3861
impl<'db> From<Term<'db>> for GenericArg<'db> {
3962
fn from(value: Term<'db>) -> Self {
4063
match value {

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ use rustc_type_ir::{
1818
shift_vars,
1919
solve::Goal,
2020
};
21-
use salsa::plumbing::AsId;
21+
use salsa::plumbing::FromId;
22+
use salsa::{Id, plumbing::AsId};
2223

2324
use crate::{
2425
ConcreteConst, ConstScalar, ImplTraitId, Interner, MemoryMap,
2526
db::{
26-
HirDatabase, InternedClosureId, InternedCoroutineId, InternedOpaqueTyId,
27-
InternedTypeOrConstParamId,
27+
HirDatabase, InternedClosureId, InternedCoroutineId, InternedLifetimeParamId,
28+
InternedOpaqueTyId, InternedTypeOrConstParamId,
2829
},
2930
from_assoc_type_id, from_chalk_trait_id,
3031
mapping::ToChalk,
@@ -55,6 +56,24 @@ pub fn to_placeholder_idx<T: Clone + std::fmt::Debug>(
5556
}
5657
}
5758

59+
pub fn bound_var_to_type_or_const_param_idx(
60+
db: &dyn HirDatabase,
61+
var: rustc_type_ir::BoundVar,
62+
) -> TypeOrConstParamId {
63+
// SAFETY: We cannot really encapsulate this unfortunately, so just hope this is sound.
64+
let interned_id = InternedTypeOrConstParamId::from_id(unsafe { Id::from_index(var.as_u32()) });
65+
interned_id.loc(db)
66+
}
67+
68+
pub fn bound_var_to_lifetime_idx(
69+
db: &dyn HirDatabase,
70+
var: rustc_type_ir::BoundVar,
71+
) -> LifetimeParamId {
72+
// SAFETY: We cannot really encapsulate this unfortunately, so just hope this is sound.
73+
let interned_id = InternedLifetimeParamId::from_id(unsafe { Id::from_index(var.as_u32()) });
74+
interned_id.loc(db)
75+
}
76+
5877
pub fn convert_binder_to_early_binder<'db, T: rustc_type_ir::TypeFoldable<DbInterner<'db>>>(
5978
interner: DbInterner<'db>,
6079
binder: rustc_type_ir::Binder<DbInterner<'db>, T>,

0 commit comments

Comments
 (0)