Skip to content

Commit d03fd87

Browse files
Adopt even more custom types in the new solver
A lot of simplification and fun.
1 parent 9621689 commit d03fd87

File tree

12 files changed

+220
-222
lines changed

12 files changed

+220
-222
lines changed

crates/hir-def/src/lang_item.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ impl LangItemTarget {
8484
_ => None,
8585
}
8686
}
87+
88+
pub fn as_adt(self) -> Option<AdtId> {
89+
match self {
90+
LangItemTarget::Union(it) => Some(it.into()),
91+
LangItemTarget::EnumId(it) => Some(it.into()),
92+
LangItemTarget::Struct(it) => Some(it.into()),
93+
_ => None,
94+
}
95+
}
8796
}
8897

8998
/// Salsa query. This will look for lang items in a specific crate.
@@ -289,6 +298,10 @@ impl LangItem {
289298
lang_item(db, start_crate, self).and_then(|t| t.as_trait())
290299
}
291300

301+
pub fn resolve_adt(self, db: &dyn DefDatabase, start_crate: Crate) -> Option<AdtId> {
302+
lang_item(db, start_crate, self).and_then(|t| t.as_adt())
303+
}
304+
292305
pub fn resolve_enum(self, db: &dyn DefDatabase, start_crate: Crate) -> Option<EnumId> {
293306
lang_item(db, start_crate, self).and_then(|t| t.as_enum())
294307
}

crates/hir-ty/src/display.rs

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ use crate::{
5959
lt_from_placeholder_idx,
6060
mir::pad16,
6161
next_solver::{
62-
BoundExistentialPredicate, Ctor, DbInterner, GenericArgs, SolverDefId,
62+
BoundExistentialPredicate, DbInterner, GenericArgs, SolverDefId,
6363
mapping::{
6464
ChalkToNextSolver, convert_args_for_result, convert_const_for_result,
6565
convert_region_for_result, convert_ty_for_result,
@@ -911,14 +911,13 @@ fn render_const_scalar_inner(
911911
f.write_str("&")?;
912912
render_const_scalar_ns(f, bytes, memory_map, t)
913913
}
914-
TyKind::Adt(adt, _) if b.len() == 2 * size_of::<usize>() => match adt.def_id() {
915-
SolverDefId::AdtId(hir_def::AdtId::StructId(s)) => {
914+
TyKind::Adt(adt, _) if b.len() == 2 * size_of::<usize>() => match adt.def_id().0 {
915+
hir_def::AdtId::StructId(s) => {
916916
let data = f.db.struct_signature(s);
917917
write!(f, "&{}", data.name.display(f.db, f.edition()))?;
918918
Ok(())
919919
}
920-
SolverDefId::AdtId(_) => f.write_str("<unsized-enum-or-union>"),
921-
_ => unreachable!(),
920+
_ => f.write_str("<unsized-enum-or-union>"),
922921
},
923922
_ => {
924923
let addr = usize::from_le_bytes(match b.try_into() {
@@ -966,10 +965,7 @@ fn render_const_scalar_inner(
966965
f.write_str(")")
967966
}
968967
TyKind::Adt(def, args) => {
969-
let def = match def.def_id() {
970-
SolverDefId::AdtId(def) => def,
971-
_ => unreachable!(),
972-
};
968+
let def = def.def_id().0;
973969
let Ok(layout) = f.db.layout_of_adt(def, args, trait_env.clone()) else {
974970
return f.write_str("<layout-error>");
975971
};
@@ -1300,12 +1296,7 @@ impl<'db> HirDisplay for crate::next_solver::Ty<'db> {
13001296
sig.hir_fmt(f)?;
13011297
}
13021298
TyKind::FnDef(def, args) => {
1303-
let def = match def {
1304-
SolverDefId::FunctionId(id) => CallableDefId::FunctionId(id),
1305-
SolverDefId::Ctor(Ctor::Enum(e)) => CallableDefId::EnumVariantId(e),
1306-
SolverDefId::Ctor(Ctor::Struct(s)) => CallableDefId::StructId(s),
1307-
_ => unreachable!(),
1308-
};
1299+
let def = def.0;
13091300
let sig = db
13101301
.callable_item_signature(def)
13111302
.substitute(Interner, &convert_args_for_result(interner, args.as_slice()));
@@ -1406,10 +1397,7 @@ impl<'db> HirDisplay for crate::next_solver::Ty<'db> {
14061397
}
14071398
}
14081399
TyKind::Adt(def, parameters) => {
1409-
let def_id = match def.def_id() {
1410-
SolverDefId::AdtId(id) => id,
1411-
_ => unreachable!(),
1412-
};
1400+
let def_id = def.def_id().0;
14131401
f.start_location_link(def_id.into());
14141402
match f.display_kind {
14151403
DisplayKind::Diagnostics | DisplayKind::Test => {
@@ -1448,7 +1436,7 @@ impl<'db> HirDisplay for crate::next_solver::Ty<'db> {
14481436
hir_fmt_generics(
14491437
f,
14501438
convert_args_for_result(interner, parameters.as_slice()).as_slice(Interner),
1451-
def.def_id().try_into().ok(),
1439+
Some(def.def_id().0.into()),
14521440
None,
14531441
)?;
14541442
}
@@ -1466,13 +1454,9 @@ impl<'db> HirDisplay for crate::next_solver::Ty<'db> {
14661454

14671455
projection_ty.hir_fmt(f)?;
14681456
}
1469-
TyKind::Foreign(type_alias) => {
1470-
let alias = match type_alias {
1471-
SolverDefId::TypeAliasId(id) => id,
1472-
_ => unreachable!(),
1473-
};
1474-
let type_alias = db.type_alias_signature(alias);
1475-
f.start_location_link(alias.into());
1457+
TyKind::Foreign(alias) => {
1458+
let type_alias = db.type_alias_signature(alias.0);
1459+
f.start_location_link(alias.0.into());
14761460
write!(f, "{}", type_alias.name.display(f.db, f.edition()))?;
14771461
f.end_location_link();
14781462
}
@@ -1549,10 +1533,7 @@ impl<'db> HirDisplay for crate::next_solver::Ty<'db> {
15491533
}
15501534
}
15511535
TyKind::Closure(id, substs) => {
1552-
let id = match id {
1553-
SolverDefId::InternedClosureId(id) => id,
1554-
_ => unreachable!(),
1555-
};
1536+
let id = id.0;
15561537
let substs = convert_args_for_result(interner, substs.as_slice());
15571538
if f.display_kind.is_source_code() {
15581539
if !f.display_kind.allows_opaque() {

crates/hir-ty/src/layout.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
consteval_nextsolver::try_const_usize,
2626
db::HirDatabase,
2727
next_solver::{
28-
DbInterner, GenericArgs, ParamEnv, SolverDefId, Ty, TyKind, TypingMode,
28+
DbInterner, GenericArgs, ParamEnv, Ty, TyKind, TypingMode,
2929
infer::{DbInternerInferExt, traits::ObligationCause},
3030
mapping::{ChalkToNextSolver, convert_args_for_result},
3131
project::solve_normalize::deeply_normalize,
@@ -323,14 +323,10 @@ pub fn layout_of_ty_query<'db>(
323323
ptr.valid_range_mut().start = 1;
324324
Layout::scalar(dl, ptr)
325325
}
326-
TyKind::Closure(c, args) => {
327-
let id = match c {
328-
SolverDefId::InternedClosureId(id) => id,
329-
_ => unreachable!(),
330-
};
331-
let def = db.lookup_intern_closure(id);
326+
TyKind::Closure(id, args) => {
327+
let def = db.lookup_intern_closure(id.0);
332328
let infer = db.infer(def.0);
333-
let (captures, _) = infer.closure_info(&id.into());
329+
let (captures, _) = infer.closure_info(&id.0.into());
334330
let fields = captures
335331
.iter()
336332
.map(|it| {

crates/hir-ty/src/method_resolution.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,7 @@ impl TyFingerprint {
161161
rustc_ast_ir::Mutability::Mut => TyFingerprint::RawPtr(Mutability::Mut),
162162
rustc_ast_ir::Mutability::Not => TyFingerprint::RawPtr(Mutability::Not),
163163
},
164-
TyKind::Foreign(def) => {
165-
let SolverDefId::TypeAliasId(def) = def else { unreachable!() };
166-
TyFingerprint::ForeignType(crate::to_foreign_def_id(def))
167-
}
164+
TyKind::Foreign(def) => TyFingerprint::ForeignType(crate::to_foreign_def_id(def.0)),
168165
TyKind::Dynamic(bounds, _, _) => {
169166
let trait_ref = bounds
170167
.as_slice()

crates/hir-ty/src/mir/eval.rs

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use crate::{
4242
layout::{Layout, LayoutError, RustcEnumVariantIdx},
4343
method_resolution::{is_dyn_method, lookup_impl_const},
4444
next_solver::{
45-
Ctor, DbInterner, SolverDefId,
45+
DbInterner,
4646
mapping::{ChalkToNextSolver, convert_args_for_result, convert_ty_for_result},
4747
},
4848
static_lifetime,
@@ -2366,8 +2366,8 @@ impl<'db> Evaluator<'db> {
23662366
let new_id = self.vtable_map.id(ty);
23672367
self.write_memory(addr, &new_id.to_le_bytes())?;
23682368
}
2369-
TyKind::Adt(id, args) => match id.def_id() {
2370-
SolverDefId::AdtId(AdtId::StructId(s)) => {
2369+
TyKind::Adt(id, args) => match id.def_id().0 {
2370+
AdtId::StructId(s) => {
23712371
for (i, (_, ty)) in self.db.field_types_ns(s.into()).iter().enumerate() {
23722372
let offset = layout.fields.offset(i).bytes_usize();
23732373
let ty = ty.instantiate(interner, args);
@@ -2380,8 +2380,8 @@ impl<'db> Evaluator<'db> {
23802380
)?;
23812381
}
23822382
}
2383-
SolverDefId::AdtId(AdtId::UnionId(_)) => (),
2384-
SolverDefId::AdtId(AdtId::EnumId(e)) => {
2383+
AdtId::UnionId(_) => (),
2384+
AdtId::EnumId(e) => {
23852385
if let Some((ev, layout)) = detect_variant_from_bytes(
23862386
&layout,
23872387
self.db,
@@ -2402,7 +2402,6 @@ impl<'db> Evaluator<'db> {
24022402
}
24032403
}
24042404
}
2405-
_ => unreachable!(),
24062405
},
24072406
TyKind::Tuple(tys) => {
24082407
for (id, ty) in tys.iter().enumerate() {
@@ -2472,38 +2471,24 @@ impl<'db> Evaluator<'db> {
24722471
let interner = DbInterner::new_with(self.db, None, None);
24732472
use rustc_type_ir::TyKind;
24742473
match next_ty.kind() {
2475-
TyKind::FnDef(def, generic_args) => {
2476-
let def = match def {
2477-
SolverDefId::FunctionId(id) => CallableDefId::FunctionId(id),
2478-
SolverDefId::Ctor(Ctor::Struct(s)) => CallableDefId::StructId(s),
2479-
SolverDefId::Ctor(Ctor::Enum(e)) => CallableDefId::EnumVariantId(e),
2480-
_ => unreachable!(),
2481-
};
2482-
self.exec_fn_def(
2483-
def,
2484-
&convert_args_for_result(interner, generic_args.as_slice()),
2485-
destination,
2486-
args,
2487-
locals,
2488-
target_bb,
2489-
span,
2490-
)
2491-
}
2492-
TyKind::Closure(id, generic_args) => {
2493-
let id = match id {
2494-
SolverDefId::InternedClosureId(id) => id,
2495-
_ => unreachable!(),
2496-
};
2497-
self.exec_closure(
2498-
id.into(),
2499-
bytes.slice(0..0),
2500-
&convert_args_for_result(interner, generic_args.as_slice()),
2501-
destination,
2502-
args,
2503-
locals,
2504-
span,
2505-
)
2506-
}
2474+
TyKind::FnDef(def, generic_args) => self.exec_fn_def(
2475+
def.0,
2476+
&convert_args_for_result(interner, generic_args.as_slice()),
2477+
destination,
2478+
args,
2479+
locals,
2480+
target_bb,
2481+
span,
2482+
),
2483+
TyKind::Closure(id, generic_args) => self.exec_closure(
2484+
id.0.into(),
2485+
bytes.slice(0..0),
2486+
&convert_args_for_result(interner, generic_args.as_slice()),
2487+
destination,
2488+
args,
2489+
locals,
2490+
span,
2491+
),
25072492
_ => Err(MirEvalError::InternalError("function pointer to non function".into())),
25082493
}
25092494
}

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

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Definition of `SolverDefId`
22
33
use hir_def::{
4-
AdtId, ConstId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, StaticId, StructId,
5-
TraitId, TypeAliasId, UnionId,
4+
AdtId, CallableDefId, ConstId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId,
5+
StaticId, StructId, TraitId, TypeAliasId, UnionId,
66
};
77
use rustc_type_ir::inherent;
88
use stdx::impl_from;
@@ -42,7 +42,8 @@ impl_from!(
4242
TypeAliasId,
4343
InternedClosureId,
4444
InternedCoroutineId,
45-
InternedOpaqueTyId
45+
InternedOpaqueTyId,
46+
Ctor
4647
for SolverDefId
4748
);
4849

@@ -145,3 +146,59 @@ macro_rules! declare_id_wrapper {
145146
}
146147

147148
declare_id_wrapper!(TraitIdWrapper, TraitId);
149+
declare_id_wrapper!(TypeAliasIdWrapper, TypeAliasId);
150+
declare_id_wrapper!(ClosureIdWrapper, InternedClosureId);
151+
declare_id_wrapper!(CoroutineIdWrapper, InternedCoroutineId);
152+
declare_id_wrapper!(AdtIdWrapper, AdtId);
153+
declare_id_wrapper!(ImplIdWrapper, ImplId);
154+
155+
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
156+
pub struct CallableIdWrapper(pub CallableDefId);
157+
158+
impl std::fmt::Debug for CallableIdWrapper {
159+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
160+
std::fmt::Debug::fmt(&self.0, f)
161+
}
162+
}
163+
impl From<CallableIdWrapper> for CallableDefId {
164+
#[inline]
165+
fn from(value: CallableIdWrapper) -> CallableDefId {
166+
value.0
167+
}
168+
}
169+
impl From<CallableDefId> for CallableIdWrapper {
170+
#[inline]
171+
fn from(value: CallableDefId) -> CallableIdWrapper {
172+
Self(value)
173+
}
174+
}
175+
impl From<CallableIdWrapper> for SolverDefId {
176+
#[inline]
177+
fn from(value: CallableIdWrapper) -> SolverDefId {
178+
match value.0 {
179+
CallableDefId::FunctionId(it) => it.into(),
180+
CallableDefId::StructId(it) => Ctor::Struct(it).into(),
181+
CallableDefId::EnumVariantId(it) => Ctor::Enum(it).into(),
182+
}
183+
}
184+
}
185+
impl TryFrom<SolverDefId> for CallableIdWrapper {
186+
type Error = ();
187+
#[inline]
188+
fn try_from(value: SolverDefId) -> Result<Self, Self::Error> {
189+
match value {
190+
SolverDefId::FunctionId(it) => Ok(Self(it.into())),
191+
SolverDefId::Ctor(Ctor::Struct(it)) => Ok(Self(it.into())),
192+
SolverDefId::Ctor(Ctor::Enum(it)) => Ok(Self(it.into())),
193+
_ => Err(()),
194+
}
195+
}
196+
}
197+
impl<'db> inherent::DefId<DbInterner<'db>> for CallableIdWrapper {
198+
fn as_local(self) -> Option<SolverDefId> {
199+
Some(self.into())
200+
}
201+
fn is_local(self) -> bool {
202+
true
203+
}
204+
}

crates/hir-ty/src/next_solver/infer/canonical/canonicalizer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,15 +400,15 @@ impl<'cx, 'db> TypeFolder<DbInterner<'db>> for Canonicalizer<'cx, 'db> {
400400
TyKind::Infer(IntVar(vid)) => {
401401
let nt = self.infcx.opportunistic_resolve_int_var(vid);
402402
if nt != t {
403-
return self.fold_ty(nt);
403+
self.fold_ty(nt)
404404
} else {
405405
self.canonicalize_ty_var(CanonicalVarKind::Int, t)
406406
}
407407
}
408408
TyKind::Infer(FloatVar(vid)) => {
409409
let nt = self.infcx.opportunistic_resolve_float_var(vid);
410410
if nt != t {
411-
return self.fold_ty(nt);
411+
self.fold_ty(nt)
412412
} else {
413413
self.canonicalize_ty_var(CanonicalVarKind::Float, t)
414414
}

crates/hir-ty/src/next_solver/infer/canonical/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ impl<'db> InferCtxt<'db> {
8282
let var_values = CanonicalVarValues::instantiate(
8383
self.interner,
8484
canonical.variables,
85-
|var_values, info| {
86-
self.instantiate_canonical_var(info, &var_values, |ui| universes[ui])
87-
},
85+
|var_values, info| self.instantiate_canonical_var(info, var_values, |ui| universes[ui]),
8886
);
8987
let result = canonical.instantiate(self.interner, &var_values);
9088
(result, var_values)

0 commit comments

Comments
 (0)