Skip to content

Commit

Permalink
lowering: Don't mutate lambda in linearize
Browse files Browse the repository at this point in the history
  • Loading branch information
mlechu committed Feb 14, 2025
1 parent be574cd commit e677990
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -4322,11 +4322,10 @@ f(x) = yt(x)
(define (linearize e)
(cond ((or (not (pair? e)) (quoted? e)) e)
((eq? (car e) 'lambda)
(set-car! (cdddr e) (compile-body (cadddr e) (append (car (caddr e))
(cadr (caddr e)))
e)))
(else (for-each linearize (cdr e))))
e)
(list-set e 3 (compile-body (cadddr e)
(append (car (caddr e))
(cadr (caddr e))) e)))
(else (cons (car e) (map linearize (cdr e))))))

(define (valid-ir-argument? e)
(or (simple-atom? e)
Expand Down
13 changes: 13 additions & 0 deletions src/utils.scm
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,16 @@
(cons (car lst) (filter (lambda (x) (not (pred x))) (cdr lst))))
(else
(cons (car lst) (keep-first pred (cdr lst))))))

(define (take lst n)
(let loop ((lst lst) (n n) (out '()))
(if (= n 0) (reverse out)
(loop (cdr lst) (- n 1) (cons (car lst) out)))))

(define (drop lst n)
(if (= n 0) lst
(drop (cdr lst) (- n 1))))

;; functional update at position i
(define (list-set lst i val)
(append (take lst i) (list val) (drop lst (+ i 1))))
4 changes: 4 additions & 0 deletions test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4088,3 +4088,7 @@ abstract type A57267{S, T} end
B57267{S} = A57267{S, 1}
const C57267 = B57267
end

# Issue #56904 - lambda linearized twice
@test (let; try 3; finally try 1; f(() -> x); catch x; end; end; x = 7; end) === 7
@test (let; try 3; finally try 4; finally try 1; f(() -> x); catch x; end; end; end; x = 7; end) === 7

0 comments on commit e677990

Please sign in to comment.