Skip to content

Commit 578f046

Browse files
authored
Merge pull request #458 from brendanzab/avoid-item-names
Avoid item names
2 parents 0b233a7 + 88f6b2d commit 578f046

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

fathom/src/surface/distillation.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ impl<'interner, 'arena, 'env> Context<'interner, 'arena, 'env> {
4545
}
4646
}
4747

48+
fn is_bound(&self, name: StringId) -> bool {
49+
(self.local_names.iter()).any(|local_name| *local_name == Some(name))
50+
|| self.item_names.iter().any(|item_name| *item_name == name)
51+
}
52+
4853
fn local_len(&mut self) -> EnvLen {
4954
self.local_names.len()
5055
}
@@ -84,12 +89,12 @@ impl<'interner, 'arena, 'env> Context<'interner, 'arena, 'env> {
8489
}
8590
}
8691

87-
/// Generate a fresh name that does not appear in `self.local_names`
92+
/// Generate a fresh name that is not currently bound in the context
8893
fn gen_fresh_name(&mut self) -> StringId {
8994
let mut counter = 0;
9095
loop {
9196
let name = self.interner.borrow_mut().get_alphabetic_name(counter);
92-
match self.local_names.iter().any(|symbol| *symbol == Some(name)) {
97+
match self.is_bound(name) {
9398
true => counter += 1,
9499
false => return name,
95100
}
@@ -125,7 +130,7 @@ impl<'interner, 'arena, 'env> Context<'interner, 'arena, 'env> {
125130
r#type,
126131
expr,
127132
} => {
128-
let r#type = scope.to_scope(self.synth(r#type));
133+
let r#type = scope.to_scope(self.check(r#type));
129134
let expr = scope.to_scope(self.check(expr));
130135

131136
Item::Def(ItemDef {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//~ mode = "module"
2+
3+
// An item to throw off fresh name generation
4+
def a = {};
5+
6+
def const1 : fun (@A : _) (@B : _) -> A -> B -> A =
7+
// The binders in the distilled term should use fresh names (starting with
8+
// `a`, `b`, ...), avoiding the binding from the let expression and the
9+
// top-level item
10+
fun @_ @_ x y =>
11+
// The following let bindings will force the type parameters to be
12+
// referenced in the type annotation of the let expressions:
13+
let x1 = x;
14+
let y1 = y;
15+
16+
x;
17+
18+
def const2 : fun (@A : _) (@B : _) -> A -> B -> A =
19+
// A let expression to throw off fresh name generation
20+
let b = {};
21+
22+
// The binders in the distilled term should use fresh names (starting with
23+
// `a`, `b`, ...), avoiding the binding from the let expression and the
24+
// top-level item
25+
fun @_ @_ x y =>
26+
// The following let bindings will force the type parameters to be
27+
// referenced in the type annotation of the let expressions:
28+
let x1 = x;
29+
let y1 = y;
30+
31+
x;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
stdout = '''
2+
def a : () = ();
3+
def const1 : fun (@A : Type) (@B : Type) -> A -> B -> A =
4+
fun @b @c x y => let x1 : b = x;
5+
let y1 : c = y;
6+
x;
7+
def const2 : fun (@A : Type) (@B : Type) -> A -> B -> A = let b : () = ();
8+
fun @c @d x y => let x1 : c = x;
9+
let y1 : d = y;
10+
x;
11+
'''
12+
stderr = ''

0 commit comments

Comments
 (0)