Skip to content

Commit 5c336e2

Browse files
Merge #5968
5968: Lookup ADT and associated type names for chalk debugging / tweak chalk interner r=flodiebold a=nathanwhit This PR improves the chalk program writing integration by looking up the names for ADTs and associated types, making the output much more readable. There are also a few small changes to the interner, which gives some nice performance improvements. We clone `Ty`s and `ProgramClause`s relatively often in chalk, so wrapping them in `Arc`s is a perf win. This takes the time for performing type inference on the rust-analyzer codebase from 40s to 33s on my machine. Co-authored-by: Nathan Whitaker <[email protected]>
2 parents 60c8941 + bf0b194 commit 5c336e2

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

crates/hir_ty/src/traits/chalk.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,17 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
244244
let id = from_chalk(self.db, trait_id);
245245
self.db.trait_data(id).name.to_string()
246246
}
247-
// FIXME: lookup names
248-
fn adt_name(&self, struct_id: chalk_ir::AdtId<Interner>) -> String {
249-
let datum = self.db.struct_datum(self.krate, struct_id);
250-
format!("{:?}", datum.name(&Interner))
247+
fn adt_name(&self, adt_id: chalk_ir::AdtId<Interner>) -> String {
248+
let id = from_chalk(self.db, adt_id);
249+
match id {
250+
hir_def::AdtId::StructId(id) => self.db.struct_data(id).name.to_string(),
251+
hir_def::AdtId::EnumId(id) => self.db.enum_data(id).name.to_string(),
252+
hir_def::AdtId::UnionId(id) => self.db.union_data(id).name.to_string(),
253+
}
251254
}
252255
fn assoc_type_name(&self, assoc_ty_id: chalk_ir::AssocTypeId<Interner>) -> String {
253-
format!("Assoc_{}", assoc_ty_id.0)
256+
let id = self.db.associated_ty_data(assoc_ty_id).name;
257+
self.db.type_alias_data(id).name.to_string()
254258
}
255259
fn opaque_type_name(&self, opaque_ty_id: chalk_ir::OpaqueTyId<Interner>) -> String {
256260
format!("Opaque_{}", opaque_ty_id.0)

crates/hir_ty/src/traits/chalk/interner.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ pub type OpaqueTyId = chalk_ir::OpaqueTyId<Interner>;
2626
pub type OpaqueTyDatum = chalk_solve::rust_ir::OpaqueTyDatum<Interner>;
2727

2828
impl chalk_ir::interner::Interner for Interner {
29-
type InternedType = Box<chalk_ir::TyData<Self>>; // FIXME use Arc?
29+
type InternedType = Arc<chalk_ir::TyData<Self>>;
3030
type InternedLifetime = chalk_ir::LifetimeData<Self>;
3131
type InternedConst = Arc<chalk_ir::ConstData<Self>>;
3232
type InternedConcreteConst = ();
3333
type InternedGenericArg = chalk_ir::GenericArgData<Self>;
3434
type InternedGoal = Arc<GoalData<Self>>;
3535
type InternedGoals = Vec<Goal<Self>>;
3636
type InternedSubstitution = Vec<GenericArg<Self>>;
37-
type InternedProgramClause = chalk_ir::ProgramClauseData<Self>;
37+
type InternedProgramClause = Arc<chalk_ir::ProgramClauseData<Self>>;
3838
type InternedProgramClauses = Arc<[chalk_ir::ProgramClause<Self>]>;
3939
type InternedQuantifiedWhereClauses = Vec<chalk_ir::QuantifiedWhereClause<Self>>;
4040
type InternedVariableKinds = Vec<chalk_ir::VariableKind<Self>>;
@@ -197,11 +197,11 @@ impl chalk_ir::interner::Interner for Interner {
197197
tls::with_current_program(|prog| Some(prog?.debug_quantified_where_clauses(clauses, fmt)))
198198
}
199199

200-
fn intern_ty(&self, ty: chalk_ir::TyData<Self>) -> Box<chalk_ir::TyData<Self>> {
201-
Box::new(ty)
200+
fn intern_ty(&self, ty: chalk_ir::TyData<Self>) -> Arc<chalk_ir::TyData<Self>> {
201+
Arc::new(ty)
202202
}
203203

204-
fn ty_data<'a>(&self, ty: &'a Box<chalk_ir::TyData<Self>>) -> &'a chalk_ir::TyData<Self> {
204+
fn ty_data<'a>(&self, ty: &'a Arc<chalk_ir::TyData<Self>>) -> &'a chalk_ir::TyData<Self> {
205205
ty
206206
}
207207

@@ -230,7 +230,7 @@ impl chalk_ir::interner::Interner for Interner {
230230
constant
231231
}
232232

233-
fn const_eq(&self, _ty: &Box<chalk_ir::TyData<Self>>, _c1: &(), _c2: &()) -> bool {
233+
fn const_eq(&self, _ty: &Arc<chalk_ir::TyData<Self>>, _c1: &(), _c2: &()) -> bool {
234234
true
235235
}
236236

@@ -284,13 +284,13 @@ impl chalk_ir::interner::Interner for Interner {
284284
fn intern_program_clause(
285285
&self,
286286
data: chalk_ir::ProgramClauseData<Self>,
287-
) -> chalk_ir::ProgramClauseData<Self> {
288-
data
287+
) -> Arc<chalk_ir::ProgramClauseData<Self>> {
288+
Arc::new(data)
289289
}
290290

291291
fn program_clause_data<'a>(
292292
&self,
293-
clause: &'a chalk_ir::ProgramClauseData<Self>,
293+
clause: &'a Arc<chalk_ir::ProgramClauseData<Self>>,
294294
) -> &'a chalk_ir::ProgramClauseData<Self> {
295295
clause
296296
}

crates/hir_ty/src/traits/chalk/mapping.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,18 @@ impl ToChalk for hir_def::ImplId {
464464
}
465465
}
466466

467+
impl ToChalk for hir_def::AdtId {
468+
type Chalk = AdtId;
469+
470+
fn to_chalk(self, _db: &dyn HirDatabase) -> Self::Chalk {
471+
chalk_ir::AdtId(self.into())
472+
}
473+
474+
fn from_chalk(_db: &dyn HirDatabase, id: AdtId) -> Self {
475+
id.0
476+
}
477+
}
478+
467479
impl ToChalk for CallableDefId {
468480
type Chalk = FnDefId;
469481

0 commit comments

Comments
 (0)