Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lowering: Don't mutate lambda in linearize #57416

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -4102,3 +4102,7 @@ module Ambig57404
using .B
end
@test Ambig57404.S == 1

# 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