Skip to content

Commit f5d68c6

Browse files
committed
[Heavy] Fix local define name hygiene issue
1 parent 8c858e0 commit f5d68c6

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

heavy/lib/OpGen.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -947,10 +947,17 @@ mlir::Value OpGen::createDefine(Value Id, Value DefineArgs, Value OrigCall) {
947947
// its syntax in the current environment.
948948
Value Env;
949949

950-
if (CurSyntaxClosure)
951-
DefineArgs = Context.CreateSyntaxClosure(DefineArgs.getSourceLocation(),
952-
DefineArgs,
953-
CurSyntaxClosure->Env);
950+
if (CurSyntaxClosure) {
951+
// Save syntactic closure information for Id.
952+
if (auto* S = dyn_cast<Symbol>(Id))
953+
Id = Context.CreateSyntaxClosure(S->getSourceLocation(),
954+
S, CurSyntaxClosure->Env);
955+
// Save syntactic closure information for DefineArgs.
956+
DefineArgs = DefineArgs == CurSyntaxClosure->Node
957+
? CurSyntaxClosure
958+
: Context.CreateSyntaxClosure(DefineArgs.getSourceLocation(),
959+
DefineArgs, CurSyntaxClosure->Env);
960+
}
954961
Binding* B = Context.CreateBinding(Id, Undefined(DefineArgs));
955962
// Push to the local environment.
956963
Context.PushLocalBinding(B);

heavy/test/Evaluate/Inputs/my/lib.sld

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,20 @@
3232
(attributes
3333
`("info", (MakeAttr Arg))))) X))))
3434

35+
(define-syntax my-lambda
36+
(syntax-rules (:)
37+
((my-lambda ((arg : type) ...) body ...)
38+
(lambda (arg ...)
39+
body ...))))
40+
3541
(write "end of init")
3642
(newline)
3743
) ; end begin
3844

3945
(export hello-module
4046
hello-module-syntax
41-
create-op-literal)
47+
create-op-literal
48+
my-lambda)
4249
)
4350
(write "end of module")
4451
(newline)

heavy/test/Evaluate/load-module.scm

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
; CHECK-NEXT: "end of init"
66
(import (my lib))
77
(import (only (heavy builtins)
8-
write newline lambda))
8+
write newline lambda
9+
+ define
10+
))
911

1012
; CHECK: "hello module!"
1113
; CHECK-NEXT: 5
@@ -18,3 +20,15 @@
1820
; CHECK: #op{"heavy.literal"() {info = #heavy<"42">}
1921
(write (create-op-literal ((lambda () #f)) 42))
2022
(newline)
23+
24+
; CHECK: 42
25+
; CHECK-NEXT: 43
26+
; CHECK-NEXT: 44
27+
((my-lambda ((x : Int) (y : Int))
28+
(define X (+ x 1)) ; define in syntax closure
29+
(write x)
30+
(newline)
31+
(write X)
32+
(newline)
33+
(write y)) 42 44)
34+
(newline)

0 commit comments

Comments
 (0)