Skip to content

Commit ee76e85

Browse files
committed
Track canonical names in CheckedModule
1 parent f705853 commit ee76e85

12 files changed

Lines changed: 66 additions & 70 deletions

File tree

compiler-core/checking2/src/core.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ pub struct ForallBinder {
5252
pub visible: bool,
5353
/// The unique identity attached to the type variable.
5454
pub name: Name,
55-
/// The source-level text of the type variable.
56-
pub text: SmolStrId,
5755
/// The kind of the type variable.
5856
pub kind: TypeId,
5957
}

compiler-core/checking2/src/core/generalise.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,7 @@ where
216216
}
217217
};
218218

219-
let text = context.queries.intern_smol_str(name.as_text());
220-
221-
let binder = ForallBinder { visible: false, name, text, kind };
219+
let binder = ForallBinder { visible: false, name, kind };
222220
let binder = context.intern_forall_binder(binder);
223221
quantified = context.intern_forall(binder, quantified);
224222
}
@@ -321,15 +319,13 @@ where
321319
for implicit_unification in implicits_unifications {
322320
match implicit_unification {
323321
ImplicitOrUnification::Implicit(name, kind) => {
324-
let text = context.queries.intern_smol_str(name.as_text());
325-
binders.push(ForallBinder { visible: false, name, text, kind })
322+
binders.push(ForallBinder { visible: false, name, kind })
326323
}
327324
ImplicitOrUnification::Unification(id, kind) => {
328325
let name = state.names.fresh();
329-
let text = context.queries.intern_smol_str(name.as_text());
330326
let rigid = context.intern_rigid(name, depth, kind);
331327
state.unifications.solve(id, rigid);
332-
binders.push(ForallBinder { visible: false, name, text, kind })
328+
binders.push(ForallBinder { visible: false, name, kind })
333329
}
334330
}
335331
}

compiler-core/checking2/src/core/pretty.rs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@ use pretty::{Arena, DocAllocator, DocBuilder};
66
use rustc_hash::FxHashMap;
77
use smol_str::{SmolStr, SmolStrBuilder};
88

9-
use crate::ExternalQueries;
109
use crate::core::{
1110
ForallBinder, ForallBinderId, Name, RowField, RowType, RowTypeId, SmolStrId, Synonym,
1211
SynonymId, Type, TypeId,
1312
};
13+
use crate::{CheckedModule, ExternalQueries};
1414

1515
type Doc<'a> = DocBuilder<'a, Arena<'a>, ()>;
1616

1717
pub struct Pretty<'a, Q> {
1818
queries: &'a Q,
1919
width: usize,
2020
signature: Option<&'a str>,
21-
names: FxHashMap<Name, SmolStr>,
21+
checked: &'a CheckedModule,
2222
}
2323

2424
impl<'a, Q: ExternalQueries> Pretty<'a, Q> {
25-
pub fn new(queries: &'a Q) -> Self {
26-
Pretty { queries, width: 100, signature: None, names: FxHashMap::default() }
25+
pub fn new(queries: &'a Q, checked: &'a CheckedModule) -> Self {
26+
Pretty { queries, width: 100, signature: None, checked }
2727
}
2828

2929
pub fn width(mut self, width: usize) -> Self {
@@ -36,15 +36,9 @@ impl<'a, Q: ExternalQueries> Pretty<'a, Q> {
3636
self
3737
}
3838

39-
pub fn names(mut self, names: impl IntoIterator<Item = (Name, SmolStr)>) -> Self {
40-
self.names.extend(names);
41-
self
42-
}
43-
4439
pub fn render(self, id: TypeId) -> SmolStr {
4540
let arena = Arena::new();
46-
let mut printer = Printer::new(&arena, self.queries);
47-
printer.names = self.names;
41+
let mut printer = Printer::new(&arena, self.queries, &self.checked.names);
4842

4943
let document = if let Some(name) = self.signature {
5044
printer.signature(name, id)
@@ -75,15 +69,19 @@ where
7569
{
7670
arena: &'a Arena<'a>,
7771
queries: &'a Q,
78-
names: FxHashMap<Name, SmolStr>,
72+
names: &'a FxHashMap<Name, SmolStrId>,
7973
}
8074

8175
impl<'a, Q> Printer<'a, Q>
8276
where
8377
Q: ExternalQueries,
8478
{
85-
fn new(arena: &'a Arena<'a>, queries: &'a Q) -> Printer<'a, Q> {
86-
Printer { arena, queries, names: FxHashMap::default() }
79+
fn new(
80+
arena: &'a Arena<'a>,
81+
queries: &'a Q,
82+
names: &'a FxHashMap<Name, SmolStrId>,
83+
) -> Printer<'a, Q> {
84+
Printer { arena, queries, names }
8785
}
8886

8987
fn lookup_type(&self, id: TypeId) -> Type {
@@ -239,10 +237,12 @@ where
239237
}
240238

241239
Type::Rigid(name, _, kind) => {
242-
let name = self.names.entry(name).or_insert_with(|| name.as_text());
243-
let name = SmolStr::clone(name);
240+
let text = match self.names.get(&name) {
241+
Some(&id) => self.lookup_smol_str(id),
242+
None => name.as_text(),
243+
};
244244
let kind = self.traverse(Precedence::Top, kind);
245-
self.arena.text(format!("({name} :: ")).append(kind).append(self.arena.text(")"))
245+
self.arena.text(format!("({text} :: ")).append(kind).append(self.arena.text(")"))
246246
}
247247

248248
Type::Unification(unification_id) => self.arena.text(format!("?{unification_id}")),
@@ -358,16 +358,13 @@ where
358358
inner = next_inner;
359359
}
360360

361-
// Register source-level names so rigid variables in the body
362-
// display their original names instead of synthetic ones.
363-
for binder in &binders {
364-
self.names.insert(binder.name, self.lookup_smol_str(binder.text));
365-
}
366-
367361
let binders = binders
368362
.iter()
369363
.map(|binder| {
370-
let text = self.lookup_smol_str(binder.text);
364+
let text = match self.names.get(&binder.name) {
365+
Some(&id) => self.lookup_smol_str(id),
366+
None => binder.name.as_text(),
367+
};
371368
let kind = self.traverse(Precedence::Top, binder.kind);
372369
self.arena
373370
.text(format!("({} :: ", text))

compiler-core/checking2/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use rustc_hash::FxHashMap;
1919
use smol_str::SmolStr;
2020

2121
use crate::core::{
22-
CheckedClass, CheckedInstance, CheckedSynonym, ForallBinder, ForallBinderId, Role, RowType,
23-
RowTypeId, Synonym, SynonymId, Type, TypeId,
22+
CheckedClass, CheckedInstance, CheckedSynonym, ForallBinder, ForallBinderId, Name, Role,
23+
RowType, RowTypeId, SmolStrId, Synonym, SynonymId, Type, TypeId,
2424
};
2525
use crate::error::CheckError;
2626

@@ -64,6 +64,7 @@ pub struct CheckedModule {
6464
pub roles: FxHashMap<TypeItemId, Arc<[Role]>>,
6565
pub nodes: CheckedNodes,
6666
pub errors: Vec<CheckError>,
67+
pub names: FxHashMap<Name, SmolStrId>,
6768
}
6869

6970
#[derive(Debug, Default, PartialEq, Eq)]
@@ -109,6 +110,10 @@ impl CheckedModule {
109110
pub fn lookup_roles(&self, id: TypeItemId) -> Option<Arc<[Role]>> {
110111
self.roles.get(&id).cloned()
111112
}
113+
114+
pub fn lookup_name(&self, name: Name) -> Option<SmolStrId> {
115+
self.names.get(&name).copied()
116+
}
112117
}
113118

114119
impl CheckedNodes {

compiler-core/checking2/src/source/type_items.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,10 @@ where
339339
};
340340

341341
let text = context.queries.intern_smol_str(text);
342+
state.checked.names.insert(name, text);
342343
let visible = equation_binding.visible;
343344

344-
binders.push(ForallBinder { visible, name, text, kind });
345+
binders.push(ForallBinder { visible, name, kind });
345346
}
346347

347348
Ok(binders)

compiler-core/checking2/src/source/types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,9 @@ where
366366
let text = if let Some(name) = &binding.name { SmolStr::clone(name) } else { name.as_text() };
367367
let text = context.queries.intern_smol_str(text);
368368

369+
state.checked.names.insert(name, text);
369370
state.bindings.bind_forall(binding.id, name, kind);
370-
Ok(ForallBinder { visible, name, text, kind })
371+
Ok(ForallBinder { visible, name, kind })
371372
}
372373

373374
pub fn infer_application_kind<Q>(

compiler-core/checking2/src/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl CheckState {
204204
Q: ExternalQueries,
205205
{
206206
let id = zonk::zonk(self, context, id)?;
207-
let pretty = pretty::Pretty::new(context.queries).render(id);
207+
let pretty = pretty::Pretty::new(context.queries, &self.checked).render(id);
208208
Ok(context.queries.intern_smol_str(pretty))
209209
}
210210

tests-integration/fixtures/checking2/015_operator_alias_invalid_kind/Main.snap

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests-integration/fixtures/checking2/018_type_operator_chain_polykind/Main.snap

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests-integration/fixtures/checking2/033_exhaustive_guards_otherwise/Main.snap

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)