Skip to content

Commit 8ccfd42

Browse files
committed
fix a macro scoping bug
1 parent 2d438e1 commit 8ccfd42

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/compile/mod.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,37 +1673,44 @@ impl Compiler {
16731673
))
16741674
}
16751675
}
1676-
fn find_name(&self, name: &str, skip_local: bool) -> Option<LocalName> {
1677-
self.find_name_impl(name, skip_local, false)
1676+
fn find_name(&self, name: &str, skip_temp: bool) -> Option<LocalName> {
1677+
self.find_name_impl(name, skip_temp, false)
16781678
}
16791679
fn find_name_impl(
16801680
&self,
16811681
name: &str,
1682-
skip_local: bool,
1682+
skip_temp: bool,
16831683
stop_at_binding: bool,
16841684
) -> Option<LocalName> {
1685-
if !skip_local {
1686-
if let Some(local) = self.scope.names.get(name).copied() {
1687-
return Some(local);
1688-
}
1689-
}
1685+
// println!("name: {name:?}, skip_temp: {skip_temp}");
1686+
// for scope in self.scopes() {
1687+
// println!(" {:?} {:?}", scope.kind, scope.names);
1688+
// }
16901689
let mut hit_file = false;
1691-
for scope in self.higher_scopes.iter().rev() {
1690+
let mut hit_temp = false;
1691+
for scope in self.scopes() {
16921692
if matches!(scope.kind, ScopeKind::File(_))
16931693
|| stop_at_binding && matches!(scope.kind, ScopeKind::Binding)
16941694
{
16951695
if hit_file {
16961696
break;
16971697
}
16981698
hit_file = true;
1699+
} else if matches!(scope.kind, ScopeKind::Temp(_)) {
1700+
if skip_temp && !hit_temp {
1701+
hit_temp = true;
1702+
continue;
1703+
}
1704+
} else if skip_temp && !hit_temp {
1705+
continue;
16991706
}
17001707
if let Some(local) = scope.names.get(name).copied() {
17011708
return Some(local);
17021709
}
17031710
}
17041711
// Attempt to look up the identifier as a non-macro
17051712
let as_non_macro =
1706-
self.find_name_impl(name.strip_suffix('!')?, skip_local, stop_at_binding)?;
1713+
self.find_name_impl(name.strip_suffix('!')?, skip_temp, stop_at_binding)?;
17071714
if let BindingKind::Module(_) | BindingKind::Import(_) =
17081715
self.asm.bindings[as_non_macro.index].kind
17091716
{

tests/macros.ua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ F! ← ⨬(
2828
F! ← -^0 i
2929
⍤⤙≍ i F!0
3030

31+
┌─╴M
32+
X ← "Inner"
33+
A! ← ^0
34+
B! ← (^0)
35+
└─╴
36+
X ← "Outer"
37+
⍤⤙≍ "Outer" M~A!X
38+
⍤⤙≍ "Outer" M~B!X
39+
3140
# Code macros
3241
F! ←^
3342
G! ←^ ⇌

0 commit comments

Comments
 (0)